
FastMCP Component Manager 实战用 HTTP 接口在运行时动态启停工具、资源与提示词【免费下载链接】fastmcp The fast, Pythonic way to build MCP servers and clients.项目地址: https://gitcode.com/GitHub_Trending/fa/fastmcpComponent Manager 是 FastMCP 生态中的一个 contrib 扩展模块它把server.enable()/server.disable()的编程式能力封装为一组 REST 端点让你可以通过 HTTP 请求在运行时动态启用或禁用工具tools、资源resources与提示词prompts。本文将以 fastmcp_slim/fastmcp/contrib/component_manager/README.md 为骨架结合仓库内的源码实现与测试用例完整讲解该模块的安装接入、端点语义、认证加固、挂载服务器隔离以及其底层的工作机制。读完本文你将能基于该模块为 FastMCP 服务器构建特性开关feature toggle、管理后台或自动化运维能力。模块定位与核心能力Component Manager 解决的问题很直接一个运行中的 FastMCP 服务器如何在不重启、不修改代码的前提下按需开放或收回某个组件它通过注册一组 HTTP 路由把启用/禁用这一管理操作从 Python 代码内部暴露到外部调用者管理后台、运维脚本、Agent 工作流并给出统一的 JSON 响应。官方 README 列出的核心特性如下可通过 HTTP 端点启用/禁用工具、资源、提示词三类组件同时支持本地组件与挂载服务器mounted server组件支持自定义API 根路径可选Auth scopes做访问控制与 FastMCP 集成成本极低最小只需一行set_up_component_manager(servermcp)。需要说明的是该模块属于fastmcp.contrib社区扩展包README 中明确标注其并非 FastMCP 核心团队官方维护而是由 gorocode 独立开发的扩展许可证沿用 FastMCP 主项目。安装与最小接入模块位于fastmcp.contrib包内无需单独安装——只要你的环境已经安装了 FastMCP本仓库对应实现位于 fastmcp_slim/fastmcp即可直接导入使用from fastmcp import FastMCP from fastmcp.contrib.component_manager import set_up_component_manager mcp FastMCP( nameComponent Manager, instructionsThis is a test server with component manager., ) set_up_component_manager(servermcp)包入口在 component_manager/init.py公开的唯一 API 是set_up_component_manager。执行上述代码后服务器会注册 6 条管理路由默认挂在根路径/下。API 端点一览模块为三类组件各注册一对 enable/disable 端点全部限定为POST方法见 component_manager.py 中的路由构建逻辑组件类型启用端点禁用端点工具POST /tools/{tool_name}/enablePOST /tools/{tool_name}/disable资源POST /resources/{uri:path}/enablePOST /resources/{uri:path}/disable提示词POST /prompts/{prompt_name}/enablePOST /prompts/{prompt_name}/disable几个实现细节值得注意资源端点使用{uri:path}路径参数因此 URI 中的/可以被完整透传例如POST /resources/data://test_resource/enable资源模板同样受支持例如POST /resources/example://test/{id}/enable。端点内部会检测路径参数中是否包含{若包含则按template组件类型处理否则按resource处理对应 component_manager.py 的类型分派逻辑支持可选的版本过滤参数?version端点会把request.query_params.get(version)原样透传给server.enable()/disable()可用于针对特定版本的组件做启停如POST /tools/my_tool/disable?versionv1这正是 FastMCP 版本化组件能力在 HTTP 层的延伸。成功的请求返回如下结构的 JSONHTTP/1.1 200 OK Content-Type: application/json { message: Disabled tool: example_tool }消息格式为{action}d {component_type}: {name}其中action为Enable或Disablecomponent_type为tool/resource/promptname为路径中解析出的组件名或资源 URI见 component_manager.py。配置选项详解set_up_component_manager共接受三个参数见 component_manager.py参数类型默认值说明serverFastMCP必填目标 FastMCP 服务器实例pathstr/管理 API 的挂载根路径required_scopeslist[str] \| NoneNone可选仅在启用认证时生效要求请求携带的 token 具备指定 scope自定义根路径当不希望管理端点与 MCP 主端点混在同一路径空间时可以挂载到任意自定义前缀下set_up_component_manager(servermcp, path/admin)挂载后端点变为POST /admin/tools/{name}/enable、POST /admin/resources/{uri:path}/disable、POST /admin/prompts/{name}/enable等。从源码看无认证时路径前缀直接拼进各Route而启用认证时则改用 Starlette 的Mount承载前缀path ! /时才挂载以便认证中间件统一拦截。用 Auth Scopes 加固管理端点启用了认证的服务器上可以要求调用者携带具备指定 scope 的 tokenmcp FastMCP( nameComponent Manager, instructionsThis is a test server with component manager., authauth, ) set_up_component_manager(servermcp, required_scopes[write, read])结合仓库中的完整示例 example.py可以看出一套可运行的 JWT 认证配置from fastmcp import FastMCP from fastmcp.contrib.component_manager import set_up_component_manager from fastmcp.server.auth.providers.jwt import JWTVerifier, RSAKeyPair key_pair RSAKeyPair.generate() auth JWTVerifier( public_keykey_pair.public_key, issuerhttps://dev.example.com, audiencemy-dev-server, required_scopes[mcp:read], ) # 主服务器组件管理需要 mcp:write 权限 mcp_token key_pair.create_token( subjectdev-user, issuerhttps://dev.example.com, audiencemy-dev-server, scopes[mcp:write, mcp:read], ) mcp FastMCP( nameComponent Manager, instructionsThis is a test server with component manager., authauth, ) set_up_component_manager(servermcp, required_scopes[mcp:write])调用时需在请求头携带 Bearer token。测试用例 tests/contrib/test_component_manager.py 严格验证了三种鉴权结果无 token 返回401、token 缺 scope 返回403、token 具备所需 scope 返回200并真正改变组件状态。用 Curl 实际操作一个工具启用认证后的典型调用如下curl -X POST \ -H Authorization: Bearer YOUR_TOKEN_HERE \ -H Content-Type: application/json \ http://localhost:8001/tools/example_tool/enable对应地禁用即为POST /tools/example_tool/disable。8001是示例中 FastMCP 服务器的 HTTP 端口YOUR_TOKEN_HERE需替换为具备required_scopes中全部 scope 的访问令牌。挂载服务器场景细粒度的权限隔离FastMCP 支持通过mcp.mount(serverchild, namespacemo)组合多个服务器。Component Manager 可以分别在主服务器和挂载服务器上各自启用、各自配置不同 scope从而实现主入口管全局、子入口只管自己的权限分层mcp FastMCP(nameComponent Manager, instructions..., authauth) set_up_component_manager(servermcp, required_scopes[mcp:write]) mounted FastMCP(nameComponent Manager, instructions..., authauth) set_up_component_manager(servermounted, required_scopes[mounted:write]) mcp.mount(servermounted, namespacemo)效果如下访问主服务器如http://localhost:8001时可以同时控制本地组件与带命名空间的挂载组件例如POST /tools/mo_example_tool/enable控制的是挂载子服务器中命名空间为mo的example_tool直接访问挂载服务器自身如http://localhost:8002时只能控制其自有组件例如POST /tools/example_tool/enable且必须持有mounted:write相关 scope。这样便可以为平台管理员和子服务负责人分配互不越权的管理面。底层原因是 FastMCP 在收集附加路由时会递归合并挂载服务器的路由_get_additional_http_routes()会遍历所有 provider若发现内层是FastMCPProvider就继续递归收集其子服务器的路由见 transport.py。因此主服务器的 HTTP app 天然能看到全部挂载组件的管理端点而每个挂载服务器自身也独立持有自己的端点。工作原理从 HTTP 请求到组件状态变更整个调用链分四层均可在仓库源码中逐一对证路由注册set_up_component_manager()把构建好的Route列表追加到server._additional_http_routes无认证时直接扩展该列表有认证时追加一个由RequireAuthMiddleware包裹的Mount。该列表在 server.py 中初始化本质是用户自定义 HTTP 路由的挂载点。路由合并进 ASGI app在构建 SSE 应用与 Streamable HTTP 应用时server_routes.extend(server._get_additional_http_routes())会把管理路由追加到全部内置路由之后见 http.py 与 http.py即最低优先级的自定义路由。端点处理_make_endpoint()为每个路由生成异步端点函数。它先从request.path_params中取出name或uri再读取可选的version查询参数随后按组件类型分派——资源带{按template处理、否则按resource工具与提示词各归其类见 component_manager.py。状态变更端点最终调用getattr(server, action)(names{name}, versionversion, componentscomponents)即server.enable()或server.disable()。这两个方法定义在 providers/base.py本质是向服务器的变换链追加一个Visibility变换enable标记匹配组件可见disable标记隐藏由于后加入的变换优先生效同一组件可以先 disable 再 enable 恢复。enable还额外支持onlyTrue的 allowlist 模式先全局禁用再精确放行。测试验证与可复现的检查清单仓库为模块提供了完整的测试覆盖tests/contrib/test_component_manager.py涵盖四个测试类可作为验证模块行为最直接的依据TestComponentManagementRoutes无认证下对工具、普通资源、资源模板、提示词分别执行 enable/disable断言 HTTP 200、JSON 响应体并通过list_tools()/list_resources()/list_resource_templates()/list_prompts()验证组件状态真实改变TestAuthComponentManagementRoutes认证场景下验证 401无 token、403scope 不足、200scope 足够三种结果TestComponentManagerWithPath/TestComponentManagerWithPathAuth验证自定义路径/test下含认证场景路由前缀与鉴权行为正确。如果你要基于该模块落地功能特性开关或管理后台可参照上述用例建立同样的回归检查先禁用组件并断言其从列表中消失再通过 HTTP 端点启用并断言其恢复可见对资源模板使用data://xxx/{id}形式的 URI 验证模板分支对启用认证的服务器务必补充 401/403 用例。小结Component Manager 以极低的接入成本为 FastMCP 服务器提供了一套完整的 HTTP 化组件治理能力。它不引入新的状态机制而是复用 FastMCP 自身的enable/disableVisibility变换体系并借助_additional_http_routes与挂载路由递归合并机制自然延伸到多服务器组合场景因此行为与核心 API 高度一致、可预测。将其与认证 scope 组合即可构建出权限分层的管理界面或自动化运维入口。【免费下载链接】fastmcp The fast, Pythonic way to build MCP servers and clients.项目地址: https://gitcode.com/GitHub_Trending/fa/fastmcp创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考