-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Allow ResourceContents objects to be returned directly from read_resource handlers #1476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Allow ResourceContents objects to be returned directly from read_resource handlers #1476
Conversation
…d_resource handlers Updated the low-level server's read_resource decorator to accept TextResourceContents and BlobResourceContents objects directly, in addition to the existing ReadResourceContents. This provides more flexibility for resource handlers to construct and return properly typed ResourceContents objects with full control over all properties. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
…ctly Updated FastMCP server and resource base classes to support returning TextResourceContents and BlobResourceContents objects directly from resource handlers, matching the low-level server functionality. - Updated Resource.read() abstract method to accept ResourceContents types - Modified FunctionResource to handle ResourceContents in wrapped functions - Updated FastMCP server read_resource to pass through ResourceContents - Updated Context.read_resource to match new return types This provides FastMCP users with more control over resource properties and maintains consistency with the low-level server API. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
…tionality Added test coverage for the new feature allowing resources to return TextResourceContents and BlobResourceContents objects directly: Low-level server tests (test_read_resource_direct.py): - Test direct TextResourceContents return - Test direct BlobResourceContents return - Test mixed direct and wrapped content - Test multiple ResourceContents objects FastMCP tests (test_resource_contents_direct.py): - Test custom resources returning ResourceContents - Test function resources returning ResourceContents - Test resource templates with ResourceContents - Test mixed traditional and direct resources All tests verify proper handling and pass-through of ResourceContents objects without wrapping them in ReadResourceContents. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Added example snippets demonstrating the main benefit of returning ResourceContents objects directly - the ability to include metadata via the _meta field: - FastMCP example: Shows various metadata use cases including timestamps, versions, authorship, image metadata, and query execution details - Low-level server example: Demonstrates metadata for documents, images, multi-part content, and code snippets The examples emphasize that metadata is the key advantage of this feature, allowing servers to provide rich contextual information about resources that helps clients better understand and work with the content. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Updated imports in base.py and types.py to add ResourceContents types to the existing mcp.types import rather than using a separate import statement. This follows the existing pattern and keeps imports cleaner. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Corrected the examples to use the proper _meta field name as specified in the MCP protocol, rather than the constructor parameter name 'meta'. This ensures the metadata appears correctly when viewed in MCP Inspector. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Move ReadResourceContents import to top of file in lowlevel example - Use union syntax (X | Y) instead of tuple in isinstance call - Fix line length issue by breaking long function signature - Add proper type annotations for consistency 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Fixed type narrowing issues in test files that were accessing attributes on union types without proper isinstance checks. The FastMCP read_resource method returns ReadResourceContents | TextResourceContents | BlobResourceContents, requiring explicit type checking before accessing type-specific attributes. Changes: - Added proper type narrowing with isinstance() checks in all affected tests - Fixed incorrect Resource base class usage in test files - Corrected AnyUrl constructor usage in resource creation - Updated lowlevel example to avoid delegation conflicts All tests pass and pyright reports 0 errors. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Fixed datetime.UTC usage which is only available in Python 3.11+. Replaced with datetime.timezone.utc for broader Python version compatibility. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
@felixweinberger here is one more related to META Improvements milestone, for resources. Similar to #1459 for tool call results. |
I think this could be implemented the same way as #1463 by just exposing the |
Adding the metadata as part of the decorator would certainly work for the OpenAI use case, since the widget metadata is static. But I was thinking that, because the |
This PR allows resources to directly return
TextResourceContents
orBlobResourceContents
instead of having it cast automatically by the lowlevel server.Inspired by #1459, but for resources.
Motivation and Context
OpenAI's Apps SDK uses the
_meta
field of a read resource result representing a "widget" to configure Content Security Policy and other behaviors.This use of
_meta
is supported by the protocol, but isn't implemented in the SDK.In order to maximize discoverability and future change-abililty, I added the ability to directly return the two
ResourceContents
classes, which already supports the_meta
field, rather than add additional tuple fields.How Has This Been Tested?
Test suite additions and example snippets are provided. Running the example shows
_meta
in the response.Breaking Changes
N/A
Types of changes
Checklist
Additional context
N/A