🏃‍♂️ refactor: Improve Agent Run Context & Misc. Changes (#6448)

* chore: bump Model Context Protocol SDK dependencies

* fix: correct indentation in MCPConnection class

* refactor: enhance SSE transport with abort controller and add error handling for empty results

* chore: remove outdated Model Context Protocol SDK dependency

* chore: update @modelcontextprotocol/sdk dependency to version 1.7.0

* chore: add debugging comments for PingRequest handling in MCPConnection class

* refactor: update callTool method to accept structured arguments and options

* refactor: simplify maxContextTokens calculation in initializeAgentOptions

* chore: update @babel/runtime dependency to version 7.26.10

* chore: update @librechat/agents dependency to version 2.2.9

* chore: update @librechat/agents dependency to version 2.3.6

* refactor: imports and prevent s3 initialization if strategy not configured

* refactor: mark redis as non-experimental

* refactor: add missing `maxContextTokens` for OpenAI parameters

* refactor: improve log message for Redis initialization

* chore: update @librechat/agents dependency to version 2.3.8

* refactor: extend `streamBuffer` condition to include BEDROCK provider as easily gets throttled by AWS

* refactor: filter out 'think' parts from message content in Anthropic and OpenAI clients
This commit is contained in:
Danny Avila 2025-03-20 22:56:57 -04:00 committed by GitHub
parent e768a07738
commit bc88ac846d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 446 additions and 382 deletions

View file

@ -134,7 +134,12 @@ export class MCPConnection extends EventEmitter {
}
const url = new URL(options.url);
this.logger?.info(`[MCP][${this.serverName}] Creating SSE transport: ${url.toString()}`);
const transport = new SSEClientTransport(url);
const abortController = new AbortController();
const transport = new SSEClientTransport(url, {
requestInit: {
signal: abortController.signal,
},
});
transport.onclose = () => {
this.logger?.info(`[MCP][${this.serverName}] SSE transport closed`);
@ -175,6 +180,17 @@ export class MCPConnection extends EventEmitter {
this.isInitializing = false;
this.shouldStopReconnecting = false;
this.reconnectAttempts = 0;
/**
* // FOR DEBUGGING
* // this.client.setRequestHandler(PingRequestSchema, async (request, extra) => {
* // this.logger?.info(`[MCP][${this.serverName}] PingRequest: ${JSON.stringify(request)}`);
* // if (getEventListeners && extra.signal) {
* // const listenerCount = getEventListeners(extra.signal, 'abort').length;
* // this.logger?.debug(`Signal has ${listenerCount} abort listeners`);
* // }
* // return {};
* // });
*/
} else if (state === 'error' && !this.isReconnecting && !this.isInitializing) {
this.handleReconnection().catch((error) => {
this.logger?.error(`[MCP][${this.serverName}] Reconnection handler failed:`, error);
@ -269,7 +285,7 @@ export class MCPConnection extends EventEmitter {
this.transport = this.constructTransport(this.options);
this.setupTransportDebugHandlers();
const connectTimeout = this.options.initTimeout ?? 10000;
const connectTimeout = this.options.initTimeout ?? 10000;
await Promise.race([
this.client.connect(this.transport),
new Promise((_resolve, reject) =>
@ -304,6 +320,9 @@ export class MCPConnection extends EventEmitter {
const originalSend = this.transport.send.bind(this.transport);
this.transport.send = async (msg) => {
if ('result' in msg && !('method' in msg) && Object.keys(msg.result ?? {}).length === 0) {
throw new Error('Empty result');
}
this.logger?.debug(`[MCP][${this.serverName}] Transport sending: ${JSON.stringify(msg)}`);
return originalSend(msg);
};