Skip to content
TypeScript

fix(server): stop flooding analytics with successful MCP tool calls (#24287)

Theo Brownefef851bee091
diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts
index 5c6c4fece44..2e0d16c535c 100644
--- a/apps/server/src/mcp/McpHttpServer.ts
+++ b/apps/server/src/mcp/McpHttpServer.ts
@@ -816,9 +816,10 @@ const handoffTargetIds = (
};
/**
- * Runs a tool registration against a server that records one anonymous
- * `mcp.tool.invoked` event per call: the tool, the calling agent's provider,
- * the outcome with its failure code, and the duration. A handoff tool also
+ * Runs a tool registration against a server that records an anonymous
+ * `mcp.tool.invoked` event for every handoff call and every failed call of
+ * other tools: the tool, the calling agent's provider, the outcome with its
+ * failure code, and the duration. A handoff tool also
* reports the caller's model, origin, and delegation depth, the settings the
* agent chose, and the provider, model, and modes of each thread that received
* the work. Ids, prompts, and free text are never recorded.
@@ -882,6 +883,11 @@ const withToolAnalytics =
result: McpSchema.CallToolResult | undefined,
durationMs: number,
) {
+ const handoff = HANDOFF_TOOLS.has(tool);
+ const outcome = toolOutcome(result);
+ // Agents call status, read, and preview tools in tight loops, so an
+ // event per successful call floods analytics. Only failures count there.
+ if (!handoff && outcome.outcome === "ok") return;
// Client credentials (no calling thread) report an unknown caller.
const invocation = Option.getOrUndefined(
yield* Effect.serviceOption(McpInvocationContext.McpInvocationContext),
@@ -889,7 +895,6 @@ const withToolAnalytics =
const providers = Option.isSome(registry) ? yield* registry.value.getProviders : [];
// Only handoff tools pay for thread lookups; the rest report the
// caller's provider from the credential.
- const handoff = HANDOFF_TOOLS.has(tool);
const caller =
handoff && invocation !== undefined
? ((yield* shellOf(invocation.threadId)) ?? undefined)
@@ -917,7 +922,7 @@ const withToolAnalytics =
),
}),
targets: targets.filter((thread) => thread !== null),
- outcome: toolOutcome(result),
+ outcome,
...(handoff
? { settings: handoffSettings(tool, args, result?.structuredContent) }
: {}),
diff --git a/apps/server/src/mcp/toolkits/core.test.ts b/apps/server/src/mcp/toolkits/core.test.ts
index 02216cec576..1e4abf2062e 100644
--- a/apps/server/src/mcp/toolkits/core.test.ts
+++ b/apps/server/src/mcp/toolkits/core.test.ts
@@ -18,6 +18,7 @@ import {
type OrchestrationV2ThreadShell,
} from "@t3tools/contracts";
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
+import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as TestClock from "effect/testing/TestClock";
import * as Layer from "effect/Layer";
@@ -48,6 +49,7 @@ import * as ServerSettings from "../../serverSettings.ts";
import * as McpHttpServer from "../McpHttpServer.ts";
import * as McpInvocationContext from "../McpInvocationContext.ts";
import * as McpToolAccessTestkit from "../McpToolAccess.testkit.ts";
+import * as AnalyticsService from "../../telemetry/AnalyticsService.ts";
import { dispatchFailure } from "../threadAccess.ts";
import * as EnvironmentHandlers from "./environment/handlers.ts";
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
),
);
+it.effect("records only failed calls of tools that hand off no work", () => {
+ // Recording runs detached from the tool response, so the first event is awaited.
+ const recorded = Deferred.makeUnsafe<{ event: string; properties: object }>();
+ return Effect.gen(function* () {
+ const server = yield* McpServer.McpServer;
+ const organize = (capabilities: McpInvocationContext.McpInvocationScope["capabilities"]) =>
+ server.callTool({ name: "t3_thread_organize", arguments: { action: "pin" } }).pipe(
+ Effect.provideService(McpInvocationContext.McpInvocationContext, {
+ ...scope,
+ capabilities,
+ }),
+ Effect.provideService(McpSchema.McpServerClient, client),
+ );
+ expect((yield* organize(scope.capabilities)).isError).not.toBe(true);
+ expect(declaredFailure(yield* organize(new Set<never>()))).toMatchObject({
+ code: "capability_denied",
+ });
+ // The successful call ran first, so it would be the first event if it were recorded.
+ expect(yield* Deferred.await(recorded)).toMatchObject({
+ event: "mcp.tool.invoked",
+ properties: { tool: "t3_thread_organize", outcome: "error" },
+ });
+ }).pipe(
+ Effect.provide(
+ layerThreadToolkit.pipe(
+ Layer.provideMerge(McpServer.McpServer.layer),
+ Layer.provide(NodeCrypto.layer),
+ Layer.provide(
+ Layer.mock(ThreadManagement.ThreadManagementService)({
+ getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)),
+ getProjectThreadRecords: ({ threadId: id }) =>
+ Effect.succeed(
+ McpToolAccessTestkit.idleThreadProjection(McpToolAccessTestkit.liveThreadShell(id)),
+ ),
+ dispatch: () => Effect.succeed({ sequence: 1 } as never),
+ }),
+ ),
+ Layer.provide(
+ Layer.succeed(
+ AnalyticsService.AnalyticsService,
+ AnalyticsService.AnalyticsService.of({
+ record: (event, properties) =>
+ Deferred.succeed(recorded, { event, properties: properties ?? {} }).pipe(
+ Effect.asVoid,
+ ),
+ flush: Effect.void,
+ recordOwners: () => Effect.void,
+ }),
+ ),
+ ),
+ ),
+ ),
+ );
+});
+
it.effect("keeps unexpected handler defects private through the production registration", () =>
Effect.gen(function* () {
const server = yield* McpServer.McpServer;