Add helper functions to distinguish between jsonRPC message types

This commit is contained in:
Frédéric Barthelet 2025-04-07 18:24:50 +02:00
commit 8215651552

View file

@ -3,6 +3,9 @@ import {
CancelledNotificationSchema,
ClientCapabilities,
ErrorCode,
isJSONRPCRequest,
isJSONRPCResponse,
isJSONRPCNotification,
JSONRPCError,
JSONRPCNotification,
JSONRPCRequest,
@ -271,12 +274,14 @@ export abstract class Protocol<
};
this._transport.onmessage = (message) => {
if (!("method" in message)) {
if (isJSONRPCResponse(message)) {
this._onresponse(message);
} else if ("id" in message) {
} else if (isJSONRPCRequest(message)) {
this._onrequest(message);
} else {
} else if (isJSONRPCNotification(message)) {
this._onnotification(message);
} else {
this._onerror(new Error(`Unknown message type: ${JSON.stringify(message)}`));
}
};
@ -437,7 +442,7 @@ export abstract class Protocol<
this._progressHandlers.delete(messageId);
this._cleanupTimeout(messageId);
if ("result" in response) {
if (isJSONRPCResponse(response)) {
handler(response);
} else {
const error = new McpError(