Skip to content

Conversation

@calvingiles
Copy link

This change enables MCP servers to include metadata in resource content using the _meta field, which is part of the MCP specification. This allows servers to provide additional context about resources, such as domain information or other custom metadata.

Changes:

  • Added meta field to ReadResourceContents helper class
  • Added _meta field to Resource base class with proper alias
  • Updated lowlevel server handler to pass through _meta when creating TextResourceContents and BlobResourceContents
  • Updated FastMCP server to pass resource._meta to ReadResourceContents
  • Added comprehensive tests for _meta support in both lowlevel and FastMCP resources

The implementation maintains backward compatibility - resources without _meta continue to work as before with meta=None.

Motivation and Context

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

This change enables MCP servers to include metadata in resource content
using the _meta field, which is part of the MCP specification. This allows
servers to provide additional context about resources, such as domain
information or other custom metadata.

Changes:
- Added meta field to ReadResourceContents helper class
- Added _meta field to Resource base class with proper alias
- Updated lowlevel server handler to pass through _meta when creating
  TextResourceContents and BlobResourceContents
- Updated FastMCP server to pass resource._meta to ReadResourceContents
- Added comprehensive tests for _meta support in both lowlevel and
  FastMCP resources

The implementation maintains backward compatibility - resources without
_meta continue to work as before with meta=None.
Adds tests to verify that _meta attributes are correctly serialized
with the underscore prefix in JSON output, not as "meta". This ensures
the implementation conforms to the MCP specification where metadata
fields use the _meta key.

Tests verify:
- Resource serialization includes "_meta" key in JSON
- Response content serialization includes "_meta" key
- JSON strings contain the literal "_meta" key
- No "meta" key appears in serialized output
…ontents

Use the field name 'meta' instead of the alias '_meta' when constructing
Pydantic model instances. The alias is only used for JSON serialization.

This fixes a bug where passing **{'_meta': meta} would cause Pydantic
validation errors since '_meta' is an alias, not a field name.
@maxisbey
Copy link
Contributor

related: #1476

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants