@@ -20,7 +20,13 @@ import * 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" ;
23+ import {
24+ type AgentThread ,
25+ agentToolProperties ,
26+ handoffSettings ,
27+ MAX_REPORTED_DEPTH ,
28+ toolOutcome ,
29+ } from "../telemetry/ProviderDimensions.ts" ;
2430import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts" ;
2531import * as McpInvocationContext from "./McpInvocationContext.ts" ;
2632import * as OrchestratorMcpService from "./OrchestratorMcpService.ts" ;
@@ -683,10 +689,11 @@ const resultThreadIds = (content: unknown): ReadonlyArray<string> => {
683689
684690/**
685691 * Runs a tool registration against a server that records one anonymous
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.
692+ * `mcp.tool.invoked` event per call: the tool, the calling agent's provider,
693+ * the outcome with its failure code, and the duration. A handoff tool also
694+ * reports the caller's model, origin, and delegation depth, the settings the
695+ * agent chose, and the provider, model, and modes of each thread that received
696+ * the work. Ids, prompts, and free text are never recorded.
690697 */
691698const withToolAnalytics = < E , R > ( registration : Effect . Effect < void , E , R > ) =>
692699 Effect . gen ( function * ( ) {
@@ -695,39 +702,88 @@ const withToolAnalytics = <E, R>(registration: Effect.Effect<void, E, R>) =>
695702 if ( Option . isNone ( analytics ) ) return yield * registration ;
696703 const registry = yield * Effect . serviceOption ( ProviderRegistry . ProviderRegistry ) ;
697704 const threads = yield * Effect . serviceOption ( ThreadManagement . ThreadManagementService ) ;
698- const modelSelectionOf = ( threadId : string ) =>
705+ const shellOf = ( threadId : string ) =>
699706 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 ) =>
707+ ? Effect . succeed ( null )
708+ : threads . value
709+ . getThreadShell ( ThreadId . make ( threadId ) )
710+ . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ;
711+ // Delegation depth: parents walked from the caller, capped.
712+ const depthOf = ( thread : AgentThread ) =>
713+ Effect . gen ( function * ( ) {
714+ let depth = 0 ;
715+ let parentId =
716+ thread . lineage . relationshipToParent === "subagent" ? thread . lineage . parentThreadId : null ;
717+ while ( parentId !== null && depth < MAX_REPORTED_DEPTH ) {
718+ depth += 1 ;
719+ const parent = yield * shellOf ( parentId ) ;
720+ parentId =
721+ parent ?. lineage . relationshipToParent === "subagent"
722+ ? parent . lineage . parentThreadId
723+ : null ;
724+ }
725+ return depth ;
726+ } ) ;
727+ // Whether the caller's active run was started by a scheduled task.
728+ const scheduledRunOf = ( threadId : string , runId : string | null ) =>
729+ Option . isNone ( threads ) || runId === null
730+ ? Effect . succeed ( false )
731+ : threads . value
732+ . getThreadRecords ( ThreadId . make ( threadId ) , [ "runs" , "messages" ] , {
733+ runIds : [ runId as never ] ,
734+ messageRoles : [ "user" ] ,
735+ } )
736+ . pipe (
737+ Effect . map ( ( records ) => {
738+ const run = records . runs . find ( ( candidate ) => candidate . id === runId ) ;
739+ return records . messages . some (
740+ ( message ) =>
741+ message . id === run ?. userMessageId && message . scheduledTaskId !== undefined ,
742+ ) ;
743+ } ) ,
744+ Effect . orElseSucceed ( ( ) => false ) ,
745+ ) ;
746+ const record = (
747+ tool : string ,
748+ args : unknown ,
749+ result : McpSchema . CallToolResult | undefined ,
750+ durationMs : number ,
751+ ) =>
706752 Effect . gen ( function * ( ) {
707753 const invocation = Option . getOrUndefined (
708754 yield * Effect . serviceOption ( McpInvocationContext . McpInvocationContext ) ,
709755 ) ;
710756 const providers = Option . isSome ( registry ) ? yield * registry . value . getProviders : [ ] ;
711757 // Only handoff tools pay for thread lookups; the rest report the
712- // caller's provider from the credential and skip its model .
758+ // caller's provider from the credential.
713759 const handoff = HANDOFF_TOOLS . has ( tool ) ;
714760 const caller =
715- invocation === undefined
716- ? undefined
717- : handoff
718- ? yield * modelSelectionOf ( invocation . threadId )
719- : { instanceId : invocation . providerInstanceId , model : "" } ;
761+ handoff && invocation !== undefined
762+ ? ( ( yield * shellOf ( invocation . threadId ) ) ?? undefined )
763+ : undefined ;
720764 const targetIds = handoff
721765 ? [ ...new Set ( resultThreadIds ( result ?. structuredContent ) ) ] . filter (
722766 ( threadId ) => threadId !== invocation ?. threadId ,
723767 )
724768 : [ ] ;
725- const targets = yield * Effect . forEach ( targetIds , modelSelectionOf ) ;
769+ const targets = yield * Effect . forEach ( targetIds , shellOf ) ;
726770 for ( const properties of agentToolProperties ( {
727771 tool,
728772 providers,
729773 caller,
730- targets : targets . filter ( ( selection ) => selection !== undefined ) ,
774+ ...( invocation === undefined
775+ ? { }
776+ : { callerProviderInstanceId : invocation . providerInstanceId } ) ,
777+ ...( caller === undefined || invocation === undefined
778+ ? { }
779+ : {
780+ callerDepth : yield * depthOf ( caller ) ,
781+ callerScheduledRun : yield * scheduledRunOf ( invocation . threadId , caller . activeRunId ) ,
782+ } ) ,
783+ targets : targets . filter ( ( thread ) => thread !== null ) ,
784+ outcome : toolOutcome ( result ) ,
785+ ...( handoff ? { settings : handoffSettings ( tool , args , result ?. structuredContent ) } : { } ) ,
786+ durationMs,
731787 } ) ) {
732788 yield * analytics . value . record ( "mcp.tool.invoked" , properties ) ;
733789 }
@@ -738,13 +794,25 @@ const withToolAnalytics = <E, R>(registration: Effect.Effect<void, E, R>) =>
738794 server . addTool ( {
739795 ...options ,
740796 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- ) ,
797+ Effect . gen ( function * ( ) {
798+ const startedAt = yield * Clock . currentTimeMillis ;
799+ return yield * options
800+ . handle ( payload )
801+ . pipe (
802+ Effect . onExit ( ( exit ) =>
803+ Clock . currentTimeMillis . pipe (
804+ Effect . flatMap ( ( endedAt ) =>
805+ record (
806+ options . tool . name ,
807+ payload ,
808+ exit . _tag === "Success" ? exit . value : undefined ,
809+ endedAt - startedAt ,
810+ ) ,
811+ ) ,
812+ ) ,
813+ ) ,
814+ ) ;
815+ } ) ,
748816 } ) ,
749817 } ) ;
750818 return yield * registration . pipe ( Effect . provideService ( McpServer . McpServer , recordingServer ) ) ;
0 commit comments