This repository was archived by the owner on May 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmcp_agent.ts
More file actions
1072 lines (942 loc) · 37.5 KB
/
Copy pathmcp_agent.ts
File metadata and controls
1072 lines (942 loc) · 37.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { BaseCallbackHandler } from '@langchain/core/callbacks/base'
import type { CallbackManagerForChainRun } from '@langchain/core/callbacks/manager'
import type { BaseLanguageModelInterface, LanguageModelLike } from '@langchain/core/language_models/base'
import type { Serialized } from '@langchain/core/load/serializable'
import type {
BaseMessage,
} from '@langchain/core/messages'
import type { StructuredToolInterface, ToolInterface } from '@langchain/core/tools'
import type { StreamEvent } from '@langchain/core/tracers/log_stream'
import type { AgentFinish, AgentStep } from 'langchain/agents'
import type { ZodSchema } from 'zod'
import type { MCPClient } from '../client.js'
import type { BaseConnector } from '../connectors/base.js'
import type { MCPSession } from '../session.js'
import { CallbackManager } from '@langchain/core/callbacks/manager'
import {
AIMessage,
HumanMessage,
SystemMessage,
ToolMessage,
} from '@langchain/core/messages'
import { OutputParserException } from '@langchain/core/output_parsers'
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from '@langchain/core/prompts'
import { AgentExecutor, createToolCallingAgent } from 'langchain/agents'
import { zodToJsonSchema } from 'zod-to-json-schema'
import { LangChainAdapter } from '../adapters/langchain_adapter.js'
import { logger } from '../logging.js'
import { ServerManager } from '../managers/server_manager.js'
import { ObservabilityManager } from '../observability/index.js'
import { extractModelInfo, Telemetry } from '../telemetry/index.js'
import { createSystemMessage } from './prompts/system_prompt_builder.js'
import { DEFAULT_SYSTEM_PROMPT_TEMPLATE, SERVER_MANAGER_SYSTEM_PROMPT_TEMPLATE } from './prompts/templates.js'
import { RemoteAgent } from './remote.js'
export class MCPAgent {
private llm?: BaseLanguageModelInterface
private client?: MCPClient
private connectors: BaseConnector[]
private maxSteps: number
private autoInitialize: boolean
private memoryEnabled: boolean
private disallowedTools: string[]
private additionalTools: StructuredToolInterface[]
private useServerManager: boolean
private verbose: boolean
private systemPrompt?: string | null
private systemPromptTemplateOverride?: string | null
private additionalInstructions?: string | null
private _initialized = false
private conversationHistory: BaseMessage[] = []
private _agentExecutor: AgentExecutor | null = null
private sessions: Record<string, MCPSession> = {}
private systemMessage: SystemMessage | null = null
private _tools: StructuredToolInterface[] = []
private adapter: LangChainAdapter
private serverManager: ServerManager | null = null
private telemetry: Telemetry
private modelProvider: string
private modelName: string
// Observability support
private observabilityManager: ObservabilityManager
private callbacks: BaseCallbackHandler[] = []
// Remote agent support
private isRemote = false
private remoteAgent: RemoteAgent | null = null
constructor(options: {
llm?: BaseLanguageModelInterface
client?: MCPClient
connectors?: BaseConnector[]
maxSteps?: number
autoInitialize?: boolean
memoryEnabled?: boolean
systemPrompt?: string | null
systemPromptTemplate?: string | null
additionalInstructions?: string | null
disallowedTools?: string[]
additionalTools?: StructuredToolInterface[]
useServerManager?: boolean
verbose?: boolean
adapter?: LangChainAdapter
serverManagerFactory?: (client: MCPClient) => ServerManager
callbacks?: BaseCallbackHandler[]
// Remote agent parameters
agentId?: string
apiKey?: string
baseUrl?: string
}) {
// Handle remote execution
if (options.agentId) {
this.isRemote = true
this.remoteAgent = new RemoteAgent({
agentId: options.agentId,
apiKey: options.apiKey,
baseUrl: options.baseUrl,
})
// Set default values for remote agent
this.maxSteps = options.maxSteps ?? 5
this.memoryEnabled = options.memoryEnabled ?? true
this.autoInitialize = options.autoInitialize ?? false
this.verbose = options.verbose ?? false
this.connectors = []
this.disallowedTools = []
this.additionalTools = []
this.useServerManager = false
this.adapter = new LangChainAdapter()
this.telemetry = Telemetry.getInstance()
this.modelProvider = 'remote'
this.modelName = 'remote-agent'
this.observabilityManager = new ObservabilityManager({ customCallbacks: options.callbacks })
this.callbacks = []
return
}
// Validate requirements for local execution
if (!options.llm) {
throw new Error('llm is required for local execution. For remote execution, provide agentId instead.')
}
this.llm = options.llm
this.client = options.client
this.connectors = options.connectors ?? []
this.maxSteps = options.maxSteps ?? 5
this.autoInitialize = options.autoInitialize ?? false
this.memoryEnabled = options.memoryEnabled ?? true
this.systemPrompt = options.systemPrompt ?? null
this.systemPromptTemplateOverride = options.systemPromptTemplate ?? null
this.additionalInstructions = options.additionalInstructions ?? null
this.disallowedTools = options.disallowedTools ?? []
this.additionalTools = options.additionalTools ?? []
this.useServerManager = options.useServerManager ?? false
this.verbose = options.verbose ?? false
if (!this.client && this.connectors.length === 0) {
throw new Error('Either \'client\' or at least one \'connector\' must be provided.')
}
if (this.useServerManager) {
if (!this.client) {
throw new Error('\'client\' must be provided when \'useServerManager\' is true.')
}
this.adapter = options.adapter ?? new LangChainAdapter(this.disallowedTools)
this.serverManager = options.serverManagerFactory?.(this.client) ?? new ServerManager(this.client, this.adapter)
}
// Let consumers swap allowed tools dynamically
else {
this.adapter = options.adapter ?? new LangChainAdapter(this.disallowedTools)
}
// Initialize telemetry
this.telemetry = Telemetry.getInstance()
// Track model info for telemetry
if (this.llm) {
const [provider, name] = extractModelInfo(this.llm as any)
this.modelProvider = provider
this.modelName = name
}
else {
this.modelProvider = 'unknown'
this.modelName = 'unknown'
}
// Set up observability callbacks using the ObservabilityManager
this.observabilityManager = new ObservabilityManager({
customCallbacks: options.callbacks,
verbose: this.verbose,
})
// Make getters configurable for test mocking
Object.defineProperty(this, 'agentExecutor', {
get: () => this._agentExecutor,
configurable: true,
})
Object.defineProperty(this, 'tools', {
get: () => this._tools,
configurable: true,
})
Object.defineProperty(this, 'initialized', {
get: () => this._initialized,
configurable: true,
})
}
public async initialize(): Promise<void> {
// Skip initialization for remote agents
if (this.isRemote) {
this._initialized = true
return
}
logger.info('🚀 Initializing MCP agent and connecting to services...')
// Initialize observability callbacks
this.callbacks = await this.observabilityManager.getCallbacks()
const handlerNames = await this.observabilityManager.getHandlerNames()
if (handlerNames.length > 0) {
logger.info(`📊 Observability enabled with: ${handlerNames.join(', ')}`)
}
// If using server manager, initialize it
if (this.useServerManager && this.serverManager) {
await this.serverManager.initialize()
// Get server management tools
const managementTools = this.serverManager.tools
this._tools = managementTools
this._tools.push(...this.additionalTools)
logger.info(
`🔧 Server manager mode active with ${managementTools.length} management tools`,
)
// Create the system message based on available tools
await this.createSystemMessageFromTools(this._tools)
}
else {
// Standard initialization - if using client, get or create sessions
if (this.client) {
// First try to get existing sessions
this.sessions = this.client.getAllActiveSessions()
logger.info(`🔌 Found ${Object.keys(this.sessions).length} existing sessions`)
// If no active sessions exist, create new ones
if (Object.keys(this.sessions).length === 0) {
logger.info('🔄 No active sessions found, creating new ones...')
this.sessions = await this.client.createAllSessions()
logger.info(`✅ Created ${Object.keys(this.sessions).length} new sessions`)
}
// Create LangChain tools directly from the client using the adapter
this._tools = await LangChainAdapter.createTools(this.client)
this._tools.push(...this.additionalTools)
logger.info(`🛠️ Created ${this._tools.length} LangChain tools from client`)
}
else {
// Using direct connector - only establish connection
logger.info(`🔗 Connecting to ${this.connectors.length} direct connectors...`)
for (const connector of this.connectors) {
if (!connector.isClientConnected) {
await connector.connect()
}
}
// Create LangChain tools using the adapter with connectors
this._tools = await this.adapter.createToolsFromConnectors(this.connectors)
this._tools.push(...this.additionalTools)
logger.info(`🛠️ Created ${this._tools.length} LangChain tools from connectors`)
}
// Get all tools for system message generation
logger.info(`🧰 Found ${this._tools.length} tools across all connectors`)
// Create the system message based on available tools
await this.createSystemMessageFromTools(this._tools)
}
// Create the agent executor and mark initialized
this._agentExecutor = this.createAgent()
this._initialized = true
logger.info('✨ Agent initialization complete')
}
private async createSystemMessageFromTools(tools: StructuredToolInterface[]): Promise<void> {
const systemPromptTemplate
= this.systemPromptTemplateOverride
?? DEFAULT_SYSTEM_PROMPT_TEMPLATE
this.systemMessage = createSystemMessage(
tools,
systemPromptTemplate,
SERVER_MANAGER_SYSTEM_PROMPT_TEMPLATE,
this.useServerManager,
this.disallowedTools,
this.systemPrompt ?? undefined,
this.additionalInstructions ?? undefined,
)
if (this.memoryEnabled) {
this.conversationHistory = [
this.systemMessage,
...this.conversationHistory.filter(m => !(m instanceof SystemMessage)),
]
}
}
private createAgent(): AgentExecutor {
if (!this.llm) {
throw new Error('LLM is required to create agent')
}
const systemContent = this.systemMessage?.content ?? 'You are a helpful assistant.'
const prompt = ChatPromptTemplate.fromMessages([
['system', systemContent],
new MessagesPlaceholder('chat_history'),
['human', '{input}'],
new MessagesPlaceholder('agent_scratchpad'),
])
const agent = createToolCallingAgent({
llm: this.llm as unknown as LanguageModelLike,
tools: this._tools,
prompt,
})
return new AgentExecutor({
agent,
tools: this._tools,
maxIterations: this.maxSteps,
verbose: this.verbose,
returnIntermediateSteps: true,
callbacks: this.callbacks,
})
}
public getConversationHistory(): BaseMessage[] {
return [...this.conversationHistory]
}
public clearConversationHistory(): void {
this.conversationHistory = this.memoryEnabled && this.systemMessage ? [this.systemMessage] : []
}
private addToHistory(message: BaseMessage): void {
if (this.memoryEnabled)
this.conversationHistory.push(message)
}
public getSystemMessage(): SystemMessage | null {
return this.systemMessage
}
public setSystemMessage(message: string): void {
this.systemMessage = new SystemMessage(message)
if (this.memoryEnabled) {
this.conversationHistory = this.conversationHistory.filter(m => !(m instanceof SystemMessage))
this.conversationHistory.unshift(this.systemMessage)
}
if (this._initialized && this._tools.length) {
this._agentExecutor = this.createAgent()
logger.debug('Agent recreated with new system message')
}
}
public setDisallowedTools(disallowedTools: string[]): void {
this.disallowedTools = disallowedTools
this.adapter = new LangChainAdapter(this.disallowedTools)
if (this._initialized) {
logger.debug('Agent already initialized. Changes will take effect on next initialization.')
}
}
public getDisallowedTools(): string[] {
return this.disallowedTools
}
private async _consumeAndReturn<T>(
generator: AsyncGenerator<AgentStep, string | T, void>,
): Promise<string | T> {
// Manually iterate through the generator to consume the steps.
// The for-await-of loop is not used because it discards the generator's
// final return value. We need to capture that value when `done` is true.
while (true) {
const { done, value } = await generator.next()
if (done) {
return value
}
}
}
/**
* Runs the agent and returns a promise for the final result.
*/
public async run(
query: string,
maxSteps?: number,
manageConnector?: boolean,
externalHistory?: BaseMessage[],
): Promise<string>
/**
* Runs the agent with structured output and returns a promise for the typed result.
*/
public async run<T>(
query: string,
maxSteps?: number,
manageConnector?: boolean,
externalHistory?: BaseMessage[],
outputSchema?: ZodSchema<T>,
): Promise<T>
public async run<T>(
query: string,
maxSteps?: number,
manageConnector?: boolean,
externalHistory?: BaseMessage[],
outputSchema?: ZodSchema<T>,
): Promise<string | T> {
// Delegate to remote agent if in remote mode
if (this.isRemote && this.remoteAgent) {
return this.remoteAgent.run(query, maxSteps, manageConnector, externalHistory, outputSchema)
}
const generator = this.stream<T>(
query,
maxSteps,
manageConnector,
externalHistory,
outputSchema,
)
return this._consumeAndReturn(generator)
}
/**
* Runs the agent and yields intermediate steps as an async generator.
* If outputSchema is provided, returns structured output of type T.
*/
public async* stream<T = string>(
query: string,
maxSteps?: number,
manageConnector = true,
externalHistory?: BaseMessage[],
outputSchema?: ZodSchema<T>,
): AsyncGenerator<AgentStep, string | T, void> {
// Delegate to remote agent if in remote mode
if (this.isRemote && this.remoteAgent) {
const result = await this.remoteAgent.run(query, maxSteps, manageConnector, externalHistory, outputSchema)
return result as string | T
}
let result = ''
let initializedHere = false
const startTime = Date.now()
const toolsUsedNames: string[] = []
let stepsTaken = 0
let success = false
// Schema-aware setup for structured output
let structuredLlm: BaseLanguageModelInterface | null = null
let schemaDescription = ''
if (outputSchema) {
query = this._enhanceQueryWithSchema(query, outputSchema)
logger.debug(`🔄 Structured output requested, schema: ${JSON.stringify(zodToJsonSchema(outputSchema), null, 2)}`)
// Check if withStructuredOutput method exists
if (this.llm && 'withStructuredOutput' in this.llm && typeof (this.llm as any).withStructuredOutput === 'function') {
structuredLlm = (this.llm as any).withStructuredOutput(outputSchema)
}
else if (this.llm) {
// Fallback: use the same LLM but we'll handle structure in our helper method
structuredLlm = this.llm
}
else {
throw new Error('LLM is required for structured output')
}
schemaDescription = JSON.stringify(zodToJsonSchema(outputSchema), null, 2)
}
try {
if (manageConnector && !this._initialized) {
await this.initialize()
initializedHere = true
}
else if (!this._initialized && this.autoInitialize) {
await this.initialize()
initializedHere = true
}
if (!this._agentExecutor) {
throw new Error('MCP agent failed to initialize')
}
const steps = maxSteps ?? this.maxSteps
this._agentExecutor.maxIterations = steps
const display_query
= query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, ' ')}...` : query.replace(/\n/g, ' ')
logger.info(`💬 Received query: '${display_query}'`)
// Prepare history (WITHOUT adding current message yet)
const historyToUse = externalHistory ?? this.conversationHistory
const langchainHistory: BaseMessage[] = []
for (const msg of historyToUse) {
if (msg instanceof HumanMessage || msg instanceof AIMessage) {
langchainHistory.push(msg)
}
}
const intermediateSteps: AgentStep[] = []
const inputs = { input: query, chat_history: langchainHistory } as Record<string, unknown>
let nameToToolMap: Record<string, StructuredToolInterface> = Object.fromEntries(this._tools.map(t => [t.name, t]))
logger.info(`🏁 Starting agent execution with max_steps=${steps}`)
// Create a run manager with our callbacks if we have any - ONCE for the entire execution
let runManager: CallbackManagerForChainRun | undefined
if (this.callbacks?.length > 0) {
// Create an async callback manager with our callbacks
const callbackManager = new CallbackManager(undefined, {
handlers: this.callbacks,
inheritableHandlers: this.callbacks,
})
// Create a run manager for this chain execution
runManager = await callbackManager.handleChainStart({
name: 'MCPAgent (mcp-use)',
id: ['MCPAgent (mcp-use)'],
lc: 1,
type: 'not_implemented',
} as Serialized, inputs)
}
for (let stepNum = 0; stepNum < steps; stepNum++) {
stepsTaken = stepNum + 1
if (this.useServerManager && this.serverManager) {
const currentTools = this.serverManager.tools
const currentToolNames = new Set(currentTools.map(t => t.name))
const existingToolNames = new Set(this._tools.map(t => t.name))
const changed
= currentTools.length !== this._tools.length
|| [...currentToolNames].some(n => !existingToolNames.has(n))
if (changed) {
logger.info(
`🔄 Tools changed before step ${stepNum + 1}, updating agent. New tools: ${[...currentToolNames].join(', ')}`,
)
this._tools = currentTools
this._tools.push(...this.additionalTools)
await this.createSystemMessageFromTools(this._tools)
this._agentExecutor = this.createAgent()
this._agentExecutor.maxIterations = steps
nameToToolMap = Object.fromEntries(this._tools.map(t => [t.name, t]))
}
}
logger.info(`👣 Step ${stepNum + 1}/${steps}`)
try {
logger.debug('Starting agent step execution')
const nextStepOutput: AgentStep[] | AgentFinish = await this._agentExecutor._takeNextStep(
nameToToolMap as Record<string, ToolInterface>,
inputs,
intermediateSteps,
runManager,
)
// Agent finish handling (AgentFinish contains returnValues property)
if ('returnValues' in nextStepOutput) {
logger.info(`✅ Agent finished at step ${stepNum + 1}`)
result = nextStepOutput.returnValues?.output ?? 'No output generated'
runManager?.handleChainEnd({ output: result })
// If structured output is requested, attempt to create it
if (outputSchema && structuredLlm) {
try {
logger.info('🔧 Attempting structured output...')
const structuredResult = await this._attemptStructuredOutput<T>(
result,
structuredLlm,
outputSchema,
schemaDescription,
)
logger.debug(`🔄 Structured result: ${JSON.stringify(structuredResult)}`)
// Add the final response to conversation history if memory is enabled
if (this.memoryEnabled) {
this.addToHistory(new AIMessage(`Structured result: ${JSON.stringify(structuredResult)}`))
}
logger.info('✅ Structured output successful')
success = true
return structuredResult as string | T
}
catch (e) {
logger.warn(`⚠️ Structured output failed: ${e}`)
// Continue execution to gather missing information
const failedStructuredOutputPrompt = `
The current result cannot be formatted into the required structure.
Error: ${String(e)}
Current information: ${result}
If information is missing, please continue working to gather the missing information needed for:
${schemaDescription}
If the information is complete, please return the result in the required structure.
`
// Add this as feedback and continue the loop
inputs.input = failedStructuredOutputPrompt
if (this.memoryEnabled) {
this.addToHistory(new HumanMessage(failedStructuredOutputPrompt))
}
logger.info('🔄 Continuing execution to gather missing information...')
continue
}
}
else {
// Regular execution without structured output
break
}
}
const stepArray = nextStepOutput as AgentStep[]
intermediateSteps.push(...stepArray)
for (const step of stepArray) {
yield step
const { action, observation } = step
const toolName = action.tool
toolsUsedNames.push(toolName)
let toolInputStr = typeof action.toolInput === 'string'
? action.toolInput
: JSON.stringify(action.toolInput, null, 2)
if (toolInputStr.length > 100)
toolInputStr = `${toolInputStr.slice(0, 97)}...`
logger.info(`🔧 Tool call: ${toolName} with input: ${toolInputStr}`)
let outputStr = String(observation)
if (outputStr.length > 100)
outputStr = `${outputStr.slice(0, 97)}...`
outputStr = outputStr.replace(/\n/g, ' ')
logger.info(`📄 Tool result: ${outputStr}`)
}
// Detect direct return
if (stepArray.length) {
const lastStep = stepArray[stepArray.length - 1]
const toolReturn: AgentFinish | null = await this._agentExecutor._getToolReturn(lastStep)
if (toolReturn) {
logger.info(`🏆 Tool returned directly at step ${stepNum + 1}`)
result = toolReturn.returnValues?.output ?? 'No output generated'
break
}
}
}
catch (e) {
if (e instanceof OutputParserException) {
logger.error(`❌ Output parsing error during step ${stepNum + 1}: ${e}`)
result = `Agent stopped due to a parsing error: ${e}`
runManager?.handleChainError(result)
break
}
logger.error(`❌ Error during agent execution step ${stepNum + 1}: ${e}`)
console.error(e)
result = `Agent stopped due to an error: ${e}`
runManager?.handleChainError(result)
break
}
}
// —–– Post‑loop handling
if (!result) {
logger.warn(`⚠️ Agent stopped after reaching max iterations (${steps})`)
result = `Agent stopped after reaching the maximum number of steps (${steps}).`
runManager?.handleChainEnd({ output: result })
}
logger.info('🎉 Agent execution complete')
success = true
// Add BOTH the user message and AI response to conversation history if memory is enabled
if (this.memoryEnabled) {
this.addToHistory(new HumanMessage(query))
if (result) {
this.addToHistory(new AIMessage(result as string))
}
}
// Return regular result
return result as string | T
}
catch (e) {
logger.error(`❌ Error running query: ${e}`)
if (initializedHere && manageConnector) {
logger.info('🧹 Cleaning up resources after initialization error in run')
await this.close()
}
throw e
}
finally {
// Track comprehensive execution data
const executionTimeMs = Date.now() - startTime
let serverCount = 0
if (this.client) {
serverCount = Object.keys(this.client.getAllActiveSessions()).length
}
else if (this.connectors) {
serverCount = this.connectors.length
}
const conversationHistoryLength = this.memoryEnabled ? this.conversationHistory.length : 0
await this.telemetry.trackAgentExecution({
executionMethod: 'stream',
query,
success,
modelProvider: this.modelProvider,
modelName: this.modelName,
serverCount,
serverIdentifiers: this.connectors.map(connector => connector.publicIdentifier),
totalToolsAvailable: this._tools.length,
toolsAvailableNames: this._tools.map(t => t.name),
maxStepsConfigured: this.maxSteps,
memoryEnabled: this.memoryEnabled,
useServerManager: this.useServerManager,
maxStepsUsed: maxSteps ?? null,
manageConnector,
externalHistoryUsed: externalHistory !== undefined,
stepsTaken,
toolsUsedCount: toolsUsedNames.length,
toolsUsedNames,
response: result,
executionTimeMs,
errorType: success ? null : 'execution_error',
conversationHistoryLength,
})
if (manageConnector && !this.client && initializedHere) {
logger.info('🧹 Closing agent after query completion')
await this.close()
}
}
}
public async close(): Promise<void> {
// Delegate to remote agent if in remote mode
if (this.isRemote && this.remoteAgent) {
await this.remoteAgent.close()
return
}
logger.info('🔌 Closing MCPAgent resources…')
// Shutdown observability handlers (important for serverless)
await this.observabilityManager.shutdown()
try {
this._agentExecutor = null
this._tools = []
if (this.client) {
logger.info('🔄 Closing sessions through client')
await this.client.closeAllSessions()
this.sessions = {}
}
else {
for (const connector of this.connectors) {
logger.info('🔄 Disconnecting connector')
await connector.disconnect()
}
}
if ('connectorToolMap' in this.adapter) {
this.adapter = new LangChainAdapter()
}
}
finally {
this._initialized = false
logger.info('👋 Agent closed successfully')
}
}
/**
* Yields LangChain StreamEvent objects from the underlying streamEvents() method.
* This provides token-level streaming and fine-grained event updates.
*/
public async* streamEvents(
query: string,
maxSteps?: number,
manageConnector = true,
externalHistory?: BaseMessage[],
): AsyncGenerator<StreamEvent, void, void> {
let initializedHere = false
const startTime = Date.now()
let success = false
let eventCount = 0
let totalResponseLength = 0
let finalResponse = ''
try {
// Initialize if needed
if (manageConnector && !this._initialized) {
await this.initialize()
initializedHere = true
}
else if (!this._initialized && this.autoInitialize) {
await this.initialize()
initializedHere = true
}
const agentExecutor = (this as any).agentExecutor
if (!agentExecutor) {
throw new Error('MCP agent failed to initialize')
}
// Set max iterations
const steps = maxSteps ?? this.maxSteps
agentExecutor.maxIterations = steps
const display_query
= query.length > 50 ? `${query.slice(0, 50).replace(/\n/g, ' ')}...` : query.replace(/\n/g, ' ')
logger.info(`💬 Received query for streamEvents: '${display_query}'`)
// Prepare history (WITHOUT adding current message yet)
const historyToUse = externalHistory ?? this.conversationHistory
const langchainHistory: BaseMessage[] = []
for (const msg of historyToUse) {
if (msg instanceof HumanMessage || msg instanceof AIMessage || msg instanceof ToolMessage) {
langchainHistory.push(msg)
}
else {
logger.info(`⚠️ Skipped message of type: ${msg.constructor.name}`)
}
}
// Prepare inputs
const inputs = { input: query, chat_history: langchainHistory }
logger.info('callbacks', this.callbacks)
// Stream events from the agent executor
const eventStream = agentExecutor.streamEvents(
inputs,
{
version: 'v2',
callbacks: this.callbacks.length > 0 ? this.callbacks : undefined,
},
)
// Yield each event
for await (const event of eventStream) {
eventCount++
// Skip null or invalid events
if (!event || typeof event !== 'object') {
continue
}
// Track response length for telemetry
if (event.event === 'on_chat_model_stream' && event.data?.chunk?.content) {
totalResponseLength += event.data.chunk.content.length
}
yield event
// Capture final response from chain end event
if (event.event === 'on_chain_end' && event.data?.output) {
const output = event.data.output
if (Array.isArray(output) && output.length > 0 && output[0]?.text) {
finalResponse = output[0].text
}
}
}
// Add BOTH the user message and AI response to conversation history if memory is enabled
if (this.memoryEnabled) {
this.addToHistory(new HumanMessage(query))
if (finalResponse) {
this.addToHistory(new AIMessage(finalResponse))
}
}
logger.info(`🎉 StreamEvents complete - ${eventCount} events emitted`)
success = true
}
catch (e) {
logger.error(`❌ Error during streamEvents: ${e}`)
if (initializedHere && manageConnector) {
logger.info('🧹 Cleaning up resources after initialization error in streamEvents')
await this.close()
}
throw e
}
finally {
// Track telemetry
const executionTimeMs = Date.now() - startTime
let serverCount = 0
if (this.client) {
serverCount = Object.keys(this.client.getAllActiveSessions()).length
}
else if (this.connectors) {
serverCount = this.connectors.length
}
const conversationHistoryLength = this.memoryEnabled ? this.conversationHistory.length : 0
await this.telemetry.trackAgentExecution({
executionMethod: 'streamEvents',
query,
success,
modelProvider: this.modelProvider,
modelName: this.modelName,
serverCount,
serverIdentifiers: this.connectors.map(connector => connector.publicIdentifier),
totalToolsAvailable: this._tools.length,
toolsAvailableNames: this._tools.map(t => t.name),
maxStepsConfigured: this.maxSteps,
memoryEnabled: this.memoryEnabled,
useServerManager: this.useServerManager,
maxStepsUsed: maxSteps ?? null,
manageConnector,
externalHistoryUsed: externalHistory !== undefined,
response: `[STREAMED RESPONSE - ${totalResponseLength} chars]`,
executionTimeMs,
errorType: success ? null : 'streaming_error',
conversationHistoryLength,
})
// Clean up if needed
if (manageConnector && !this.client && initializedHere) {
logger.info('🧹 Closing agent after streamEvents completion')
await this.close()
}
}
}
/**
* Attempt to create structured output from raw result with validation and retry logic.
*/
private async _attemptStructuredOutput<T>(
rawResult: string | any,
structuredLlm: BaseLanguageModelInterface,
outputSchema: ZodSchema<T>,
schemaDescription: string,
): Promise<T> {
logger.info(`🔄 Attempting structured output with schema: ${outputSchema}`)
logger.info(`🔄 Schema description: ${schemaDescription}`)
logger.info(`🔄 Raw result: ${JSON.stringify(rawResult, null, 2)}`)
// Handle different input formats - rawResult might be an array or object from the agent
let textContent: string = ''
if (typeof rawResult === 'string') {
textContent = rawResult
}
else if (rawResult && typeof rawResult === 'object') {
// Handle object format
textContent = JSON.stringify(rawResult)
}
// If we couldn't extract text, use the stringified version
if (!textContent) {
textContent = JSON.stringify(rawResult)
}
// Get detailed schema information for better prompting
const maxRetries = 3
let lastError: string = ''
for (let attempt = 1; attempt <= maxRetries; attempt++) {
logger.info(`🔄 Structured output attempt ${attempt}/${maxRetries}`)
let formatPrompt = `
Please format the following information according to the EXACT schema specified below.
You must use the exact field names and types as shown in the schema.
Required schema format:
${schemaDescription}
Content to extract from:
${textContent}
IMPORTANT:
- Use ONLY the field names specified in the schema
- Match the data types exactly (string, number, boolean, array, etc.)
- Include ALL required fields
- Return valid JSON that matches the schema structure exactly
`
// Add specific error feedback for retry attempts
if (attempt > 1) {
formatPrompt += `
PREVIOUS ATTEMPT FAILED with error: ${lastError}
Please fix the issues mentioned above and ensure the output matches the schema exactly.
`
}
try {
const structuredResult = await structuredLlm.invoke(formatPrompt)
logger.info(`🔄 Structured result attempt ${attempt}: ${JSON.stringify(structuredResult, null, 2)}`)
// Validate the structured result
const validatedResult = this._validateStructuredResult(structuredResult, outputSchema)
logger.info(`✅ Structured output successful on attempt ${attempt}`)
return validatedResult
}
catch (e) {
lastError = e instanceof Error ? e.message : String(e)
logger.warn(`⚠️ Structured output attempt ${attempt} failed: ${lastError}`)
if (attempt === maxRetries) {
logger.error(`❌ All ${maxRetries} structured output attempts failed`)
throw new Error(`Failed to generate valid structured output after ${maxRetries} attempts. Last error: ${lastError}`)
}