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

オープン 初心者向け
#1,330 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java
領域
backend

調査の方向性

McpAsyncServer と McpStatelessAsyncServer から始めて、テキストのフォールバックが CallToolResult を再構築する Utils.isEmpty(result.content()) の分岐を確認してください。再現手順は、mcp-test モジュールの io.modelcontextprotocol.server パッケージにある JUnit 5 のテストで、ステートレスハンドラーの handleRequest を呼び出すものです。このテストが通り、両方のサーバーで再構築後も _meta が残り、既存の SSE、Streamable HTTP、ステートレスの統合テストがすべて通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Java
スター
3.8k
フォーク
1.1k
平均マージ
1日 3時間
マージ済み PR(30日)
38

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/java-sdk のほかの issue

modelcontextprotocol/java-sdk の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。