让仅支持工具的客户端也能访问资源
部分 MCP 客户端不支持资源协议,只能调用工具。FastMCP 的 ResourcesAsTools 转换会生成 list_resources 与 read_resource 两个工具,使资源操作经由服务器中间件链执行,并区分静态资源与模板。
一些 MCP 客户端只支持工具。 它们不能直接列出或读取资源, 因为它们缺少资源协议支持 。 `ResourcesAsTools` 通过生成提供访问您服务器资源的工具来改变这一差距。 添加时 `ResourcesAsTools` 在服务器上,它创建了客户端可以调用而不是使用资源协议的两个工具: - **`list_resources`** 返回描述所有可用的资源和模板的 JSON - **`read_resource`** 由 URI 读取特定资源 这意味着任何可以调用工具的客户端现在都可以访问资源,即使客户端没有本土资源支持. ## 基本用途 将您的 FastMCP 服务器传给 `ResourcesAsTools` 在添加转换时。运行时通过服务器生成的工具路由,这意味着所有服务器中间软件——认证、可见度、速率限制——自动适用于资源操作,完全如用于直接 `resources/read` 电话 `ResourcesAsTools` (并) `PromptsAsTools`) 应用到 FastMCP 服务器实例,而不是原始的提供方。生成的工具在运行时会召回到服务器的中端软件链,所以它们需要一个服务器来进行路由。如果您想要只曝光一个子集的资源,请为这些资源创建一个专用的 FastMCP 服务器,并在那里应用变换 。 ```python from fastmcp import FastMCP from fastmcp.server.transforms import ResourcesAsTools mcp = FastMCP("My Server") @mcp.resource("config://app") def app_config() -> str: """Application configuration.""" return '{"app_name": "My App", "version": "1.0.0"}' @mcp.resource("user://{user_id}/profile") def user_profile(user_id: str) -> str: """Get a user's profile by ID.""" return f'{{"user_id": "{user_id}", "name": "User {user_id}"}}' # Add the transform - creates list_resources and read_resource tools mcp.add_transform(ResourcesAsTools(mcp)) ``` 现在客户端看到 `list_resources` 财务报告和财务报告 `read_resource` 与您直接定义的任何工具并列 。 两个生成的工具都附有说明 `readOnlyHint=True`,因为他们只读取数据。尊重工具说明(如 Cursor)的客户端可以使用此功能自动确认这些工具的调用,而无需提示用户。 ## 静态资源对模板 资源有两种形式: `list_resources` 工具在其 JSON 输出中区分它们。 静态资源有固定的 URI 。 它们是存在于已知位置的具体数据。 在列表输出中, 静态资源包括 a `uri` 字段中包含要请求的精确的 URI 。 资源模板将 URI 参数化,有占位符,例如 `{user_id}`。它们代表了获取动态数据的模式。在列表输出中,模板包括: `uri_template` 字段以占位符显示图案。 当客户打电话时 `list_resources`,它接收JSON 这样: ```json [ { "uri": "config://app", "name": "app_config", "description": "Application configuration.", "mime_type": "text/plain" }, { "uri_template": "user://{user_id}/profile", "name": "user_profile", "description": "Get a user's profile by ID." } ] ``` 客户端可以通过检查哪个字段存在来区分资源类型 : `uri` 静态资源, `uri_template` 用于模板。 ## 阅读资源 那个... `read_resource` 工具接受单个 `uri` 参数。对于静态资源,请通过精确的 URI。对于模板,请填写占位符中的实际值。 ```python from fastmcp import Client async with Client(mcp) as client: # Reading a static resource result = await client.call_tool("read_resource", {"uri": "config://app"}) print(result.data) # '{"app_name": "My App", "version": "1.0.0"}' # Reading a templated resource - fill in {user_id} with an actual ID result = await client.call_tool("read_resource", {"uri": "user://42/profile"}) print(result.data) # '{"user_id": "42", "name": "User 42"}' ``` 变换会自动处理匹配的模板。 当您要求时 `user://42/profile`,它匹配与 `user://{user_id}/profile` 模板,摘录 `user_id=42`,并使用该参数调用资源函数。 ## 二进制内容 返回二进制数据的资源( 如图像或文件) 在读取时会自动编码到基数64- encode `read_resource` 工具。这保证了二进制内容可以在工具响应中作为字符串传输。返回多个内容项的资源会作为 JSON 对象阵列返回。 `content` 财务报告和财务报告 `mime_type` 字段。 ```python import base64 from fastmcp import Client @mcp.resource("data://binary", mime_type="application/octet-stream") def binary_data() -> bytes: return b"\x00\x01\x02\x03" async with Client(mcp) as client: # Client receives base64-encoded string result = await client.call_tool("read_resource", {"uri": "data://binary"}) decoded = base64.b64decode(result.data) # b'\x00\x01\x02\x03' ```
查看原始来源