MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / handleMessage

Method handleMessage

src/mcp/client.ts:192–240  ·  view source on GitHub ↗
(msg: any)

Source from the content-addressed store, hash-verified

190 }
191
192 private handleMessage(msg: any): void {
193 if (msg.jsonrpc !== '2.0') {
194 logger.warn(`MCP ${this.name} sent non-JSON-RPC-2.0 message`, { msg });
195 return;
196 }
197
198 if ('id' in msg && (msg.result !== undefined || msg.error !== undefined)) {
199 const pending = this.pending.get(msg.id);
200 if (!pending) {
201 logger.debug(`MCP ${this.name} unmatched response`, { id: msg.id });
202 return;
203 }
204 this.pending.delete(msg.id);
205 if (pending.timeoutHandle) clearTimeout(pending.timeoutHandle);
206 if (msg.error) {
207 const err = new Error(`MCP error from ${this.name} (${pending.method}): ${msg.error.message ?? 'unknown'} [code=${msg.error.code}]`);
208 (err as any).code = msg.error.code;
209 (err as any).data = msg.error.data;
210 pending.reject(err);
211 } else {
212 pending.resolve(msg.result);
213 }
214 return;
215 }
216
217 if ('method' in msg && 'id' in msg) {
218 // Server-initiated request — reply not implemented
219 this.send({
220 jsonrpc: '2.0',
221 id: msg.id,
222 error: {
223 code: MCP_ERROR_CODES.MethodNotFound,
224 message: `qodex does not implement server-initiated method ${msg.method}`,
225 },
226 } as JsonRpcResponse).catch(() => {});
227 return;
228 }
229
230 if ('method' in msg) {
231 logger.debug(`MCP ${this.name} notification`, { method: msg.method });
232 if (msg.method === 'notifications/tools/list_changed') {
233 this.refreshTools().catch(e => logger.warn('Failed to refresh tools', { err: e.message }));
234 }
235 this.emit('notification', msg);
236 return;
237 }
238
239 logger.warn(`MCP ${this.name} unrecognized message`, { msg });
240 }
241
242 private async refreshTools(): Promise<void> {
243 try {

Callers 2

startMethod · 0.95
mcp-server.test.tsFile · 0.45

Calls 8

sendMethod · 0.95
refreshToolsMethod · 0.95
warnMethod · 0.80
debugMethod · 0.80
deleteMethod · 0.80
resolveMethod · 0.80
getMethod · 0.45
emitMethod · 0.45

Tested by

no test coverage detected