FastAPI + MCP + Gemini Integration
Enables Gemini AI to interact with a FastAPI application through MCP tools for user management, task management, and dice rolling functionality. Provides natural language access to REST API endpoints including CRUD operations, health checks, and application statistics.
README
FastAPI + MCP + Gemini Integration
This project demonstrates how to integrate a FastAPI application with Google's Gemini AI using a simplified MCP (Model Context Protocol) server implementation.
🏗️ Architecture
- FastAPI App (
app.py): A sample REST API with user management, task management, and dice rolling - Simple MCP Server (
simple_mcp_server.py): Simplified MCP server that exposes FastAPI endpoints as tools - Gemini Integration (
simple_gemini_integration.py): Connects Gemini AI with the MCP server
🚀 Features
FastAPI Application
- User management (CRUD operations)
- Task management with completion tracking
- Dice rolling functionality
- Health checks and statistics
- RESTful API endpoints
MCP Server Tools
get_health_status(): Check application healthget_app_info(): Get application informationget_all_users(): Retrieve all userscreate_user(): Create new usersget_user_by_id(): Get specific userget_all_tasks(): Retrieve all taskscreate_task(): Create new taskscomplete_task(): Mark tasks as completedroll_dice(): Roll dice with custom parametersget_app_statistics(): Get application statisticssearch_users_by_name(): Search users by nameget_pending_tasks(): Get incomplete tasksget_completed_tasks(): Get completed tasks
📋 Prerequisites
- Python 3.8+
- Google Gemini API key (optional - demo works in simulation mode)
- Basic Python packages (fastapi, uvicorn, aiohttp, google-generativeai)
🛠️ Installation
-
Clone or download the project files
-
Install dependencies:
pip install fastapi uvicorn aiohttp google-generativeai python-dotenv requests -
Set up environment variables (optional): Create a
.envfile and add your Gemini API key:GEMINI_API_KEY=your_actual_api_key_hereNote: The demo works without an API key in simulation mode.
-
Get a Gemini API key:
- Visit Google AI Studio
- Create a new API key
- Add it to your
.envfile
🎯 Usage
0. Look for the video demo
You can look for the zip file in which screen recording is present. That includes a demo question and an answer.
1. Start the FastAPI Server
python app.py
The FastAPI server will run on http://localhost:8000
2. Test the FastAPI Endpoints
You can test the API directly:
# Health check
curl http://localhost:8000/health
# Get app info
curl http://localhost:8000/
# Create a user
curl -X POST "http://localhost:8000/users?name=John&email=john@example.com&age=30"
# Create a task
curl -X POST "http://localhost:8000/tasks?title=Learn%20FastMCP&description=Study%20FastMCP%20integration"
# Roll dice
curl "http://localhost:8000/dice/roll?sides=6&count=3"
3. Run the Gemini Integration
Demo Mode (Predefined Queries)
python simple_gemini_integration.py
Interactive Mode
python simple_gemini_integration.py --interactive
Automated Demo
python start_simple_demo.py
4. Example Gemini Queries
In interactive mode, you can ask questions like:
- "Check the health status of the FastAPI application"
- "Create a new user named 'Alice' with email 'alice@example.com' and age 25"
- "Create a task called 'Learn Python' with description 'Study Python programming'"
- "Roll 5 dice with 10 sides each"
- "Show me all users and get the application statistics"
- "Mark the first task as completed"
- "Show me all pending tasks"
🔧 Configuration
FastAPI Server
- Default port: 8000
- Host: 0.0.0.0 (accessible from all interfaces)
- Modify
app.pyto change these settings
MCP Server
- Connects to FastAPI server at
http://localhost:8000 - Modify
API_BASE_URLinmcp_server.pyif needed
Gemini Integration
- Uses Gemini 2.0 Flash model
- Configure API key via environment variable
- Modify model settings in
gemini_integration.py
📁 Project Structure
.
├── app.py # FastAPI application
├── simple_mcp_server.py # Simplified MCP server with tools
├── simple_gemini_integration.py # Gemini + MCP integration
├── start_simple_demo.py # Automated startup script
├── test_simple_integration.py # Integration testing
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore file
└── README.md # This file
🧪 Testing
Test FastAPI Endpoints
# Start the server
python app.py
# In another terminal, test endpoints
curl http://localhost:8000/health
curl http://localhost:8000/users
curl http://localhost:8000/tasks
Test MCP Server
python simple_mcp_server.py
Test Gemini Integration
# Make sure FastAPI server is running
python app.py
# In another terminal, run integration
python simple_gemini_integration.py
Test Everything
python test_simple_integration.py
🔍 Troubleshooting
Common Issues
-
"Please set GEMINI_API_KEY environment variable"
- Make sure you have a
.envfile with your API key - Check that the API key is valid
- Make sure you have a
-
"Error connecting to MCP server"
- Ensure the FastAPI server is running on port 8000
- Check that all dependencies are installed
-
"ModuleNotFoundError"
- Run
pip install -r requirements.txt - Make sure you're using Python 3.8+
- Run
Debug Mode
To see more detailed error messages, you can modify the integration script to include more logging.
🚀 Next Steps
- Add more FastAPI endpoints
- Create additional MCP tools
- Implement authentication
- Add database persistence
- Create a web interface
- Deploy to cloud platforms
📚 Learn More
🤝 Contributing
Feel free to submit issues and enhancement requests!
📄 License
This project is open source and available under the MIT License.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。