Skip to content

Commit 8c2086e

Browse files
committed
refactor handle request types
1 parent af64a0e commit 8c2086e

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

src/server/streamableHttp.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ describe("StreamableHTTPServerTransport", () => {
344344
"Allow": "POST, DELETE"
345345
}));
346346
expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('"jsonrpc":"2.0"'));
347-
expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('Server does not offer an SSE stream at this endpoint'));
347+
expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('Method not allowed'));
348348
});
349349

350350
it("should reject POST requests without proper Accept header", async () => {

src/server/streamableHttp.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,20 @@ export class StreamableHTTPServerTransport implements Transport {
8484
* Handles an incoming HTTP request, whether GET or POST
8585
*/
8686
async handleRequest(req: IncomingMessage, res: ServerResponse, parsedBody?: unknown): Promise<void> {
87-
if (req.method === "GET") {
88-
await this.handleGetRequest(req, res);
89-
} else if (req.method === "POST") {
87+
if (req.method === "POST") {
9088
await this.handlePostRequest(req, res, parsedBody);
9189
} else if (req.method === "DELETE") {
9290
await this.handleDeleteRequest(req, res);
9391
} else {
94-
res.writeHead(405).end(JSON.stringify({
95-
jsonrpc: "2.0",
96-
error: {
97-
code: -32000,
98-
message: "Method not allowed"
99-
},
100-
id: null
101-
}));
92+
await this.handleUnsupportedRequest(req, res);
10293
}
10394
}
10495

10596
/**
106-
* Handles GET requests to establish SSE connections
107-
* According to the MCP Streamable HTTP transport spec, the server MUST either return SSE or 405.
108-
* We choose to return 405 Method Not Allowed as we don't support GET SSE connections yet.
97+
* Handles unsupported requests (GET, PUT, PATCH, etc.)
98+
* For now we support only POST and DELETE requests. Support for GET for SSE connections will be added later.
10999
*/
110-
private async handleGetRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
111-
// Check session validity first for GET requests when session management is enabled
100+
private async handleUnsupportedRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
112101
if (this._sessionId !== undefined && !this.validateSession(req, res)) {
113102
return;
114103
}
@@ -119,7 +108,7 @@ export class StreamableHTTPServerTransport implements Transport {
119108
jsonrpc: "2.0",
120109
error: {
121110
code: -32000,
122-
message: "Method not allowed: Server does not offer an SSE stream at this endpoint"
111+
message: "Method not allowed."
123112
},
124113
id: null
125114
}));
@@ -322,6 +311,8 @@ export class StreamableHTTPServerTransport implements Transport {
322311

323312
async send(message: JSONRPCMessage, options?: { relatedRequestId?: RequestId }): Promise<void> {
324313
const relatedRequestId = options?.relatedRequestId;
314+
// SSE connections are established per POST request, for now we don't support it through the GET
315+
// this will be changed when we implement the GET SSE connection
325316
if (relatedRequestId === undefined) {
326317
throw new Error("relatedRequestId is required for Streamable HTTP transport");
327318
}

0 commit comments

Comments
 (0)