Skip to content

Commit 4f91804

Browse files
feat(server): name the receiving agent for every handoff tool
Folds delegation into mcp.tool.invoked. Every tool call reports the caller's provider; handoff tools (delegate_task, create_threads, t3_thread_launch, t3_thread_send, send_attachments, fork, merge_back, queue edit/steer, pending-request respond, schedule_task, run_scheduled_task_now) also report the provider and model of each thread that received the work, read from the tool result. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 824b743 commit 4f91804

6 files changed

Lines changed: 206 additions & 111 deletions

File tree

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

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import * as Stream from "effect/Stream";
1313
import type * as Types from "effect/Types";
1414
import { McpProtocol, McpSchema, McpServer, Tool, type Toolkit } from "effect/unstable/ai";
1515
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
16-
import { PreviewAutomationError } from "@t3tools/contracts";
16+
import { PreviewAutomationError, ThreadId } from "@t3tools/contracts";
1717

1818
import packageJson from "../../package.json" with { type: "json" };
1919
import * as ServerConfig from "../config.ts";
2020
import * as DeviceService from "../device/DeviceService.ts";
2121
import * as ProviderRegistry from "../provider/Services/ProviderRegistry.ts";
2222
import * as AnalyticsService from "../telemetry/AnalyticsService.ts";
23+
import { agentToolProperties } from "../telemetry/ProviderDimensions.ts";
24+
import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
2325
import * as McpInvocationContext from "./McpInvocationContext.ts";
2426
import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
2527
import { PreviewControlsToolkit } from "./toolkits/previewControls/tools.ts";
@@ -649,38 +651,100 @@ const registerDeviceScreenshot = Effect.fn("McpHttpServer.registerDeviceScreensh
649651
);
650652
});
651653

