Skip to content

Propagate @McpResource title to generated resources - #6787

Open
alpin87 wants to merge 1 commit into
spring-projects:mainfrom
alpin87:fix/mcp-resource-title
Open

Propagate @McpResource title to generated resources#6787
alpin87 wants to merge 1 commit into
spring-projects:mainfrom
alpin87:fix/mcp-resource-title

Conversation

@alpin87

@alpin87 alpin87 commented Aug 12, 2026

Copy link
Copy Markdown

Fixes #6786

Background@McpResource(title = "...") never reached the client. MCP clients then fall back to name, which defaults to the Java method name, so a server author who sets a title sees no effect and no error.

Cause — The four resource providers build McpSchema.Resource / ResourceTemplate without ever reading McpResource#title:

var mcpResource = McpSchema.Resource.builder(uri, name)
    .description(description)
    .mimeType(mimeType)
    .meta(meta)
    .build();               // no .title(...)

These are the runtime path — SyncMcpAnnotationProviders and AsyncMcpAnnotationProviders delegate to them. ResourceAdapter#asResource does set the title, but nothing in production calls that adapter. @McpTool and @McpPrompt both propagate their title, so resources were the only family dropping it.

Fix — Set the title in all four providers, for both the resource and the resource-template paths (8 sites). It is applied only when it has text, so resources without a title keep serializing exactly as before rather than gaining an empty title: "".

I deliberately kept this narrow. Routing the providers through ResourceAdapter would fix #6749 at the same time, but that pulls in the lastModified design question raised there which has no maintainer answer yet. Happy to do it that way instead if you prefer — just say so.

Validation

Added three tests to each of the four provider test classes: title propagated on a resource, title propagated on a resource template, and title absent when unset.

Verified they actually catch the bug by reverting the main-code change and re-running — 8 failures, 2 per provider:

expected: "My Resource Title"
 but was: null

With the fix applied:

$ ./mvnw -pl mcp/mcp-annotations -am -Dtest='*ResourceProviderTests' test
Tests run: 21, Failures: 0, Errors: 0, Skipped: 0 -- SyncMcpResourceProviderTests
Tests run: 19, Failures: 0, Errors: 0, Skipped: 0 -- AsyncMcpResourceProviderTests
Tests run: 18, Failures: 0, Errors: 0, Skipped: 0 -- SyncStatelessMcpResourceProviderTests
Tests run: 19, Failures: 0, Errors: 0, Skipped: 0 -- AsyncStatelessMcpResourceProviderTests
Tests run: 77, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS

Checkstyle passes. The commit is signed off per the DCO.

The resource providers built McpSchema.Resource and ResourceTemplate
without ever reading McpResource#title, so a title declared on the
annotation never reached the client and clients fell back to the
resource name, which defaults to the Java method name.

McpTool and McpPrompt already propagate their title, and
ResourceAdapter reads it correctly, but no production code calls that
adapter. Set the title in all four resource providers instead, and only
when it has text so that resources without a title keep serializing
without one.

Signed-off-by: seungmin <qortmdals94@naver.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug - MCP server] @McpResource(title = ...) is dropped by the resource providers [Bug - MCP server] McpResource.annotations is ignored/does nothing

2 participants