MCP HubMCP Hub
panz2018

fastapi_mcp_sse

by: panz2018

A working example to create a FastAPI server with SSE-based MCP support

29created 09/03/2025
Visit
FastAPI
SSE

📌Overview

Purpose: To provide a comprehensive implementation of Server-Sent Events (SSE) using the FastAPI framework, integrated with the Model Context Protocol (MCP) for enhanced AI model functionality.

Overview: This project showcases how to utilize the FastAPI framework to implement SSE while integrating MCP, allowing AI models to interact with external tools and data sources. It emphasizes the seamless operation of server-sent events within a full-featured web application architecture.

Key Features:

  • MCP Integration with SSE: Facilitates real-time data communication between clients and servers, enabling AI models to dynamically access updated information.

  • Custom Route Support: Implements a flexible routing system that allows developers to easily add and manage custom endpoints alongside standard web routes.

  • Clear Separation of Concerns: Maintains a modular structure where MCP functionalities are distinct from web application features, enhancing maintainability and readability.


FastAPI MCP SSE

English

A Server-Sent Events (SSE) implementation using the FastAPI framework with Model Context Protocol (MCP) integration.

What is MCP?

The Model Context Protocol (MCP) is an open standard that enables AI models to interact with external tools and data sources. MCP addresses key challenges in AI development:

  • Context limitations: Enables models to access up-to-date information beyond their training data
  • Tool integration: Standardizes how models use external tools and APIs
  • Interoperability: Provides a common interface for various AI models and tools
  • Extensibility: Simplifies adding new capabilities to AI systems without retraining

This project demonstrates MCP implementation using Server-Sent Events (SSE) in a FastAPI web application.

Description

This project shows how to implement SSE with FastAPI while integrating MCP functionality. It features seamless integration of MCP's SSE capabilities within a full-featured FastAPI app including custom routes.

Features

  • SSE implementation with MCP
  • FastAPI framework integration with custom routes
  • Unified web application with MCP and standard web endpoints
  • Customizable route structure
  • Clear separation between MCP and web functionality

Architecture

The architecture:

  1. Integrates MCP SSE endpoints (/sse and /messages/) into FastAPI
  2. Provides standard web routes (/, /about, /status, /docs, /redoc)
  3. Maintains separation between MCP functionality and web routes

Installation & Usage Options

Prerequisites

Install UV Package Manager, a fast Python package installer written in Rust:

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Option 1: Quick Run Without Installation

Run the application directly without cloning the repo using UV's execution tool:

uvx --from git+https://github.com/panz2018/fastapi_mcp_sse.git start

Option 2: Full Installation

Create Virtual Environment

uv venv

Activate Virtual Environment

.venv\Scripts\activate

Install Dependencies

uv pip install -r pyproject.toml

Start the Integrated Server

python src/server.py

or

uv run start

Available Endpoints

Once the server is running, the following endpoints are available:

Debug with MCP Inspector

Use MCP Inspector for testing and debugging MCP:

mcp dev ./src/weather.py

Connect to MCP Inspector

  1. Open MCP Inspector at http://localhost:5173
  2. Configure connection:
  3. Click Connect

Test the Functions

  1. Go to Tools section
  2. Click List Tools to view:
    • get_alerts: Get weather alerts
    • get_forcast: Get weather forecast
  3. Select a function, enter parameters, then Run Tool

Extending the Application

Adding Custom Routes

Add new routes using FastAPI's APIRouter:

@router.get("/new-route")
async def new_route():
    return {"message": "This is a new route"}

Routes defined with this router are automatically included in the main app.

Customizing MCP Integration

MCP SSE integration in server.py involves:

  • Creating an SSE transport
  • Setting up an SSE handler
  • Adding MCP routes to the FastAPI app

Integration with Continue

To integrate this MCP server with the Continue VS Code extension, add the following config to your Continue settings:

{
  "experimental": {
    "modelContextProtocolServers": [
      {
        "transport": {
          "name": "weather",
          "type": "sse",
          "url": "http://localhost:8000/sse"
        }
      }
    ]
  }
}