Skip to content

Better handling of MCP tool ResourceLinks #3099

@fennb

Description

@fennb

Description

Currently, MCP Resource Links returned in MCP tool results are transparently downloaded by the client and included in the tool response directly, despite Embedded Resources being a different concept. This could issues in many cases as described below.

From: #3024 (comment) (@fennb):

ie: Imagine a hypothetical tool called find_relevant_documents(query: str, limit: int = 50) that returned 50 resource links to documents (each linked document being 1MB) so that a user can click on the one they want to load/select it/whatever. At the moment, Pydantic AI would try to fetch all the docs immediately and include them in context transparently, which seems... wrong.

An approach would be to leave them as links and then it's up to the user to prompt the agent appropriately in telling it what to do with them (ie: display them verbatim for the actual client to handle or whatever). This is less controversial if the ResourceLink.uri is https://some.server.com/path/file.ext but weirder if it's document://legal/archive/blah.txt (or whatever). I don't really know what an agent would do in that case. Arguably, ResourceLinks maybe shouldn't be used this way, but I think blindly fetching them may not be the right default?

Reply from @DouweM:

I agree the resource link behavior is probably incorrect. I tried to get clarification on that in modelcontextprotocol/modelcontextprotocol#872 (reply in thread), but didn't hear back. It would be easier if the model had a read_resource tool available to it, so it can choose which to load, but that'd need to be provided by the user. Maybe MCPServer should have a flag to include that as a tool, and if provided, we wouldn't auto-read resource links?

Once #3024 is completed, users will have the ability to fetch resources by URI by using MCPServer.read_resource(), which could be exposed to agents by users as a read_resource tool, however, this causes some challenges, as it won't necessarily be clear to the agent/user which MCP server a given resource was returned by (if they have more than 1 MCP server with resources) and they could have overlapping URI schemes (ie: file:// or whatever).

This could potentially be solved by exposing all Resource Links to the agent as (uri, mcp_server_id) tuples and a native/injected read_resource(uri, mcp_server_id) tool that knows how to route the requests appropriately to the correct MCP server.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions