@@ -24,6 +24,7 @@ import {
2424import { LangChainAdapter } from '../adapters/langchain_adapter.js'
2525import { logger } from '../logging.js'
2626import { ServerManager } from '../managers/server_manager.js'
27+ import { extractModelInfo , Telemetry } from '../telemetry/index.js'
2728import { createSystemMessage } from './prompts/system_prompt_builder.js'
2829import { DEFAULT_SYSTEM_PROMPT_TEMPLATE , SERVER_MANAGER_SYSTEM_PROMPT_TEMPLATE } from './prompts/templates.js'
2930
@@ -50,6 +51,9 @@ export class MCPAgent {
5051 private tools : StructuredToolInterface [ ] = [ ]
5152 private adapter : LangChainAdapter
5253 private serverManager : ServerManager | null = null
54+ private telemetry : Telemetry
55+ private modelProvider : string
56+ private modelName : string
5357
5458 constructor ( options : {
5559 llm : BaseLanguageModelInterface
@@ -98,6 +102,13 @@ export class MCPAgent {
98102 else {
99103 this . adapter = options . adapter ?? new LangChainAdapter ( this . disallowedTools )
100104 }
105+
106+ // Initialize telemetry
107+ this . telemetry = Telemetry . getInstance ( )
108+ // Track model info for telemetry
109+ const [ provider , name ] = extractModelInfo ( this . llm as any )
110+ this . modelProvider = provider
111+ this . modelName = name
101112 }
102113
103114 public async initialize ( ) : Promise < void > {
@@ -298,6 +309,10 @@ export class MCPAgent {
298309 ) : AsyncGenerator < AgentStep , string , void > {
299310 let result = ''
300311 let initializedHere = false
312+ const startTime = Date . now ( )
313+ const toolsUsedNames : string [ ] = [ ]
314+ let stepsTaken = 0
315+ let success = false
301316
302317 try {
303318 if ( manageConnector && ! this . initialized ) {
@@ -340,6 +355,7 @@ export class MCPAgent {
340355 logger . info ( `🏁 Starting agent execution with max_steps=${ steps } ` )
341356
342357 for ( let stepNum = 0 ; stepNum < steps ; stepNum ++ ) {
358+ stepsTaken = stepNum + 1
343359 if ( this . useServerManager && this . serverManager ) {
344360 const currentTools = this . serverManager . tools
345361 const currentToolNames = new Set ( currentTools . map ( t => t . name ) )
@@ -385,6 +401,7 @@ export class MCPAgent {
385401 yield step
386402 const { action, observation } = step
387403 const toolName = action . tool
404+ toolsUsedNames . push ( toolName )
388405 let toolInputStr = String ( action . toolInput )
389406 if ( toolInputStr . length > 100 )
390407 toolInputStr = `${ toolInputStr . slice ( 0 , 97 ) } ...`
@@ -432,6 +449,7 @@ export class MCPAgent {
432449 }
433450
434451 logger . info ( '🎉 Agent execution complete' )
452+ success = true
435453 return result
436454 }
437455 catch ( e ) {
@@ -443,6 +461,44 @@ export class MCPAgent {
443461 throw e
444462 }
445463 finally {
464+ // Track comprehensive execution data
465+ const executionTimeMs = Date . now ( ) - startTime
466+
467+ let serverCount = 0
468+ if ( this . client ) {
469+ serverCount = Object . keys ( await this . client . getAllActiveSessions ( ) ) . length
470+ }
471+ else if ( this . connectors ) {
472+ serverCount = this . connectors . length
473+ }
474+
475+ const conversationHistoryLength = this . memoryEnabled ? this . conversationHistory . length : 0
476+
477+ await this . telemetry . trackAgentExecution ( {
478+ executionMethod : 'stream' ,
479+ query,
480+ success,
481+ modelProvider : this . modelProvider ,
482+ modelName : this . modelName ,
483+ serverCount,
484+ serverIdentifiers : this . connectors . map ( connector => connector . publicIdentifier ) ,
485+ totalToolsAvailable : this . tools . length ,
486+ toolsAvailableNames : this . tools . map ( t => t . name ) ,
487+ maxStepsConfigured : this . maxSteps ,
488+ memoryEnabled : this . memoryEnabled ,
489+ useServerManager : this . useServerManager ,
490+ maxStepsUsed : maxSteps ?? null ,
491+ manageConnector,
492+ externalHistoryUsed : externalHistory !== undefined ,
493+ stepsTaken,
494+ toolsUsedCount : toolsUsedNames . length ,
495+ toolsUsedNames,
496+ response : result ,
497+ executionTimeMs,
498+ errorType : success ? null : 'execution_error' ,
499+ conversationHistoryLength,
500+ } )
501+
446502 if ( manageConnector && ! this . client && initializedHere ) {
447503 logger . info ( '🧹 Closing agent after query completion' )
448504 await this . close ( )
0 commit comments