The best way to control Claude Code, Codex and more from any computer or phone
fix(server): stop flooding analytics with successful MCP tool calls (#24287)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Oct 24, 2026 at 03:40 UTC
fef851bee09102641061aeb78a3092c2ba2c72ab
2 files changed
+67
-5
apps/server/src/mcp/McpHttpServer.ts
+10
-5
@@ -816,9 +816,10 @@ const handoffTargetIds = (
816
};
817
818
/**
819
- * Runs a tool registration against a server that records one anonymous
820
- * `mcp.tool.invoked` event per call: the tool, the calling agent's provider,
821
- * the outcome with its failure code, and the duration. A handoff tool also
819
+ * Runs a tool registration against a server that records an anonymous
820
+ * `mcp.tool.invoked` event for every handoff call and every failed call of
821
+ * other tools: the tool, the calling agent's provider, the outcome with its
822
+ * failure code, and the duration. A handoff tool also
823
* reports the caller's model, origin, and delegation depth, the settings the
824
* agent chose, and the provider, model, and modes of each thread that received
825
* the work. Ids, prompts, and free text are never recorded.
@@ -882,6 +883,11 @@ const withToolAnalytics =
883
result: McpSchema.CallToolResult | undefined,
884
durationMs: number,
885
) {
886
+ const handoff = HANDOFF_TOOLS.has(tool);
887
+ const outcome = toolOutcome(result);
888
+ // Agents call status, read, and preview tools in tight loops, so an
889
+ // event per successful call floods analytics. Only failures count there.
890
+ if (!handoff && outcome.outcome === "ok") return;
891
// Client credentials (no calling thread) report an unknown caller.
892
const invocation = Option.getOrUndefined(
893
yield* Effect.serviceOption(McpInvocationContext.McpInvocationContext),
@@ -889,7 +895,6 @@ const withToolAnalytics =
895
const providers = Option.isSome(registry) ? yield* registry.value.getProviders : [];
896
// Only handoff tools pay for thread lookups; the rest report the
897
// caller's provider from the credential.
892
- const handoff = HANDOFF_TOOLS.has(tool);
898
const caller =
899
handoff && invocation !== undefined
900
? ((yield* shellOf(invocation.threadId)) ?? undefined)
@@ -917,7 +922,7 @@ const withToolAnalytics =
922
),
923
}),
924
targets: targets.filter((thread) => thread !== null),
920
- outcome: toolOutcome(result),
925
+ outcome,
926
...(handoff
927
? { settings: handoffSettings(tool, args, result?.structuredContent) }
928
: {}),
apps/server/src/mcp/toolkits/core.test.ts
+57
@@ -18,6 +18,7 @@ import {
18
type OrchestrationV2ThreadShell,
19
} from "@t3tools/contracts";
20
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
21
+import * as Deferred from "effect/Deferred";
22
import * as Effect from "effect/Effect";
23
import * as TestClock from "effect/testing/TestClock";
24
import * as Layer from "effect/Layer";
@@ -48,6 +49,7 @@ import * as ServerSettings from "../../serverSettings.ts";
49
import * as McpHttpServer from "../McpHttpServer.ts";
50
import * as McpInvocationContext from "../McpInvocationContext.ts";
51
import * as McpToolAccessTestkit from "../McpToolAccess.testkit.ts";
52
+import * as AnalyticsService from "../../telemetry/AnalyticsService.ts";
53
import { dispatchFailure } from "../threadAccess.ts";
54
import * as EnvironmentHandlers from "./environment/handlers.ts";
55
import * as AttachmentHandlers from "./attachment/handlers.ts";
@@ -287,6 +289,61 @@ it.effect("returns an HTML render reference that Codex and Claude tool rows both
289
),
290
);
291
292
+it.effect("records only failed calls of tools that hand off no work", () => {
293
+ // Recording runs detached from the tool response, so the first event is awaited.
294
+ const recorded = Deferred.makeUnsafe<{ event: string; properties: object }>();
295
+ return Effect.gen(function* () {
296
+ const server = yield* McpServer.McpServer;
297
+ const organize = (capabilities: McpInvocationContext.McpInvocationScope["capabilities"]) =>
298
+ server.callTool({ name: "t3_thread_organize", arguments: { action: "pin" } }).pipe(
299
+ Effect.provideService(McpInvocationContext.McpInvocationContext, {
300
+ ...scope,
301
+ capabilities,
302
+ }),
303
+ Effect.provideService(McpSchema.McpServerClient, client),
304
+ );
305
+ expect((yield* organize(scope.capabilities)).isError).not.toBe(true);
306
+ expect(declaredFailure(yield* organize(new Set<never>()))).toMatchObject({
307
+ code: "capability_denied",
308
+ });
309
+ // The successful call ran first, so it would be the first event if it were recorded.
310
+ expect(yield* Deferred.await(recorded)).toMatchObject({
311
+ event: "mcp.tool.invoked",
312
+ properties: { tool: "t3_thread_organize", outcome: "error" },
313
+ });
314
+ }).pipe(
315
+ Effect.provide(
316
+ layerThreadToolkit.pipe(
317
+ Layer.provideMerge(McpServer.McpServer.layer),
318
+ Layer.provide(NodeCrypto.layer),
319
+ Layer.provide(
320
+ Layer.mock(ThreadManagement.ThreadManagementService)({
321
+ getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)),
322
+ getProjectThreadRecords: ({ threadId: id }) =>
323
+ Effect.succeed(
324
+ McpToolAccessTestkit.idleThreadProjection(McpToolAccessTestkit.liveThreadShell(id)),
325
+ ),
326
+ dispatch: () => Effect.succeed({ sequence: 1 } as never),
327
+ }),
328
+ ),
329
+ Layer.provide(
330
+ Layer.succeed(
331
+ AnalyticsService.AnalyticsService,
332
+ AnalyticsService.AnalyticsService.of({
333
+ record: (event, properties) =>
334
+ Deferred.succeed(recorded, { event, properties: properties ?? {} }).pipe(
335
+ Effect.asVoid,
336
+ ),
337
+ flush: Effect.void,
338
+ recordOwners: () => Effect.void,
339
+ }),
340
+ ),
341
+ ),
342
+ ),
343
+ ),
344
+ );
345
+});
346
+
347
it.effect("keeps unexpected handler defects private through the production registration", () =>
348
Effect.gen(function* () {
349
const server = yield* McpServer.McpServer;