keryx 0.34.1 → 0.35.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.
@@ -4,6 +4,16 @@ import { loadFromEnvIfSet } from "../../util/config";
4
4
  export const configServerMcp = {
5
5
  enabled: await loadFromEnvIfSet("MCP_SERVER_ENABLED", false),
6
6
  route: await loadFromEnvIfSet("MCP_SERVER_ROUTE", "/mcp"),
7
+ allowedOrigins: await loadFromEnvIfSet(
8
+ "MCP_ALLOWED_ORIGINS",
9
+ [
10
+ "https://claude.ai", // Anthropic Claude web connector
11
+ "https://claude.com", // Anthropic Claude web connector
12
+ "https://chatgpt.com", // OpenAI ChatGPT connectors
13
+ "https://vscode.dev", // VS Code for the Web
14
+ "https://github.dev", // github.dev web editor
15
+ ].join(","),
16
+ ),
7
17
  instructions: await loadFromEnvIfSet(
8
18
  "MCP_SERVER_INSTRUCTIONS",
9
19
  pkg.description as string,
@@ -6,7 +6,12 @@ import { Initializer } from "../classes/Initializer";
6
6
  import { ErrorType, TypedError } from "../classes/TypedError";
7
7
  import { config } from "../config";
8
8
  import { ansi } from "../util/ansi";
9
- import { buildCorsHeaders, getExternalOrigin } from "../util/http";
9
+ import {
10
+ buildCorsHeaders,
11
+ getExternalOrigin,
12
+ getMcpAllowedOrigins,
13
+ isOriginAllowed,
14
+ } from "../util/http";
10
15
  import {
11
16
  createMcpServer,
12
17
  formatToolName,
@@ -142,26 +147,32 @@ export class McpInitializer extends Initializer {
142
147
  ): Promise<Response> => {
143
148
  const method = req.method.toUpperCase();
144
149
  const requestOrigin = req.headers.get("origin") ?? undefined;
145
- const corsHeaders = buildCorsHeaders(requestOrigin, {
146
- "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
147
- "Access-Control-Allow-Headers":
148
- "Content-Type, mcp-session-id, Authorization",
149
- "Access-Control-Expose-Headers": "mcp-session-id",
150
- });
150
+ const mcpAllowedOrigins = getMcpAllowedOrigins();
151
+ const corsHeaders = buildCorsHeaders(
152
+ requestOrigin,
153
+ {
154
+ "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
155
+ // MCP-Protocol-Version is sent by clients on every request after
156
+ // initialize (spec 2025-06-18+). Browser connectors preflight it, so
157
+ // it must be allow-listed or those requests are silently blocked.
158
+ "Access-Control-Allow-Headers":
159
+ "Content-Type, mcp-session-id, mcp-protocol-version, Authorization",
160
+ "Access-Control-Expose-Headers": "mcp-session-id",
161
+ },
162
+ mcpAllowedOrigins,
163
+ );
151
164
 
152
- // Reject requests from unrecognized origins when APPLICATION_URL is set
153
- if (requestOrigin) {
154
- const appUrl = config.server.web.applicationUrl;
155
- if (appUrl && !appUrl.startsWith("http://localhost")) {
156
- const allowedOrigin = new URL(appUrl).origin;
157
- if (requestOrigin !== allowedOrigin) {
158
- return mcpJsonResponse(
159
- { error: "Origin not allowed" },
160
- 403,
161
- corsHeaders,
162
- );
163
- }
164
- }
165
+ // Reject browser requests from unrecognized origins. Requests with no
166
+ // Origin (non-browser clients like the Claude Code CLI) always pass; the
167
+ // bearer token is the real security boundary for this public endpoint.
168
+ // Uses the same allowlist as buildCorsHeaders above so the 403 gate and
169
+ // the Access-Control-Allow-Origin reflection can never disagree.
170
+ if (requestOrigin && !isOriginAllowed(requestOrigin, mcpAllowedOrigins)) {
171
+ return mcpJsonResponse(
172
+ { error: "Origin not allowed" },
173
+ 403,
174
+ corsHeaders,
175
+ );
165
176
  }
166
177
 
167
178
  // Handle OPTIONS for CORS preflight
@@ -6,6 +6,7 @@ import {
6
6
  appendHeaders,
7
7
  buildCorsHeaders,
8
8
  getExternalOrigin,
9
+ getMcpAllowedOrigins,
9
10
  } from "../util/http";
10
11
  import {
11
12
  handleAuthorizeGet,
@@ -60,10 +61,21 @@ export class OAuthInitializer extends Initializer {
60
61
  const method = req.method.toUpperCase();
61
62
  const origin = getExternalOrigin(req, url);
62
63
  const requestOrigin = req.headers.get("origin") ?? undefined;
63
- const corsHeaders = buildCorsHeaders(requestOrigin, {
64
- "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
65
- "Access-Control-Allow-Headers": "Content-Type, Authorization",
66
- });
64
+ // OAuth discovery, dynamic client registration, and token exchange are
65
+ // performed as browser fetches by browser-based MCP connectors (e.g.
66
+ // claude.ai). Reflect Access-Control-Allow-Origin for the same connector
67
+ // origins the MCP endpoint admits, or the browser blocks the responses
68
+ // (the requests still return 2xx, so nothing errors server-side) and the
69
+ // connector silently fails to complete. These endpoints are public by
70
+ // design — protected by PKCE and registration, not by Origin.
71
+ const corsHeaders = buildCorsHeaders(
72
+ requestOrigin,
73
+ {
74
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
75
+ "Access-Control-Allow-Headers": "Content-Type, Authorization",
76
+ },
77
+ getMcpAllowedOrigins(),
78
+ );
67
79
 
68
80
  // Handle CORS preflight for OAuth endpoints
69
81
  if (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keryx",
3
- "version": "0.34.1",
3
+ "version": "0.35.1",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/util/http.ts CHANGED
@@ -2,12 +2,49 @@ import { config } from "../config";
2
2
 
3
3
  /**
4
4
  * Check whether a request origin is permitted by the configured allowed-origins list.
5
+ *
6
+ * @param origin - The request `Origin` to check.
7
+ * @param extraAllowedOrigins - Additional origins to permit beyond
8
+ * `WEB_SERVER_ALLOWED_ORIGINS`. Used by the MCP endpoint to admit
9
+ * browser connector origins (see {@link getMcpAllowedOrigins}) so
10
+ * the origin gate and CORS reflection share one allowlist. When
11
+ * `WEB_SERVER_ALLOWED_ORIGINS` is `"*"`, all origins are allowed and
12
+ * this list is not consulted.
5
13
  */
6
- export function isOriginAllowed(origin: string): boolean {
14
+ export function isOriginAllowed(
15
+ origin: string,
16
+ extraAllowedOrigins: string[] = [],
17
+ ): boolean {
7
18
  const allowedOrigins = config.server.web.allowedOrigins;
8
19
  if (allowedOrigins === "*") return true;
9
20
  const allowed = allowedOrigins.split(",").map((o) => o.trim());
10
- return allowed.includes(origin);
21
+ return allowed.includes(origin) || extraAllowedOrigins.includes(origin);
22
+ }
23
+
24
+ /**
25
+ * Compute the extra origins permitted to reach the MCP endpoint from a browser,
26
+ * on top of `WEB_SERVER_ALLOWED_ORIGINS`. Always includes the `applicationUrl`
27
+ * origin (the server's own domain) plus the configurable `MCP_ALLOWED_ORIGINS`
28
+ * list (defaults to the popular browser-based agent connector origins).
29
+ *
30
+ * Passed to both {@link buildCorsHeaders} and {@link isOriginAllowed} in the MCP
31
+ * request handler so the 403 origin gate and the CORS `Access-Control-Allow-Origin`
32
+ * reflection can never disagree.
33
+ */
34
+ export function getMcpAllowedOrigins(): string[] {
35
+ const origins: string[] = [];
36
+ const appUrl = config.server.web.applicationUrl;
37
+ if (appUrl) origins.push(new URL(appUrl).origin);
38
+ const configured = config.server.mcp.allowedOrigins;
39
+ if (configured) {
40
+ origins.push(
41
+ ...configured
42
+ .split(",")
43
+ .map((o) => o.trim())
44
+ .filter(Boolean),
45
+ );
46
+ }
47
+ return origins;
11
48
  }
12
49
 
13
50
  /**
@@ -16,17 +53,24 @@ export function isOriginAllowed(origin: string): boolean {
16
53
  * @param requestOrigin - The `Origin` header from the incoming request (if any).
17
54
  * @param extra - Additional CORS headers to merge (e.g. `Access-Control-Allow-Methods`).
18
55
  * Any key not already set will be added.
56
+ * @param extraAllowedOrigins - Additional origins to permit beyond
57
+ * `WEB_SERVER_ALLOWED_ORIGINS` when deciding whether to reflect
58
+ * `Access-Control-Allow-Origin` (see {@link isOriginAllowed}).
19
59
  */
20
60
  export function buildCorsHeaders(
21
61
  requestOrigin: string | undefined,
22
62
  extra?: Record<string, string>,
63
+ extraAllowedOrigins: string[] = [],
23
64
  ): Record<string, string> {
24
65
  const headers: Record<string, string> = { ...extra };
25
66
  const allowedOrigins = config.server.web.allowedOrigins;
26
67
 
27
68
  if (allowedOrigins === "*" && !requestOrigin) {
28
69
  headers["Access-Control-Allow-Origin"] = "*";
29
- } else if (requestOrigin && isOriginAllowed(requestOrigin)) {
70
+ } else if (
71
+ requestOrigin &&
72
+ isOriginAllowed(requestOrigin, extraAllowedOrigins)
73
+ ) {
30
74
  headers["Access-Control-Allow-Origin"] = requestOrigin;
31
75
  headers["Vary"] = "Origin";
32
76
  }