@tsed/platform-mcp 8.43.2 → 8.44.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.
@@ -39,8 +39,8 @@ export interface McpHttpUpstreamSettings extends McpUpstreamBaseSettings {
39
39
  type: "http" | "sse";
40
40
  url: string;
41
41
  /**
42
- * Headers sent with every upstream request. Values support the `${OAUTH_TOKEN}`, `${OAUTH_CLIENT_ID}`
43
- * and `${OAUTH_SCOPES}` placeholders, replaced with the verified identity of the caller.
42
+ * Headers sent with every upstream request. Values support the `${OAUTH_TOKEN}`, `${OAUTH_CLIENT_ID}`,
43
+ * `${OAUTH_SCOPES}` and `${OAUTH_SUB}` placeholders, replaced with the verified identity of the caller.
44
44
  */
45
45
  headers?: Record<string, string>;
46
46
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/platform-mcp",
3
- "version": "8.43.2",
3
+ "version": "8.44.0",
4
4
  "description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
5
5
  "source": "./src/http/index.ts",
6
6
  "type": "module",
@@ -51,14 +51,14 @@
51
51
  "uuid": ">=11.0.2"
52
52
  },
53
53
  "devDependencies": {
54
- "@tsed/barrels": "8.43.2",
55
- "@tsed/core": "8.43.2",
56
- "@tsed/di": "8.43.2",
57
- "@tsed/exceptions": "8.43.2",
58
- "@tsed/json-mapper": "8.43.2",
59
- "@tsed/platform-http": "8.43.2",
60
- "@tsed/schema": "8.43.2",
61
- "@tsed/typescript": "8.43.2",
54
+ "@tsed/barrels": "8.44.0",
55
+ "@tsed/core": "8.44.0",
56
+ "@tsed/di": "8.44.0",
57
+ "@tsed/exceptions": "8.44.0",
58
+ "@tsed/json-mapper": "8.44.0",
59
+ "@tsed/platform-http": "8.44.0",
60
+ "@tsed/schema": "8.44.0",
61
+ "@tsed/typescript": "8.44.0",
62
62
  "jose": "^6.1.3",
63
63
  "typescript": "6.0.2",
64
64
  "vitest": "4.1.4"
@@ -41,8 +41,8 @@ export interface McpHttpUpstreamSettings extends McpUpstreamBaseSettings {
41
41
  type: "http" | "sse";
42
42
  url: string;
43
43
  /**
44
- * Headers sent with every upstream request. Values support the `${OAUTH_TOKEN}`, `${OAUTH_CLIENT_ID}`
45
- * and `${OAUTH_SCOPES}` placeholders, replaced with the verified identity of the caller.
44
+ * Headers sent with every upstream request. Values support the `${OAUTH_TOKEN}`, `${OAUTH_CLIENT_ID}`,
45
+ * `${OAUTH_SCOPES}` and `${OAUTH_SUB}` placeholders, replaced with the verified identity of the caller.
46
46
  */
47
47
  headers?: Record<string, string>;
48
48
  /**
@@ -64,11 +64,32 @@ describe("resolveUpstream()", () => {
64
64
  });
65
65
  });
66
66
 
67
+ describe("resolveUpstream() with ${OAUTH_SUB}", () => {
68
+ const upstream = {type: "stdio" as const, command: "node", args: ["--user=${OAUTH_SUB}"], env: {USER_ID: "${OAUTH_SUB}"}};
69
+
70
+ it("interpolates the subject exposed in the claims of the caller", () => {
71
+ expect(resolveUpstream(upstream, {...authInfo, extra: {sub: "user-1"}})).toEqual({
72
+ ...upstream,
73
+ args: ["--user=user-1"],
74
+ env: {USER_ID: "user-1"}
75
+ });
76
+ });
77
+
78
+ it.each([
79
+ ["the claims hold no subject", {...authInfo, extra: {}}],
80
+ ["the subject is not a string", {...authInfo, extra: {sub: 42}}],
81
+ ["the caller exposes no claims", authInfo]
82
+ ])("replaces the placeholder with an empty value when %s", (_, info) => {
83
+ expect(resolveUpstream(upstream, info)).toEqual({...upstream, args: ["--user="], env: {USER_ID: ""}});
84
+ });
85
+ });
86
+
67
87
  describe("hasUpstreamPlaceholders()", () => {
68
88
  it("detects placeholders in headers, args and env", () => {
69
89
  expect(hasUpstreamPlaceholders({type: "http", url: "http://localhost", headers: {a: "${OAUTH_TOKEN}"}})).toBe(true);
70
90
  expect(hasUpstreamPlaceholders({type: "stdio", command: "node", args: ["${OAUTH_CLIENT_ID}"]})).toBe(true);
71
91
  expect(hasUpstreamPlaceholders({type: "stdio", command: "node", env: {A: "${OAUTH_SCOPES}"}})).toBe(true);
92
+ expect(hasUpstreamPlaceholders({type: "stdio", command: "node", env: {A: "${OAUTH_SUB}"}})).toBe(true);
72
93
  });
73
94
 
74
95
  it("ignores static definitions", () => {
@@ -2,7 +2,7 @@ import type {AuthInfo} from "@modelcontextprotocol/server";
2
2
  import type {PlatformMcpUpstreamSettings} from "../interfaces/PlatformMcpUpstreamSettings.js";
3
3
  import {hasPlaceholders, interpolate} from "./interpolate.js";
4
4
 
5
- const OAUTH_PLACEHOLDERS = ["OAUTH_TOKEN", "OAUTH_CLIENT_ID", "OAUTH_SCOPES"] as const;
5
+ const OAUTH_PLACEHOLDERS = ["OAUTH_TOKEN", "OAUTH_CLIENT_ID", "OAUTH_SCOPES", "OAUTH_SUB"] as const;
6
6
 
7
7
  function mapValues(record: Record<string, string> | undefined, fn: (value: string) => string) {
8
8
  return record && Object.fromEntries(Object.entries(record).map(([key, value]) => [key, fn(value)]));
@@ -14,6 +14,15 @@ function getValues(upstream: PlatformMcpUpstreamSettings): string[] {
14
14
  : Object.values(upstream.headers || {});
15
15
  }
16
16
 
17
+ /**
18
+ * Reads the subject of the caller from the claims the verifier (or the `preAuth` check) exposes in `extra`.
19
+ */
20
+ function getSubject(authInfo?: AuthInfo) {
21
+ const sub = authInfo?.extra?.sub;
22
+
23
+ return typeof sub === "string" ? sub : undefined;
24
+ }
25
+
17
26
  /**
18
27
  * Tells whether the upstream definition references the identity of the caller.
19
28
  */
@@ -32,7 +41,8 @@ export function resolveUpstream<T extends PlatformMcpUpstreamSettings>(upstream:
32
41
  const variables: Record<(typeof OAUTH_PLACEHOLDERS)[number], string | undefined> = {
33
42
  OAUTH_TOKEN: authInfo?.token,
34
43
  OAUTH_CLIENT_ID: authInfo?.clientId,
35
- OAUTH_SCOPES: authInfo?.scopes?.join(" ")
44
+ OAUTH_SCOPES: authInfo?.scopes?.join(" "),
45
+ OAUTH_SUB: getSubject(authInfo)
36
46
  };
37
47
  const resolve = (value: string) => interpolate(value, variables);
38
48
 
@@ -269,6 +269,36 @@ describe("PlatformMcpAuthService", () => {
269
269
  expect(result.headers.get("www-authenticate")).toContain('scope="mcp:write"');
270
270
  });
271
271
 
272
+ it("advertises the supported scopes in the challenge when no scope is required", async () => {
273
+ const service = inject(PlatformMcpAuthService);
274
+ const auth = createAuth({verifier: createVerifier(), scopesSupported: ["openid", "profile", "email"]});
275
+
276
+ const anonymous = (await service.verifyMcpRequest(auth, createContext())) as Response;
277
+
278
+ expect(anonymous.status).toBe(401);
279
+ expect(anonymous.headers.get("www-authenticate")).toContain('scope="openid profile email"');
280
+ // the supported scopes are only a hint: a token without them is still accepted
281
+ await expect(service.verifyMcpRequest(auth, createContext({authorization: "Bearer valid"}))).resolves.toEqual(authInfo);
282
+ });
283
+
284
+ it("advertises the required scopes rather than the supported ones", async () => {
285
+ const service = inject(PlatformMcpAuthService);
286
+ const auth = createAuth({verifier: createVerifier(), scopesSupported: ["mcp:read", "mcp:write"], requiredScopes: ["mcp:read"]});
287
+
288
+ const result = (await service.verifyMcpRequest(auth, createContext())) as Response;
289
+
290
+ expect(result.headers.get("www-authenticate")).toContain('scope="mcp:read"');
291
+ expect(result.headers.get("www-authenticate")).not.toContain("mcp:write");
292
+ });
293
+
294
+ it("sends no scope in the challenge when none is configured", async () => {
295
+ const service = inject(PlatformMcpAuthService);
296
+
297
+ const result = (await service.verifyMcpRequest(createAuth({verifier: createVerifier()}), createContext())) as Response;
298
+
299
+ expect(result.headers.get("www-authenticate")).not.toContain("scope=");
300
+ });
301
+
272
302
  it("ignores the Host header of the request in the challenge", async () => {
273
303
  const service = inject(PlatformMcpAuthService);
274
304
  const auth = createAuth({verifier: createVerifier()});
@@ -81,7 +81,11 @@ export class PlatformMcpAuthService {
81
81
  try {
82
82
  return await verifyBearerToken($ctx.request.headers.authorization, options);
83
83
  } catch (error) {
84
- return bearerAuthChallengeResponse(error, options);
84
+ return bearerAuthChallengeResponse(error, {
85
+ ...options,
86
+ // tell the client which scopes to request: the required ones, or the supported ones when none is enforced
87
+ requiredScopes: auth.requiredScopes?.length ? auth.requiredScopes : auth.scopesSupported
88
+ });
85
89
  }
86
90
  }
87
91