Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -770,12 +770,15 @@ private static long addCacheCreationMetrics(Map<String, Object> 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}.
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,43 @@ void pairsServerToolUseWithResultAndRedactsEncryptedContent() {
assertEquals("<redacted>", 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<SpanData> 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 =
Expand Down
Loading