FastAPI MCP SSE

Integrations

  • The MCP server is implemented using the FastAPI framework, providing a web application with both MCP and standard web endpoints.

  • The MCP server provides Swagger UI documentation through its /docs endpoint.

FastAPI MCP SSE

使用 FastAPI 框架和模型上下文协议 (MCP)集成的服务器发送事件 (SSE) 实现。

什么是 MCP?

模型上下文协议 (MCP)是一个开放标准,使 AI 模型能够与外部工具和数据源进行交互。MCP 解决了 AI 开发中的几个关键挑战:

  • 上下文限制:允许模型访问训练数据之外的最新信息
  • 工具集成:为模型使用外部工具和 API 提供标准化的方式
  • 互操作性:在不同的 AI 模型和工具之间创建通用接口
  • 可扩展性:无需重新训练即可轻松向 AI 系统添加新功能

该项目演示了如何在 FastAPI Web 应用程序中使用服务器发送事件 (SSE) 实现 MCP。

描述

该项目演示了如何使用 FastAPI 框架实现服务器发送事件 (SSE),同时集成模型上下文协议 (MCP) 功能。其主要功能是将 MCP 的 SSE 功能无缝集成到包含自定义路由的全功能 FastAPI Web 应用程序中。

特征

  • 使用 MCP 实现服务器发送事件 (SSE)
  • FastAPI 框架与自定义路由集成
  • 具有 MCP 和标准 Web 端点的统一 Web 应用程序
  • 可定制的路线结构
  • MCP 和 Web 功能之间的关注点清晰分离

建筑学

该项目展示了一种模块化架构:

  1. 将 MCP SSE 端点( /sse/messages/ )集成到 FastAPI 应用程序中
  2. 提供标准网络路线( //about/status/docs/redoc
  3. 演示如何保持 MCP 功能与 Web 路由之间的分离

安装和使用选项

先决条件

安装UV 包管理器- 用 Rust 编写的快速 Python 包安装程序:

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

选项 1:无需安装即可快速运行

使用 UV 的执行工具直接运行应用程序,而无需克隆存储库:

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

选项 2:完整安装

创建虚拟环境

为项目创建一个独立的 Python 环境:

uv venv

激活虚拟环境

激活虚拟环境以使用它:

.venv\Scripts\activate

安装依赖项

安装所有必需的软件包:

uv pip install -r pyproject.toml

启动集成服务器

启动具有 MCP SSE 功能的集成 FastAPI 服务器:

python src/server.py

或者

uv run start

可用端点

启动服务器(使用选项 1 或选项 2)后,以下端点将可用:

使用 MCP Inspector 进行调试

要测试和调试 MCP 功能,请使用 MCP Inspector:

mcp dev ./src/weather.py

连接到 MCP 检查器

  1. 打开 MCP 检查器,网址为http://localhost:5173
  2. 配置连接:

测试功能

  1. 导航至Tools部分
  2. 单击List Tools可查看可用功能:
    • get_alerts :获取天气警报
    • get_forcast :获取天气预报
  3. 选择一个函数
  4. 输入所需参数
  5. 点击Run Tool执行

扩展应用程序

添加自定义路线

应用程序结构使得使用 FastAPI 的 APIRouter 添加新路由变得容易:

  1. 使用 APIRouter 在 routes.py 中定义新的路由处理程序:
    @router.get("/new-route") async def new_route(): return {"message": "This is a new route"}
  2. 使用路由器定义的所有路由将自动包含在主应用程序中

自定义 MCP 集成

MCP SSE 功能通过以下方式集成到 server.py 中:

  • 创建 SSE 传输
  • 设置 SSE 处理程序
  • 向 FastAPI 应用程序添加 MCP 路由

Continue集成

要将此 MCP 服务器与 Continue VS Code 扩展一起使用,请将以下配置添加到您的 Continue 设置中:

{ "experimental": { "modelContextProtocolServers": [ { "transport": { "name": "weather", "type": "sse", "url": "http://localhost:8000/sse" } } ] } }
-
security - not tested
A
license - permissive license
-
quality - not tested

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

使用 FastAPI 框架实现的服务器发送事件集成了模型上下文协议 (MCP),允许 AI 模型访问外部工具和数据源,例如天气信息。

  1. What is MCP?
    1. Description
      1. Features
        1. Architecture
          1. Installation & Usage Options
            1. Prerequisites
            2. Option 1: Quick Run Without Installation
            3. Option 2: Full Installation
            4. Available Endpoints
            5. Debug with MCP Inspector
            6. Connect to MCP Inspector
            7. Test the Functions
          2. Extending the Application
            1. Adding Custom Routes
            2. Customizing MCP Integration
          3. Integration with Continue
            ID: 3cw73h6fyg