@@ -39,28 +39,22 @@ export class MCPClient {
3939 throw error ;
4040 } ) ;
4141
42- this . client . onerror = ( error ) => {
43- logger . error ( 'MCP client connection error' , { url : this . url , error } ) ;
44- this . connectionPromise = null ;
45- } ;
46- this . client . onclose = ( ) => {
47- this . connectionPromise = null ;
48- } ;
49-
5042 return this . connectionPromise ;
5143 }
5244
5345 private async doConnect ( ) : Promise < Client > {
46+ // 避免连接重复,强制关闭一次
47+ await this . client . close ( ) . catch ( ( ) => { } ) ;
48+
49+ logger . debug ( 'Start connect mcp client' , { url : this . url } ) ;
5450 try {
5551 const transport = new StreamableHTTPClientTransport ( new URL ( this . url ) , {
5652 requestInit : {
5753 headers : this . headers
5854 }
5955 } ) ;
6056 await this . client . connect ( transport ) ;
61- return this . client ;
6257 } catch ( error ) {
63- await this . client . close ( ) . catch ( ( ) => { } ) ;
6458 await this . client . connect (
6559 new SSEClientTransport ( new URL ( this . url ) , {
6660 requestInit : {
@@ -90,8 +84,19 @@ export class MCPClient {
9084 }
9185 } )
9286 ) ;
93- return this . client ;
9487 }
88+
89+ this . client . onerror = ( error ) => {
90+ // 忽略掉不支持 streamable 的错误
91+ if ( error ?. message ?. includes ( 'SSE stream: Not Found' ) ) return ;
92+ logger . warn ( 'MCP client connection error' , { url : this . url , error } ) ;
93+ this . connectionPromise = null ;
94+ } ;
95+ this . client . onclose = ( ) => {
96+ this . connectionPromise = null ;
97+ } ;
98+
99+ return this . client ;
95100 }
96101
97102 // 内部方法:关闭连接
0 commit comments