modelcontextprotocol / modelcontextprotocol/python-sdk

[Feature] De-couple Starlette from FastMCP to make it easier to implement MCP endpoints in other frameworks

未关闭
#622 1 条评论 15 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement needs decision P3 v2
主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

If someone wanted to implement an MCP server in another framework such as Django or some other ASGI implementation, one would have to use the mcp.server.lowlevel.Server class and implement handlers for the low-level server. This means we have to sacrifice a ton of functionality from FastMCP that really has nothing to do with the python server backend. For example tool/resource/prompt managers, decorators, etc would have to be re-impemented.

It seems that these features of FastMCP could be decoupled in to a base class or mixin that handles all the tool/resource/prompt management & decorators, as well as the integration with the low-level Server. You could then extend that class to implement your own server with all the routing and request/response logic.

The proposed base class would have minimal dependencies on these framework-specific concerns:

Base Class Dependencies:

  • MCPServer from mcp.server.lowlevel.server
  • Tool/Resource/Prompt managers
  • Basic handler methods (list_tools, call_tool, etc.)
  • The decorators (@tool, @resource, @prompt)

Server-specific Responsibilities:

  • Settings handling (Settings class with HTTP config)
  • Transport logic (stdio vs SSE)
  • Routing (custom_route, Starlette routing)
class FastMCPBase:
    """Base class or mixin with tool/resource/prompt management and core handlers"""
    
    def __init__(self, name: str, instructions: str = None):
        self._mcp_server = MCPServer(name=name, instructions=instructions)
        self._tool_manager = ToolManager()
        self._resource_manager = ResourceManager()
        self._prompt_manager = PromptManager()
        self._setup_handlers()
    
    # All the @tool, @resource, @prompt decorators
    # All the list_tools, call_tool, etc. handler methods
    # Context management

class FastMCP(FastMCPBase):
    """Starlette-specific implementation"""
    
    def __init__(self, name: str, **settings):
        super().__init__(name)
        self.settings = Settings(**settings)
        self._custom_starlette_routes = []
    
    # Only Starlette-specific methods like sse_app, routing, etc.

The implementation in the other library could look like this:

class DjangoMCP(FastMCPBase):
    """Django-specific implementation"""
    # Django-specific routing & transport logic

One slight complication could be the Context class as it has a reference to the FastMCP instance (self._fastmcp. However the only function that's used is read_resource which would already be on the base class. So possible just need to change the type hint to use the base class.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

审查 issue 中描述的 MCPServer 低级集成、tool/resource/prompt managers、装饰器和 handler 方法。将特定于 Starlette 的设置、传输和路由与所提议的 FastMCPBase 边界进行比较,包括 Context 对 FastMCP 的引用;完成的标准是,与 framework 无关的代码保留这些能力,同时由 FastMCP 保留 Starlette 相关事项。

由索引模型根据 Issue 内容生成。

评估

技术栈
django, python
领域
api, backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。