otel middleware: no tool spans for server tools called through the ai-sandbox tool bridge (Claude Code harness) #2223
TanStack AI version#
@tanstack/ai 1.8.0, @tanstack/ai-sandbox 1.5.0 (both latest on npm), seen with @tanstack/ai-claude-code 1.0.3
Framework/Library version#
Node 26.12.0, server only (no UI framework involved)
Describe the bug and the steps to reproduce it#
otelMiddleware opens a tool span only in onBeforeToolCall and closes it in onAfterToolCall (src/middlewares/otel.ts:897 and :968). With a harness adapter (ai-claude-code with withSandbox), the sandbox tool bridge calls tool.execute() directly (@tanstack/ai-sandbox src/tool-bridge.ts:191), so those hooks never run. The tool runs, but there is no execute_tool span. onChunk (otel.ts:785) reads only text deltas and RUN_FINISHED, so the TOOL_CALL_* events in the stream do not produce one either.
Steps:
- Install
@tanstack/ai@1.8.0,@tanstack/ai-sandbox@1.5.0,@opentelemetry/sdk-trace-base@2.12.1and@opentelemetry/api@1.9.1in an ESM package. - Save the script below as
repro.mjsand runnode repro.mjs.
The script runs the same server tool twice: through fakeText (the engine executes it), and through a small adapter shaped like ai-claude-code's chatStream. That adapter hands chat()'s tools to createToolBridgeCore and calls bridge.callTool(), which is what the bridge's MCP tools/call handler forwards to. Then it reports the call as resolved TOOL_CALL_* events and finishes with stop, as the real adapter does.
repro.mjs:
import { chat, toolDefinition } from '@tanstack/ai'
import { otelMiddleware } from '@tanstack/ai/middlewares/otel'
import { FakeTextAdapter, fakeText } from '@tanstack/ai/testing'
import { createToolBridgeCore } from '@tanstack/ai-sandbox'
import {
BasicTracerProvider,
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base'
const exporter = new InMemorySpanExporter()
const tracer = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(exporter)],
}).getTracer('repro')
const executed = []
const getWeather = toolDefinition({
name: 'get_weather',
description: 'Weather for a city',
inputSchema: { type: 'object', properties: { city: { type: 'string' } } },
}).server(async ({ city }) => {
executed.push(city)
return { city, sky: 'sunny' }
})
// A harness adapter in miniature, shaped like ai-claude-code's chatStream: it
// hands chat()'s tools to the bridge, the in-sandbox agent calls
// mcp__tanstack__get_weather (here: core.callTool, which the bridge's HTTP
// transport calls per MCP tools/call), and the stream reports the call as
// already resolved TOOL_CALL_START/ARGS/END + TOOL_CALL_RESULT, then 'stop'.
class HarnessAdapter extends FakeTextAdapter {
async *chatStream(options) {
const base = { model: this.model, timestamp: Date.now() }
const ids = { runId: 'run-1', threadId: options.threadId }
const bridge = createToolBridgeCore(options.tools, { threadId: options.threadId })
const toolCallId = 'toolu_1'
const input = { city: 'Paris' }
yield { type: 'RUN_STARTED', ...ids, ...base }
yield { type: 'TOOL_CALL_START', toolCallId, toolCallName: 'get_weather', ...base }
yield { type: 'TOOL_CALL_ARGS', toolCallId, delta: JSON.stringify(input), ...base }
yield { type: 'TOOL_CALL_END', toolCallId, toolCallName: 'get_weather', input, ...base }
const result = await bridge.callTool('get_weather', input)
yield { type: 'TOOL_CALL_RESULT', toolCallId, messageId: 'm-1', content: result.content[0].text, ...base }
yield { type: 'RUN_FINISHED', ...ids, finishReason: 'stop', ...base }
}
}
async function run(adapter) {
exporter.reset()
executed.length = 0
const stream = chat({
adapter,
threadId: 'thread-1',
messages: [{ role: 'user', content: 'Weather in Paris?' }],
tools: [getWeather],
middleware: [otelMiddleware({ tracer })],
})
for await (const _ of stream);
return { spans: exporter.getFinishedSpans().map((s) => s.name), executed: [...executed] }
}
const normal = fakeText()
normal.setResponses([
{ toolCalls: [{ name: 'get_weather', input: { city: 'Paris' } }] },
{ text: 'Sunny in Paris.' },
])
const show = (label, r) => console.log(label, JSON.stringify(r.spans), 'executed:', JSON.stringify(r.executed))
show('normal: ', await run(normal))
show('bridged:', await run(new HarnessAdapter('fake-model', {})))
// expected bridged: ["execute_tool get_weather","chat fake-model #0","chat fake-model"] executed: ["Paris"]
// actual bridged: ["chat fake-model #0","chat fake-model"] executed: ["Paris"]
Output:
normal: ["execute_tool get_weather","chat fake-model #0","chat fake-model #1","chat fake-model"] executed: ["Paris"]
bridged: ["chat fake-model #0","chat fake-model"] executed: ["Paris"]
Expected: the bridged run also has an execute_tool get_weather span. Either bridged tool calls run the tool-call middleware hooks, or the middleware builds tool spans from the stream's TOOL_CALL_* events when no hook fires.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
Inline above. Plain Node script, no network, no model, no claude CLI.
Screenshots or Videos (Optional)#
None.
Do you intend to try to help solve this bug with your own PR?#
Maybe, I'll investigate and start debugging
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) assignedjherr
github-actions[bot](GitHub profile, opens in a new tab) added labelsneeds-repro
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
github-actions[bot](GitHub profile, opens in a new tab) removed labelsneeds-repro
I added a minimal, runnable reproduction to the description: a plain Node script with no network, no model and no API key, tested on the current npm releases.
Sign in to join this conversation.
Thanks for the report, @ozolcar! To help us reproduce this, could you add a minimal reproduction?
A runnable example makes fixes dramatically faster — you can fork one of the official examples, or use StackBlitz / CodeSandbox. For TypeScript-only issues, a TS Playground link works too.
Automated triage — a maintainer will follow up.