1+ import type { BaseCallbackHandler } from '@langchain/core/callbacks/base'
12import type { BaseLanguageModelInterface , LanguageModelLike } from '@langchain/core/language_models/base'
23import type {
34 BaseMessage ,
@@ -29,6 +30,7 @@ import { zodToJsonSchema } from 'zod-to-json-schema'
2930import { LangChainAdapter } from '../adapters/langchain_adapter.js'
3031import { logger } from '../logging.js'
3132import { ServerManager } from '../managers/server_manager.js'
33+ import { ObservabilityManager } from '../observability/index.js'
3234import { extractModelInfo , Telemetry } from '../telemetry/index.js'
3335import { createSystemMessage } from './prompts/system_prompt_builder.js'
3436import { DEFAULT_SYSTEM_PROMPT_TEMPLATE , SERVER_MANAGER_SYSTEM_PROMPT_TEMPLATE } from './prompts/templates.js'
@@ -61,6 +63,10 @@ export class MCPAgent {
6163 private modelProvider : string
6264 private modelName : string
6365
66+ // Observability support
67+ private observabilityManager : ObservabilityManager
68+ private callbacks : BaseCallbackHandler [ ] = [ ]
69+
6470 // Remote agent support
6571 private isRemote = false
6672 private remoteAgent : RemoteAgent | null = null
@@ -81,6 +87,7 @@ export class MCPAgent {
8187 verbose ?: boolean
8288 adapter ?: LangChainAdapter
8389 serverManagerFactory ?: ( client : MCPClient ) => ServerManager
90+ callbacks ?: BaseCallbackHandler [ ]
8491 // Remote agent parameters
8592 agentId ?: string
8693 apiKey ?: string
@@ -107,6 +114,8 @@ export class MCPAgent {
107114 this . telemetry = Telemetry . getInstance ( )
108115 this . modelProvider = 'remote'
109116 this . modelName = 'remote-agent'
117+ this . observabilityManager = new ObservabilityManager ( { customCallbacks : options . callbacks } )
118+ this . callbacks = [ ]
110119 return
111120 }
112121
@@ -159,6 +168,12 @@ export class MCPAgent {
159168 this . modelName = 'unknown'
160169 }
161170
171+ // Set up observability callbacks using the ObservabilityManager
172+ this . observabilityManager = new ObservabilityManager ( {
173+ customCallbacks : options . callbacks ,
174+ verbose : this . verbose ,
175+ } )
176+
162177 // Make getters configurable for test mocking
163178 Object . defineProperty ( this , 'agentExecutor' , {
164179 get : ( ) => this . _agentExecutor ,
@@ -183,6 +198,13 @@ export class MCPAgent {
183198
184199 logger . info ( '🚀 Initializing MCP agent and connecting to services...' )
185200
201+ // Initialize observability callbacks
202+ this . callbacks = await this . observabilityManager . getCallbacks ( )
203+ const handlerNames = await this . observabilityManager . getHandlerNames ( )
204+ if ( handlerNames . length > 0 ) {
205+ logger . info ( `📊 Observability enabled with: ${ handlerNames . join ( ', ' ) } ` )
206+ }
207+
186208 // If using server manager, initialize it
187209 if ( this . useServerManager && this . serverManager ) {
188210 await this . serverManager . initialize ( )
@@ -202,7 +224,7 @@ export class MCPAgent {
202224 // Standard initialization - if using client, get or create sessions
203225 if ( this . client ) {
204226 // First try to get existing sessions
205- this . sessions = await this . client . getAllActiveSessions ( )
227+ this . sessions = this . client . getAllActiveSessions ( )
206228 logger . info ( `🔌 Found ${ Object . keys ( this . sessions ) . length } existing sessions` )
207229
208230 // If no active sessions exist, create new ones
@@ -294,6 +316,7 @@ export class MCPAgent {
294316 maxIterations : this . maxSteps ,
295317 verbose : this . verbose ,
296318 returnIntermediateSteps : true ,
319+ callbacks : this . callbacks ,
297320 } )
298321 }
299322
@@ -642,7 +665,7 @@ export class MCPAgent {
642665
643666 let serverCount = 0
644667 if ( this . client ) {
645- serverCount = Object . keys ( await this . client . getAllActiveSessions ( ) ) . length
668+ serverCount = Object . keys ( this . client . getAllActiveSessions ( ) ) . length
646669 }
647670 else if ( this . connectors ) {
648671 serverCount = this . connectors . length
@@ -690,6 +713,9 @@ export class MCPAgent {
690713 }
691714
692715 logger . info ( '🔌 Closing MCPAgent resources…' )
716+
717+ // Shutdown observability handlers (important for serverless)
718+ await this . observabilityManager . shutdown ( )
693719 try {
694720 this . _agentExecutor = null
695721 this . _tools = [ ]
@@ -779,7 +805,10 @@ export class MCPAgent {
779805 // Stream events from the agent executor
780806 const eventStream = agentExecutor . streamEvents (
781807 inputs ,
782- { version : 'v2' } ,
808+ {
809+ version : 'v2' ,
810+ callbacks : this . callbacks . length > 0 ? this . callbacks : undefined ,
811+ } ,
783812 )
784813
785814 // Yield each event
@@ -829,7 +858,7 @@ export class MCPAgent {
829858
830859 let serverCount = 0
831860 if ( this . client ) {
832- serverCount = Object . keys ( await this . client . getAllActiveSessions ( ) ) . length
861+ serverCount = Object . keys ( this . client . getAllActiveSessions ( ) ) . length
833862 }
834863 else if ( this . connectors ) {
835864 serverCount = this . connectors . length
0 commit comments