@tsed/platform-mcp 8.41.2 → 8.42.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 +11 -0
- package/lib/esm/common/interfaces/PlatformMcpAuthSettings.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 +112 -0
- package/lib/esm/http/services/PlatformMcpModule.js +60 -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 +11 -0
- package/lib/types/common/interfaces/PlatformMcpAuthSettings.d.ts +81 -0
- package/lib/types/common/interfaces/PlatformMcpSettings.d.ts +10 -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 +61 -0
- package/lib/types/http/services/PlatformMcpModule.d.ts +9 -2
- package/package.json +11 -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 +11 -0
- package/src/common/interfaces/PlatformMcpAuthSettings.ts +82 -0
- package/src/common/interfaces/PlatformMcpSettings.ts +10 -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 +239 -0
- package/src/http/services/PlatformMcpAuthService.ts +137 -0
- package/src/http/services/PlatformMcpModule.spec.ts +89 -0
- package/src/http/services/PlatformMcpModule.ts +79 -9
package/lib/esm/http/index.js
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { bearerAuthChallengeResponse, buildOAuthProtectedResourceMetadata, getOAuthProtectedResourceMetadataUrl, verifyBearerToken } from "@modelcontextprotocol/server";
|
|
2
|
+
import { injectable, logger } from "@tsed/di";
|
|
3
|
+
import { PlatformTokenVerifier } from "../../common/domain/PlatformTokenVerifier.js";
|
|
4
|
+
export const PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource";
|
|
5
|
+
/**
|
|
6
|
+
* OAuth resource-server behavior of the protected MCP endpoints: configuration validation,
|
|
7
|
+
* protected resource metadata and bearer token verification.
|
|
8
|
+
*
|
|
9
|
+
* @module platform/mcp
|
|
10
|
+
*/
|
|
11
|
+
export class PlatformMcpAuthService {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.verifiers = new WeakMap();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Returns the token verifier of an endpoint, created once per `auth` configuration.
|
|
17
|
+
*/
|
|
18
|
+
getVerifier(auth, resource) {
|
|
19
|
+
let verifier = this.verifiers.get(auth);
|
|
20
|
+
if (!verifier) {
|
|
21
|
+
verifier = new PlatformTokenVerifier(auth, resource);
|
|
22
|
+
this.verifiers.set(auth, verifier);
|
|
23
|
+
}
|
|
24
|
+
return verifier;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Verifies the bearer token of the incoming request.
|
|
28
|
+
*
|
|
29
|
+
* @returns The verified identity, or the `401`/`403` challenge to send back to the client.
|
|
30
|
+
*/
|
|
31
|
+
async verifyMcpRequest(auth, $ctx) {
|
|
32
|
+
const resource = this.getResourceUrl(auth);
|
|
33
|
+
const options = {
|
|
34
|
+
verifier: this.getVerifier(auth, resource),
|
|
35
|
+
requiredScopes: auth.requiredScopes,
|
|
36
|
+
resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(resource)
|
|
37
|
+
};
|
|
38
|
+
try {
|
|
39
|
+
return await verifyBearerToken($ctx.request.headers.authorization, options);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
return bearerAuthChallengeResponse(error, options);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Rejects an `auth` configuration that cannot work or would be unsafe, before any route is mounted.
|
|
47
|
+
*/
|
|
48
|
+
validate(path, auth) {
|
|
49
|
+
const mode = PlatformTokenVerifier.getMode(auth);
|
|
50
|
+
if (!auth.verifier && mode === "introspection" && !(auth.clientId && auth.clientSecret)) {
|
|
51
|
+
throw new Error(`MCP endpoint "${path}": the introspection mode requires auth.clientId and auth.clientSecret.`);
|
|
52
|
+
}
|
|
53
|
+
if (!auth.verifier && auth.audience === false) {
|
|
54
|
+
if (mode === "offline") {
|
|
55
|
+
throw new Error(`MCP endpoint "${path}": auth.audience cannot be disabled in offline mode.`);
|
|
56
|
+
}
|
|
57
|
+
logger().warn({
|
|
58
|
+
event: "MCP_AUTH_AUDIENCE_DISABLED",
|
|
59
|
+
message: `MCP endpoint "${path}": the audience check is disabled, any active token of ${auth.issuer} is accepted.`
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
if (!auth.resource) {
|
|
63
|
+
// the resource is never derived from the request: the Host header is controlled by the caller
|
|
64
|
+
throw new Error(`MCP endpoint "${path}": auth.resource is required.`);
|
|
65
|
+
}
|
|
66
|
+
if (!URL.canParse(auth.resource)) {
|
|
67
|
+
throw new Error(`MCP endpoint "${path}": auth.resource must be an absolute URL.`);
|
|
68
|
+
}
|
|
69
|
+
if (!URL.canParse(auth.issuer)) {
|
|
70
|
+
throw new Error(`MCP endpoint "${path}": auth.issuer must be an absolute URL.`);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
// surfaces at startup what the metadata route would otherwise fail on (insecure issuer, invalid documentation URL)
|
|
74
|
+
this.getProtectedResourceMetadata(auth, this.getResourceUrl(auth));
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw new Error(`MCP endpoint "${path}": invalid auth configuration. ${error.message}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Builds the OAuth 2.0 Protected Resource Metadata (RFC 9728) advertised for the given resource.
|
|
82
|
+
*
|
|
83
|
+
* @throws When the issuer is not an HTTPS URL (outside localhost) and `allowInsecureRequests` is not set.
|
|
84
|
+
*/
|
|
85
|
+
getProtectedResourceMetadata(auth, resource) {
|
|
86
|
+
return buildOAuthProtectedResourceMetadata({
|
|
87
|
+
// the SDK builder only reads the issuer of the authorization server metadata
|
|
88
|
+
oauthMetadata: { issuer: auth.issuer },
|
|
89
|
+
resourceServerUrl: resource,
|
|
90
|
+
scopesSupported: auth.scopesSupported,
|
|
91
|
+
resourceName: auth.resourceName,
|
|
92
|
+
serviceDocumentationUrl: auth.resourceDocumentation ? new URL(auth.resourceDocumentation) : undefined,
|
|
93
|
+
dangerouslyAllowInsecureIssuerUrl: auth.allowInsecureRequests
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Path of the RFC 9728 metadata document describing the MCP endpoint mounted on `path`.
|
|
98
|
+
*/
|
|
99
|
+
getProtectedResourceMetadataPath(path) {
|
|
100
|
+
return `${PROTECTED_RESOURCE_METADATA_PATH}${path === "/" ? "" : path}`;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Canonical URL of the MCP endpoint, as configured in `auth.resource`.
|
|
104
|
+
*
|
|
105
|
+
* It is used for the metadata document, the bearer challenge and the expected audience, and is never
|
|
106
|
+
* derived from the incoming request.
|
|
107
|
+
*/
|
|
108
|
+
getResourceUrl(auth) {
|
|
109
|
+
return new URL(auth.resource);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
injectable(PlatformMcpAuthService);
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { application } from "@tsed/platform-http";
|
|
2
|
-
import { constant, injectable } from "@tsed/di";
|
|
2
|
+
import { constant, inject, injectable } from "@tsed/di";
|
|
3
3
|
import { NodeStreamableHTTPServerTransport } from "@modelcontextprotocol/node";
|
|
4
4
|
import { useContextHandler } from "@tsed/platform-router";
|
|
5
|
+
import { attachUpstream } from "../../common/utils/attachUpstream.js";
|
|
6
|
+
import { hasUpstreamPlaceholders } from "../../common/utils/resolveUpstream.js";
|
|
5
7
|
import { createMcpServer, resolveMcpServerOptions } from "../../common/utils/createMcpServer.js";
|
|
8
|
+
import { PlatformMcpAuthService } from "./PlatformMcpAuthService.js";
|
|
6
9
|
/**
|
|
7
10
|
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
8
11
|
*
|
|
@@ -14,6 +17,7 @@ export class PlatformMcpModule {
|
|
|
14
17
|
this.settings = constant("mcp", {});
|
|
15
18
|
this.app = application();
|
|
16
19
|
this.mcps = [];
|
|
20
|
+
this.platformAuthService = inject(PlatformMcpAuthService);
|
|
17
21
|
this.loaded = false;
|
|
18
22
|
}
|
|
19
23
|
$onRoutesInit() {
|
|
@@ -26,7 +30,20 @@ export class PlatformMcpModule {
|
|
|
26
30
|
continue;
|
|
27
31
|
}
|
|
28
32
|
const path = opts.path || "/mcp";
|
|
29
|
-
|
|
33
|
+
this.validate(path, opts);
|
|
34
|
+
const resolvedSettings = {
|
|
35
|
+
...resolveMcpServerOptions(opts),
|
|
36
|
+
path
|
|
37
|
+
};
|
|
38
|
+
if (opts.auth) {
|
|
39
|
+
const metadataPath = this.platformAuthService.getProtectedResourceMetadataPath(path);
|
|
40
|
+
this.app.get(metadataPath, useContextHandler(($ctx) => this.metadata(resolvedSettings, $ctx)));
|
|
41
|
+
this.mcps.push({
|
|
42
|
+
method: "GET",
|
|
43
|
+
name: "PlatformMcpModule.metadata()",
|
|
44
|
+
url: metadataPath
|
|
45
|
+
});
|
|
46
|
+
}
|
|
30
47
|
this.app.post(path, useContextHandler(async ($ctx) => this.dispatch(resolvedSettings, $ctx)));
|
|
31
48
|
this.mcps.push({
|
|
32
49
|
method: "POST",
|
|
@@ -39,15 +56,50 @@ export class PlatformMcpModule {
|
|
|
39
56
|
$logRoutes(routes) {
|
|
40
57
|
return [...routes, ...this.mcps].filter(Boolean);
|
|
41
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Rejects a configuration that cannot work or would be unsafe, before any route is mounted.
|
|
61
|
+
*/
|
|
62
|
+
validate(path, opts) {
|
|
63
|
+
if (opts.auth) {
|
|
64
|
+
this.platformAuthService.validate(path, opts.auth);
|
|
65
|
+
}
|
|
66
|
+
if (opts.upstream && !opts.auth && hasUpstreamPlaceholders(opts.upstream)) {
|
|
67
|
+
throw new Error(`MCP endpoint "${path}": the upstream uses \${OAUTH_*} placeholders but the endpoint declares no auth.`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
metadata(settings, $ctx) {
|
|
71
|
+
const auth = settings.auth;
|
|
72
|
+
const resource = this.platformAuthService.getResourceUrl(auth);
|
|
73
|
+
const body = this.platformAuthService.getProtectedResourceMetadata(auth, resource);
|
|
74
|
+
return $ctx.response
|
|
75
|
+
.status(200)
|
|
76
|
+
.setHeaders({
|
|
77
|
+
"content-type": "application/json",
|
|
78
|
+
"cache-control": "public, max-age=3600",
|
|
79
|
+
"access-control-allow-origin": "*"
|
|
80
|
+
})
|
|
81
|
+
.body(body);
|
|
82
|
+
}
|
|
42
83
|
async dispatch(settings, $ctx) {
|
|
84
|
+
let authInfo;
|
|
85
|
+
if (settings.auth) {
|
|
86
|
+
const result = await this.platformAuthService.verifyMcpRequest(settings.auth, $ctx);
|
|
87
|
+
if (result instanceof Response) {
|
|
88
|
+
return $ctx.response
|
|
89
|
+
.status(result.status)
|
|
90
|
+
.setHeaders(Object.fromEntries(result.headers))
|
|
91
|
+
.body(await result.text());
|
|
92
|
+
}
|
|
93
|
+
authInfo = result;
|
|
94
|
+
// the SDK transport reads `req.auth` and exposes it to handlers as `ctx.http.authInfo`
|
|
95
|
+
$ctx.getReq().auth = authInfo;
|
|
96
|
+
}
|
|
43
97
|
const server = createMcpServer(settings);
|
|
44
98
|
const transport = new NodeStreamableHTTPServerTransport({
|
|
45
99
|
sessionIdGenerator: undefined,
|
|
46
100
|
enableJsonResponse: true,
|
|
47
101
|
...settings.transportOptions
|
|
48
102
|
});
|
|
49
|
-
const { request, response } = $ctx;
|
|
50
|
-
const res = response.getRes();
|
|
51
103
|
let closed = false;
|
|
52
104
|
const closeServer = async () => {
|
|
53
105
|
if (closed) {
|
|
@@ -56,14 +108,15 @@ export class PlatformMcpModule {
|
|
|
56
108
|
closed = true;
|
|
57
109
|
await server.close();
|
|
58
110
|
};
|
|
59
|
-
|
|
111
|
+
$ctx.getRes()?.once("close", closeServer);
|
|
60
112
|
try {
|
|
113
|
+
await attachUpstream(server, settings, authInfo);
|
|
61
114
|
await server.connect(transport);
|
|
62
|
-
await transport.handleRequest(
|
|
115
|
+
await transport.handleRequest($ctx.getReq(), $ctx.getRes(), $ctx.request.body);
|
|
63
116
|
}
|
|
64
117
|
finally {
|
|
65
118
|
if (settings.transportOptions?.enableJsonResponse !== false) {
|
|
66
|
-
|
|
119
|
+
$ctx.getRes()?.off?.("close", closeServer);
|
|
67
120
|
await closeServer();
|
|
68
121
|
}
|
|
69
122
|
}
|