@tsed/platform-mcp 8.42.0 → 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.
@@ -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/domain/PlatformTokenVerifier.js";
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({
@@ -60,8 +60,12 @@ export class PlatformMcpModule {
60
60
  * Rejects a configuration that cannot work or would be unsafe, before any route is mounted.
61
61
  */
62
62
  validate(path, opts) {
63
- if (opts.auth) {
64
- this.platformAuthService.validate(path, opts.auth);
63
+ const { auth } = opts;
64
+ if (this.platformAuthService.isOAuth(auth)) {
65
+ this.platformAuthService.validate(path, auth);
66
+ }
67
+ else if (auth && !auth.preAuth) {
68
+ throw new Error(`MCP endpoint "${path}": auth requires an issuer, a preAuth check, or both.`);
65
69
  }
66
70
  if (opts.upstream && !opts.auth && hasUpstreamPlaceholders(opts.upstream)) {
67
71
  throw new Error(`MCP endpoint "${path}": the upstream uses \${OAUTH_*} placeholders but the endpoint declares no auth.`);
@@ -81,9 +85,17 @@ export class PlatformMcpModule {
81
85
  .body(body);
82
86
  }
83
87
  async dispatch(settings, $ctx) {
88
+ const { auth } = settings;
84
89
  let authInfo;
85
- if (settings.auth) {
86
- const result = await this.platformAuthService.verifyMcpRequest(settings.auth, $ctx);
90
+ if (auth?.preAuth) {
91
+ authInfo = await this.platformAuthService.preAuth(auth.preAuth, $ctx);
92
+ }
93
+ if (auth && !authInfo) {
94
+ if (!this.platformAuthService.isOAuth(auth)) {
95
+ // the custom check did not authenticate the request and there is no OAuth to fall back on
96
+ return $ctx.response.status(401).body({ error: "unauthorized", error_description: "Authentication required" });
97
+ }
98
+ const result = await this.platformAuthService.verifyMcpRequest(auth, $ctx);
87
99
  if (result instanceof Response) {
88
100
  return $ctx.response
89
101
  .status(result.status)
@@ -91,6 +103,8 @@ export class PlatformMcpModule {
91
103
  .body(await result.text());
92
104
  }
93
105
  authInfo = result;
106
+ }
107
+ if (authInfo) {
94
108
  // the SDK transport reads `req.auth` and exposes it to handlers as `ctx.http.authInfo`
95
109
  $ctx.getReq().auth = authInfo;
96
110
  }