@tsed/platform-mcp 8.42.0 → 8.43.1
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/mcpStreamableServer.js +9 -0
- package/lib/esm/common/index.js +1 -0
- package/lib/esm/common/interfaces/PlatformMcpPreAuth.js +1 -0
- package/lib/esm/http/services/PlatformMcpAuthService.js +21 -2
- package/lib/esm/http/services/PlatformMcpModule.js +32 -5
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/common/index.d.ts +1 -0
- package/lib/types/common/interfaces/PlatformMcpAuthSettings.d.ts +6 -0
- package/lib/types/common/interfaces/PlatformMcpPreAuth.d.ts +33 -0
- package/lib/types/common/interfaces/PlatformMcpSettings.d.ts +4 -2
- package/lib/types/http/services/PlatformMcpAuthService.d.ts +12 -2
- package/lib/types/http/services/PlatformMcpModule.d.ts +5 -0
- package/package.json +9 -8
- package/src/cli/services/mcpStreamableServer.spec.ts +16 -0
- package/src/cli/services/mcpStreamableServer.ts +11 -0
- package/src/common/index.ts +1 -0
- package/src/common/interfaces/PlatformMcpAuthSettings.ts +6 -0
- package/src/common/interfaces/PlatformMcpPreAuth.ts +37 -0
- package/src/common/interfaces/PlatformMcpSettings.ts +4 -2
- package/src/http/services/PlatformMcpAuthService.spec.ts +59 -1
- package/src/http/services/PlatformMcpAuthService.ts +31 -3
- package/src/http/services/PlatformMcpModule.spec.ts +57 -0
- package/src/http/services/PlatformMcpModule.ts +45 -7
|
@@ -29,6 +29,15 @@ export async function mcpStreamableServer(createServer) {
|
|
|
29
29
|
await closeServer();
|
|
30
30
|
}
|
|
31
31
|
});
|
|
32
|
+
// the endpoint is stateless: there is no event stream to open nor session to delete
|
|
33
|
+
const methodNotAllowed = (_req, res) => {
|
|
34
|
+
res
|
|
35
|
+
.status(405)
|
|
36
|
+
.set("Allow", "POST")
|
|
37
|
+
.json({ jsonrpc: "2.0", error: { code: -32000, message: "Method not allowed." }, id: null });
|
|
38
|
+
};
|
|
39
|
+
app.get("/mcp", methodNotAllowed);
|
|
40
|
+
app.delete("/mcp", methodNotAllowed);
|
|
32
41
|
const port = parseInt(process.env.PORT || "3000");
|
|
33
42
|
return new Promise((resolve, reject) => {
|
|
34
43
|
app
|
package/lib/esm/common/index.js
CHANGED
|
@@ -12,6 +12,7 @@ export * from "./fn/definePrompt.js";
|
|
|
12
12
|
export * from "./fn/defineResource.js";
|
|
13
13
|
export * from "./fn/defineTool.js";
|
|
14
14
|
export * from "./interfaces/PlatformMcpAuthSettings.js";
|
|
15
|
+
export * from "./interfaces/PlatformMcpPreAuth.js";
|
|
15
16
|
export * from "./interfaces/PlatformMcpSettings.js";
|
|
16
17
|
export * from "./interfaces/PlatformMcpUpstreamSettings.js";
|
|
17
18
|
export * from "./services/PlatformMcpGatewayService.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { bearerAuthChallengeResponse, buildOAuthProtectedResourceMetadata, getOAuthProtectedResourceMetadataUrl, verifyBearerToken } from "@modelcontextprotocol/server";
|
|
2
|
-
import { injectable, logger } from "@tsed/di";
|
|
3
|
-
import { PlatformTokenVerifier } from "../../common/
|
|
2
|
+
import { inject, injectable, injector, logger } from "@tsed/di";
|
|
3
|
+
import { PlatformTokenVerifier } from "../../common/index.js";
|
|
4
4
|
export const PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource";
|
|
5
5
|
/**
|
|
6
6
|
* OAuth resource-server behavior of the protected MCP endpoints: configuration validation,
|
|
@@ -12,6 +12,25 @@ export class PlatformMcpAuthService {
|
|
|
12
12
|
constructor() {
|
|
13
13
|
this.verifiers = new WeakMap();
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Tells whether the endpoint is protected by an OAuth authorization server, and not only by a custom check.
|
|
17
|
+
*/
|
|
18
|
+
isOAuth(auth) {
|
|
19
|
+
return Boolean(auth?.issuer);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Runs the custom authentication check of an endpoint.
|
|
23
|
+
*
|
|
24
|
+
* @returns The identity of the caller, or `undefined` when the check does not apply to the request.
|
|
25
|
+
*/
|
|
26
|
+
async preAuth(preAuth, $ctx) {
|
|
27
|
+
if (!injector().providers.has(preAuth)) {
|
|
28
|
+
// not a registered provider: the option is the check itself
|
|
29
|
+
return preAuth($ctx);
|
|
30
|
+
}
|
|
31
|
+
const instance = inject(preAuth);
|
|
32
|
+
return instance.preAuth($ctx);
|
|
33
|
+
}
|
|
15
34
|
/**
|
|
16
35
|
* Returns the token verifier of an endpoint, created once per `auth` configuration.
|
|
17
36
|
*/
|
|
@@ -35,7 +35,7 @@ export class PlatformMcpModule {
|
|
|
35
35
|
...resolveMcpServerOptions(opts),
|
|
36
36
|
path
|
|
37
37
|
};
|
|
38
|
-
if (opts.auth) {
|
|
38
|
+
if (this.platformAuthService.isOAuth(opts.auth)) {
|
|
39
39
|
const metadataPath = this.platformAuthService.getProtectedResourceMetadataPath(path);
|
|
40
40
|
this.app.get(metadataPath, useContextHandler(($ctx) => this.metadata(resolvedSettings, $ctx)));
|
|
41
41
|
this.mcps.push({
|
|
@@ -50,6 +50,9 @@ export class PlatformMcpModule {
|
|
|
50
50
|
name: "PlatformMcpModule.dispatch()",
|
|
51
51
|
url: path
|
|
52
52
|
});
|
|
53
|
+
// the endpoint is stateless: there is no event stream to open nor session to delete
|
|
54
|
+
this.app.get(path, useContextHandler(($ctx) => this.methodNotAllowed($ctx)));
|
|
55
|
+
this.app.delete(path, useContextHandler(($ctx) => this.methodNotAllowed($ctx)));
|
|
53
56
|
}
|
|
54
57
|
this.loaded = true;
|
|
55
58
|
}
|
|
@@ -60,13 +63,27 @@ export class PlatformMcpModule {
|
|
|
60
63
|
* Rejects a configuration that cannot work or would be unsafe, before any route is mounted.
|
|
61
64
|
*/
|
|
62
65
|
validate(path, opts) {
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
const { auth } = opts;
|
|
67
|
+
if (this.platformAuthService.isOAuth(auth)) {
|
|
68
|
+
this.platformAuthService.validate(path, auth);
|
|
69
|
+
}
|
|
70
|
+
else if (auth && !auth.preAuth) {
|
|
71
|
+
throw new Error(`MCP endpoint "${path}": auth requires an issuer, a preAuth check, or both.`);
|
|
65
72
|
}
|
|
66
73
|
if (opts.upstream && !opts.auth && hasUpstreamPlaceholders(opts.upstream)) {
|
|
67
74
|
throw new Error(`MCP endpoint "${path}": the upstream uses \${OAUTH_*} placeholders but the endpoint declares no auth.`);
|
|
68
75
|
}
|
|
69
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Answers the methods of the Streamable HTTP transport that a stateless endpoint does not serve,
|
|
79
|
+
* so MCP clients know there is no event stream instead of receiving the application's 404 page.
|
|
80
|
+
*/
|
|
81
|
+
methodNotAllowed($ctx) {
|
|
82
|
+
return $ctx.response
|
|
83
|
+
.status(405)
|
|
84
|
+
.setHeaders({ allow: "POST" })
|
|
85
|
+
.body({ jsonrpc: "2.0", error: { code: -32000, message: "Method not allowed." }, id: null });
|
|
86
|
+
}
|
|
70
87
|
metadata(settings, $ctx) {
|
|
71
88
|
const auth = settings.auth;
|
|
72
89
|
const resource = this.platformAuthService.getResourceUrl(auth);
|
|
@@ -81,9 +98,17 @@ export class PlatformMcpModule {
|
|
|
81
98
|
.body(body);
|
|
82
99
|
}
|
|
83
100
|
async dispatch(settings, $ctx) {
|
|
101
|
+
const { auth } = settings;
|
|
84
102
|
let authInfo;
|
|
85
|
-
if (
|
|
86
|
-
|
|
103
|
+
if (auth?.preAuth) {
|
|
104
|
+
authInfo = await this.platformAuthService.preAuth(auth.preAuth, $ctx);
|
|
105
|
+
}
|
|
106
|
+
if (auth && !authInfo) {
|
|
107
|
+
if (!this.platformAuthService.isOAuth(auth)) {
|
|
108
|
+
// the custom check did not authenticate the request and there is no OAuth to fall back on
|
|
109
|
+
return $ctx.response.status(401).body({ error: "unauthorized", error_description: "Authentication required" });
|
|
110
|
+
}
|
|
111
|
+
const result = await this.platformAuthService.verifyMcpRequest(auth, $ctx);
|
|
87
112
|
if (result instanceof Response) {
|
|
88
113
|
return $ctx.response
|
|
89
114
|
.status(result.status)
|
|
@@ -91,6 +116,8 @@ export class PlatformMcpModule {
|
|
|
91
116
|
.body(await result.text());
|
|
92
117
|
}
|
|
93
118
|
authInfo = result;
|
|
119
|
+
}
|
|
120
|
+
if (authInfo) {
|
|
94
121
|
// the SDK transport reads `req.auth` and exposes it to handlers as `ctx.http.authInfo`
|
|
95
122
|
$ctx.getReq().auth = authInfo;
|
|
96
123
|
}
|