fastapi_mcp_sse

This project demonstrates the implementation of Server-Sent Events (SSE) using the FastAPI framework while integrating Model Context Protocol (MCP) functionality. MCP is an open standard that enables AI models to interact with external tools and data sources, addressing context limitations, providing tool integration, and ensuring interoperability. This project illustrates how to implement MCP in a FastAPI application.

GitHub Stars

49

User Rating

Not Rated

Favorites

0

Views

31

Forks

10

Issues

4

Installation
Difficulty
Intermediate
Estimated Time
10-20 minutes
Requirements
Python 3.7以上
FastAPI 0.68.0以上
+1 more

Installation

Installation

Prerequisites

Required software and versions:
UV Package Manager: Latest version

Installation Steps

1. Install UV Package Manager

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

2. Quick Run Without Installation

Run the application directly without cloning the repository:
cmd
uvx --from git+https://github.com/panz2018/fastapi_mcp_sse.git start

3. Full Installation

#### Create Virtual Environment
cmd
uv venv
#### Activate Virtual Environment
cmd
.venv\Scripts\activate
#### Install Dependencies
cmd
uv pip install -r pyproject.toml
#### Start the Integrated Server
cmd
python src/server.py
or
cmd
uv run start

Troubleshooting

Issue: Server won't start Solution: Check Python version and reinstall dependencies.

Configuration

Configuration

Basic Configuration

FastAPI Setup

Configure the FastAPI application in src/server.py. Define endpoints as follows:
python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Hello World"}

Advanced Configuration

Security Settings

Store API keys in environment variables or secure configuration files
Set appropriate file access permissions

Performance Tuning

Configure timeout values
Limit concurrent executions

Configuration Example

Basic Configuration

python
@app.get("/sse")
async def sse_endpoint():
    # SSE implementation
    pass

Examples

Examples

Basic Usage

Here are basic usage examples for the MCP server:

Programmatic Usage

python
import requests

def call_mcp_tool(tool_name, params):
    response = requests.post(
        'http://localhost:8000/mcp/call',
        json={
            'tool': tool_name,
            'parameters': params
        }
    )
    return response.json()

Usage example

result = call_mcp_tool('analyze', { 'input': 'sample data', 'options': {'format': 'json'} })

Use Cases

Using MCP for AI models to fetch up-to-date data from external APIs.
Utilizing SSE for applications that require real-time data streaming.
Integrating multiple AI tools to provide a seamless user experience.
Building a web application with custom routes using FastAPI.

Additional Resources