keryx 0.34.1 → 0.35.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/config/server/mcp.ts +10 -0
- package/initializers/mcp.ts +28 -20
- package/package.json +1 -1
- package/util/http.ts +47 -3
package/config/server/mcp.ts
CHANGED
|
@@ -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,
|
package/initializers/mcp.ts
CHANGED
|
@@ -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 {
|
|
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,29 @@ 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
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
150
|
+
const mcpAllowedOrigins = getMcpAllowedOrigins();
|
|
151
|
+
const corsHeaders = buildCorsHeaders(
|
|
152
|
+
requestOrigin,
|
|
153
|
+
{
|
|
154
|
+
"Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
|
|
155
|
+
"Access-Control-Allow-Headers":
|
|
156
|
+
"Content-Type, mcp-session-id, Authorization",
|
|
157
|
+
"Access-Control-Expose-Headers": "mcp-session-id",
|
|
158
|
+
},
|
|
159
|
+
mcpAllowedOrigins,
|
|
160
|
+
);
|
|
151
161
|
|
|
152
|
-
// Reject requests from unrecognized origins
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
}
|
|
162
|
+
// Reject browser requests from unrecognized origins. Requests with no
|
|
163
|
+
// Origin (non-browser clients like the Claude Code CLI) always pass; the
|
|
164
|
+
// bearer token is the real security boundary for this public endpoint.
|
|
165
|
+
// Uses the same allowlist as buildCorsHeaders above so the 403 gate and
|
|
166
|
+
// the Access-Control-Allow-Origin reflection can never disagree.
|
|
167
|
+
if (requestOrigin && !isOriginAllowed(requestOrigin, mcpAllowedOrigins)) {
|
|
168
|
+
return mcpJsonResponse(
|
|
169
|
+
{ error: "Origin not allowed" },
|
|
170
|
+
403,
|
|
171
|
+
corsHeaders,
|
|
172
|
+
);
|
|
165
173
|
}
|
|
166
174
|
|
|
167
175
|
// Handle OPTIONS for CORS preflight
|
package/package.json
CHANGED
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(
|
|
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 (
|
|
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
|
}
|