MCPcopy Create free account
hub / github.com/echoVic/blade-code / connect

Method connect

src/mcp/client/MCPClient.ts:38–76  ·  view source on GitHub ↗

* 连接到 MCP 服务器

(config: MCPConnectionConfig)

Source from the content-addressed store, hash-verified

36 * 连接到 MCP 服务器
37 */
38 async connect(config: MCPConnectionConfig): Promise<MCPSession> {
39 const sessionId = `${config.name}-${Date.now()}`;
40
41 try {
42 let connection: WebSocket | any;
43
44 switch (config.transport) {
45 case 'ws':
46 connection = await this.connectWebSocket(config);
47 break;
48 case 'stdio':
49 connection = await this.connectStdio(config);
50 break;
51 case 'sse':
52 throw new Error('SSE transport not implemented yet');
53 default:
54 throw new Error(`Unsupported transport: ${config.transport}`);
55 }
56
57 const session: MCPSession = {
58 id: sessionId,
59 config,
60 connected: true,
61 lastActivity: new Date(),
62 };
63
64 this.sessions.set(sessionId, session);
65 this.connections.set(sessionId, connection);
66
67 // 执行初始化握手
68 await this.performHandshake(sessionId);
69
70 this.emit('connected', session);
71 return session;
72 } catch (error) {
73 this.emit('error', error);
74 throw error;
75 }
76 }
77
78 /**
79 * 断开连接

Callers 2

mcpCommandFunction · 0.95
connectToServerMethod · 0.80

Calls 5

connectWebSocketMethod · 0.95
connectStdioMethod · 0.95
performHandshakeMethod · 0.95
setMethod · 0.80
emitMethod · 0.45

Tested by

no test coverage detected