From 8e248d7bed5f51990da646e62095742c8f82a07e Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Wed, 1 Apr 2026 19:04:42 +0800 Subject: [PATCH] mcp perf (#6697) * mcp perf * fix: null object --- packages/service/core/app/mcp.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/service/core/app/mcp.ts b/packages/service/core/app/mcp.ts index 8ce0959862..1c8ac736ea 100644 --- a/packages/service/core/app/mcp.ts +++ b/packages/service/core/app/mcp.ts @@ -39,18 +39,14 @@ export class MCPClient { throw error; }); - this.client.onerror = (error) => { - logger.error('MCP client connection error', { url: this.url, error }); - this.connectionPromise = null; - }; - this.client.onclose = () => { - this.connectionPromise = null; - }; - return this.connectionPromise; } private async doConnect(): Promise { + // 避免连接重复,强制关闭一次 + await this.client.close().catch(() => {}); + + logger.debug('Start connect mcp client', { url: this.url }); try { const transport = new StreamableHTTPClientTransport(new URL(this.url), { requestInit: { @@ -58,9 +54,7 @@ export class MCPClient { } }); await this.client.connect(transport); - return this.client; } catch (error) { - await this.client.close().catch(() => {}); await this.client.connect( new SSEClientTransport(new URL(this.url), { requestInit: { @@ -90,8 +84,19 @@ export class MCPClient { } }) ); - return this.client; } + + this.client.onerror = (error) => { + // 忽略掉不支持 streamable 的错误 + if (error?.message?.includes('SSE stream: Not Found')) return; + logger.warn('MCP client connection error', { url: this.url, error }); + this.connectionPromise = null; + }; + this.client.onclose = () => { + this.connectionPromise = null; + }; + + return this.client; } // 内部方法:关闭连接