Skip to content

EmanHamdyMohamed/fastapi-mcp-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  1. Clone the repository:

    git clone <repository-url>
    cd fastapi-mcp-server
  2. Install dependencies:

    uv sync
  3. 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:

  1. Open Cursor Settings:

    • Press Cmd/Ctrl + , to open settings
    • Go to "Features" → "Model Context Protocol"
  2. Add MCP Server Configuration:

    {
      "mcpServers": {
        "fastapi-mcp-server": {
          "command": "uv",
          "args": ["run", "python", "-m", "fastapi_mcp.server"],
          "cwd": "/path/to/fastapi-mcp-server"
        }
      }
    }
  3. 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"
        }
      }
    }
  4. 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 directory
  • python_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 configuration
  • docker-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 directory
  • model_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 methods
  • app/routers/{model}.py - FastAPI router with REST endpoints

Generated Endpoints:

  • POST /{model}s/ - Create new record
  • GET /{model}s/ - List records with pagination
  • GET /{model}s/{id} - Get record by ID
  • PUT /{model}s/{id} - Update record
  • DELETE /{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: /health endpoint for monitoring
  • Root Endpoint: Welcome message at /
  • Dependencies: FastAPI, Uvicorn, Pydantic, and Pydantic Settings
  • Environment Configuration: .env.example file 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:

  1. Add a new tool definition in the handle_list_tools() function
  2. Add the corresponding handler in the call_tool() function
  3. 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 implementation
  • pydantic>=2.12.0: Data validation and settings management
  • pydantic-settings>=2.11.0: Settings management for Pydantic

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages