Skip to content

Commit a5144dc

Browse files
committed
use utility to check for message types
1 parent 8775c1f commit a5144dc

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/server/streamableHttp.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IncomingMessage, ServerResponse } from "node:http";
22
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";
44
import getRawBody from "raw-body";
55
import contentType from "content-type";
66
import { randomUUID } from "node:crypto";
@@ -415,7 +415,7 @@ export class StreamableHTTPServerTransport implements Transport {
415415
// Store the response for this request to send messages back through this connection
416416
// We need to track by request ID to maintain the connection
417417
for (const message of messages) {
418-
if ('method' in message && 'id' in message) {
418+
if (isJSONRPCRequest(message)) {
419419
this._streamMapping.set(streamId, res);
420420
this._requestToStreamMapping.set(message.id, streamId);
421421
}
@@ -535,7 +535,7 @@ export class StreamableHTTPServerTransport implements Transport {
535535

536536
async send(message: JSONRPCMessage, options?: { relatedRequestId?: RequestId }): Promise<void> {
537537
let requestId = options?.relatedRequestId;
538-
if ('result' in message || 'error' in message) {
538+
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
539539
// If the message is a response, use the request ID from the message
540540
requestId = message.id;
541541
}
@@ -545,7 +545,7 @@ export class StreamableHTTPServerTransport implements Transport {
545545
// Those will be sent via dedicated response SSE streams
546546
if (requestId === undefined) {
547547
// 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)) {
549549
throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
550550
}
551551
const standaloneSse = this._streamMapping.get(this._standaloneSseStreamId)

0 commit comments

Comments
 (0)