You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A FastAPI application that demonstrates Server-Sent Events (SSE) integration with the Model Context Protocol
Overview
This project showcases how to use Server-Sent Events (SSE) as a transport layer for MCP in a FastAPI application. It provides a simple echo service that can:
Handle FastAPI HTTP requests
Implement MCP tools, resources and prompts using the MCP python-sdk
Requirements
Python 3.12+
FastAPI 0.115.11+
MCP 1.4.1+
Installation
Clone this repository:
git clone https://github.com/ragieai/fastapi-sse-mcp.git
cd fastapi-sse-mcp
GET /sse/ - SSE endpoint for establishing connections
POST /messages/ - Endpoint for sending messages over SSE
Examples
The application provides three example MCP functions:
Tool Function: Echoes messages
@mcp.tool()defecho_tool(message: str) ->str:
"""Echo a message as a tool"""returnf"Tool echo: {message}"
Prompt Function: Creates echo prompts
@mcp.prompt()defecho_prompt(message: str) ->str:
"""Create an echo prompt"""returnf"Please process this message: {message}"
Resource Function: Handles resource requests
@mcp.resource("echo://{message}")defecho_resource(message: str) ->str:
"""Echo a message as a resource"""returnf"Resource echo: {message}"
License
MIT License
Copyright (c) 2024 Ragie Corp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
About
A FastAPI application that demonstrates Server-Sent Events (SSE) integration with the Model Context Protocol