Commit 8e248d7b by Archer Committed by GitHub

mcp perf (#6697)

* mcp perf

* fix: null object
parent e382a74e
...@@ -39,18 +39,14 @@ export class MCPClient { ...@@ -39,18 +39,14 @@ export class MCPClient {
throw error; 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; return this.connectionPromise;
} }
private async doConnect(): Promise<Client> { private async doConnect(): Promise<Client> {
// 避免连接重复,强制关闭一次
await this.client.close().catch(() => {});
logger.debug('Start connect mcp client', { url: this.url });
try { try {
const transport = new StreamableHTTPClientTransport(new URL(this.url), { const transport = new StreamableHTTPClientTransport(new URL(this.url), {
requestInit: { requestInit: {
...@@ -58,9 +54,7 @@ export class MCPClient { ...@@ -58,9 +54,7 @@ export class MCPClient {
} }
}); });
await this.client.connect(transport); await this.client.connect(transport);
return this.client;
} catch (error) { } catch (error) {
await this.client.close().catch(() => {});
await this.client.connect( await this.client.connect(
new SSEClientTransport(new URL(this.url), { new SSEClientTransport(new URL(this.url), {
requestInit: { requestInit: {
...@@ -90,8 +84,19 @@ export class MCPClient { ...@@ -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;
} }
// 内部方法:关闭连接 // 内部方法:关闭连接
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment