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.
├── 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
- 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
- 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
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
./demo.shThis interactive script will guide you through the entire setup and testing process.
./setup.shsource venv/bin/activate
python sample_app.pyThe FastAPI app will be available at http://localhost:8000
source venv/bin/activate
python mcp_server.pynpm install -g @google/gemini-cli@latestgemini mcp add fastapi-sample stdio python $(pwd)/mcp_server.py# 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_statsIf you prefer to set up manually:
pip3 install -r requirements.txt- FastAPI app:
python3 sample_app.py - MCP server:
python3 mcp_server.py
npm install -g @google/gemini-cli@latest
gemini mcp add fastapi-sample stdio python3 /path/to/mcp_server.pyThe 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
# 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 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 1gemini call fastapi-sample get_stats- 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
To run the FastAPI app in debug mode:
uvicorn sample_app:app --reload --host 0.0.0.0 --port 8000gemini mcp list- Add the endpoint to
sample_app.py - Add the corresponding tool to
mcp_server.pyin thehandle_list_tools()function - Add the tool handler in the
handle_call_tool()function
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/usersThis project is for educational purposes and demonstrates MCP integration patterns.