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

Method init

src/agent/MCPComponent.ts:79–119  ·  view source on GitHub ↗

* 初始化 MCP 组件

()

Source from the content-addressed store, hash-verified

77 * 初始化 MCP 组件
78 */
79 async init(): Promise<void> {
80 if (!this.config.enabled) {
81 this.log('MCP 组件已禁用');
82 return;
83 }
84
85 this.client = new MCPClient();
86
87 // 设置事件监听
88 this.client.on('connected', session => {
89 this.log(`MCP 服务器连接成功: ${session.config.name}`);
90 this.sessions.set(session.config.name, session);
91 this.emit('serverConnected', session);
92 });
93
94 this.client.on('disconnected', sessionId => {
95 this.log(`MCP 服务器断开连接: ${sessionId}`);
96 // 清理相关数据
97 for (const [name, session] of this.sessions.entries()) {
98 if (session.id === sessionId) {
99 this.sessions.delete(name);
100 this.resources.delete(sessionId);
101 this.tools.delete(sessionId);
102 break;
103 }
104 }
105 this.emit('serverDisconnected', sessionId);
106 });
107
108 this.client.on('error', error => {
109 this.log(`MCP 客户端错误: ${error.message}`);
110 this.emit('error', error);
111 });
112
113 // 自动连接配置的服务器
114 if (this.config.autoConnect && this.config.servers) {
115 await this.connectToServers(this.config.servers);
116 }
117
118 this.log('MCP 组件初始化完成');
119 }
120
121 /**
122 * 销毁 MCP 组件

Callers

nothing calls this directly

Calls 6

logMethod · 0.95
emitMethod · 0.95
connectToServersMethod · 0.95
setMethod · 0.80
deleteMethod · 0.80
onMethod · 0.45

Tested by

no test coverage detected