Skip to content

Add annotations field to MCP::Resource and MCP::ResourceTemplate per MCP specification#2

Open
yuki3738 wants to merge 1 commit into
mainfrom
add_annotations_field_to_resource
Open

Add annotations field to MCP::Resource and MCP::ResourceTemplate per MCP specification#2
yuki3738 wants to merge 1 commit into
mainfrom
add_annotations_field_to_resource

Conversation

@yuki3738

Copy link
Copy Markdown
Owner

Note: This is a practice / self-review PR inside my own fork. Not the upstream contribution.

Motivation and Context

The MCP specification defines an optional annotations field on both the Resource and ResourceTemplate types:
https://modelcontextprotocol.io/specification/2025-11-25/server/resources#annotations

Annotations provide hints to clients about how to use or display a resource (audience, priority, lastModified).

The SDK already ships the MCP::Annotations class, and MCP::Tool and the content classes already accept annotations, but plain MCP::Resource and MCP::ResourceTemplate had no way to declare them.

Following MCP::Tool's pattern, the field accepts an MCP::Annotations instance and serializes it via annotations&.to_h, which also normalizes last_modified to the spec's lastModified key.

How Has This Been Tested?

Added tests in test/mcp/resource_test.rb and test/mcp/resource_template_test.rb:

  • #to_h omits annotations when nil
  • #to_h includes annotations when present (also asserting that last_modified is serialized as lastModified)

The full unit suite and RuboCop pass locally.

Breaking Changes

None. annotations: is a new optional keyword argument defaulting to nil, and it is omitted from #to_h output when not set.

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

…` per MCP specification

## Motivation and Context

The MCP specification defines an optional `annotations` field on both the
`Resource` and `ResourceTemplate` types:
https://modelcontextprotocol.io/specification/2025-11-25/server/resources#annotations

Annotations provide hints to clients about how to use or display a resource
(`audience`, `priority`, `lastModified`).

The SDK already ships the `MCP::Annotations` class, and `MCP::Tool` and the
content classes already accept annotations, but plain `MCP::Resource` and
`MCP::ResourceTemplate` had no way to declare them.

Following `MCP::Tool`'s pattern, the field accepts an `MCP::Annotations`
instance and serializes it via `annotations&.to_h`, which also normalizes
`last_modified` to the spec's `lastModified` key.

## How Has This Been Tested?

Added tests in `test/mcp/resource_test.rb` and
`test/mcp/resource_template_test.rb`:

- `#to_h` omits `annotations` when nil
- `#to_h` includes `annotations` when present (also asserting that
  `last_modified` is serialized as `lastModified`)

The full unit suite and RuboCop pass locally.

## Breaking Changes

None. `annotations:` is a new optional keyword argument defaulting to `nil`,
and it is omitted from `#to_h` output when not set.
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.

1 participant