FastAPI MCP Server

FastAPI MCP Server

Wraps a FastAPI application as an MCP server, enabling user and task management operations through Gemini CLI tool calling with full CRUD functionality.

Category
访问服务器

README

FastAPI + MCP Server Integration with Gemini CLI

This project demonstrates how to build a FastAPI application, wrap it as an MCP (Model Context Protocol) Server, and integrate it with Gemini CLI for direct tool calling.

Project Structure

├── sample_app.py      # FastAPI application with user and task management
├── mcp_server.py      # MCP server that wraps the FastAPI app
├── requirements.txt   # Python dependencies
├── setup.sh          # Setup script
├── demo.sh           # Interactive demonstration script
├── test_integration.py # Integration test script
├── venv/             # Python virtual environment
└── README.md         # This file

Features

FastAPI Application (sample_app.py)

  • User Management: Create, read users with name, email, and age
  • Task Management: Create, read, update, delete tasks
  • Statistics: Get overview of users and tasks
  • Health Check: Basic health monitoring endpoint

MCP Server (mcp_server.py)

  • Tool Integration: Exposes all FastAPI endpoints as MCP tools
  • Error Handling: Proper HTTP error handling and logging
  • Type Safety: Full type annotations and schema validation

Available MCP Tools

  1. get_app_info - Get basic app information
  2. get_health - Check app health status
  3. get_users - List all users
  4. create_user - Create a new user
  5. get_user - Get user by ID
  6. get_tasks - List all tasks
  7. create_task - Create a new task
  8. get_task - Get task by ID
  9. update_task - Update an existing task
  10. delete_task - Delete a task
  11. get_stats - Get user and task statistics

Quick Start

Option 1: Automated Demo

./demo.sh

This interactive script will guide you through the entire setup and testing process.

Option 2: Manual Setup

1. Run Setup Script

./setup.sh

2. Start the FastAPI Application

source venv/bin/activate
python sample_app.py

The FastAPI app will be available at http://localhost:8000

3. Start the MCP Server (in another terminal)

source venv/bin/activate
python mcp_server.py

4. Install Gemini CLI

npm install -g @google/gemini-cli@latest

5. Add MCP Server to Gemini CLI

gemini mcp add fastapi-sample stdio python $(pwd)/mcp_server.py

6. Test the Integration

# List available tools
gemini mcp list

# Call a tool
gemini call fastapi-sample get_app_info

# Create a user
gemini call fastapi-sample create_user --name "John Doe" --email "john@example.com" --age 30

# Get all users
gemini call fastapi-sample get_users

# Create a task
gemini call fastapi-sample create_task --title "Learn MCP" --description "Study Model Context Protocol" --user_id 1

# Get statistics
gemini call fastapi-sample get_stats

Manual Setup

If you prefer to set up manually:

1. Install Python Dependencies

pip3 install -r requirements.txt

2. Start Services

  • FastAPI app: python3 sample_app.py
  • MCP server: python3 mcp_server.py

3. Install and Configure Gemini CLI

npm install -g @google/gemini-cli@latest
gemini mcp add fastapi-sample stdio python3 /path/to/mcp_server.py

API Endpoints

The FastAPI application provides the following REST endpoints:

  • GET / - App information
  • GET /health - Health check
  • GET /users - List users
  • POST /users - Create user
  • GET /users/{user_id} - Get user by ID
  • GET /tasks - List tasks
  • POST /tasks - Create task
  • GET /tasks/{task_id} - Get task by ID
  • PUT /tasks/{task_id} - Update task
  • DELETE /tasks/{task_id} - Delete task
  • GET /stats - Get statistics

MCP Tool Examples

Create and Manage Users

# Create a user
gemini call fastapi-sample create_user --name "Alice Smith" --email "alice@example.com" --age 25

# Get user by ID
gemini call fastapi-sample get_user --user_id 1

# List all users
gemini call fastapi-sample get_users

Create and Manage Tasks

# Create a task
gemini call fastapi-sample create_task --title "Complete project" --description "Finish the MCP integration" --user_id 1

# Update a task
gemini call fastapi-sample update_task --task_id 1 --title "Complete project" --description "Finish the MCP integration" --user_id 1 --completed true

# Delete a task
gemini call fastapi-sample delete_task --task_id 1

Get Statistics

gemini call fastapi-sample get_stats

Troubleshooting

Common Issues

  1. Port already in use: Make sure port 8000 is available for the FastAPI app
  2. MCP server connection failed: Ensure the FastAPI app is running before starting the MCP server
  3. Gemini CLI not found: Make sure Node.js and npm are installed, then install Gemini CLI globally

Debug Mode

To run the FastAPI app in debug mode:

uvicorn sample_app:app --reload --host 0.0.0.0 --port 8000

Check MCP Server Status

gemini mcp list

Development

Adding New Endpoints

  1. Add the endpoint to sample_app.py
  2. Add the corresponding tool to mcp_server.py in the handle_list_tools() function
  3. Add the tool handler in the handle_call_tool() function

Testing

You can test the FastAPI endpoints directly using curl:

# Test app info
curl http://localhost:8000/

# Create a user
curl -X POST http://localhost:8000/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Test User", "email": "test@example.com", "age": 30}'

# Get users
curl http://localhost:8000/users

License

This project is for educational purposes and demonstrates MCP integration patterns.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选