Skip to main content
Glama

FastAPI MCP SSE

by panz2018

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

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

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

  1. 什么是 MCP?
    1. 描述
      1. 特征
        1. 建筑学
          1. 安装和使用选项
            1. 先决条件
            2. 选项 1:无需安装即可快速运行
            3. 选项 2:完整安装
            4. 可用端点
            5. 使用 MCP Inspector 进行调试
            6. 连接到 MCP 检查器
            7. 测试功能
          2. 扩展应用程序
            1. 添加自定义路线
            2. 自定义 MCP 集成
          3. 与Continue集成

            Related MCP Servers

            • A
              security
              F
              license
              A
              quality
              A Model Context Protocol server that provides AI agents with tools to retrieve weather alerts and detailed forecasts for US locations using the National Weather Service API.
              Last updated -
              2
              80
              TypeScript
            • A
              security
              F
              license
              A
              quality
              An implementation of the Model Context Protocol (MCP) server using Server-Sent Events (SSE) for real-time communication, providing tools for calculations and dynamic resource templates.
              Last updated -
              1
              JavaScript
            • -
              security
              A
              license
              -
              quality
              A customizable Model Context Protocol server implementation that enables AI models to interact with external tools including weather queries, Google search, and camera control functionality.
              Last updated -
              8
              Python
              Apache 2.0
              • Linux
              • Apple
            • -
              security
              -
              license
              -
              quality
              A simple server that implements the Model Context Protocol, allowing AI models like Claude to fetch real-time weather information for any location using the wttr.in API.
              Last updated -
              JavaScript

            View all related MCP servers

            MCP directory API

            We provide all the information about MCP servers via our MCP API.

            curl -X GET 'https://glama.ai/api/mcp/v1/servers/panz2018/fastapi_mcp_sse'

            If you have feedback or need assistance with the MCP directory API, please join our Discord server