Skip to content

Commit 0055bd7

Browse files
fix(server): MCP tool responses no longer wait on analytics lookups
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a30d041 commit 0055bd7

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,13 @@ const withToolAnalytics = <A, E, R>(registration: Effect.Effect<A, E, R>) =>
927927
const result = exit._tag === "Success" ? exit.value : undefined;
928928
// A call that asks for input is retried with it, and that call is the one counted.
929929
if (result instanceof McpSchema.InputRequired) return Effect.void;
930+
// Handoff tools look up threads to record; the response must not wait on them.
930931
return Clock.currentTimeMillis.pipe(
931932
Effect.flatMap((endedAt) =>
932933
record(options.tool.name, payload, result, endedAt - startedAt),
933934
),
935+
Effect.forkDetach,
936+
Effect.asVoid,
934937
);
935938
}),
936939
);

‎apps/server/src/mcp/toolkits/core.test.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type OrchestrationV2ProjectedTurnItem,
1919
type OrchestrationV2ThreadShell,
2020
} from "@t3tools/contracts";
21+
import * as Deferred from "effect/Deferred";
2122
import * as Effect from "effect/Effect";
2223
import * as TestClock from "effect/testing/TestClock";
2324
import * as Layer from "effect/Layer";
@@ -364,7 +365,12 @@ it.effect("keeps unexpected handler defects private through the production regis
364365
);
365366

366367
it.effect("records the calling agent and the agent a tool acted on", () => {
367-
const recorded: Array<{ event: string; properties: unknown }> = [];
368+
// Recording runs detached from the tool response, so each expected event is awaited.
369+
const recorded = [
370+
Deferred.makeUnsafe<{ event: string; properties: object }>(),
371+
Deferred.makeUnsafe<{ event: string; properties: object }>(),
372+
];
373+
let recordedCount = 0;
368374
const shell = (id: ThreadId, instanceId: string, model: string) =>
369375
({
370376
id,
@@ -392,8 +398,7 @@ it.effect("records the calling agent and the agent a tool acted on", () => {
392398
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
393399
Effect.provideService(McpSchema.McpServerClient, client),
394400
);
395-
expect(recorded).toHaveLength(1);
396-
const [{ event, properties }] = recorded as [{ event: string; properties: object }];
401+
const { event, properties } = yield* Deferred.await(recorded[0]!);
397402
expect(event).toBe("mcp.tool.invoked");
398403
expect(properties).toMatchObject({
399404
tool: "t3_thread_fork",
@@ -423,7 +428,8 @@ it.effect("records the calling agent and the agent a tool acted on", () => {
423428
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
424429
Effect.provideService(McpSchema.McpServerClient, client),
425430
);
426-
expect(recorded[1]?.properties).toMatchObject({
431+
// The fork recorded a single event, so the next one is the queue edit.
432+
expect((yield* Deferred.await(recorded[1]!)).properties).toMatchObject({
427433
tool: "t3_queue_edit",
428434
outcome: "ok",
429435
callerProvider: "codex",
@@ -470,7 +476,10 @@ it.effect("records the calling agent and the agent a tool acted on", () => {
470476
AnalyticsService.AnalyticsService,
471477
AnalyticsService.AnalyticsService.of({
472478
record: (event, properties) =>
473-
Effect.sync(() => void recorded.push({ event, properties })),
479+
Deferred.succeed(recorded[recordedCount++]!, {
480+
event,
481+
properties: properties ?? {},
482+
}).pipe(Effect.asVoid),
474483
flush: Effect.void,
475484
}),
476485
),

0 commit comments

Comments
 (0)