-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add _meta support in resource content responses #2444
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?
Add _meta support in resource content responses #2444
Conversation
OpenAI's ChatGPT apps SDK uses _meta in resource content to pass widget domain information to clients (see https://developers.openai.com/apps-sdk/build/mcp-server/). FastMCP resources support metadata via the meta parameter, but this wasn't being included in the content returned to clients. The MCP specification defines a _meta field on ResourceContents for this purpose. FastMCP now populates this field with the resource's metadata when serving content, and ensures resources created from templates inherit the template's metadata.
WalkthroughThe pull request extends metadata support throughout the resource management system. The Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/fastmcp/server/server.py (1)
471-524: Refine exception handling and add match default case.Two improvement opportunities:
- Line 491: The bare
except Exceptionis too broad. Sinceget_resourcecan raiseNotFoundError(and potentially other specific exceptions), catch those explicitly to avoid masking unexpected errors:# Get the resource to access its meta try: resource = await self._resource_manager.get_resource(uri) resource_meta = resource.get_meta( include_fastmcp_meta=self.include_fastmcp_meta ) -except Exception: +except (NotFoundError, ValueError): resource_meta = None
- Lines 495-514: The
matchstatement lacks a default case. Ifcontent_item.contentis neitherstrnorbytes, it will be silently skipped, which could mask bugs:for content_item in result: match content_item.content: case str() as data: contents_list.append( mcp.types.TextResourceContents( uri=uri, text=data, mimeType=content_item.mime_type or "text/plain", _meta=resource_meta, ) ) case bytes() as data: contents_list.append( mcp.types.BlobResourceContents( uri=uri, blob=base64.b64encode(data).decode(), mimeType=content_item.mime_type or "application/octet-stream", _meta=resource_meta, ) ) + case _: + logger.warning(f"Unexpected content type for resource {uri}: {type(content_item.content)}")
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
tests/resources/test_resource_content_meta.pyis excluded by none and included by none
📒 Files selected for processing (2)
src/fastmcp/resources/template.py(1 hunks)src/fastmcp/server/server.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/fastmcp/server/server.py (2)
src/fastmcp/resources/resource_manager.py (1)
get_resource(244-287)src/fastmcp/utilities/components.py (1)
get_meta(75-97)
src/fastmcp/resources/template.py (1)
src/fastmcp/server/server.py (1)
icons(380-384)
🪛 Ruff (0.14.5)
src/fastmcp/server/server.py
491-491: Do not catch blind exception: Exception
(BLE001)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Run tests: Python 3.10 on windows-latest
🔇 Additional comments (2)
src/fastmcp/resources/template.py (1)
183-195: LGTM! Metadata propagation implemented correctly.The template now properly forwards metadata fields (title, icons, annotations, meta) when creating resource instances. This enables resources instantiated from templates to inherit the template's metadata, which will then be included in the _meta field of resource content responses.
src/fastmcp/server/server.py (1)
523-523: Approach confirmed valid and appropriate.Direct programmatic registration is recommended when you need full control over request/response shaping and low-level protocol handling. The direct dictionary assignment at line 523 is the correct pattern for this use case, particularly when implementing custom response transformation as done here. No changes needed.
OpenAI's ChatGPT apps SDK uses _meta in resource content to pass widget domain information to clients (see https://developers.openai.com/apps-sdk/build/mcp-server/). FastMCP resources support metadata via the meta parameter, but this wasn't being included in the content returned to clients.
The MCP specification defines a _meta field on ResourceContents for this purpose. FastMCP now populates this field with the resource's metadata when serving content, and ensures resources created from templates inherit the template's metadata.
Description
Contributors Checklist
Review Checklist