fastapi_mcp_sse
by: panz2018
A working example to create a FastAPI server with SSE-based MCP support
📌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:
- Integrates MCP SSE endpoints (
/sse
and/messages/
) into FastAPI - Provides standard web routes (
/
,/about
,/status
,/docs
,/redoc
) - 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:
- Main server: http://localhost:8000
- Standard web routes:
- Home: http://localhost:8000/
- About: http://localhost:8000/about
- Status API: http://localhost:8000/status
- Swagger UI docs: http://localhost:8000/docs
- ReDoc docs: http://localhost:8000/redoc
- MCP SSE endpoints:
- SSE endpoint: http://localhost:8000/sse
- Message posting: http://localhost:8000/messages/
Debug with MCP Inspector
Use MCP Inspector for testing and debugging MCP:
mcp dev ./src/weather.py
Connect to MCP Inspector
- Open MCP Inspector at http://localhost:5173
- Configure connection:
- Transport Type:
SSE
- URL: http://localhost:8000/sse
- Transport Type:
- Click
Connect
Test the Functions
- Go to
Tools
section - Click
List Tools
to view:get_alerts
: Get weather alertsget_forcast
: Get weather forecast
- 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"
}
}
]
}
}