@tsed/platform-mcp 8.41.3 → 8.43.0
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.
- package/lib/esm/cli/services/mcpServerConnect.js +17 -1
- package/lib/esm/cli/services/mcpStreamableServer.js +1 -1
- package/lib/esm/common/domain/PlatformTokenVerifier.js +169 -0
- package/lib/esm/common/errors/PlatformInvalidToken.js +11 -0
- package/lib/esm/common/errors/PlatformServerError.js +11 -0
- package/lib/esm/common/index.js +12 -0
- package/lib/esm/common/interfaces/PlatformMcpAuthSettings.js +1 -0
- package/lib/esm/common/interfaces/PlatformMcpPreAuth.js +1 -0
- package/lib/esm/common/interfaces/PlatformMcpUpstreamSettings.js +1 -0
- package/lib/esm/common/services/PlatformMcpGatewayService.js +167 -0
- package/lib/esm/common/utils/attachUpstream.js +140 -0
- package/lib/esm/common/utils/createUpstreamTransport.js +21 -0
- package/lib/esm/common/utils/interpolate.js +24 -0
- package/lib/esm/common/utils/listAll.js +13 -0
- package/lib/esm/common/utils/resolveUpstream.js +35 -0
- package/lib/esm/http/index.js +1 -0
- package/lib/esm/http/services/PlatformMcpAuthService.js +131 -0
- package/lib/esm/http/services/PlatformMcpModule.js +74 -7
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/cli/services/mcpStreamableServer.d.ts +1 -1
- package/lib/types/common/domain/PlatformTokenVerifier.d.ts +59 -0
- package/lib/types/common/errors/PlatformInvalidToken.d.ts +9 -0
- package/lib/types/common/errors/PlatformServerError.d.ts +9 -0
- package/lib/types/common/index.d.ts +12 -0
- package/lib/types/common/interfaces/PlatformMcpAuthSettings.d.ts +87 -0
- package/lib/types/common/interfaces/PlatformMcpPreAuth.d.ts +33 -0
- package/lib/types/common/interfaces/PlatformMcpSettings.d.ts +12 -0
- package/lib/types/common/interfaces/PlatformMcpUpstreamSettings.d.ts +77 -0
- package/lib/types/common/services/PlatformMcpGatewayService.d.ts +66 -0
- package/lib/types/common/utils/attachUpstream.d.ts +14 -0
- package/lib/types/common/utils/createUpstreamTransport.d.ts +10 -0
- package/lib/types/common/utils/interpolate.d.ts +19 -0
- package/lib/types/common/utils/listAll.d.ts +3 -0
- package/lib/types/common/utils/resolveUpstream.d.ts +14 -0
- package/lib/types/http/index.d.ts +1 -0
- package/lib/types/http/services/PlatformMcpAuthService.d.ts +71 -0
- package/lib/types/http/services/PlatformMcpModule.d.ts +9 -2
- package/package.json +12 -8
- package/readme.md +2 -0
- package/src/cli/services/mcpServerConnect.spec.ts +40 -4
- package/src/cli/services/mcpServerConnect.ts +21 -1
- package/src/cli/services/mcpStreamableServer.ts +2 -2
- package/src/common/domain/PlatformTokenVerifier.spec.ts +313 -0
- package/src/common/domain/PlatformTokenVerifier.ts +230 -0
- package/src/common/errors/PlatformInvalidToken.ts +12 -0
- package/src/common/errors/PlatformServerError.ts +12 -0
- package/src/common/index.ts +12 -0
- package/src/common/interfaces/PlatformMcpAuthSettings.ts +88 -0
- package/src/common/interfaces/PlatformMcpPreAuth.ts +37 -0
- package/src/common/interfaces/PlatformMcpSettings.ts +12 -0
- package/src/common/interfaces/PlatformMcpUpstreamSettings.ts +80 -0
- package/src/common/services/PlatformMcpGatewayService.spec.ts +282 -0
- package/src/common/services/PlatformMcpGatewayService.ts +247 -0
- package/src/common/utils/attachUpstream.spec.ts +305 -0
- package/src/common/utils/attachUpstream.ts +207 -0
- package/src/common/utils/createUpstreamTransport.spec.ts +94 -0
- package/src/common/utils/createUpstreamTransport.ts +29 -0
- package/src/common/utils/interpolate.spec.ts +33 -0
- package/src/common/utils/interpolate.ts +28 -0
- package/src/common/utils/listAll.spec.ts +28 -0
- package/src/common/utils/listAll.ts +17 -0
- package/src/common/utils/resolveUpstream.spec.ts +78 -0
- package/src/common/utils/resolveUpstream.ts +44 -0
- package/src/http/index.ts +1 -0
- package/src/http/services/PlatformMcpAuthService.spec.ts +297 -0
- package/src/http/services/PlatformMcpAuthService.ts +165 -0
- package/src/http/services/PlatformMcpModule.spec.ts +120 -0
- package/src/http/services/PlatformMcpModule.ts +97 -10
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import {application, type OnRoutesInit, PlatformContext, type PlatformRouteDetails} from "@tsed/platform-http";
|
|
2
|
-
import {constant, injectable} from "@tsed/di";
|
|
3
|
-
import type {PlatformMcpSettings} from "../../common/index.js";
|
|
1
|
+
import {application, type OnRoutesInit, type PlatformContext, type PlatformRouteDetails} from "@tsed/platform-http";
|
|
2
|
+
import {constant, inject, injectable} from "@tsed/di";
|
|
3
|
+
import type {PlatformMcpAuthSettings, PlatformMcpSettings} from "../../common/index.js";
|
|
4
4
|
import {NodeStreamableHTTPServerTransport} from "@modelcontextprotocol/node";
|
|
5
|
+
import type {AuthInfo} from "@modelcontextprotocol/server";
|
|
5
6
|
import {useContextHandler} from "@tsed/platform-router";
|
|
6
|
-
import {
|
|
7
|
+
import {attachUpstream} from "../../common/utils/attachUpstream.js";
|
|
8
|
+
import {hasUpstreamPlaceholders} from "../../common/utils/resolveUpstream.js";
|
|
9
|
+
import {createMcpServer, type CreateMcpServerOpts, resolveMcpServerOptions} from "../../common/utils/createMcpServer.js";
|
|
10
|
+
import {PlatformMcpAuthService} from "./PlatformMcpAuthService.js";
|
|
7
11
|
|
|
8
12
|
/**
|
|
9
13
|
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
@@ -15,6 +19,7 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
15
19
|
protected settings = constant<PlatformMcpSettings | PlatformMcpSettings[]>("mcp", {});
|
|
16
20
|
protected app = application();
|
|
17
21
|
protected mcps: PlatformRouteDetails[] = [];
|
|
22
|
+
protected platformAuthService = inject(PlatformMcpAuthService);
|
|
18
23
|
private loaded = false;
|
|
19
24
|
|
|
20
25
|
$onRoutesInit() {
|
|
@@ -30,7 +35,28 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
const path = opts.path || "/mcp";
|
|
33
|
-
|
|
38
|
+
|
|
39
|
+
this.validate(path, opts);
|
|
40
|
+
|
|
41
|
+
const resolvedSettings = {
|
|
42
|
+
...resolveMcpServerOptions(opts),
|
|
43
|
+
path
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
if (this.platformAuthService.isOAuth(opts.auth)) {
|
|
47
|
+
const metadataPath = this.platformAuthService.getProtectedResourceMetadataPath(path);
|
|
48
|
+
|
|
49
|
+
this.app.get(
|
|
50
|
+
metadataPath,
|
|
51
|
+
useContextHandler(($ctx) => this.metadata(resolvedSettings, $ctx as PlatformContext))
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
this.mcps.push({
|
|
55
|
+
method: "GET",
|
|
56
|
+
name: "PlatformMcpModule.metadata()",
|
|
57
|
+
url: metadataPath
|
|
58
|
+
} as PlatformRouteDetails);
|
|
59
|
+
}
|
|
34
60
|
|
|
35
61
|
this.app.post(
|
|
36
62
|
path,
|
|
@@ -51,7 +77,69 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
51
77
|
return [...routes, ...this.mcps].filter(Boolean);
|
|
52
78
|
}
|
|
53
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Rejects a configuration that cannot work or would be unsafe, before any route is mounted.
|
|
82
|
+
*/
|
|
83
|
+
protected validate(path: string, opts: PlatformMcpSettings) {
|
|
84
|
+
const {auth} = opts;
|
|
85
|
+
|
|
86
|
+
if (this.platformAuthService.isOAuth(auth)) {
|
|
87
|
+
this.platformAuthService.validate(path, auth);
|
|
88
|
+
} else if (auth && !auth.preAuth) {
|
|
89
|
+
throw new Error(`MCP endpoint "${path}": auth requires an issuer, a preAuth check, or both.`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (opts.upstream && !opts.auth && hasUpstreamPlaceholders(opts.upstream)) {
|
|
93
|
+
throw new Error(`MCP endpoint "${path}": the upstream uses \${OAUTH_*} placeholders but the endpoint declares no auth.`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected metadata(settings: CreateMcpServerOpts, $ctx: PlatformContext) {
|
|
98
|
+
const auth = settings.auth as PlatformMcpAuthSettings;
|
|
99
|
+
const resource = this.platformAuthService.getResourceUrl(auth);
|
|
100
|
+
const body = this.platformAuthService.getProtectedResourceMetadata(auth, resource);
|
|
101
|
+
|
|
102
|
+
return $ctx.response
|
|
103
|
+
.status(200)
|
|
104
|
+
.setHeaders({
|
|
105
|
+
"content-type": "application/json",
|
|
106
|
+
"cache-control": "public, max-age=3600",
|
|
107
|
+
"access-control-allow-origin": "*"
|
|
108
|
+
})
|
|
109
|
+
.body(body);
|
|
110
|
+
}
|
|
111
|
+
|
|
54
112
|
protected async dispatch(settings: CreateMcpServerOpts, $ctx: PlatformContext) {
|
|
113
|
+
const {auth} = settings;
|
|
114
|
+
let authInfo: AuthInfo | undefined;
|
|
115
|
+
|
|
116
|
+
if (auth?.preAuth) {
|
|
117
|
+
authInfo = await this.platformAuthService.preAuth(auth.preAuth, $ctx);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (auth && !authInfo) {
|
|
121
|
+
if (!this.platformAuthService.isOAuth(auth)) {
|
|
122
|
+
// the custom check did not authenticate the request and there is no OAuth to fall back on
|
|
123
|
+
return $ctx.response.status(401).body({error: "unauthorized", error_description: "Authentication required"});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const result = await this.platformAuthService.verifyMcpRequest(auth, $ctx);
|
|
127
|
+
|
|
128
|
+
if (result instanceof Response) {
|
|
129
|
+
return $ctx.response
|
|
130
|
+
.status(result.status)
|
|
131
|
+
.setHeaders(Object.fromEntries(result.headers))
|
|
132
|
+
.body(await result.text());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
authInfo = result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (authInfo) {
|
|
139
|
+
// the SDK transport reads `req.auth` and exposes it to handlers as `ctx.http.authInfo`
|
|
140
|
+
($ctx.getReq() as {auth?: AuthInfo}).auth = authInfo;
|
|
141
|
+
}
|
|
142
|
+
|
|
55
143
|
const server = createMcpServer(settings);
|
|
56
144
|
|
|
57
145
|
const transport = new NodeStreamableHTTPServerTransport({
|
|
@@ -60,8 +148,6 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
60
148
|
...settings.transportOptions
|
|
61
149
|
});
|
|
62
150
|
|
|
63
|
-
const {request, response} = $ctx;
|
|
64
|
-
const res = response.getRes() as any;
|
|
65
151
|
let closed = false;
|
|
66
152
|
|
|
67
153
|
const closeServer = async () => {
|
|
@@ -73,14 +159,15 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
73
159
|
await server.close();
|
|
74
160
|
};
|
|
75
161
|
|
|
76
|
-
|
|
162
|
+
$ctx.getRes()?.once("close", closeServer);
|
|
77
163
|
|
|
78
164
|
try {
|
|
165
|
+
await attachUpstream(server, settings, authInfo);
|
|
79
166
|
await server.connect(transport as any);
|
|
80
|
-
await transport.handleRequest(
|
|
167
|
+
await transport.handleRequest($ctx.getReq(), $ctx.getRes(), $ctx.request.body);
|
|
81
168
|
} finally {
|
|
82
169
|
if (settings.transportOptions?.enableJsonResponse !== false) {
|
|
83
|
-
|
|
170
|
+
$ctx.getRes()?.off?.("close", closeServer);
|
|
84
171
|
await closeServer();
|
|
85
172
|
}
|
|
86
173
|
}
|