FastAPI MCP SSE

FastAPI MCP SSE

A Server-Sent Events implementation using FastAPI framework that integrates Model Context Protocol (MCP), allowing AI models to access external tools and data sources like weather information.

panz2018

Developer Tools
Browser Automation
Python
Visit Server

README

FastAPI MCP SSE

<p align="center"> <strong>English</strong> | <a href="/README.zh-CN.md">简体中文</a> </p>

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:

  • Main server: http://localhost:8000
  • Standard web routes:
    • Home page: http://localhost:8000/
    • About page: http://localhost:8000/about
    • Status API: http://localhost:8000/status
    • Documentation (Swagger UI): http://localhost:8000/docs
    • Documentation (ReDoc): http://localhost:8000/redoc
  • MCP SSE endpoints:
    • SSE endpoint: http://localhost:8000/sse
    • Message posting: http://localhost:8000/messages/

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:
    • Set Transport Type to SSE
    • Enter URL: http://localhost:8000/sse
    • Click Connect

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"
        }
      }
    ]
  }
}

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Playwright MCP Server

Playwright MCP Server

Provides a server utilizing Model Context Protocol to enable human-like browser automation with Playwright, allowing control over browser actions such as navigation, element interaction, and scrolling.

Featured
Local
TypeScript
MCP Package Docs Server

MCP Package Docs Server

Facilitates LLMs to efficiently access and fetch structured documentation for packages in Go, Python, and NPM, enhancing software development with multi-language support and performance optimization.

Featured
Local
TypeScript
@kazuph/mcp-fetch

@kazuph/mcp-fetch

Model Context Protocol server for fetching web content and processing images. This allows Claude Desktop (or any MCP client) to fetch web content and handle images appropriately.

Featured
Local
JavaScript
Claude Code MCP

Claude Code MCP

An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.

Featured
Local
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

Model Context Protocol server for Task Management. This allows Claude Desktop (or any MCP client) to manage and execute tasks in a queue-based system.

Featured
Local
JavaScript
DuckDuckGo MCP Server

DuckDuckGo MCP Server

A Model Context Protocol (MCP) server that provides web search capabilities through DuckDuckGo, with additional features for content fetching and parsing.

Featured
Python
Linear MCP Server

Linear MCP Server

Enables interaction with Linear's API for managing issues, teams, and projects programmatically through the Model Context Protocol.

Featured
JavaScript
YouTube Transcript MCP Server

YouTube Transcript MCP Server

This server retrieves transcripts for given YouTube video URLs, enabling integration with Goose CLI or Goose Desktop for transcript extraction and processing.

Featured
Python