From e69b72ce3ace5784762c86086429744fbd8d37e1 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:58:23 +0530 Subject: [PATCH] fix: pair Anthropic MCP connector tool calls with their results Anthropic's MCP connector emits mcp_tool_use / mcp_tool_result blocks. Only server_tool_use was registered as a call, so mcp_tool_result spans were emitted unpaired, named "mcp_tool" and missing input. Fixes #191 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- .../InstrumentationSemConv.java | 14 +++++-- .../InstrumentationSemConvTest.java | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/braintrust-sdk/src/main/java/dev/braintrust/instrumentation/InstrumentationSemConv.java b/braintrust-sdk/src/main/java/dev/braintrust/instrumentation/InstrumentationSemConv.java index 04ee585f..9e350dc5 100644 --- a/braintrust-sdk/src/main/java/dev/braintrust/instrumentation/InstrumentationSemConv.java +++ b/braintrust-sdk/src/main/java/dev/braintrust/instrumentation/InstrumentationSemConv.java @@ -770,12 +770,15 @@ private static long addCacheCreationMetrics(Map metrics, JsonNod } private static final String ANTHROPIC_SERVER_TOOL_USE_TYPE = "server_tool_use"; + // MCP connector tool calls use their own call block type, paired with "mcp_tool_result". + private static final String ANTHROPIC_MCP_TOOL_USE_TYPE = "mcp_tool_use"; private static final String ANTHROPIC_TOOL_RESULT_SUFFIX = "_tool_result"; /** * Emit child tool spans for Anthropic server-side tool use. In the Message {@code content} - * array these appear as a {@code server_tool_use} block (the call) and a matching {@code - * *_tool_result} block (e.g. {@code web_search_tool_result}), linked by {@code id} /{@code + * array these appear as a {@code server_tool_use} block (or {@code mcp_tool_use} for MCP + * connector calls) and a matching {@code *_tool_result} block (e.g. {@code + * web_search_tool_result} or {@code mcp_tool_result}), linked by {@code id} /{@code * tool_use_id}. Calls and results are paired (buffering results that arrive before their call); * unmatched calls and results each still get a span. Mirrors the Python SDK's {@code * _log_server_tool_spans}. @@ -797,7 +800,7 @@ private static void addAnthropicServerSideChildSpans( continue; } String itemType = item.path("type").asText(null); - if (ANTHROPIC_SERVER_TOOL_USE_TYPE.equals(itemType)) { + if (isAnthropicServerToolCallType(itemType)) { JsonNode id = item.get("id"); if (id != null && id.isTextual()) { callsById.put(id.asText(), item); @@ -847,6 +850,11 @@ private static void addAnthropicServerSideChildSpans( } } + private static boolean isAnthropicServerToolCallType(@Nullable String type) { + return ANTHROPIC_SERVER_TOOL_USE_TYPE.equals(type) + || ANTHROPIC_MCP_TOOL_USE_TYPE.equals(type); + } + private static boolean isAnthropicToolResultType(@Nullable String type) { return type != null && type.endsWith(ANTHROPIC_TOOL_RESULT_SUFFIX) diff --git a/braintrust-sdk/src/test/java/dev/braintrust/instrumentation/InstrumentationSemConvTest.java b/braintrust-sdk/src/test/java/dev/braintrust/instrumentation/InstrumentationSemConvTest.java index c58bd4f2..942a7767 100644 --- a/braintrust-sdk/src/test/java/dev/braintrust/instrumentation/InstrumentationSemConvTest.java +++ b/braintrust-sdk/src/test/java/dev/braintrust/instrumentation/InstrumentationSemConvTest.java @@ -329,6 +329,43 @@ void pairsServerToolUseWithResultAndRedactsEncryptedContent() { assertEquals("", output.get(0).path("encrypted_content").asText()); } + @Test + void pairsMcpToolUseWithMcpToolResult() { + // MCP connector calls use "mcp_tool_use" (not "server_tool_use") for the call block. + String body = + """ + { + "content": [ + {"type": "mcp_tool_use", "id": "mcptoolu_1", "name": "ask_wiki_question", + "server_name": "deepwiki", "input": {"repoName": "braintrustdata/sdk"}}, + {"type": "mcp_tool_result", "tool_use_id": "mcptoolu_1", "is_error": false, + "content": [{"type": "text", "text": "it is written in Java"}]}, + {"type": "text", "text": "here is the answer"} + ] + } + """; + + List tools = emitAnthropic(body); + assertEquals(1, tools.size()); + SpanData mcp = byName(tools, "ask_wiki_question"); + + SpanData llm = byName(exporter.getFinishedSpanItems(), "llm"); + assertEquals(llm.getSpanId(), mcp.getParentSpanId()); + assertEquals("{\"type\":\"tool\"}", mcp.getAttributes().get(SPAN_ATTRIBUTES)); + + assertEquals( + "braintrustdata/sdk", + json(mcp.getAttributes().get(INPUT_JSON)).path("repoName").asText()); + + JsonNode md = json(mcp.getAttributes().get(METADATA)); + assertEquals("mcptoolu_1", md.path("tool_use_id").asText()); + assertEquals("mcp_tool_use", md.path("tool_call_type").asText()); + assertEquals("mcp_tool_result", md.path("tool_result_type").asText()); + + JsonNode output = json(mcp.getAttributes().get(OUTPUT_JSON)); + assertEquals("it is written in Java", output.get(0).path("text").asText()); + } + @Test void serverToolResultErrorSetsErrorStatus() { String body =