Tool result _meta is lost when the server automatically adds text content for structured output

Open Beginner friendly
#1,330 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
backend

Research direction

Start in McpAsyncServer and McpStatelessAsyncServer, at the Utils.isEmpty(result.content()) branches where the text fallback rebuilds the CallToolResult. The reproduction is a JUnit 5 test in the mcp-test module, package io.modelcontextprotocol.server, that calls handleRequest on the stateless handler. Done when that test passes, _meta survives the rebuild in both servers, and the existing SSE, Streamable HTTP and stateless integration tests still pass.

Written by the indexing model from the issue text.

Description

area/server legacy P3

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 at c531bb80
  • Java: JDK 17.0.12
  • Reproduced with the existing servlet integration tests

Steps to reproduce

  1. Register a tool with an output schema:

    {"type": "object"}
    
  2. 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();
    
  3. 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.

Dominant language
Java
Stars
3.8k
Forks
1.1k
Avg merge
1d 3h
Merged PRs (30d)
38

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelcontextprotocol/java-sdk

All issues in modelcontextprotocol/java-sdk

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.