654+
/**
655+
* Tools that hand work to another thread's agent: they create it, message it,
656+
* steer it, or move context into it. Their events name the receiving agent too.
657+
*/
658+
const HANDOFF_TOOLS: ReadonlySet<string> = new Set([
659+
"delegate_task",
660+
"create_threads",
661+
"t3_thread_launch",
662+
"t3_thread_send",
663+
"t3_thread_send_attachments",
664+
"t3_thread_fork",
665+
"t3_thread_merge_back",
666+
"t3_queue_edit",
667+
"t3_queue_promote_to_steer",
668+
"t3_pending_request_respond",
669+
"schedule_task",
670+
"run_scheduled_task_now",
671+
]);
672+
673+
const resultThreadKeys = ["childThreadId", "targetThreadId", "boundThreadId", "threadId"] as const;
674+
675+
/** Thread ids a handoff tool result names: the thread that received the work. */
676+
const resultThreadIds = (content: unknown): ReadonlyArray<string> => {
677+
if (typeof content !== "object" || content === null) return [];
678+
const record = content as Readonly<Record<string, unknown>>;
679+
if (Array.isArray(record.threads)) return record.threads.flatMap(resultThreadIds);
680+
const key = resultThreadKeys.find((candidate) => typeof record[candidate] === "string");
681+
return key === undefined ? [] : [record[key] as string];
682+
};
683+
652684
/**
653685
* Runs a tool registration against a server that records one anonymous
654-
* `mcp.tool.invoked` event per call: the tool name and the calling agent's
655-
* driver kind. Arguments, results, and ids are never recorded.
686+
* `mcp.tool.invoked` event per call. The event names the tool and the calling
687+
* agent's provider and model. A handoff tool also names the provider and model
688+
* of each thread that received the work, so delegation between providers is
689+
* visible. Arguments, results, ids, and text are never recorded.
656690
*/
657691
const withToolAnalytics = <E, R>(registration: Effect.Effect<void, E, R>) =>
658692
Effect.gen(function* () {
659693
const server = yield* McpServer.McpServer;
660694
const analytics = yield* Effect.serviceOption(AnalyticsService.AnalyticsService);
661695
if (Option.isNone(analytics)) return yield* registration;
662696
const registry = yield* Effect.serviceOption(ProviderRegistry.ProviderRegistry);
663-
const record = (tool: string) =>
697+
const threads = yield* Effect.serviceOption(ThreadManagement.ThreadManagementService);
698+
const modelSelectionOf = (threadId: string) =>
699+
Option.isNone(threads)
700+
? Effect.succeed(undefined)
701+
: threads.value.getThreadShell(ThreadId.make(threadId)).pipe(
702+
Effect.map((shell) => shell?.modelSelection),
703+
Effect.orElseSucceed(() => undefined),
704+
);
705+
const record = (tool: string, result: McpSchema.CallToolResult | undefined) =>
664706
Effect.gen(function* () {
665-
const invocation = yield* Effect.serviceOption(McpInvocationContext.McpInvocationContext);
707+
const invocation = Option.getOrUndefined(
708+
yield* Effect.serviceOption(McpInvocationContext.McpInvocationContext),
709+
);
666710
const providers = Option.isSome(registry) ? yield* registry.value.getProviders : [];
667-
const caller = Option.isSome(invocation)
668-
? providers.find(
669-
(provider) => provider.instanceId === invocation.value.providerInstanceId,
711+
// Only handoff tools pay for thread lookups; the rest report the
712+
// caller's provider from the credential and skip its model.
713+
const handoff = HANDOFF_TOOLS.has(tool);
714+
const caller =
715+
invocation === undefined
716+
? undefined
717+
: handoff
718+
? yield* modelSelectionOf(invocation.threadId)
719+
: { instanceId: invocation.providerInstanceId, model: "" };
720+
const targetIds = handoff
721+
? [...new Set(resultThreadIds(result?.structuredContent))].filter(
722+
(threadId) => threadId !== invocation?.threadId,
670723
)
671-
: undefined;
672-
yield* analytics.value.record("mcp.tool.invoked", {
724+
: [];
725+
const targets = yield* Effect.forEach(targetIds, modelSelectionOf);
726+
for (const properties of agentToolProperties({
673727
tool,
674-
provider: caller?.driver ?? "unknown",
675-
});
728+
providers,
729+
caller,
730+
targets: targets.filter((selection) => selection !== undefined),
731+
})) {
732+
yield* analytics.value.record("mcp.tool.invoked", properties);
733+
}
676734
}).pipe(Effect.ignoreCause);
677735
const recordingServer = McpServer.McpServer.of({
678736
...server,
679737
addTool: (options) =>
680738
server.addTool({
681739
...options,
682740
handle: (payload) =>
683-
record(options.tool.name).pipe(Effect.andThen(options.handle(payload))),
741+
options
742+
.handle(payload)
743+
.pipe(
744+
Effect.onExit((exit) =>
745+
record(options.tool.name, exit._tag === "Success" ? exit.value : undefined),
746+
),
747+
),
684748
}),
685749
});
686750
return yield* registration.pipe(Effect.provideService(McpServer.McpServer, recordingServer));

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ import {
6969
} from "../orchestration-v2/SubagentProjection.ts";
7070
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
7171
import * as ProviderRegistry from "../provider/Services/ProviderRegistry.ts";
72-
import * as AnalyticsService from "../telemetry/AnalyticsService.ts";
73-
import { delegationProperties } from "../telemetry/ProviderDimensions.ts";
7472
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
7573
import type { McpInvocationScope } from "./McpInvocationContext.ts";
7674

@@ -762,14 +760,6 @@ const make = Effect.gen(function* () {
762760
const providerRegistry = yield* ProviderRegistry.ProviderRegistry;
763761
const providerAdapters = yield* ProviderAdapterRegistry.ProviderAdapterRegistryV2;
764762
const scheduledTasks = yield* ScheduledTaskService.ScheduledTaskService;
765-
const analytics = yield* Effect.serviceOption(AnalyticsService.AnalyticsService);
766-
767-
const recordDelegation = (input: Parameters<typeof delegationProperties>[0]) =>
768-
Option.isNone(analytics)
769-
? Effect.void
770-
: analytics.value
771-
.record("mcp.agent.delegated", delegationProperties(input))
772-
.pipe(Effect.ignoreCause);
773763

774764
const requireCapability = (scope: McpInvocationScope) =>
775765
scope.capabilities.has("orchestration")
@@ -1448,12 +1438,6 @@ const make = Effect.gen(function* () {
14481438
);
14491439
}
14501440
const taskId = taskEvent.event.payload.id;
1451-
yield* recordDelegation({
1452-
tool: "delegate_task",
1453-
providers,
1454-
caller: parent.thread.modelSelection,
1455-
target: target.modelSelection,
1456-
});
14571441

14581442
if (input.mode !== "wait") {
14591443
return yield* readTask(scope, taskId, false, true);
@@ -1721,12 +1705,6 @@ const make = Effect.gen(function* () {
17211705
),
17221706
),
17231707
);
1724-
yield* recordDelegation({
1725-
tool: "create_threads",
1726-
providers,
1727-
caller: parent.thread.modelSelection,
1728-
target: target.modelSelection,
1729-
});
17301708
return {
17311709
threadId,
17321710
runId: run?.id ?? null,

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,39 @@ it.effect("returns a bounded public failure without serializing storage causes",
159159
),
160160
);
161161

162-
it.effect("records one anonymous analytics event per tool call", () => {
162+
it.effect("records the calling agent and the agent a tool acted on", () => {
163163
const recorded: Array<{ event: string; properties: unknown }> = [];
164+
const shell = (id: ThreadId, instanceId: string, model: string) =>
165+
({
166+
id,
167+
projectId: "mcp-core-project",
168+
providerInstanceId: ProviderInstanceId.make(instanceId),
169+
modelSelection: { instanceId: ProviderInstanceId.make(instanceId), model },
170+
runtimeMode: "full-access",
171+
interactionMode: "default",
172+
archivedAt: null,
173+
deletedAt: null,
174+
activeRunId: "mcp-core-run",
175+
}) as never;
164176
return Effect.gen(function* () {
165177
const server = yield* McpServer.McpServer;
166178
yield* server
167-
.callTool({ name: "t3_thread_organize", arguments: { action: "pin" } })
179+
.callTool({ name: "t3_thread_fork", arguments: { sourcePoint: { type: "latest_stable" } } })
168180
.pipe(
169181
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
170182
Effect.provideService(McpSchema.McpServerClient, client),
171183
);
172184
expect(recorded).toEqual([
173185
{
174186
event: "mcp.tool.invoked",
175-
properties: { tool: "t3_thread_organize", provider: "codex" },
187+
properties: {
188+
tool: "t3_thread_fork",
189+
callerProvider: "codex",
190+
callerModel: "gpt-5.5",
191+
targetProvider: "claudeAgent",
192+
targetModel: "claude-opus-5-5",
193+
crossProvider: true,
194+
},
176195
},
177196
]);
178197
}).pipe(
@@ -182,13 +201,30 @@ it.effect("records one anonymous analytics event per tool call", () => {
182201
Layer.provide(NodeCrypto.layer),
183202
Layer.provide(
184203
Layer.mock(ThreadManagement.ThreadManagementService)({
185-
getThreadShell: () => Effect.succeed(null),
204+
getThreadShell: (id) =>
205+
Effect.succeed(
206+
id === threadId
207+
? shell(threadId, "codex", "gpt-5.5")
208+
: shell(id, "claudeAgent", "claude-opus-5-5"),
209+
),
210+
getProjectThreadRecords: () =>
211+
Effect.succeed({ thread: shell(threadId, "codex", "gpt-5.5") } as never),
212+
dispatch: () => Effect.succeed({ sequence: 1 } as never),
186213
}),
187214
),
188215
Layer.provide(
189216
Layer.mock(ProviderRegistry.ProviderRegistry)({
190217
getProviders: Effect.succeed([
191-
{ instanceId: ProviderInstanceId.make("codex"), driver: "codex" } as never,
218+
{
219+
instanceId: ProviderInstanceId.make("codex"),
220+
driver: "codex",
221+
models: [{ slug: "gpt-5.5", isCustom: false }],
222+
} as never,
223+
{
224+
instanceId: ProviderInstanceId.make("claudeAgent"),
225+
driver: "claudeAgent",
226+
models: [{ slug: "claude-opus-5-5", isCustom: false }],
227+
} as never,
192228
]),
193229
}),
194230
),

‎apps/server/src/mcp/toolkits/project/handlers.ts‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
import {
2-
MessageId,
3-
type ModelSelection,
4-
ThreadId,
5-
OrchestratorMcpFailure,
6-
ProjectId,
7-
} from "@t3tools/contracts";
1+
import { MessageId, ThreadId, OrchestratorMcpFailure, ProjectId } from "@t3tools/contracts";
82
import * as Effect from "effect/Effect";
93
import * as Option from "effect/Option";
104
import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts";
115
import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts";
126
import * as Project from "../../../project/ProjectService.ts";
137
import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts";
148
import * as Repositories from "../../../sourceControl/SourceControlRepositoryService.ts";
15-
import * as ProviderRegistry from "../../../provider/Services/ProviderRegistry.ts";
16-
import * as AnalyticsService from "../../../telemetry/AnalyticsService.ts";
17-
import { delegationProperties } from "../../../telemetry/ProviderDimensions.ts";
189
import { newCommandId, readCaller, readMutationCaller, unavailable } from "../../threadAccess.ts";
1910
import { ProjectToolkit } from "./tools.ts";
2011

@@ -46,19 +37,6 @@ const mutation = Effect.gen(function* () {
4637
});
4738
return yield* Project.ProjectService;
4839
});
49-
/** Records which agent launched a thread on which provider, without ids or prompts. */
50-
const recordLaunch = (caller: ModelSelection, target: ModelSelection) =>
51-
Effect.gen(function* () {
52-
const analytics = yield* Effect.serviceOption(AnalyticsService.AnalyticsService);
53-
const registry = yield* Effect.serviceOption(ProviderRegistry.ProviderRegistry);
54-
if (Option.isNone(analytics) || Option.isNone(registry)) return;
55-
const providers = yield* registry.value.getProviders;
56-
yield* analytics.value.record(
57-
"mcp.agent.delegated",
58-
delegationProperties({ tool: "t3_thread_launch", providers, caller, target }),
59-
);
60-
}).pipe(Effect.ignoreCause);
61-
6240
export const ProjectHandlersLive = ProjectToolkit.toLayer({
6341
t3_thread_launch: (input) =>
6442
Effect.gen(function* () {
@@ -129,7 +107,6 @@ export const ProjectHandlersLive = ProjectToolkit.toLayer({
129107
);
130108
const thread = result.projection.thread;
131109
const run = result.projection.runs.find((run) => run.userMessageId === messageId);
132-
yield* recordLaunch(caller.modelSelection, thread.modelSelection);
133110
return {
134111
threadId: thread.id,
135112
projectId: thread.projectId,

0 commit comments

Comments
 (0)