Tool result _meta is lost when the server automatically adds text content for structured output
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 McpAsyncServer 和 McpStatelessAsyncServer 开始,查看 Utils.isEmpty(result.content()) 分支中文本回退逻辑重建 CallToolResult 的位置。复现方式是在 mcp-test 模块、包 io.modelcontextprotocol.server 中编写一个 JUnit 5 测试,调用无状态处理器上的 handleRequest。完成标准是:该测试通过,_meta 在两个服务器中重建后都得以保留,并且现有的 SSE、Streamable HTTP 和无状态集成测试仍然全部通过。
由索引模型根据 Issue 内容生成。
描述
Bug description
When a tool has an outputSchema and returns valid structuredContent with empty content, the SDK adds a text fallback but drops _meta when rebuilding the result.
Both McpAsyncServer and McpStatelessAsyncServer have this issue. Results with non-empty content keep their metadata.
Environment
- Java SDK:
2.2.0-SNAPSHOT, main atc531bb80 - Java: JDK 17.0.12
- Reproduced with the existing servlet integration tests
Steps to reproduce
-
Register a tool with an output schema:
{"type": "object"} -
Return structured content and metadata from the handler, without adding
text content:return CallToolResult.builder() .structuredContent(Map.of("value", 42)) .meta(Map.of("example.com/trace-id", "trace-123")) .build(); -
Call the tool from an MCP client and inspect the result.
Expected behavior
The generated text, structured content, and metadata are all present:
{
"content": [
{"type": "text", "text": "{\"value\":42}"}
],
"isError": false,
"structuredContent": {"value": 42},
"_meta": {"example.com/trace-id": "trace-123"}
}
Actual behavior
The generated text and structuredContent are present, but _meta is
missing. The Java client receives result.meta() == null.
Minimal Complete Reproducible example
Failing JUnit 5 test using Mockito, in the mcp-test module,
package io.modelcontextprotocol.server:
@Test
void structuredOutputTextFallbackPreservesMetadata() {
var transport = mock(McpStatelessServerTransport.class);
Map<String, Object> metadata = Map.of("example.com/trace-id", "trace-123");
var tool = McpSchema.Tool.builder("example", Map.of("type", "object"))
.outputSchema(Map.of("type", "object"))
.build();
McpServer.sync(transport)
.serverInfo("test-server", "1.0.0")
.toolCall(tool, (context, request) -> McpSchema.CallToolResult.builder()
.structuredContent(Map.of("value", 42))
.meta(metadata)
.build())
.build();
var handlerCaptor = ArgumentCaptor.forClass(McpStatelessServerHandler.class);
verify(transport).setMcpHandler(handlerCaptor.capture());
var response = handlerCaptor.getValue()
.handleRequest(McpTransportContext.EMPTY,
new McpSchema.JSONRPCRequest(McpSchema.METHOD_TOOLS_CALL, "1",
McpSchema.CallToolRequest.builder("example").build()))
.block(Duration.ofSeconds(5));
// Fails before the fix: actual metadata is null.
assertThat(((McpSchema.CallToolResult) response.result()).meta()).isEqualTo(metadata);
}
Proposed fix
In the Utils.isEmpty(result.content()) branches of both
McpAsyncServer and McpStatelessAsyncServer, preserve the metadata
when rebuilding the result:
return CallToolResult.builder()
.content(List.of(
McpSchema.TextContent.builder(validation.jsonStructuredOutput()).build()
))
.isError(result.isError())
.structuredContent(result.structuredContent())
.meta(result.meta())
.build();
Tested locally with the existing SSE, Streamable HTTP, and stateless HTTP integration tests.
I’d like to submit a fix with regression tests, Please let me
know if this approach looks good and whether I can take this on.
- 主要语言
- Java
- 星标
- 3.8k
- 派生
- 1.1k
- 平均合并
- 1 天 3 小时
- 30 天内合并 PR
- 38
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/java-sdk 的其他 Issue
-
Modern `HttpServletMcpTransport` does not check `Mcp-Name` when `params.name` or `params.uri` is not a JSON string可能已有人在做 @starryeye 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
modelcontextprotocol/java-sdk#1352 · 1 条评论 ·
维护者通常 1 天内回复
-
Unknown tool error always says "Unknown tool: invalid_tool_name" instead of the name that was called未关闭
难度 1/5 1 小时以内 新手友好度 85/100
modelcontextprotocol/java-sdk#1346 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
modelcontextprotocol/java-sdk#1345 · 1 条评论 ·
维护者通常 1 天内回复
-
Client request handlers that complete empty send no JSON-RPC response可能已有人在做 @1fanwang 于 83 天前认领。 未关闭area/client bug P2
难度 2/5 1-3 小时 新手友好度 84/100
modelcontextprotocol/java-sdk#1124 · 6 条评论 ·
维护者通常 1 天内回复
-
ServerCapabilities.logging is added unconditionally, overriding the caller's explicit capabilities可能已有人在做 @1yuxiangJ 于 98 天前认领。 未关闭bug P2 ready for work
难度 2/5 1-3 小时 新手友好度 68/100
modelcontextprotocol/java-sdk#1086 · 2 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/java-sdk 的全部 Issue
相似的 Issue
-
crash
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 74/100
-
account bug course exam iris lecture
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100