EmanHamdyMohamed/fastapi-mcp-server
If you are the rightful owner of fastapi-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
A Model Context Protocol (MCP) server designed to facilitate the creation of FastAPI applications with a standardized project structure.
FastAPI MCP Server
A Model Context Protocol (MCP) server that provides tools for creating FastAPI applications with a standard project structure.
Features
- Create FastAPI Projects: Generate new FastAPI projects with a well-organized directory structure
- Docker Configuration: Generate Docker and Docker Compose files for containerization
- CRUD Generation: Automatically generate complete CRUD operations for any model
- Standard Structure: Includes routers, models, schemas, services, middleware, and more
- Ready to Use: Generated projects include all necessary dependencies and configuration
Installation
This project uses uv for dependency management. Make sure you have uv installed:
curl -LsSf https://astral.sh/uv/install.sh | sh
Then install the project dependencies:
uv sync
Local MCP Server Setup
Setting up the MCP Server Locally
-
Clone the repository:
git clone <repository-url> cd fastapi-mcp-server -
Install dependencies:
uv sync -
Test the MCP server:
uv run python -m fastapi_mcp.server
Using with MCP Clients
Claude Desktop Integration
To use this MCP server with Claude Desktop, add the following to your Claude Desktop configuration:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"fastapi-mcp-server": {
"command": "uv",
"args": ["run", "python", "-m", "fastapi_mcp.server"],
"cwd": "/path/to/fastapi-mcp-server"
}
}
}
Cursor Integration
To use this MCP server with Cursor, you need to configure it in your Cursor settings:
-
Open Cursor Settings:
- Press
Cmd/Ctrl + ,to open settings - Go to "Features" → "Model Context Protocol"
- Press
-
Add MCP Server Configuration:
{ "mcpServers": { "fastapi-mcp-server": { "command": "uv", "args": ["run", "python", "-m", "fastapi_mcp.server"], "cwd": "/path/to/fastapi-mcp-server" } } } -
Alternative: Using Cursor's MCP Configuration File: Create or edit
~/.cursor/mcp_config.json:{ "mcpServers": { "fastapi-mcp-server": { "command": "uv", "args": ["run", "python", "-m", "fastapi_mcp.server"], "cwd": "/path/to/fastapi-mcp-server" } } } -
Restart Cursor after adding the configuration.
Usage
Running the MCP Server
uv run python -m fastapi_mcp.server
Available Tools
create_fastapi_project
Creates a new FastAPI project with the following structure:
project_name/
├── app/
│ ├── __init__.py
│ ├── main.py # Main FastAPI application
│ ├── routers/ # API route handlers
│ ├── models/ # Database models
│ ├── schemas/ # Pydantic schemas
│ ├── services/ # Business logic
│ ├── middleware/ # Custom middleware
│ ├── utils/ # Utility functions
│ └── core/ # Core configuration
├── tests/ # Test files
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
└── README.md # Project documentation
Parameters:
project_name(string, required): Name of the FastAPI project to create
Example Usage:
{
"name": "create_fastapi_project",
"arguments": {
"project_name": "my-awesome-api"
}
}
generate_docker_config
Generates Docker configuration files for an existing FastAPI project.
Parameters:
project_path(string, required): Path to the FastAPI project directorypython_version(string, optional): Python version to use (default: "3.12")port(integer, optional): Port to expose the application on (default: 8000)
Generated Files:
Dockerfile- Multi-stage Docker build configurationdocker-compose.yml- Docker Compose configuration
Example Usage:
{
"name": "generate_docker_config",
"arguments": {
"project_path": "./my-awesome-api",
"python_version": "3.12",
"port": 8000,
}
}
generate_crud_operations
Generates complete CRUD operations for a given model including schemas, services, and API endpoints.
Parameters:
project_path(string, required): Path to the FastAPI project directorymodel_name(string, required): Name of the model (e.g., 'User', 'Product')fields(array, required): List of fields for the model
Field Structure:
{
"name": "field_name",
"type": "str",
"required": true,
"description": "Field description"
}
Generated Files:
app/schemas/{model}.py- Pydantic schemas (Create, Update, Response)app/services/{model}_service.py- Service layer with CRUD methodsapp/routers/{model}.py- FastAPI router with REST endpoints
Generated Endpoints:
POST /{model}s/- Create new recordGET /{model}s/- List records with paginationGET /{model}s/{id}- Get record by IDPUT /{model}s/{id}- Update recordDELETE /{model}s/{id}- Delete record
Example Usage:
{
"name": "generate_crud_operations",
"arguments": {
"project_path": "./my-awesome-api",
"model_name": "User",
"fields": [
{
"name": "email",
"type": "str",
"required": true,
"description": "User email address"
},
{
"name": "name",
"type": "str",
"required": true,
"description": "User full name"
},
{
"name": "age",
"type": "int",
"required": false,
"description": "User age"
}
],
}
}
Generated Project Features
The generated FastAPI projects include:
- CORS Middleware: Configured for development with permissive settings
- Health Check Endpoint:
/healthendpoint for monitoring - Root Endpoint: Welcome message at
/ - Dependencies: FastAPI, Uvicorn, Pydantic, and Pydantic Settings
- Environment Configuration:
.env.examplefile for environment variables - Documentation: README with setup and usage instructions
Development
Project Structure
src/
└── fastapi_mcp/
├── __init__.py
└── server.py # Main MCP server implementation
Adding New Tools
To add new tools to the MCP server:
- Add a new tool definition in the
handle_list_tools()function - Add the corresponding handler in the
call_tool()function - Implement the tool logic as a separate async function
Requirements
- Python >= 3.12
- uv (for dependency management)
Dependencies
mcp>=0.1.0: Model Context Protocol server implementationpydantic>=2.12.0: Data validation and settings managementpydantic-settings>=2.11.0: Settings management for Pydantic
License
This project is open source and available under the MIT License.