@tsed/platform-mcp 8.31.1 → 8.31.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.
@@ -14,12 +14,15 @@ export class PlatformMcpModule {
14
14
  this.settings = constant("mcp", {});
15
15
  this.app = application();
16
16
  this.server = inject(MCP_SERVER);
17
+ this.loaded = false;
17
18
  }
18
19
  $onRoutesInit() {
19
- if (this.isEnabled()) {
20
- const path = this.settings?.path || "/mcp";
21
- this.app.post(path, useContextHandler(async ($ctx) => this.dispatch($ctx)));
20
+ if (!this.isEnabled() || this.loaded) {
21
+ return;
22
22
  }
23
+ const path = this.settings?.path || "/mcp";
24
+ this.app.post(path, useContextHandler(async ($ctx) => this.dispatch($ctx)));
25
+ this.loaded = true;
23
26
  }
24
27
  $logRoutes(routes) {
25
28
  return [
@@ -38,13 +41,23 @@ export class PlatformMcpModule {
38
41
  ...this.settings.transportOptions
39
42
  });
40
43
  const { request, response } = $ctx;
41
- response.raw?.on("close", () => transport.close());
44
+ const res = response.getRes();
45
+ let closed = false;
46
+ const closeTransport = async () => {
47
+ if (closed) {
48
+ return;
49
+ }
50
+ closed = true;
51
+ await transport.close();
52
+ };
53
+ res?.once("close", closeTransport);
42
54
  try {
43
55
  await this.server.connect(transport);
44
- await transport.handleRequest(request.getReq(), response.getRes(), request.body);
56
+ await transport.handleRequest(request.getReq(), res, request.body);
45
57
  }
46
58
  finally {
47
- await transport.close();
59
+ res?.off?.("close", closeTransport);
60
+ await closeTransport();
48
61
  }
49
62
  }
50
63
  isEnabled() {