* mcp perf

* fix: null object
This commit is contained in:
Archer
2026-04-01 19:04:42 +08:00
committed by GitHub
parent e382a74e62
commit 8e248d7bed

View File

@@ -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<Client> {
// 避免连接重复,强制关闭一次
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;
}
// 内部方法:关闭连接