Skip to main content

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

Project description

FastAPI MCP SSE

English | 简体中文

A Server-Sent Events (SSE) implementation using 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 solves several key challenges in AI development:

  • Context limitations: Allows models to access up-to-date information beyond their training data
  • Tool integration: Provides a standardized way for models to use external tools and APIs
  • Interoperability: Creates a common interface between different AI models and tools
  • Extensibility: Makes it easy to add new capabilities to AI systems without retraining

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

Description

This project demonstrates how to implement Server-Sent Events (SSE) using the FastAPI framework while integrating Model Context Protocol (MCP) functionality. The key feature is the seamless integration of MCP's SSE capabilities within a full-featured FastAPI web application that includes custom routes.

Features

  • Server-Sent Events (SSE) implementation with MCP
  • FastAPI framework integration with custom routes
  • Unified web application with both MCP and standard web endpoints
  • Customizable route structure
  • Clean separation of concerns between MCP and web functionality

Architecture

This project showcases a modular architecture that:

  1. Integrates MCP SSE endpoints (/sse and /messages/) into a FastAPI application
  2. Provides standard web routes (/, /about, /status, /docs, /redoc)
  3. Demonstrates how to maintain 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 repository using UV's execution tool:

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

Option 2: Full Installation

Create Virtual Environment

Create an isolated Python environment for the project:

uv venv

Activate Virtual Environment

Activate the virtual environment to use it:

.venv\Scripts\activate

Install Dependencies

Install all required packages:

uv pip install -r pyproject.toml

Start the Integrated Server

Launch the integrated FastAPI server with MCP SSE functionality:

python src/server.py

or

uv run start

Available Endpoints

After starting the server (using either Option 1 or Option 2), the following endpoints will be available:

Debug with MCP Inspector

For testing and debugging MCP functionality, use the MCP Inspector:

mcp dev ./src/weather.py

Connect to MCP Inspector

  1. Open MCP Inspector at http://localhost:5173
  2. Configure the connection:

Test the Functions

  1. Navigate to Tools section
  2. Click List Tools to see available functions:
    • get_alerts : Get weather alerts
    • get_forcast : Get weather forecast
  3. Select a function
  4. Enter required parameters
  5. Click Run Tool to execute

Extending the Application

Adding Custom Routes

The application structure makes it easy to add new routes using FastAPI's APIRouter:

  1. Define new route handlers in routes.py using the APIRouter:

    @router.get("/new-route")
    async def new_route():
        return {"message": "This is a new route"}
    
  2. All routes defined with the router will be automatically included in the main application

Customizing MCP Integration

The MCP SSE functionality is integrated in server.py through:

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

Integration with Continue

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orange_fastapi_mcp_sse-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

orange_fastapi_mcp_sse-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file orange_fastapi_mcp_sse-0.1.0.tar.gz.

File metadata

  • Download URL: orange_fastapi_mcp_sse-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for orange_fastapi_mcp_sse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 168e0d53fe14412a852f2629960b0c79ec0e5be9be8edcfaf2801fdcb20f5fa5
MD5 16488b85343909f2a4eded3df88eb0f6
BLAKE2b-256 98b6ab774538b85382cd2732c78bb340ab71dda49325ad9d6a04613340c64536

See more details on using hashes here.

File details

Details for the file orange_fastapi_mcp_sse-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for orange_fastapi_mcp_sse-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee452eb9fbd3ff059d02cf96f9226d759f0e0d7d2f9e9de05293ecbad67acb96
MD5 8d636161b3a76690b3b24cfc0e0862a5
BLAKE2b-256 9628c46dcf830f8e46c6ff1342af19e62024b7a58b0be8ef098198e8dd8c18b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page