File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,10 @@ import {
3
3
CancelledNotificationSchema ,
4
4
ClientCapabilities ,
5
5
ErrorCode ,
6
+ isJSONRPCError ,
7
+ isJSONRPCRequest ,
8
+ isJSONRPCResponse ,
9
+ isJSONRPCNotification ,
6
10
JSONRPCError ,
7
11
JSONRPCNotification ,
8
12
JSONRPCRequest ,
@@ -271,12 +275,14 @@ export abstract class Protocol<
271
275
} ;
272
276
273
277
this . _transport . onmessage = ( message ) => {
274
- if ( ! ( "method" in message ) ) {
278
+ if ( isJSONRPCResponse ( message ) || isJSONRPCError ( message ) ) {
275
279
this . _onresponse ( message ) ;
276
- } else if ( "id" in message ) {
280
+ } else if ( isJSONRPCRequest ( message ) ) {
277
281
this . _onrequest ( message ) ;
278
- } else {
282
+ } else if ( isJSONRPCNotification ( message ) ) {
279
283
this . _onnotification ( message ) ;
284
+ } else {
285
+ this . _onerror ( new Error ( `Unknown message type: ${ JSON . stringify ( message ) } ` ) ) ;
280
286
}
281
287
} ;
282
288
@@ -437,7 +443,7 @@ export abstract class Protocol<
437
443
this . _progressHandlers . delete ( messageId ) ;
438
444
this . _cleanupTimeout ( messageId ) ;
439
445
440
- if ( "result" in response ) {
446
+ if ( isJSONRPCResponse ( response ) ) {
441
447
handler ( response ) ;
442
448
} else {
443
449
const error = new McpError (
Original file line number Diff line number Diff line change @@ -78,6 +78,9 @@ export const JSONRPCRequestSchema = z
78
78
. merge ( RequestSchema )
79
79
. strict ( ) ;
80
80
81
+ export const isJSONRPCRequest = ( value : unknown ) : value is JSONRPCRequest =>
82
+ JSONRPCRequestSchema . safeParse ( value ) . success ;
83
+
81
84
/**
82
85
* A notification which does not expect a response.
83
86
*/
@@ -88,6 +91,11 @@ export const JSONRPCNotificationSchema = z
88
91
. merge ( NotificationSchema )
89
92
. strict ( ) ;
90
93
94
+ export const isJSONRPCNotification = (
95
+ value : unknown
96
+ ) : value is JSONRPCNotification =>
97
+ JSONRPCNotificationSchema . safeParse ( value ) . success ;
98
+
91
99
/**
92
100
* A successful (non-error) response to a request.
93
101
*/
@@ -99,6 +107,9 @@ export const JSONRPCResponseSchema = z
99
107
} )
100
108
. strict ( ) ;
101
109
110
+ export const isJSONRPCResponse = ( value : unknown ) : value is JSONRPCResponse =>
111
+ JSONRPCResponseSchema . safeParse ( value ) . success ;
112
+
102
113
/**
103
114
* Error codes defined by the JSON-RPC specification.
104
115
*/
@@ -139,6 +150,9 @@ export const JSONRPCErrorSchema = z
139
150
} )
140
151
. strict ( ) ;
141
152
153
+ export const isJSONRPCError = ( value : unknown ) : value is JSONRPCError =>
154
+ JSONRPCErrorSchema . safeParse ( value ) . success ;
155
+
142
156
export const JSONRPCMessageSchema = z . union ( [
143
157
JSONRPCRequestSchema ,
144
158
JSONRPCNotificationSchema ,
You can’t perform that action at this time.
0 commit comments