Wraps a FastAPI application as an MCP server, exposing REST endpoints as MCP tools for user management, task management, and statistics retrieval.
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
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
get_app_info- Get basic app informationget_health- Check app health statusget_users- List all userscreate_user- Create a new userget_user- Get user by IDget_tasks- List all taskscreate_task- Create a new taskget_task- Get task by IDupdate_task- Update an existing taskdelete_task- Delete a taskget_stats- Get user and task statistics
Quick Start
Option 1: Automated Demo
This interactive script will guide you through the entire setup and testing process.
Option 2: Manual Setup
1. Run Setup Script
2. Start the FastAPI Application
The FastAPI app will be available at http://localhost:8000
3. Start the MCP Server (in another terminal)
4. Install Gemini CLI
5. Add MCP Server to Gemini CLI
6. Test the Integration
Manual Setup
If you prefer to set up manually:
1. Install Python Dependencies
2. Start Services
FastAPI app:
python3 sample_app.pyMCP server:
python3 mcp_server.py
3. Install and Configure Gemini CLI
API Endpoints
The FastAPI application provides the following REST endpoints:
GET /- App informationGET /health- Health checkGET /users- List usersPOST /users- Create userGET /users/{user_id}- Get user by IDGET /tasks- List tasksPOST /tasks- Create taskGET /tasks/{task_id}- Get task by IDPUT /tasks/{task_id}- Update taskDELETE /tasks/{task_id}- Delete taskGET /stats- Get statistics
MCP Tool Examples
Create and Manage Users
Create and Manage Tasks
Get Statistics
Troubleshooting
Common Issues
Port already in use: Make sure port 8000 is available for the FastAPI app
MCP server connection failed: Ensure the FastAPI app is running before starting the MCP server
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:
Check MCP Server Status
Development
Adding New Endpoints
Add the endpoint to
sample_app.pyAdd the corresponding tool to
mcp_server.pyin thehandle_list_tools()functionAdd the tool handler in the
handle_call_tool()function
Testing
You can test the FastAPI endpoints directly using curl:
License
This project is for educational purposes and demonstrates MCP integration patterns.
This server cannot be installed
local-only server
The server can only run on the client's local machine because it depends on local resources.
Wraps a FastAPI application as an MCP server, enabling user and task management operations through Gemini CLI tool calling with full CRUD functionality.