1
1
import { IncomingMessage , ServerResponse } from "node:http" ;
2
2
import { Transport } from "../shared/transport.js" ;
3
- import { isInitializeRequest , isJSONRPCRequest , isJSONRPCResponse , JSONRPCMessage , JSONRPCMessageSchema , RequestId } from "../types.js" ;
3
+ import { isInitializeRequest , isJSONRPCError , isJSONRPCRequest , isJSONRPCResponse , JSONRPCMessage , JSONRPCMessageSchema , RequestId } from "../types.js" ;
4
4
import getRawBody from "raw-body" ;
5
5
import contentType from "content-type" ;
6
6
import { randomUUID } from "node:crypto" ;
@@ -415,7 +415,7 @@ export class StreamableHTTPServerTransport implements Transport {
415
415
// Store the response for this request to send messages back through this connection
416
416
// We need to track by request ID to maintain the connection
417
417
for ( const message of messages ) {
418
- if ( 'method' in message && 'id' in message ) {
418
+ if ( isJSONRPCRequest ( message ) ) {
419
419
this . _streamMapping . set ( streamId , res ) ;
420
420
this . _requestToStreamMapping . set ( message . id , streamId ) ;
421
421
}
@@ -535,7 +535,7 @@ export class StreamableHTTPServerTransport implements Transport {
535
535
536
536
async send ( message : JSONRPCMessage , options ?: { relatedRequestId ?: RequestId } ) : Promise < void > {
537
537
let requestId = options ?. relatedRequestId ;
538
- if ( 'result' in message || 'error' in message ) {
538
+ if ( isJSONRPCResponse ( message ) || isJSONRPCError ( message ) ) {
539
539
// If the message is a response, use the request ID from the message
540
540
requestId = message . id ;
541
541
}
@@ -545,7 +545,7 @@ export class StreamableHTTPServerTransport implements Transport {
545
545
// Those will be sent via dedicated response SSE streams
546
546
if ( requestId === undefined ) {
547
547
// For standalone SSE streams, we can only send requests and notifications
548
- if ( 'result' in message || 'error' in message ) {
548
+ if ( isJSONRPCResponse ( message ) || isJSONRPCError ( message ) ) {
549
549
throw new Error ( "Cannot send a response on a standalone SSE stream unless resuming a previous client request" ) ;
550
550
}
551
551
const standaloneSse = this . _streamMapping . get ( this . _standaloneSseStreamId )
0 commit comments