From 8c2086ec975a94b778662ca792c6ef7ecc9c616b Mon Sep 17 00:00:00 2001 From: ihrpr Date: Mon, 7 Apr 2025 16:15:09 +0100 Subject: [PATCH] refactor handle request types --- src/server/streamableHttp.test.ts | 2 +- src/server/streamableHttp.ts | 25 ++++++++----------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/server/streamableHttp.test.ts b/src/server/streamableHttp.test.ts index 6c5c8705..981ae17b 100644 --- a/src/server/streamableHttp.test.ts +++ b/src/server/streamableHttp.test.ts @@ -344,7 +344,7 @@ describe("StreamableHTTPServerTransport", () => { "Allow": "POST, DELETE" })); expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('"jsonrpc":"2.0"')); - expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('Server does not offer an SSE stream at this endpoint')); + expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('Method not allowed')); }); it("should reject POST requests without proper Accept header", async () => { diff --git a/src/server/streamableHttp.ts b/src/server/streamableHttp.ts index d7f44160..a58370f1 100644 --- a/src/server/streamableHttp.ts +++ b/src/server/streamableHttp.ts @@ -84,31 +84,20 @@ export class StreamableHTTPServerTransport implements Transport { * Handles an incoming HTTP request, whether GET or POST */ async handleRequest(req: IncomingMessage, res: ServerResponse, parsedBody?: unknown): Promise { - if (req.method === "GET") { - await this.handleGetRequest(req, res); - } else if (req.method === "POST") { + if (req.method === "POST") { await this.handlePostRequest(req, res, parsedBody); } else if (req.method === "DELETE") { await this.handleDeleteRequest(req, res); } else { - res.writeHead(405).end(JSON.stringify({ - jsonrpc: "2.0", - error: { - code: -32000, - message: "Method not allowed" - }, - id: null - })); + await this.handleUnsupportedRequest(req, res); } } /** - * Handles GET requests to establish SSE connections - * According to the MCP Streamable HTTP transport spec, the server MUST either return SSE or 405. - * We choose to return 405 Method Not Allowed as we don't support GET SSE connections yet. + * Handles unsupported requests (GET, PUT, PATCH, etc.) + * For now we support only POST and DELETE requests. Support for GET for SSE connections will be added later. */ - private async handleGetRequest(req: IncomingMessage, res: ServerResponse): Promise { - // Check session validity first for GET requests when session management is enabled + private async handleUnsupportedRequest(req: IncomingMessage, res: ServerResponse): Promise { if (this._sessionId !== undefined && !this.validateSession(req, res)) { return; } @@ -119,7 +108,7 @@ export class StreamableHTTPServerTransport implements Transport { jsonrpc: "2.0", error: { code: -32000, - message: "Method not allowed: Server does not offer an SSE stream at this endpoint" + message: "Method not allowed." }, id: null })); @@ -322,6 +311,8 @@ export class StreamableHTTPServerTransport implements Transport { async send(message: JSONRPCMessage, options?: { relatedRequestId?: RequestId }): Promise { const relatedRequestId = options?.relatedRequestId; + // SSE connections are established per POST request, for now we don't support it through the GET + // this will be changed when we implement the GET SSE connection if (relatedRequestId === undefined) { throw new Error("relatedRequestId is required for Streamable HTTP transport"); }