FastAPI + MCP + Gemini Integration

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.

Category
访问服务器

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 health
  • get_app_info(): Get application information
  • get_all_users(): Retrieve all users
  • create_user(): Create new users
  • get_user_by_id(): Get specific user
  • get_all_tasks(): Retrieve all tasks
  • create_task(): Create new tasks
  • complete_task(): Mark tasks as completed
  • roll_dice(): Roll dice with custom parameters
  • get_app_statistics(): Get application statistics
  • search_users_by_name(): Search users by name
  • get_pending_tasks(): Get incomplete tasks
  • get_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

  1. Clone or download the project files

  2. Install dependencies:

    pip install fastapi uvicorn aiohttp google-generativeai python-dotenv requests
    
  3. Set up environment variables (optional): Create a .env file and add your Gemini API key:

    GEMINI_API_KEY=your_actual_api_key_here
    

    Note: The demo works without an API key in simulation mode.

  4. Get a Gemini API key:

🎯 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.py to change these settings

MCP Server

  • Connects to FastAPI server at http://localhost:8000
  • Modify API_BASE_URL in mcp_server.py if 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

  1. "Please set GEMINI_API_KEY environment variable"

    • Make sure you have a .env file with your API key
    • Check that the API key is valid
  2. "Error connecting to MCP server"

    • Ensure the FastAPI server is running on port 8000
    • Check that all dependencies are installed
  3. "ModuleNotFoundError"

    • Run pip install -r requirements.txt
    • Make sure you're using Python 3.8+

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

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选