@tsed/platform-mcp 8.43.0 → 8.43.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,6 +20,11 @@ export declare class PlatformMcpModule implements OnRoutesInit {
20
20
  * Rejects a configuration that cannot work or would be unsafe, before any route is mounted.
21
21
  */
22
22
  protected validate(path: string, opts: PlatformMcpSettings): void;
23
+ /**
24
+ * Answers the methods of the Streamable HTTP transport that a stateless endpoint does not serve,
25
+ * so MCP clients know there is no event stream instead of receiving the application's 404 page.
26
+ */
27
+ protected methodNotAllowed($ctx: PlatformContext): import("@tsed/platform-http").PlatformResponse<any>;
23
28
  protected metadata(settings: CreateMcpServerOpts, $ctx: PlatformContext): import("@tsed/platform-http").PlatformResponse<any>;
24
29
  protected dispatch(settings: CreateMcpServerOpts, $ctx: PlatformContext): Promise<import("@tsed/platform-http").PlatformResponse<any> | undefined>;
25
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/platform-mcp",
3
- "version": "8.43.0",
3
+ "version": "8.43.2",
4
4
  "description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
5
5
  "source": "./src/http/index.ts",
6
6
  "type": "module",
@@ -51,14 +51,14 @@
51
51
  "uuid": ">=11.0.2"
52
52
  },
