Tool result _meta is lost when the server automatically adds text content for structured output
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 72/100
Rechercherichtung
Beginne bei McpAsyncServer und McpStatelessAsyncServer, bei den Verzweigungen Utils.isEmpty(result.content()), in denen der Text-Fallback das CallToolResult neu aufbaut. Die Reproduktion ist ein JUnit 5-Test im Modul mcp-test, Paket io.modelcontextprotocol.server, der handleRequest am stateless Handler aufruft. Fertig ist es, wenn dieser Test besteht, _meta den Neuaufbau in beiden Servern übersteht und die bestehenden Integrationstests für SSE, Streamable HTTP und stateless weiterhin bestehen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Java
- Sterne
- 3.8k
- Forks
- 1.1k
- Ø Merge
- 1 T. 3 Std.
- Gemergte PRs (30 T.)
- 38
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/java-sdk
-
Modern `HttpServletMcpTransport` does not check `Mcp-Name` when `params.name` or `params.uri` is not a JSON stringEvtl. vergeben @starryeye hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
modelcontextprotocol/java-sdk#1352 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Unknown tool error always says "Unknown tool: invalid_tool_name" instead of the name that was calledOffen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
modelcontextprotocol/java-sdk#1346 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
modelcontextprotocol/java-sdk#1345 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Client request handlers that complete empty send no JSON-RPC responseEvtl. vergeben @1fanwang hat das vor 83 Tagen übernommen. Offenarea/client bug P2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
modelcontextprotocol/java-sdk#1124 · 6 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
ServerCapabilities.logging is added unconditionally, overriding the caller's explicit capabilitiesEvtl. vergeben @1yuxiangJ hat das vor 98 Tagen übernommen. Offenbug P2 ready for work
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
modelcontextprotocol/java-sdk#1086 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in modelcontextprotocol/java-sdk
Ähnliche Issues
-
crash
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
account bug course exam iris lecture
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100