@@ -13,13 +13,15 @@ import * as Stream from "effect/Stream";
1313import type * as Types from "effect/Types" ;
1414import { McpProtocol , McpSchema , McpServer , Tool , type Toolkit } from "effect/unstable/ai" ;
1515import { HttpRouter , HttpServerRequest , HttpServerResponse } from "effect/unstable/http" ;
16- import { PreviewAutomationError } from "@t3tools/contracts" ;
16+ import { PreviewAutomationError , ThreadId } from "@t3tools/contracts" ;
1717
1818import packageJson from "../../package.json" with { type : "json" } ;
1919import * as ServerConfig from "../config.ts" ;
2020import * as DeviceService from "../device/DeviceService.ts" ;
2121import * as ProviderRegistry from "../provider/Services/ProviderRegistry.ts" ;
2222import * as AnalyticsService from "../telemetry/AnalyticsService.ts" ;
23+ import { agentToolProperties } from "../telemetry/ProviderDimensions.ts" ;
24+ import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts" ;
2325import * as McpInvocationContext from "./McpInvocationContext.ts" ;
2426import * as OrchestratorMcpService from "./OrchestratorMcpService.ts" ;
2527import { 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 */
657691const 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 ) ) ;
0 commit comments