Exposes FastAPI REST endpoints as MCP tools, providing user management (create, read, delete), task management (create, read, update, delete), mathematical calculations, and health check monitoring capabilities.
FastAPI MCP Server Integration with Gemini CLI
This project demonstrates how to create a FastAPI application, wrap it with an MCP (Model Context Protocol) server, and integrate it with Gemini CLI for natural language interactions.
Project Structure
Features
FastAPI Application (app.py)
User Management: Create, read, and delete users
Task Management: Create, read, update (complete), and delete tasks
Calculations: Perform mathematical operations (add, subtract, multiply, divide)
Health Check: Monitor application status
MCP Server (mcp_server.py)
Exposes 11 FastAPI endpoints as MCP tools:
get_health- Check API health statusget_users- Retrieve all usersget_user- Get a specific user by IDcreate_user- Create a new userdelete_user- Delete a user by IDget_tasks- Retrieve all tasksget_task- Get a specific task by IDcreate_task- Create a new taskcomplete_task- Mark a task as completeddelete_task- Delete a task by IDcalculate- Perform mathematical calculations
Setup Instructions
1. Install Dependencies
2. Start the FastAPI Application
In one terminal, start the FastAPI server:
Or using uvicorn directly:
The API will be available at http://localhost:8000
3. Install Gemini CLI
Install the Gemini CLI globally using npm:
4. Configure Gemini CLI
Add the MCP server to Gemini CLI configuration. The configuration file is typically located at:
Windows:
%APPDATA%\Gemini CLI\config.jsonmacOS/Linux:
~/.config/gemini-cli/config.json
Or use the Gemini CLI command:
Alternatively, you can manually add this to your Gemini CLI config file:
Note: Make sure to provide the full path to mcp_server.py if running from a different directory.
5. Verify Setup
Verify that the MCP server is properly configured:
Practical Demonstration with Gemini CLI
Once everything is set up, you can interact with your FastAPI application through Gemini CLI using natural language. Here are some examples:
Example 1: Check API Health
Gemini CLI will call the get_health tool and return the API status.
Example 2: Create a User
This will invoke the create_user tool with the provided parameters.
Example 3: List All Users
This uses the get_users tool to retrieve all users from the API.
Example 4: Create a Task
The create_task tool will be called to create the task.
Example 5: Perform Calculation
Or:
This will use the calculate tool to perform the mathematical operation.
Example 6: Complete a Task
This will call the complete_task tool.
Example 7: Get Task Details
Uses the get_task tool to retrieve task details.
Example 8: Delete a User
Invokes the delete_user tool.
Testing the API Directly
You can also test the FastAPI endpoints directly using curl or a REST client:
API Documentation
Once the FastAPI application is running, you can access interactive API documentation at:
Swagger UI:
http://localhost:8000/docsReDoc:
http://localhost:8000/redoc
Troubleshooting
MCP Server Not Found
If Gemini CLI cannot find the MCP server:
Ensure you're in the correct directory when starting Gemini CLI
Check that the path to
mcp_server.pyin the config is correctVerify Python is in your PATH
Connection Errors
If you get connection errors:
Ensure the FastAPI application is running on port 8000
Check that the
API_URLinmcp_server.pymatches your FastAPI server URLVerify there are no firewall restrictions
Import Errors
If you encounter import errors:
Make sure all dependencies are installed:
pip install -r requirements.txtVerify you're using the correct Python environment
Check that the
mcppackage is installed correctly
Requirements
Python 3.8 or higher
Node.js and npm (for Gemini CLI)
All packages listed in
requirements.txt
License
This is a sample project for educational purposes.
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 FastAPI REST endpoints as MCP tools, enabling natural language interaction with user management, task management, and mathematical calculations through Gemini CLI.