53
53
  "devDependencies": {
54
- "@tsed/barrels": "8.43.0",
55
- "@tsed/core": "8.43.0",
56
- "@tsed/di": "8.43.0",
57
- "@tsed/exceptions": "8.43.0",
58
- "@tsed/json-mapper": "8.43.0",
59
- "@tsed/platform-http": "8.43.0",
60
- "@tsed/schema": "8.43.0",
61
- "@tsed/typescript": "8.43.0",
54
+ "@tsed/barrels": "8.43.2",
55
+ "@tsed/core": "8.43.2",
56
+ "@tsed/di": "8.43.2",
57
+ "@tsed/exceptions": "8.43.2",
58
+ "@tsed/json-mapper": "8.43.2",
59
+ "@tsed/platform-http": "8.43.2",
60
+ "@tsed/schema": "8.43.2",
61
+ "@tsed/typescript": "8.43.2",
62
62
  "jose": "^6.1.3",
63
63
  "typescript": "6.0.2",
64
64
  "vitest": "4.1.4"
@@ -13,6 +13,8 @@ const {app, listenServer, NodeStreamableHTTPServerTransport, express, routeHandl
13
13
  const app = {
14
14
  use: vi.fn(),
15
15
  post: vi.fn((path: string, handler: Function) => routeHandlers.set(path, handler)),
16
+ get: vi.fn((path: string, handler: Function) => routeHandlers.set(`GET ${path}`, handler)),
17
+ delete: vi.fn((path: string, handler: Function) => routeHandlers.set(`DELETE ${path}`, handler)),
16
18
  listen: vi.fn((_: number, handler: Function) => {
17
19
  handler();
18
20
  return listenServer;
@@ -71,6 +73,20 @@ describe("mcpStreamableServer", () => {
71
73
  expect(app.post).toHaveBeenCalledWith("/mcp", expect.any(Function));
72
74
  });
73
75
 
76
+ it.each(["GET", "DELETE"])("answers 405 to %s requests, which a stateless endpoint does not serve", async (method) => {
77
+ void mcpStreamableServer(createServer);
78
+ await vi.waitFor(() => expect(routeHandlers.get(`${method} /mcp`)).toBeDefined());
79
+
80
+ const res = {status: vi.fn().mockReturnThis(), set: vi.fn().mockReturnThis(), json: vi.fn()};
81
+
82
+ routeHandlers.get(`${method} /mcp`)!({}, res);
83
+
84
+ expect(res.status).toHaveBeenCalledWith(405);
85
+ expect(res.set).toHaveBeenCalledWith("Allow", "POST");
86
+ expect(res.json).toHaveBeenCalledWith({jsonrpc: "2.0", error: {code: -32000, message: "Method not allowed."}, id: null});
87
+ expect(createServer).not.toHaveBeenCalled();
88
+ });
89
+
74
90
  it("creates a transport and dispatches each MCP request", async () => {
75
91
  void mcpStreamableServer(createServer);
76
92
  await vi.waitFor(() => expect(routeHandlers.get("/mcp")).toBeDefined());
@@ -37,6 +37,17 @@ export async function mcpStreamableServer(createServer: () => McpServer | Promis
37
37
  }
38
38
  });
39
39
 
40
+ // the endpoint is stateless: there is no event stream to open nor session to delete
41
+ const methodNotAllowed = (_req: any, res: any) => {
42
+ res
43
+ .status(405)
44
+ .set("Allow", "POST")
45
+ .json({jsonrpc: "2.0", error: {code: -32000, message: "Method not allowed."}, id: null});
46
+ };
47
+
48
+ app.get("/mcp", methodNotAllowed);
49
+ app.delete("/mcp", methodNotAllowed);
50
+
40
51
  const port = parseInt(process.env.PORT || "3000");
41
52
 
42
53
  return new Promise((resolve, reject) => {
@@ -107,6 +107,32 @@ describe("PlatformMcpModule", () => {
107
107
  });
108
108
 
109
109
  describe("$onRoutesInit()", () => {
110
+ it("answers 405 to GET and DELETE on the MCP path without logging them as routes", async () => {
111
+ const {module} = await createModule();
112
+ module["settings"] = {path: "/ai/mcp"};
113
+
114
+ vi.spyOn(application(), "post").mockReturnValue(undefined as never);
115
+ vi.spyOn(application(), "get").mockReturnValue(undefined as never);
116
+ vi.spyOn(application(), "delete").mockReturnValue(undefined as never);
117
+
118
+ module.$onRoutesInit();
119
+
120
+ expect(application().get).toHaveBeenCalledExactlyOnceWith("/ai/mcp", expect.any(Function));
121
+ expect(application().delete).toHaveBeenCalledExactlyOnceWith("/ai/mcp", expect.any(Function));
122
+ expect(module.$logRoutes([])).toEqual([{method: "POST", name: "PlatformMcpModule.dispatch()", url: "/ai/mcp"}]);
123
+
124
+ const {$ctx} = createExpressContext();
125
+ const status = vi.spyOn($ctx.response, "status").mockReturnThis();
126
+ const setHeaders = vi.spyOn($ctx.response, "setHeaders").mockReturnThis();
127
+ const body = vi.spyOn($ctx.response, "body").mockReturnThis();
128
+
129
+ module["methodNotAllowed"]($ctx);
130
+
131
+ expect(status).toHaveBeenCalledWith(405);
132
+ expect(setHeaders).toHaveBeenCalledWith({allow: "POST"});
133
+ expect(body).toHaveBeenCalledWith({jsonrpc: "2.0", error: {code: -32000, message: "Method not allowed."}, id: null});
134
+ });
135
+
110
136
  it("should register the MCP route only once", async () => {
111
137
  const {module} = await createModule();
112
138
 
@@ -210,7 +236,7 @@ describe("PlatformMcpModule", () => {
210
236
 
211
237
  module.$onRoutesInit();
212
238
 
213
- expect(application().get).not.toHaveBeenCalled();
239
+ expect(application().get).not.toHaveBeenCalledWith(expect.stringContaining("/.well-known/"), expect.any(Function));
214
240
  expect(module.$logRoutes([])).toEqual([{method: "POST", name: "PlatformMcpModule.dispatch()", url: "/mcp/a"}]);
215
241
  });
216
242
 
@@ -68,6 +68,16 @@ export class PlatformMcpModule implements OnRoutesInit {
68
68
  name: "PlatformMcpModule.dispatch()",
69
69
  url: path
70
70
  } as PlatformRouteDetails);
71
+
72
+ // the endpoint is stateless: there is no event stream to open nor session to delete
73
+ this.app.get(
74
+ path,
75
+ useContextHandler(($ctx) => this.methodNotAllowed($ctx as PlatformContext))
76
+ );
77
+ this.app.delete(
78
+ path,
79
+ useContextHandler(($ctx) => this.methodNotAllowed($ctx as PlatformContext))
80
+ );
71
81
  }
72
82
 
73
83
  this.loaded = true;
@@ -94,6 +104,17 @@ export class PlatformMcpModule implements OnRoutesInit {
94
104
  }
95
105
  }
96
106
 
107
+ /**
108
+ * Answers the methods of the Streamable HTTP transport that a stateless endpoint does not serve,
109
+ * so MCP clients know there is no event stream instead of receiving the application's 404 page.
110
+ */
111
+ protected methodNotAllowed($ctx: PlatformContext) {
112
+ return $ctx.response
113
+ .status(405)
114
+ .setHeaders({allow: "POST"})
115
+ .body({jsonrpc: "2.0", error: {code: -32000, message: "Method not allowed."}, id: null});
116
+ }
117
+
97
118
  protected metadata(settings: CreateMcpServerOpts, $ctx: PlatformContext) {
98
119
  const auth = settings.auth as PlatformMcpAuthSettings;
99
120
  const resource = this.platformAuthService.getResourceUrl(auth);