@quantakrypto/mcp 0.4.4 → 0.5.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/HOSTING.md +15 -1
- package/README.md +22 -1
- package/dist/fsconfig.d.ts +4 -3
- package/dist/fsconfig.d.ts.map +1 -1
- package/dist/fsconfig.js.map +1 -1
- package/dist/http.d.ts +21 -18
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +23 -12
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/protocol.d.ts +3 -6
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/resources.d.ts +4 -3
- package/dist/resources.d.ts.map +1 -1
- package/dist/resources.js.map +1 -1
- package/dist/rules.d.ts +2 -1
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +6 -0
- package/dist/server.js.map +1 -1
- package/dist/stdio.d.ts +0 -3
- package/dist/stdio.d.ts.map +1 -1
- package/dist/stdio.js +0 -5
- package/dist/stdio.js.map +1 -1
- package/dist/tools.d.ts +9 -4
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +181 -5
- package/dist/tools.js.map +1 -1
- package/package.json +3 -2
package/HOSTING.md
CHANGED
|
@@ -5,6 +5,17 @@ service rather than a per-user local stdio process. The shipped `src/http.ts` is
|
|
|
5
5
|
the minimal, working core of this design; everything below is the path from that
|
|
6
6
|
scaffold to production.
|
|
7
7
|
|
|
8
|
+
> **A production reference implementation of this design now exists — and it's
|
|
9
|
+
> live.** [`quantakrypto/mcp-gateway`](https://github.com/quantakrypto/mcp-gateway)
|
|
10
|
+
> runs at **`https://mcp.quantakrypto.com/mcp`** and realises §2–§7 below:
|
|
11
|
+
> [Better Auth](https://better-auth.com) as the **OAuth 2.1** provider (§3, Google
|
|
12
|
+
> + GitHub + email, 30-day tokens), accounts/sessions/tokens in Postgres (§2), and
|
|
13
|
+
> the filesystem/network tools withheld over HTTP (§0). Connect a client with
|
|
14
|
+
> `claude mcp add --transport http quantakrypto https://mcp.quantakrypto.com/mcp`.
|
|
15
|
+
> Read that repo as the worked example of everything described here; the sections
|
|
16
|
+
> below remain the reference for *why* each control exists (and for running your
|
|
17
|
+
> own).
|
|
18
|
+
|
|
8
19
|
## 0. Safe-by-default posture (what `src/http.ts` enforces today)
|
|
9
20
|
|
|
10
21
|
The stdio transport trusts the local user and is fully featured. The HTTP
|
|
@@ -142,7 +153,10 @@ stdio trusts the local user; a hosted endpoint must not.
|
|
|
142
153
|
- **OAuth 2.1.** The MCP spec defines an OAuth flow for HTTP transports
|
|
143
154
|
(Protected Resource Metadata + Authorization Server). For first-party clients,
|
|
144
155
|
scoped API keys are simpler; add OAuth when third-party clients must connect on
|
|
145
|
-
a user's behalf.
|
|
156
|
+
a user's behalf. **This is what the hosted [`mcp-gateway`](https://github.com/quantakrypto/mcp-gateway)
|
|
157
|
+
does** — Better Auth publishes the discovery metadata and runs the
|
|
158
|
+
authorize/token endpoints, and the `/mcp` route validates the Bearer token in
|
|
159
|
+
the same process via `withMcpAuth`.
|
|
146
160
|
- **mTLS / network policy.** For internal/enterprise deployments, terminate mTLS
|
|
147
161
|
at the gateway and keep `/mcp` private.
|
|
148
162
|
- Never log request bodies that may contain source code; treat scanned content
|
package/README.md
CHANGED
|
@@ -10,7 +10,9 @@ cryptography and recommends NIST post-quantum / hybrid migrations, all backed by
|
|
|
10
10
|
The only dependency is `@quantakrypto/core`.
|
|
11
11
|
- **Two transports.** A `stdio` transport (the `quantakrypto-mcp` bin) for local agents
|
|
12
12
|
like Claude, and a hostable `http` transport for running quantakrypto as a remote
|
|
13
|
-
service (see [HOSTING.md](./HOSTING.md)).
|
|
13
|
+
service (see [HOSTING.md](./HOSTING.md)). quantakrypto also runs a **hosted,
|
|
14
|
+
OAuth-gated instance at [`mcp.quantakrypto.com`](https://mcp.quantakrypto.com/mcp)**
|
|
15
|
+
you can connect to directly — see [Hosted HTTP server](#hosted-http-server-safe-by-default).
|
|
14
16
|
- **Transport-agnostic core.** All protocol logic lives in a pure, unit-tested
|
|
15
17
|
`McpServer` class; transports only do I/O.
|
|
16
18
|
|
|
@@ -191,6 +193,18 @@ the `initialize` capabilities, all offline/static):
|
|
|
191
193
|
|
|
192
194
|
## Hosted HTTP server (safe-by-default)
|
|
193
195
|
|
|
196
|
+
> **Just want to use it?** quantakrypto runs a hosted, **OAuth-gated** instance at
|
|
197
|
+
> **`https://mcp.quantakrypto.com/mcp`** — no install, no token to manage:
|
|
198
|
+
> ```bash
|
|
199
|
+
> claude mcp add --transport http quantakrypto https://mcp.quantakrypto.com/mcp
|
|
200
|
+
> ```
|
|
201
|
+
> It signs you in (Google / GitHub / email) and issues a 30-day token. Being
|
|
202
|
+
> multi-tenant, it exposes only the content-based tools — the filesystem/network
|
|
203
|
+
> tools (`scan_path`, `inventory_crypto`, `generate_cbom`, `plan_migration`,
|
|
204
|
+
> `probe_endpoint`) are withheld; run the MCP **locally** for those. Source and
|
|
205
|
+
> ops: [`quantakrypto/mcp-gateway`](https://github.com/quantakrypto/mcp-gateway).
|
|
206
|
+
> The rest of this section is for running the **package's own** `http.ts` yourself.
|
|
207
|
+
|
|
194
208
|
The same `McpServer` can be served over HTTP (a Streamable-HTTP-style JSON-RPC
|
|
195
209
|
endpoint) for remote deployments. The stdio transport trusts the local user and
|
|
196
210
|
is fully featured; the **HTTP transport is hardened**, because a hosted endpoint
|
|
@@ -209,6 +223,12 @@ is reachable by untrusted peers:
|
|
|
209
223
|
`list_rules`, `get_fix_examples`, `verify_fix`, `check_dependency`,
|
|
210
224
|
`score_delta`, `triage_findings`, `apply_triage`, `remediate_findings`) are
|
|
211
225
|
always available. `tools/list` and `tools/call` both reflect the gating.
|
|
226
|
+
- **The networked probe tool is disabled by default.** `probe_endpoint` (active
|
|
227
|
+
TLS/SSH probing) is the only tool that opens a socket. Over HTTP it is exposed
|
|
228
|
+
only when `QUANTAKRYPTO_MCP_ALLOW_NETWORK=1` — a hosted server should not probe
|
|
229
|
+
arbitrary hosts. On the local stdio transport it is always available. It still
|
|
230
|
+
requires the per-call ownership attestation (`i_own_this=true`) and refuses
|
|
231
|
+
ranges/CIDRs regardless of transport.
|
|
212
232
|
- **Filesystem tools are root-confined.** Even with `QUANTAKRYPTO_MCP_ALLOW_FS=1`,
|
|
213
233
|
every scanned path must resolve inside the `QUANTAKRYPTO_MCP_ROOT` allow-list
|
|
214
234
|
(`:`-separated; the process CWD by default). `..` traversal and out-of-root
|
|
@@ -231,6 +251,7 @@ is reachable by untrusted peers:
|
|
|
231
251
|
| `PORT` | `3000` | Listen port. |
|
|
232
252
|
| `QUANTAKRYPTO_MCP_TOKEN` | _(unset)_ | When set, requires `Authorization: Bearer <token>`. |
|
|
233
253
|
| `QUANTAKRYPTO_MCP_ALLOW_FS` | _(off)_ | `1`/`true` exposes the filesystem tools over HTTP. |
|
|
254
|
+
| `QUANTAKRYPTO_MCP_ALLOW_NETWORK` | _(off)_ | `1`/`true` exposes the networked `probe_endpoint` tool over HTTP. |
|
|
234
255
|
| `QUANTAKRYPTO_MCP_ROOT` | _(cwd)_ | `:`-separated allow-list of directories the FS tools may scan. |
|
|
235
256
|
| `QUANTAKRYPTO_MCP_ALLOW_ORIGIN` | _(loopback)_ | Comma-separated extra `Origin` hosts allowed on `/mcp`. |
|
|
236
257
|
| `QUANTAKRYPTO_MCP_TIMEOUT_MS` | `30000` | Per-request deadline; aborts the in-flight scan on timeout. |
|
package/dist/fsconfig.d.ts
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
* it is fully unit-testable; only {@link resolveFsConfig} reads `process.env`.
|
|
22
22
|
*/
|
|
23
23
|
/** Minimal env shape so the resolver stays pure and testable. */
|
|
24
|
-
|
|
24
|
+
type FsEnv = Record<string, string | undefined>;
|
|
25
25
|
/** Default file-count budget for a single FS tool call. */
|
|
26
26
|
export declare const DEFAULT_MAX_FILES = 25000;
|
|
27
27
|
/** Hard cap on the file-count budget, even when raised via env. */
|
|
@@ -31,7 +31,7 @@ export declare const DEFAULT_MAX_BYTES: number;
|
|
|
31
31
|
/** Hard cap on the cumulative-bytes budget, even when raised via env (2 GiB). */
|
|
32
32
|
export declare const MAX_MAX_BYTES: number;
|
|
33
33
|
/** Resolved FS-tool policy: where scans may read and how much work they may do. */
|
|
34
|
-
|
|
34
|
+
interface FsConfig {
|
|
35
35
|
/**
|
|
36
36
|
* Absolute, normalized roots a scan may read inside. Always non-empty; defaults
|
|
37
37
|
* to `[process.cwd()]` when `QUANTAKRYPTO_MCP_ROOT` is unset.
|
|
@@ -48,7 +48,7 @@ export interface FsConfig {
|
|
|
48
48
|
*/
|
|
49
49
|
export declare function resolveFsConfig(env: FsEnv, cwd?: string): FsConfig;
|
|
50
50
|
/** Outcome of validating a requested path against the root allow-list. */
|
|
51
|
-
|
|
51
|
+
type PathDecision = {
|
|
52
52
|
ok: true;
|
|
53
53
|
path: string;
|
|
54
54
|
} | {
|
|
@@ -76,4 +76,5 @@ export declare function realpathInsideRoots(resolved: string, config: FsConfig):
|
|
|
76
76
|
* symlinks; the allow-list is the trust boundary).
|
|
77
77
|
*/
|
|
78
78
|
export declare function resolveScanPath(config: FsConfig, requested: string): PathDecision;
|
|
79
|
+
export {};
|
|
79
80
|
//# sourceMappingURL=fsconfig.d.ts.map
|
package/dist/fsconfig.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fsconfig.d.ts","sourceRoot":"","sources":["../src/fsconfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAMH,iEAAiE;AACjE,
|
|
1
|
+
{"version":3,"file":"fsconfig.d.ts","sourceRoot":"","sources":["../src/fsconfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAMH,iEAAiE;AACjE,KAAK,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,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AAE,EAAE,KAAK;QACT,MAAM,EACJ,+CAA+C;YAC/C,2EAA2E;KAC9E,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Filesystem-tool safety policy for the quantakrypto MCP (P0 — FS confinement\n * and work budgets).\n *\n * The FS-backed tools (`scan_path`, `inventory_crypto`, `generate_cbom`) pass a\n * caller-supplied `path` straight into `@quantakrypto/core`'s `scan()`. With the\n * filesystem tools enabled (`QUANTAKRYPTO_MCP_ALLOW_FS=1`) that turns the server\n * into an arbitrary-file-read oracle: `path` could be `/etc/passwd` or any tree\n * on the host (SECURITY.md warns about exactly this). This module turns that\n * warning into enforced policy:\n *\n * - **Root allow-list** (`QUANTAKRYPTO_MCP_ROOT`, `:`-separated): every scanned\n * path must resolve inside one of the configured roots. When unset, the\n * process CWD is the single implicit root.\n * - **`..` traversal rejection**: a resolved path that escapes every root is\n * refused, even if the literal string contained no `..`.\n * - **Work budgets** (`maxFiles` / `maxBytes`): bounded by default and capped,\n * so a single `scan_path` cannot exhaust host resources.\n *\n * Everything here is a pure function of an env snapshot + the requested path so\n * it is fully unit-testable; only {@link resolveFsConfig} reads `process.env`.\n */\n\nimport * as path from \"node:path\";\nimport process from \"node:process\";\nimport { realpath } from \"node:fs/promises\";\n\n/** Minimal env shape so the resolver stays pure and testable. */\nexport type FsEnv = Record<string, string | undefined>;\n\n/** Default file-count budget for a single FS tool call. */\nexport const DEFAULT_MAX_FILES = 25_000;\n\n/** Hard cap on the file-count budget, even when raised via env. */\nexport const MAX_MAX_FILES = 250_000;\n\n/** Default cumulative-bytes budget for a single FS tool call (256 MiB). */\nexport const DEFAULT_MAX_BYTES = 256 * 1024 * 1024;\n\n/** Hard cap on the cumulative-bytes budget, even when raised via env (2 GiB). */\nexport const MAX_MAX_BYTES = 2 * 1024 * 1024 * 1024;\n\n/** Resolved FS-tool policy: where scans may read and how much work they may do. */\nexport interface FsConfig {\n /**\n * Absolute, normalized roots a scan may read inside. Always non-empty; defaults\n * to `[process.cwd()]` when `QUANTAKRYPTO_MCP_ROOT` is unset.\n */\n roots: string[];\n /** Max files a single scan may read before {@link BudgetExceededError}. */\n maxFiles: number;\n /** Max cumulative bytes a single scan may read before {@link BudgetExceededError}. */\n maxBytes: number;\n}\n\n/** Parse a positive integer from an env string, clamped to `[1, cap]`. */\nfunction toBudget(value: string | undefined, fallback: number, cap: number): number {\n if (value === undefined) return Math.min(fallback, cap);\n const n = Number(value);\n if (!Number.isFinite(n) || n <= 0) return Math.min(fallback, cap);\n return Math.min(Math.floor(n), cap);\n}\n\n/** Split the `:`-separated root allow-list, dropping empty segments. */\nfunction parseRoots(value: string | undefined, cwd: string): string[] {\n const raw = (value ?? \"\")\n .split(path.delimiter)\n .map((s) => s.trim())\n .filter((s) => s.length > 0);\n const list = raw.length > 0 ? raw : [cwd];\n // Normalize to absolute, de-duplicated roots.\n const seen = new Set<string>();\n const roots: string[] = [];\n for (const r of list) {\n const abs = path.resolve(cwd, r);\n if (!seen.has(abs)) {\n seen.add(abs);\n roots.push(abs);\n }\n }\n return roots;\n}\n\n/**\n * Resolve the FS-tool policy from an env snapshot. Pure aside from the supplied\n * `cwd` default (so callers/tests can pin it).\n */\nexport function resolveFsConfig(env: FsEnv, cwd: string = process.cwd()): FsConfig {\n return {\n roots: parseRoots(env.QUANTAKRYPTO_MCP_ROOT, cwd),\n maxFiles: toBudget(env.QUANTAKRYPTO_MCP_MAX_FILES, DEFAULT_MAX_FILES, MAX_MAX_FILES),\n maxBytes: toBudget(env.QUANTAKRYPTO_MCP_MAX_BYTES, DEFAULT_MAX_BYTES, MAX_MAX_BYTES),\n };\n}\n\n/** Outcome of validating a requested path against the root allow-list. */\nexport type PathDecision = { ok: true; path: string } | { ok: false; reason: string };\n\n/** True when `child` is `root` itself or lives beneath it (no `..` escape). */\nfunction isInsideRoot(child: string, root: string): boolean {\n if (child === root) return true;\n const rel = path.relative(root, child);\n // `rel` starting with \"..\" (or being absolute) means `child` escapes `root`.\n return rel.length > 0 && !rel.startsWith(\"..\") && !path.isAbsolute(rel);\n}\n\n/**\n * Re-verify containment against the REAL (symlink-resolved) paths. The lexical\n * check in {@link resolveScanPath} trusts path strings, but a symlink planted\n * inside an allowed root can point outside it (`<root>/link -> /etc`), and the\n * walker `stat`s through symlinks. Resolving both the target and the roots with\n * `realpath` closes that escape (audit: mcp #1). Falls back to the lexical path\n * when `realpath` fails (e.g. the path doesn't exist yet) — the scan then fails\n * with ENOENT as before, never reads out of root.\n */\nexport async function realpathInsideRoots(resolved: string, config: FsConfig): Promise<boolean> {\n const real = await realpath(resolved).catch(() => resolved);\n const realRoots = await Promise.all(config.roots.map((r) => realpath(r).catch(() => r)));\n return realRoots.some((root) => isInsideRoot(real, root));\n}\n\n/**\n * Validate and resolve a caller-supplied scan path against the allow-list.\n *\n * Relative paths are resolved against the FIRST configured root (the primary\n * scan root), not the process CWD, so a relative request can never reach outside\n * the allow-list. Absolute paths must already sit inside a configured root. Any\n * path that resolves outside every root — whether via an absolute path or a\n * `..` traversal — is rejected. Pure: does no filesystem I/O (it does not follow\n * symlinks; the allow-list is the trust boundary).\n */\nexport function resolveScanPath(config: FsConfig, requested: string): PathDecision {\n const trimmed = requested.trim();\n if (trimmed.length === 0) {\n return { ok: false, reason: \"path must be a non-empty string\" };\n }\n\n const primaryRoot = config.roots[0];\n const resolved = path.isAbsolute(trimmed)\n ? path.resolve(trimmed)\n : path.resolve(primaryRoot, trimmed);\n\n for (const root of config.roots) {\n if (isInsideRoot(resolved, root)) {\n return { ok: true, path: resolved };\n }\n }\n return {\n ok: false,\n reason:\n \"path is outside the configured scan root(s). \" +\n \"Set QUANTAKRYPTO_MCP_ROOT to allow-list the directories the MCP may scan.\",\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"fsconfig.js","sourceRoot":"","sources":["../src/fsconfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,OAAO,MAAM,cAAc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,EAAE,QAAQ,EAAE,CAAC;QACtC,CAAC;IACH,CAAC;IACD,OAAO;QACL,EAAE,EAAE,KAAK;QACT,MAAM,EACJ,+CAA+C;YAC/C,2EAA2E;KAC9E,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Filesystem-tool safety policy for the quantakrypto MCP (P0 — FS confinement\n * and work budgets).\n *\n * The FS-backed tools (`scan_path`, `inventory_crypto`, `generate_cbom`) pass a\n * caller-supplied `path` straight into `@quantakrypto/core`'s `scan()`. With the\n * filesystem tools enabled (`QUANTAKRYPTO_MCP_ALLOW_FS=1`) that turns the server\n * into an arbitrary-file-read oracle: `path` could be `/etc/passwd` or any tree\n * on the host (SECURITY.md warns about exactly this). This module turns that\n * warning into enforced policy:\n *\n * - **Root allow-list** (`QUANTAKRYPTO_MCP_ROOT`, `:`-separated): every scanned\n * path must resolve inside one of the configured roots. When unset, the\n * process CWD is the single implicit root.\n * - **`..` traversal rejection**: a resolved path that escapes every root is\n * refused, even if the literal string contained no `..`.\n * - **Work budgets** (`maxFiles` / `maxBytes`): bounded by default and capped,\n * so a single `scan_path` cannot exhaust host resources.\n *\n * Everything here is a pure function of an env snapshot + the requested path so\n * it is fully unit-testable; only {@link resolveFsConfig} reads `process.env`.\n */\n\nimport * as path from \"node:path\";\nimport process from \"node:process\";\nimport { realpath } from \"node:fs/promises\";\n\n/** Minimal env shape so the resolver stays pure and testable. */\ntype FsEnv = Record<string, string | undefined>;\n\n/** Default file-count budget for a single FS tool call. */\nexport const DEFAULT_MAX_FILES = 25_000;\n\n/** Hard cap on the file-count budget, even when raised via env. */\nexport const MAX_MAX_FILES = 250_000;\n\n/** Default cumulative-bytes budget for a single FS tool call (256 MiB). */\nexport const DEFAULT_MAX_BYTES = 256 * 1024 * 1024;\n\n/** Hard cap on the cumulative-bytes budget, even when raised via env (2 GiB). */\nexport const MAX_MAX_BYTES = 2 * 1024 * 1024 * 1024;\n\n/** Resolved FS-tool policy: where scans may read and how much work they may do. */\ninterface FsConfig {\n /**\n * Absolute, normalized roots a scan may read inside. Always non-empty; defaults\n * to `[process.cwd()]` when `QUANTAKRYPTO_MCP_ROOT` is unset.\n */\n roots: string[];\n /** Max files a single scan may read before {@link BudgetExceededError}. */\n maxFiles: number;\n /** Max cumulative bytes a single scan may read before {@link BudgetExceededError}. */\n maxBytes: number;\n}\n\n/** Parse a positive integer from an env string, clamped to `[1, cap]`. */\nfunction toBudget(value: string | undefined, fallback: number, cap: number): number {\n if (value === undefined) return Math.min(fallback, cap);\n const n = Number(value);\n if (!Number.isFinite(n) || n <= 0) return Math.min(fallback, cap);\n return Math.min(Math.floor(n), cap);\n}\n\n/** Split the `:`-separated root allow-list, dropping empty segments. */\nfunction parseRoots(value: string | undefined, cwd: string): string[] {\n const raw = (value ?? \"\")\n .split(path.delimiter)\n .map((s) => s.trim())\n .filter((s) => s.length > 0);\n const list = raw.length > 0 ? raw : [cwd];\n // Normalize to absolute, de-duplicated roots.\n const seen = new Set<string>();\n const roots: string[] = [];\n for (const r of list) {\n const abs = path.resolve(cwd, r);\n if (!seen.has(abs)) {\n seen.add(abs);\n roots.push(abs);\n }\n }\n return roots;\n}\n\n/**\n * Resolve the FS-tool policy from an env snapshot. Pure aside from the supplied\n * `cwd` default (so callers/tests can pin it).\n */\nexport function resolveFsConfig(env: FsEnv, cwd: string = process.cwd()): FsConfig {\n return {\n roots: parseRoots(env.QUANTAKRYPTO_MCP_ROOT, cwd),\n maxFiles: toBudget(env.QUANTAKRYPTO_MCP_MAX_FILES, DEFAULT_MAX_FILES, MAX_MAX_FILES),\n maxBytes: toBudget(env.QUANTAKRYPTO_MCP_MAX_BYTES, DEFAULT_MAX_BYTES, MAX_MAX_BYTES),\n };\n}\n\n/** Outcome of validating a requested path against the root allow-list. */\ntype PathDecision = { ok: true; path: string } | { ok: false; reason: string };\n\n/** True when `child` is `root` itself or lives beneath it (no `..` escape). */\nfunction isInsideRoot(child: string, root: string): boolean {\n if (child === root) return true;\n const rel = path.relative(root, child);\n // `rel` starting with \"..\" (or being absolute) means `child` escapes `root`.\n return rel.length > 0 && !rel.startsWith(\"..\") && !path.isAbsolute(rel);\n}\n\n/**\n * Re-verify containment against the REAL (symlink-resolved) paths. The lexical\n * check in {@link resolveScanPath} trusts path strings, but a symlink planted\n * inside an allowed root can point outside it (`<root>/link -> /etc`), and the\n * walker `stat`s through symlinks. Resolving both the target and the roots with\n * `realpath` closes that escape (audit: mcp #1). Falls back to the lexical path\n * when `realpath` fails (e.g. the path doesn't exist yet) — the scan then fails\n * with ENOENT as before, never reads out of root.\n */\nexport async function realpathInsideRoots(resolved: string, config: FsConfig): Promise<boolean> {\n const real = await realpath(resolved).catch(() => resolved);\n const realRoots = await Promise.all(config.roots.map((r) => realpath(r).catch(() => r)));\n return realRoots.some((root) => isInsideRoot(real, root));\n}\n\n/**\n * Validate and resolve a caller-supplied scan path against the allow-list.\n *\n * Relative paths are resolved against the FIRST configured root (the primary\n * scan root), not the process CWD, so a relative request can never reach outside\n * the allow-list. Absolute paths must already sit inside a configured root. Any\n * path that resolves outside every root — whether via an absolute path or a\n * `..` traversal — is rejected. Pure: does no filesystem I/O (it does not follow\n * symlinks; the allow-list is the trust boundary).\n */\nexport function resolveScanPath(config: FsConfig, requested: string): PathDecision {\n const trimmed = requested.trim();\n if (trimmed.length === 0) {\n return { ok: false, reason: \"path must be a non-empty string\" };\n }\n\n const primaryRoot = config.roots[0];\n const resolved = path.isAbsolute(trimmed)\n ? path.resolve(trimmed)\n : path.resolve(primaryRoot, trimmed);\n\n for (const root of config.roots) {\n if (isInsideRoot(resolved, root)) {\n return { ok: true, path: resolved };\n }\n }\n return {\n ok: false,\n reason:\n \"path is outside the configured scan root(s). \" +\n \"Set QUANTAKRYPTO_MCP_ROOT to allow-list the directories the MCP may scan.\",\n };\n}\n"]}
|
package/dist/http.d.ts
CHANGED
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
* host WITHOUT a token is refused at startup (it would be an open relay).
|
|
27
27
|
* - The filesystem tools (scan_path, inventory_crypto, generate_cbom) read
|
|
28
28
|
* arbitrary server paths and are DISABLED over HTTP unless QUANTAKRYPTO_MCP_ALLOW_FS=1
|
|
29
|
-
* (security audit Q-01). The
|
|
29
|
+
* (security audit Q-01). The networked probe_endpoint tool is likewise DISABLED
|
|
30
|
+
* over HTTP unless QUANTAKRYPTO_MCP_ALLOW_NETWORK=1 (a hosted server should not
|
|
31
|
+
* probe arbitrary hosts). The knowledge tools (explain_finding, suggest_hybrid,
|
|
30
32
|
* list_rules) are always available. Gating is enforced by registering only the
|
|
31
33
|
* permitted tools, so tools/list and tools/call both reflect it.
|
|
32
34
|
* - Per-request timeout (QUANTAKRYPTO_MCP_TIMEOUT_MS) and a response-size cap
|
|
@@ -37,9 +39,7 @@
|
|
|
37
39
|
import type { Server } from "node:http";
|
|
38
40
|
import type { ToolDefinition } from "./protocol.js";
|
|
39
41
|
import type { McpServer } from "./server.js";
|
|
40
|
-
|
|
41
|
-
export declare const SESSION_HEADER = "mcp-session-id";
|
|
42
|
-
export interface HttpServerOptions {
|
|
42
|
+
interface HttpServerOptions {
|
|
43
43
|
/** Port to listen on. Defaults to env PORT or 3000. */
|
|
44
44
|
port?: number;
|
|
45
45
|
/** Host/interface to bind. Defaults to QUANTAKRYPTO_MCP_HOST / HOST or "127.0.0.1". */
|
|
@@ -48,21 +48,29 @@ export interface HttpServerOptions {
|
|
|
48
48
|
token?: string;
|
|
49
49
|
/** Expose the filesystem tools over HTTP. Defaults to QUANTAKRYPTO_MCP_ALLOW_FS=1. */
|
|
50
50
|
allowFs?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Expose the networked probe_endpoint tool over HTTP. Off by default (a hosted
|
|
53
|
+
* server should not probe arbitrary hosts); enable only on a trusted deployment.
|
|
54
|
+
* Defaults to QUANTAKRYPTO_MCP_ALLOW_NETWORK=1.
|
|
55
|
+
*/
|
|
56
|
+
allowNetwork?: boolean;
|
|
51
57
|
/** Per-request tool-execution timeout (ms). Defaults to QUANTAKRYPTO_MCP_TIMEOUT_MS. */
|
|
52
58
|
timeoutMs?: number;
|
|
53
59
|
/** Response-body size cap (bytes). Defaults to QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES. */
|
|
54
60
|
maxResponseBytes?: number;
|
|
55
61
|
}
|
|
56
62
|
/** A minimal env shape so the config resolver is pure and testable. */
|
|
57
|
-
|
|
63
|
+
type HttpEnv = Record<string, string | undefined>;
|
|
58
64
|
/** Resolved, validated HTTP transport configuration. */
|
|
59
|
-
|
|
65
|
+
interface HttpConfig {
|
|
60
66
|
host: string;
|
|
61
67
|
port: number;
|
|
62
68
|
/** The bearer token, or "" when auth is disabled. */
|
|
63
69
|
token: string;
|
|
64
70
|
/** Whether the filesystem tools are exposed over HTTP. */
|
|
65
71
|
allowFs: boolean;
|
|
72
|
+
/** Whether the networked probe_endpoint tool is exposed over HTTP (default off). */
|
|
73
|
+
allowNetwork: boolean;
|
|
66
74
|
timeoutMs: number;
|
|
67
75
|
maxResponseBytes: number;
|
|
68
76
|
/** True when the host is a loopback interface (safe without auth). */
|
|
@@ -111,7 +119,7 @@ export declare function originDecision(allowedHosts: ReadonlySet<string>, origin
|
|
|
111
119
|
/** The hosts an `Origin` may name. Loopback by default, plus the bind host. */
|
|
112
120
|
export declare function allowedOriginHosts(config: HttpConfig): Set<string>;
|
|
113
121
|
/** A request-authorization outcome. */
|
|
114
|
-
|
|
122
|
+
interface AuthDecision {
|
|
115
123
|
authorized: boolean;
|
|
116
124
|
/** HTTP status to use when not authorized. */
|
|
117
125
|
status?: number;
|
|
@@ -134,11 +142,12 @@ export declare function authorizeRequest(token: string, authorizationHeader: str
|
|
|
134
142
|
export declare function timingSafeEqualStr(a: string, b: string): boolean;
|
|
135
143
|
/**
|
|
136
144
|
* Select the tools to expose over HTTP for a given policy. The knowledge tools
|
|
137
|
-
* are always returned; the filesystem tools are included only when
|
|
138
|
-
*
|
|
139
|
-
*
|
|
145
|
+
* are always returned; the filesystem tools are included only when `allowFs` is
|
|
146
|
+
* true, and the networked probe tool only when `allowNetwork` is true (both off by
|
|
147
|
+
* default). Pure function of its inputs — the single source of truth for HTTP tool
|
|
148
|
+
* gating, so tools/list and tools/call stay consistent.
|
|
140
149
|
*/
|
|
141
|
-
export declare function gateHttpTools(tools: readonly ToolDefinition[], allowFs: boolean): ToolDefinition[];
|
|
150
|
+
export declare function gateHttpTools(tools: readonly ToolDefinition[], allowFs: boolean, allowNetwork?: boolean): ToolDefinition[];
|
|
142
151
|
/**
|
|
143
152
|
* Error thrown by {@link readBody} when the request body exceeds the size cap.
|
|
144
153
|
* Distinguished from a transport/I/O error so the caller can map it to HTTP 413
|
|
@@ -148,11 +157,6 @@ export declare class BodyTooLargeError extends Error {
|
|
|
148
157
|
readonly name = "BodyTooLargeError";
|
|
149
158
|
constructor(message?: string);
|
|
150
159
|
}
|
|
151
|
-
/** Error raised by {@link withTimeout} when the request deadline elapses. */
|
|
152
|
-
export declare class RequestTimeoutError extends Error {
|
|
153
|
-
readonly name = "RequestTimeoutError";
|
|
154
|
-
constructor(message?: string);
|
|
155
|
-
}
|
|
156
160
|
/**
|
|
157
161
|
* Build (but do not start) the HTTP server wrapping an {@link McpServer}.
|
|
158
162
|
* Exposed for testing and for embedding in a larger process. The `config`
|
|
@@ -164,6 +168,5 @@ export declare function createHttpServer(server: McpServer, config: HttpConfig):
|
|
|
164
168
|
* with the filesystem tools gated per policy. Exposed for tests.
|
|
165
169
|
*/
|
|
166
170
|
export declare function createHttpMcpServer(config: HttpConfig): McpServer;
|
|
167
|
-
|
|
168
|
-
export declare function startHttpServer(options?: HttpServerOptions): Promise<Server>;
|
|
171
|
+
export {};
|
|
169
172
|
//# sourceMappingURL=http.d.ts.map
|
package/dist/http.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":";AACA
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"m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KD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,UAAU,GAAG,SAAS,CAIjE"}
|
package/dist/http.js
CHANGED
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
* host WITHOUT a token is refused at startup (it would be an open relay).
|
|
27
27
|
* - The filesystem tools (scan_path, inventory_crypto, generate_cbom) read
|
|
28
28
|
* arbitrary server paths and are DISABLED over HTTP unless QUANTAKRYPTO_MCP_ALLOW_FS=1
|
|
29
|
-
* (security audit Q-01). The
|
|
29
|
+
* (security audit Q-01). The networked probe_endpoint tool is likewise DISABLED
|
|
30
|
+
* over HTTP unless QUANTAKRYPTO_MCP_ALLOW_NETWORK=1 (a hosted server should not
|
|
31
|
+
* probe arbitrary hosts). The knowledge tools (explain_finding, suggest_hybrid,
|
|
30
32
|
* list_rules) are always available. Gating is enforced by registering only the
|
|
31
33
|
* permitted tools, so tools/list and tools/call both reflect it.
|
|
32
34
|
* - Per-request timeout (QUANTAKRYPTO_MCP_TIMEOUT_MS) and a response-size cap
|
|
@@ -41,9 +43,9 @@ import { fileURLToPath } from "node:url";
|
|
|
41
43
|
import process from "node:process";
|
|
42
44
|
import { createQuantakryptoServer } from "./index.js";
|
|
43
45
|
import { ErrorCode, makeFailure } from "./protocol.js";
|
|
44
|
-
import { quantakryptoTools, FS_TOOL_NAMES } from "./tools.js";
|
|
46
|
+
import { quantakryptoTools, FS_TOOL_NAMES, NETWORK_TOOL_NAMES } from "./tools.js";
|
|
45
47
|
/** Header carrying the MCP session id (per the Streamable HTTP transport). */
|
|
46
|
-
|
|
48
|
+
const SESSION_HEADER = "mcp-session-id";
|
|
47
49
|
/** Maximum accepted request body size (1 MiB) — a basic abuse guard. */
|
|
48
50
|
const MAX_BODY_BYTES = 1024 * 1024;
|
|
49
51
|
/** Default per-request tool-execution deadline (ms). */
|
|
@@ -62,6 +64,8 @@ export function resolveHttpConfig(env, options = {}) {
|
|
|
62
64
|
const token = (options.token ?? env.QUANTAKRYPTO_MCP_TOKEN ?? "").trim();
|
|
63
65
|
const allowFs = options.allowFs ??
|
|
64
66
|
(env.QUANTAKRYPTO_MCP_ALLOW_FS === "1" || env.QUANTAKRYPTO_MCP_ALLOW_FS === "true");
|
|
67
|
+
const allowNetwork = options.allowNetwork ??
|
|
68
|
+
(env.QUANTAKRYPTO_MCP_ALLOW_NETWORK === "1" || env.QUANTAKRYPTO_MCP_ALLOW_NETWORK === "true");
|
|
65
69
|
const timeoutMs = options.timeoutMs ?? toInt(env.QUANTAKRYPTO_MCP_TIMEOUT_MS, DEFAULT_TIMEOUT_MS);
|
|
66
70
|
const maxResponseBytes = options.maxResponseBytes ??
|
|
67
71
|
toInt(env.QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES, DEFAULT_MAX_RESPONSE_BYTES);
|
|
@@ -71,6 +75,7 @@ export function resolveHttpConfig(env, options = {}) {
|
|
|
71
75
|
port,
|
|
72
76
|
token,
|
|
73
77
|
allowFs,
|
|
78
|
+
allowNetwork,
|
|
74
79
|
timeoutMs,
|
|
75
80
|
maxResponseBytes,
|
|
76
81
|
loopback: isLoopbackHost(host),
|
|
@@ -201,13 +206,16 @@ export function timingSafeEqualStr(a, b) {
|
|
|
201
206
|
}
|
|
202
207
|
/**
|
|
203
208
|
* Select the tools to expose over HTTP for a given policy. The knowledge tools
|
|
204
|
-
* are always returned; the filesystem tools are included only when
|
|
205
|
-
*
|
|
206
|
-
*
|
|
209
|
+
* are always returned; the filesystem tools are included only when `allowFs` is
|
|
210
|
+
* true, and the networked probe tool only when `allowNetwork` is true (both off by
|
|
211
|
+
* default). Pure function of its inputs — the single source of truth for HTTP tool
|
|
212
|
+
* gating, so tools/list and tools/call stay consistent.
|
|
207
213
|
*/
|
|
208
|
-
export function gateHttpTools(tools, allowFs) {
|
|
214
|
+
export function gateHttpTools(tools, allowFs, allowNetwork = false) {
|
|
209
215
|
const fsNames = new Set(FS_TOOL_NAMES);
|
|
210
|
-
|
|
216
|
+
const networkNames = new Set(NETWORK_TOOL_NAMES);
|
|
217
|
+
return tools.filter((t) => (allowNetwork || !networkNames.has(t.name)) && // networked tools: opt-in on HTTP
|
|
218
|
+
(allowFs || !fsNames.has(t.name)));
|
|
211
219
|
}
|
|
212
220
|
/* -------------------------------------------------------------------------- */
|
|
213
221
|
/* HTTP plumbing */
|
|
@@ -272,7 +280,7 @@ function sendJson(res, status, body, headers = {}) {
|
|
|
272
280
|
res.end(payload);
|
|
273
281
|
}
|
|
274
282
|
/** Error raised by {@link withTimeout} when the request deadline elapses. */
|
|
275
|
-
|
|
283
|
+
class RequestTimeoutError extends Error {
|
|
276
284
|
name = "RequestTimeoutError";
|
|
277
285
|
constructor(message = "request timed out") {
|
|
278
286
|
super(message);
|
|
@@ -451,10 +459,12 @@ async function handleMcpPost(server, config, req, res) {
|
|
|
451
459
|
* with the filesystem tools gated per policy. Exposed for tests.
|
|
452
460
|
*/
|
|
453
461
|
export function createHttpMcpServer(config) {
|
|
454
|
-
return createQuantakryptoServer({
|
|
462
|
+
return createQuantakryptoServer({
|
|
463
|
+
tools: gateHttpTools(quantakryptoTools, config.allowFs, config.allowNetwork),
|
|
464
|
+
});
|
|
455
465
|
}
|
|
456
466
|
/** Start the HTTP server, resolving once it is listening. */
|
|
457
|
-
|
|
467
|
+
function startHttpServer(options = {}) {
|
|
458
468
|
const config = resolveHttpConfig(process.env, options);
|
|
459
469
|
const decision = startupDecision(config);
|
|
460
470
|
if (!decision.ok) {
|
|
@@ -470,8 +480,9 @@ export function startHttpServer(options = {}) {
|
|
|
470
480
|
httpServer.listen(config.port, config.host, () => {
|
|
471
481
|
const auth = config.token ? "bearer-auth" : "no-auth";
|
|
472
482
|
const fs = config.allowFs ? "fs-tools:on" : "fs-tools:off";
|
|
483
|
+
const probe = config.allowNetwork ? "probe:on" : "probe:off";
|
|
473
484
|
process.stderr.write(`quantakrypto MCP server (http) listening on http://${config.host}:${config.port} ` +
|
|
474
|
-
`[${auth}, ${fs}]\n`);
|
|
485
|
+
`[${auth}, ${fs}, ${probe}]\n`);
|
|
475
486
|
process.stderr.write(` POST http://${config.host}:${config.port}/mcp ` +
|
|
476
487
|
`GET http://${config.host}:${config.port}/health\n`);
|
|
477
488
|
resolve(httpServer);
|
package/dist/http.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAG9D,8EAA8E;AAC9E,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAE/C,wEAAwE;AACxE,MAAM,cAAc,GAAG,IAAI,GAAG,IAAI,CAAC;AAEnC,wDAAwD;AACxD,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,2DAA2D;AAC3D,MAAM,0BAA0B,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAEnD,mEAAmE;AACnE,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC;AA2ClE;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAY,EAAE,UAA6B,EAAE;IAC7E,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,GAAG,CAAC,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AC;AACT,CAAC","sourcesContent":["#!/usr/bin/env node\n/**\n * quantakrypto MCP — hostable HTTP transport (Streamable-HTTP-style JSON-RPC).\n *\n * A zero-dependency {@link node:http} server that exposes the same\n * {@link McpServer} over HTTP so the quantakrypto MCP can run as a remote service:\n *\n * POST /mcp — body is a single JSON-RPC 2.0 message; the JSON-RPC\n * response is returned as the 200 response body\n * (`application/json`). Notifications get HTTP 202 with no body.\n * GET /health — liveness probe, returns `{ status: \"ok\", ... }`.\n *\n * This is the minimal-but-working core of the MCP Streamable HTTP transport.\n * The full spec also supports an SSE response (`text/event-stream`) for\n * server-initiated messages; this server speaks the JSON request/response half,\n * which is sufficient for stateless tool calls. See HOSTING.md for the\n * production design (auth, multi-tenant sessions, rate limiting, scaling).\n *\n * SAFE-BY-DEFAULT POSTURE (P0-1). Unlike the stdio transport — which trusts the\n * local user and stays fully featured — the HTTP transport is hardened because a\n * hosted endpoint is reachable by untrusted peers:\n *\n * - Binds to 127.0.0.1 by default (NOT 0.0.0.0). Override with QUANTAKRYPTO_MCP_HOST.\n * - Bearer-token auth: when QUANTAKRYPTO_MCP_TOKEN is set, every /mcp request must\n * send `Authorization: Bearer <token>` (else 401). Binding to a non-loopback\n * host WITHOUT a token is refused at startup (it would be an open relay).\n * - The filesystem tools (scan_path, inventory_crypto, generate_cbom) read\n * arbitrary server paths and are DISABLED over HTTP unless QUANTAKRYPTO_MCP_ALLOW_FS=1\n * (security audit Q-01). The knowledge tools (explain_finding, suggest_hybrid,\n * list_rules) are always available. Gating is enforced by registering only the\n * permitted tools, so tools/list and tools/call both reflect it.\n * - Per-request timeout (QUANTAKRYPTO_MCP_TIMEOUT_MS) and a response-size cap\n * (QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES), in addition to the 1 MiB request-body cap.\n *\n * Run with `node dist/http.js` (PORT/QUANTAKRYPTO_MCP_* from env).\n */\n\nimport { createServer } from \"node:http\";\nimport type { IncomingMessage, Server, ServerResponse } from \"node:http\";\nimport { createHash, randomUUID, timingSafeEqual } from \"node:crypto\";\nimport { realpathSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\nimport process from \"node:process\";\n\nimport { createQuantakryptoServer } from \"./index.js\";\nimport { ErrorCode, makeFailure } from \"./protocol.js\";\nimport type { JsonRpcResponse, ToolDefinition } from \"./protocol.js\";\nimport { quantakryptoTools, FS_TOOL_NAMES } from \"./tools.js\";\nimport type { McpServer } from \"./server.js\";\n\n/** Header carrying the MCP session id (per the Streamable HTTP transport). */\nexport const SESSION_HEADER = \"mcp-session-id\";\n\n/** Maximum accepted request body size (1 MiB) — a basic abuse guard. */\nconst MAX_BODY_BYTES = 1024 * 1024;\n\n/** Default per-request tool-execution deadline (ms). */\nconst DEFAULT_TIMEOUT_MS = 30_000;\n\n/** Default cap on the serialized response body (4 MiB). */\nconst DEFAULT_MAX_RESPONSE_BYTES = 4 * 1024 * 1024;\n\n/** Loopback hosts that are safe to bind without authentication. */\nconst LOOPBACK_HOSTS = new Set([\"127.0.0.1\", \"::1\", \"localhost\"]);\n\nexport interface HttpServerOptions {\n /** Port to listen on. Defaults to env PORT or 3000. */\n port?: number;\n /** Host/interface to bind. Defaults to QUANTAKRYPTO_MCP_HOST / HOST or \"127.0.0.1\". */\n host?: string;\n /** Bearer token required on /mcp. Defaults to QUANTAKRYPTO_MCP_TOKEN (empty = none). */\n token?: string;\n /** Expose the filesystem tools over HTTP. Defaults to QUANTAKRYPTO_MCP_ALLOW_FS=1. */\n allowFs?: boolean;\n /** Per-request tool-execution timeout (ms). Defaults to QUANTAKRYPTO_MCP_TIMEOUT_MS. */\n timeoutMs?: number;\n /** Response-body size cap (bytes). Defaults to QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES. */\n maxResponseBytes?: number;\n}\n\n/* -------------------------------------------------------------------------- */\n/* Pure policy (P0-1) — host/auth/tool-gating decisions, fully unit-testable. */\n/* -------------------------------------------------------------------------- */\n\n/** A minimal env shape so the config resolver is pure and testable. */\nexport type HttpEnv = Record<string, string | undefined>;\n\n/** Resolved, validated HTTP transport configuration. */\nexport interface HttpConfig {\n host: string;\n port: number;\n /** The bearer token, or \"\" when auth is disabled. */\n token: string;\n /** Whether the filesystem tools are exposed over HTTP. */\n allowFs: boolean;\n timeoutMs: number;\n maxResponseBytes: number;\n /** True when the host is a loopback interface (safe without auth). */\n loopback: boolean;\n /**\n * Extra origin hosts to allow on `POST /mcp` (lower-cased hostnames), beyond\n * the implicit loopback set and the bind host. From QUANTAKRYPTO_MCP_ALLOW_ORIGIN.\n */\n allowedOrigins: string[];\n}\n\n/**\n * Resolve the HTTP transport config from env + explicit overrides. Pure: does\n * no I/O and never reads `process` directly. Overrides win over env.\n */\nexport function resolveHttpConfig(env: HttpEnv, options: HttpServerOptions = {}): HttpConfig {\n const host = options.host ?? env.QUANTAKRYPTO_MCP_HOST ?? env.HOST ?? \"127.0.0.1\";\n const port = options.port ?? toInt(env.PORT, 3000);\n const token = (options.token ?? env.QUANTAKRYPTO_MCP_TOKEN ?? \"\").trim();\n const allowFs =\n options.allowFs ??\n (env.QUANTAKRYPTO_MCP_ALLOW_FS === \"1\" || env.QUANTAKRYPTO_MCP_ALLOW_FS === \"true\");\n const timeoutMs = options.timeoutMs ?? toInt(env.QUANTAKRYPTO_MCP_TIMEOUT_MS, DEFAULT_TIMEOUT_MS);\n const maxResponseBytes =\n options.maxResponseBytes ??\n toInt(env.QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES, DEFAULT_MAX_RESPONSE_BYTES);\n const allowedOrigins = parseOriginList(env.QUANTAKRYPTO_MCP_ALLOW_ORIGIN);\n return {\n host,\n port,\n token,\n allowFs,\n timeoutMs,\n maxResponseBytes,\n loopback: isLoopbackHost(host),\n allowedOrigins,\n };\n}\n\n/**\n * Parse the comma-separated origin allow-list into lower-cased hostnames. Each\n * entry may be a bare host or a full origin URL; either way only the hostname is\n * retained (the value compared against `new URL(Origin).hostname`).\n */\nfunction parseOriginList(value: string | undefined): string[] {\n const out = new Set<string>();\n for (const raw of (value ?? \"\").split(\",\")) {\n const entry = raw.trim().toLowerCase();\n if (entry.length === 0) continue;\n try {\n out.add(new URL(entry).hostname);\n } catch {\n out.add(entry); // bare hostname (no scheme).\n }\n }\n return [...out];\n}\n\n/** Parse a positive integer from an env string, falling back on bad input. */\nfunction toInt(value: string | undefined, fallback: number): number {\n if (value === undefined) return fallback;\n const n = Number(value);\n return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;\n}\n\n/** True when binding to `host` keeps the server private to this machine. */\nexport function isLoopbackHost(host: string): boolean {\n return LOOPBACK_HOSTS.has(host.trim().toLowerCase());\n}\n\n/**\n * Decide whether a non-loopback bind is permitted. A server reachable from the\n * network MUST require auth; binding wide-open without a token would be an open,\n * unauthenticated arbitrary-tool relay. Pure decision used at startup.\n */\nexport function startupDecision(config: HttpConfig): {\n ok: boolean;\n reason?: string;\n} {\n if (!config.loopback && config.token.length === 0) {\n return {\n ok: false,\n reason:\n `refusing to bind to non-loopback host \"${config.host}\" without a token. ` +\n `Set QUANTAKRYPTO_MCP_TOKEN to require Bearer auth, or bind to 127.0.0.1.`,\n };\n }\n return { ok: true };\n}\n\n/**\n * Decide whether a request's `Origin` / `Host` is acceptable, to defend the\n * default no-token loopback config against DNS-rebinding and localhost-CSRF: a\n * malicious web page can POST to `http://127.0.0.1:<port>/mcp`, but the browser\n * stamps a foreign `Origin` we can reject. Pure and testable.\n *\n * Policy:\n * - No `Origin` header (curl, a native MCP client, same-origin GET) → allow;\n * the header is a browser artifact and absence is not an attack signal here.\n * - An `Origin` present → its host must be a configured loopback host (or a\n * host explicitly added to the allow-list). A foreign origin is rejected.\n * - When the server itself binds a non-loopback interface, a token is already\n * mandatory (see {@link startupDecision}); the loopback allow-list still\n * applies but auth is the primary control there.\n */\nexport function originDecision(\n allowedHosts: ReadonlySet<string>,\n originHeader: string | undefined,\n): { ok: boolean; reason?: string } {\n const origin = (originHeader ?? \"\").trim();\n if (origin.length === 0) return { ok: true }; // non-browser client; nothing to check.\n if (origin.toLowerCase() === \"null\") {\n return { ok: false, reason: \"opaque/null Origin is not allowed\" };\n }\n let host: string;\n try {\n host = new URL(origin).hostname.toLowerCase();\n } catch {\n return { ok: false, reason: \"malformed Origin header\" };\n }\n if (allowedHosts.has(host)) return { ok: true };\n return { ok: false, reason: `Origin host \"${host}\" is not in the allow-list` };\n}\n\n/** The hosts an `Origin` may name. Loopback by default, plus the bind host. */\nexport function allowedOriginHosts(config: HttpConfig): Set<string> {\n const hosts = new Set<string>([\"127.0.0.1\", \"::1\", \"localhost\"]);\n // Allow the interface the server is actually bound to (e.g. a LAN address with\n // a token), so a same-host browser client keeps working.\n hosts.add(config.host.trim().toLowerCase());\n for (const extra of config.allowedOrigins) hosts.add(extra);\n return hosts;\n}\n\n/** A request-authorization outcome. */\nexport interface AuthDecision {\n authorized: boolean;\n /** HTTP status to use when not authorized. */\n status?: number;\n message?: string;\n}\n\n/**\n * Decide whether a request is authorized given the configured token and the\n * incoming Authorization header. When no token is configured, all requests are\n * allowed (the loopback / trusted-edge case). Pure and testable.\n */\nexport function authorizeRequest(\n token: string,\n authorizationHeader: string | undefined,\n): AuthDecision {\n if (token.length === 0) return { authorized: true };\n const header = (authorizationHeader ?? \"\").trim();\n const match = /^Bearer\\s+(.+)$/i.exec(header);\n const presented = match?.[1]?.trim();\n if (!presented) {\n return { authorized: false, status: 401, message: \"missing bearer token\" };\n }\n if (!timingSafeEqualStr(presented, token)) {\n return { authorized: false, status: 401, message: \"invalid bearer token\" };\n }\n return { authorized: true };\n}\n\n/**\n * Constant-time string compare. Both inputs are hashed with the same algorithm\n * to fixed-length digests before {@link timingSafeEqual}, so the comparison runs\n * over equal-length buffers and the early length-mismatch return (which leaked\n * the configured token's length) is gone. The hashing is a domain-separation /\n * length-equalization step, not a secrecy measure: `timingSafeEqual` still does\n * the constant-time work and rejects unequal digests.\n */\nexport function timingSafeEqualStr(a: string, b: string): boolean {\n const ha = createHash(\"sha256\").update(a, \"utf8\").digest();\n const hb = createHash(\"sha256\").update(b, \"utf8\").digest();\n // Same algorithm ⇒ identical digest length ⇒ timingSafeEqual never throws.\n return timingSafeEqual(ha, hb);\n}\n\n/**\n * Select the tools to expose over HTTP for a given policy. The knowledge tools\n * are always returned; the filesystem tools are included only when\n * `allowFs` is true. Pure function of its inputs — the single source of truth\n * for HTTP tool gating, so tools/list and tools/call stay consistent.\n */\nexport function gateHttpTools(\n tools: readonly ToolDefinition[],\n allowFs: boolean,\n): ToolDefinition[] {\n const fsNames = new Set(FS_TOOL_NAMES);\n return tools.filter((t) => allowFs || !fsNames.has(t.name));\n}\n\n/* -------------------------------------------------------------------------- */\n/* HTTP plumbing */\n/* -------------------------------------------------------------------------- */\n\n/**\n * Error thrown by {@link readBody} when the request body exceeds the size cap.\n * Distinguished from a transport/I/O error so the caller can map it to HTTP 413\n * specifically, while genuine read failures map to 400/500 (M1).\n */\nexport class BodyTooLargeError extends Error {\n override readonly name = \"BodyTooLargeError\";\n constructor(message = \"request body too large\") {\n super(message);\n }\n}\n\n/**\n * Read a request body fully, enforcing the size cap. Resolves to the raw string.\n * Rejects with a {@link BodyTooLargeError} when the cap is exceeded, and with the\n * original transport error (an I/O failure) otherwise — the two are mapped to\n * different HTTP statuses by the caller (M1).\n */\nfunction readBody(req: IncomingMessage): Promise<string> {\n return new Promise((resolve, reject) => {\n let size = 0;\n let done = false;\n const chunks: Buffer[] = [];\n req.on(\"data\", (chunk: Buffer) => {\n if (done) return;\n size += chunk.length;\n if (size > MAX_BODY_BYTES) {\n // Stop consuming and reject. We deliberately do NOT destroy the socket\n // here: the caller still needs to flush a 413 response, so it owns the\n // teardown (resume-and-drain) once that response is written.\n done = true;\n req.pause();\n reject(new BodyTooLargeError());\n return;\n }\n chunks.push(chunk);\n });\n req.on(\"end\", () => {\n if (!done) resolve(Buffer.concat(chunks).toString(\"utf8\"));\n });\n req.on(\"error\", (err: Error) => {\n if (!done) {\n done = true;\n reject(err);\n }\n });\n });\n}\n\n/** Write a JSON response with the given status and optional extra headers. */\nfunction sendJson(\n res: ServerResponse,\n status: number,\n body: unknown,\n headers: Record<string, string> = {},\n): void {\n const payload = JSON.stringify(body);\n res.writeHead(status, {\n \"content-type\": \"application/json; charset=utf-8\",\n \"content-length\": Buffer.byteLength(payload).toString(),\n ...headers,\n });\n res.end(payload);\n}\n\n/** Error raised by {@link withTimeout} when the request deadline elapses. */\nexport class RequestTimeoutError extends Error {\n override readonly name = \"RequestTimeoutError\";\n constructor(message = \"request timed out\") {\n super(message);\n }\n}\n\n/**\n * Race a handler against a deadline. On timeout the returned promise rejects with\n * a {@link RequestTimeoutError} AND the supplied {@link AbortController} is\n * aborted, so the in-flight work (a `scan()` wired to `controller.signal`) stops\n * cooperatively instead of running unbounded in the background after the 504.\n *\n * The controller is always aborted once the race settles (success, failure, or\n * timeout) so no scan keeps running past its response.\n */\nfunction withTimeout<T>(promise: Promise<T>, ms: number, controller: AbortController): Promise<T> {\n if (!(ms > 0)) return promise;\n return new Promise<T>((resolve, reject) => {\n const timer = setTimeout(() => {\n controller.abort(); // cancel the underlying scan — no leaked background work.\n reject(new RequestTimeoutError());\n }, ms);\n timer.unref?.();\n promise.then(\n (value) => {\n clearTimeout(timer);\n controller.abort(); // settle the signal so nothing lingers post-response.\n resolve(value);\n },\n (err: unknown) => {\n clearTimeout(timer);\n controller.abort();\n reject(err instanceof Error ? err : new Error(String(err)));\n },\n );\n });\n}\n\n/**\n * Build (but do not start) the HTTP server wrapping an {@link McpServer}.\n * Exposed for testing and for embedding in a larger process. The `config`\n * carries the resolved auth / timeout / response-cap policy.\n */\nexport function createHttpServer(server: McpServer, config: HttpConfig): Server {\n return createServer((req, res) => {\n void handleRequest(server, config, req, res).catch((err: unknown) => {\n // Last-resort handler: log the detail locally, return a generic message so\n // a remote caller never sees server internals (paths, stack traces).\n logServerError(\"unhandled request error\", err);\n if (!res.headersSent) {\n sendJson(res, 500, makeFailure(null, ErrorCode.InternalError, \"internal error\"));\n } else {\n res.end();\n }\n });\n });\n}\n\n/** Log a server-side error detail to stderr (never sent to the remote caller). */\nfunction logServerError(context: string, err: unknown): void {\n const detail = err instanceof Error ? (err.stack ?? err.message) : String(err);\n process.stderr.write(`quantakrypto MCP (http): ${context}: ${detail}\\n`);\n}\n\n/** Extract the (single) `Origin` request header, if present. */\nfunction originHeader(req: IncomingMessage): string | undefined {\n const value = req.headers.origin;\n return Array.isArray(value) ? value[0] : value;\n}\n\n/** Route and handle a single HTTP request. */\nasync function handleRequest(\n server: McpServer,\n config: HttpConfig,\n req: IncomingMessage,\n res: ServerResponse,\n): Promise<void> {\n const url = req.url ?? \"/\";\n const method = req.method ?? \"GET\";\n const path = url.split(\"?\")[0];\n\n if (method === \"GET\" && path === \"/health\") {\n sendJson(res, 200, { status: \"ok\", server: \"quantakrypto\", transport: \"http\" });\n return;\n }\n\n if (path === \"/mcp\") {\n if (method !== \"POST\") {\n // The full transport also allows GET for an SSE stream; we don't here.\n sendJson(res, 405, makeFailure(null, ErrorCode.InvalidRequest, \"method not allowed\"), {\n allow: \"POST\",\n });\n return;\n }\n // DNS-rebinding / localhost-CSRF guard: reject a foreign browser Origin\n // BEFORE auth/body, so the default no-token loopback config is not a\n // confused deputy for a malicious web page. (P0 — origin validation.)\n const origin = originDecision(allowedOriginHosts(config), originHeader(req));\n if (!origin.ok) {\n sendJson(\n res,\n 403,\n makeFailure(null, ErrorCode.InvalidRequest, origin.reason ?? \"forbidden origin\"),\n );\n return;\n }\n // Authenticate BEFORE reading the body or dispatching (Q-02).\n const auth = authorizeRequest(config.token, req.headers.authorization);\n if (!auth.authorized) {\n sendJson(\n res,\n auth.status ?? 401,\n makeFailure(null, ErrorCode.InvalidRequest, auth.message ?? \"unauthorized\"),\n { \"www-authenticate\": \"Bearer\" },\n );\n return;\n }\n await handleMcpPost(server, config, req, res);\n return;\n }\n\n sendJson(res, 404, makeFailure(null, ErrorCode.MethodNotFound, \"not found\"));\n}\n\n/** Handle `POST /mcp`: parse one JSON-RPC message, dispatch, return the result. */\nasync function handleMcpPost(\n server: McpServer,\n config: HttpConfig,\n req: IncomingMessage,\n res: ServerResponse,\n): Promise<void> {\n // Session handling (stateless here): echo a provided session id, else mint one.\n // A production server would look this up in a session store — see HOSTING.md.\n const incomingSession = req.headers[SESSION_HEADER];\n const sessionId =\n (Array.isArray(incomingSession) ? incomingSession[0] : incomingSession) ?? randomUUID();\n const sessionHeaders = { [SESSION_HEADER]: sessionId };\n\n let raw: string;\n try {\n raw = await readBody(req);\n } catch (err) {\n // M1: only the size-cap rejection is a 413. A genuine transport/I/O error\n // (the client aborted, a socket reset) is a 400 — it is the request that\n // failed, not a server fault — and never echoes the raw error text.\n if (err instanceof BodyTooLargeError) {\n // Flush the 413 first, then tear down the connection so a still-uploading\n // client stops sending. Destroying on `finish` guarantees the response is\n // written before the socket goes away (avoids a connection-reset race).\n res.on(\"finish\", () => req.destroy());\n sendJson(res, 413, makeFailure(null, ErrorCode.InvalidRequest, \"request body too large\"), {\n ...sessionHeaders,\n connection: \"close\",\n });\n } else {\n logServerError(\"error reading request body\", err);\n sendJson(\n res,\n 400,\n makeFailure(null, ErrorCode.InvalidRequest, \"error reading request body\"),\n sessionHeaders,\n );\n }\n return;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n sendJson(res, 400, makeFailure(null, ErrorCode.ParseError, \"parse error\"), sessionHeaders);\n return;\n }\n\n // Cancellation: the controller's signal is threaded into the tool handler (and\n // thus into `scan()`); withTimeout aborts it on deadline so a timed-out\n // request stops the underlying work instead of leaking it (P0 — timeout/abort).\n const controller = new AbortController();\n let response: JsonRpcResponse | null;\n try {\n response = await withTimeout(\n server.handle(parsed, { signal: controller.signal }),\n config.timeoutMs,\n controller,\n );\n } catch (err) {\n if (err instanceof RequestTimeoutError) {\n sendJson(\n res,\n 504,\n makeFailure(null, ErrorCode.InternalError, \"request timed out\"),\n sessionHeaders,\n );\n } else {\n // server.handle never rejects in practice (it catches internally), but be\n // defensive and never leak a raw message.\n logServerError(\"error dispatching request\", err);\n sendJson(\n res,\n 500,\n makeFailure(null, ErrorCode.InternalError, \"internal error\"),\n sessionHeaders,\n );\n }\n return;\n }\n\n if (response === null) {\n // Notification — acknowledge with 202 and no body.\n res.writeHead(202, sessionHeaders);\n res.end();\n return;\n }\n\n // Enforce the response-size cap (Q-03): never stream back an unbounded body.\n const serialized = JSON.stringify(response);\n if (Buffer.byteLength(serialized) > config.maxResponseBytes) {\n sendJson(\n res,\n 500,\n makeFailure(null, ErrorCode.InternalError, \"response too large\"),\n sessionHeaders,\n );\n return;\n }\n sendJson(res, 200, response, sessionHeaders);\n}\n\n/**\n * Build the HTTP-facing {@link McpServer}: the same server used over stdio but\n * with the filesystem tools gated per policy. Exposed for tests.\n */\nexport function createHttpMcpServer(config: HttpConfig): McpServer {\n return createQuantakryptoServer({ tools: gateHttpTools(quantakryptoTools, config.allowFs) });\n}\n\n/** Start the HTTP server, resolving once it is listening. */\nexport function startHttpServer(options: HttpServerOptions = {}): Promise<Server> {\n const config = resolveHttpConfig(process.env, options);\n\n const decision = startupDecision(config);\n if (!decision.ok) {\n return Promise.reject(new Error(decision.reason ?? \"refusing to start\"));\n }\n if (!config.loopback) {\n process.stderr.write(\n `quantakrypto MCP (http): WARNING binding to non-loopback host ${config.host}; ` +\n `Bearer auth is required and active.\\n`,\n );\n }\n\n const mcp = createHttpMcpServer(config);\n const httpServer = createHttpServer(mcp, config);\n return new Promise((resolve) => {\n httpServer.listen(config.port, config.host, () => {\n const auth = config.token ? \"bearer-auth\" : \"no-auth\";\n const fs = config.allowFs ? \"fs-tools:on\" : \"fs-tools:off\";\n process.stderr.write(\n `quantakrypto MCP server (http) listening on http://${config.host}:${config.port} ` +\n `[${auth}, ${fs}]\\n`,\n );\n process.stderr.write(\n ` POST http://${config.host}:${config.port}/mcp ` +\n `GET http://${config.host}:${config.port}/health\\n`,\n );\n resolve(httpServer);\n });\n });\n}\n\n/** Entry point when run directly. */\nfunction main(): void {\n startHttpServer().catch((err: unknown) => {\n const messageText = err instanceof Error ? err.message : String(err);\n process.stderr.write(`quantakrypto MCP (http) failed to start: ${messageText}\\n`);\n process.exitCode = 1;\n });\n}\n\n/** True when this module is the program's entry point (handles symlinks). */\nfunction isMainModule(): boolean {\n const argv1 = process.argv[1];\n if (!argv1) return false;\n const thisPath = fileURLToPath(import.meta.url);\n try {\n return realpathSync(argv1) === realpathSync(thisPath);\n } catch {\n return argv1 === thisPath;\n }\n}\n\n// Only auto-run when invoked as a script, not when imported by tests.\nif (isMainModule()) {\n main();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAGlF,8EAA8E;AAC9E,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAExC,wEAAwE;AACxE,MAAM,cAAc,GAAG,IAAI,GAAG,IAAI,CAAC;AAEnC,wDAAwD;AACxD,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,2DAA2D;AAC3D,MAAM,0BAA0B,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAEnD,mEAAmE;AACnE,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,KAAK,EAAE,W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AAO,CAAC,MAAM,CAAC,KAAK,CAAC,4CAA4C,WAAW,IAAI,CAAC,CAAC;QAClF,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;IACvB,CAAC,CAAC,CAAC;AACL,CAAC;AAED,6EAA6E;AAC7E,SAAS,YAAY;IACnB,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9B,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAC;IACzB,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAChD,IAAI,CAAC;QACH,OAAO,YAAY,CAAC,KAAK,CAAC,KAAK,YAAY,CAAC,QAAQ,CAAC,CAAC;IACxD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,KAAK,QAAQ,CAAC;IAC5B,CAAC;AACH,CAAC;AAED,sEAAsE;AACtE,IAAI,YAAY,EAAE,EAAE,CAAC;IACnB,IAAI,EAAE,CAAC;AACT,CAAC","sourcesContent":["#!/usr/bin/env node\n/**\n * quantakrypto MCP — hostable HTTP transport (Streamable-HTTP-style JSON-RPC).\n *\n * A zero-dependency {@link node:http} server that exposes the same\n * {@link McpServer} over HTTP so the quantakrypto MCP can run as a remote service:\n *\n * POST /mcp — body is a single JSON-RPC 2.0 message; the JSON-RPC\n * response is returned as the 200 response body\n * (`application/json`). Notifications get HTTP 202 with no body.\n * GET /health — liveness probe, returns `{ status: \"ok\", ... }`.\n *\n * This is the minimal-but-working core of the MCP Streamable HTTP transport.\n * The full spec also supports an SSE response (`text/event-stream`) for\n * server-initiated messages; this server speaks the JSON request/response half,\n * which is sufficient for stateless tool calls. See HOSTING.md for the\n * production design (auth, multi-tenant sessions, rate limiting, scaling).\n *\n * SAFE-BY-DEFAULT POSTURE (P0-1). Unlike the stdio transport — which trusts the\n * local user and stays fully featured — the HTTP transport is hardened because a\n * hosted endpoint is reachable by untrusted peers:\n *\n * - Binds to 127.0.0.1 by default (NOT 0.0.0.0). Override with QUANTAKRYPTO_MCP_HOST.\n * - Bearer-token auth: when QUANTAKRYPTO_MCP_TOKEN is set, every /mcp request must\n * send `Authorization: Bearer <token>` (else 401). Binding to a non-loopback\n * host WITHOUT a token is refused at startup (it would be an open relay).\n * - The filesystem tools (scan_path, inventory_crypto, generate_cbom) read\n * arbitrary server paths and are DISABLED over HTTP unless QUANTAKRYPTO_MCP_ALLOW_FS=1\n * (security audit Q-01). The networked probe_endpoint tool is likewise DISABLED\n * over HTTP unless QUANTAKRYPTO_MCP_ALLOW_NETWORK=1 (a hosted server should not\n * probe arbitrary hosts). The knowledge tools (explain_finding, suggest_hybrid,\n * list_rules) are always available. Gating is enforced by registering only the\n * permitted tools, so tools/list and tools/call both reflect it.\n * - Per-request timeout (QUANTAKRYPTO_MCP_TIMEOUT_MS) and a response-size cap\n * (QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES), in addition to the 1 MiB request-body cap.\n *\n * Run with `node dist/http.js` (PORT/QUANTAKRYPTO_MCP_* from env).\n */\n\nimport { createServer } from \"node:http\";\nimport type { IncomingMessage, Server, ServerResponse } from \"node:http\";\nimport { createHash, randomUUID, timingSafeEqual } from \"node:crypto\";\nimport { realpathSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\nimport process from \"node:process\";\n\nimport { createQuantakryptoServer } from \"./index.js\";\nimport { ErrorCode, makeFailure } from \"./protocol.js\";\nimport type { JsonRpcResponse, ToolDefinition } from \"./protocol.js\";\nimport { quantakryptoTools, FS_TOOL_NAMES, NETWORK_TOOL_NAMES } from \"./tools.js\";\nimport type { McpServer } from \"./server.js\";\n\n/** Header carrying the MCP session id (per the Streamable HTTP transport). */\nconst SESSION_HEADER = \"mcp-session-id\";\n\n/** Maximum accepted request body size (1 MiB) — a basic abuse guard. */\nconst MAX_BODY_BYTES = 1024 * 1024;\n\n/** Default per-request tool-execution deadline (ms). */\nconst DEFAULT_TIMEOUT_MS = 30_000;\n\n/** Default cap on the serialized response body (4 MiB). */\nconst DEFAULT_MAX_RESPONSE_BYTES = 4 * 1024 * 1024;\n\n/** Loopback hosts that are safe to bind without authentication. */\nconst LOOPBACK_HOSTS = new Set([\"127.0.0.1\", \"::1\", \"localhost\"]);\n\ninterface HttpServerOptions {\n /** Port to listen on. Defaults to env PORT or 3000. */\n port?: number;\n /** Host/interface to bind. Defaults to QUANTAKRYPTO_MCP_HOST / HOST or \"127.0.0.1\". */\n host?: string;\n /** Bearer token required on /mcp. Defaults to QUANTAKRYPTO_MCP_TOKEN (empty = none). */\n token?: string;\n /** Expose the filesystem tools over HTTP. Defaults to QUANTAKRYPTO_MCP_ALLOW_FS=1. */\n allowFs?: boolean;\n /**\n * Expose the networked probe_endpoint tool over HTTP. Off by default (a hosted\n * server should not probe arbitrary hosts); enable only on a trusted deployment.\n * Defaults to QUANTAKRYPTO_MCP_ALLOW_NETWORK=1.\n */\n allowNetwork?: boolean;\n /** Per-request tool-execution timeout (ms). Defaults to QUANTAKRYPTO_MCP_TIMEOUT_MS. */\n timeoutMs?: number;\n /** Response-body size cap (bytes). Defaults to QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES. */\n maxResponseBytes?: number;\n}\n\n/* -------------------------------------------------------------------------- */\n/* Pure policy (P0-1) — host/auth/tool-gating decisions, fully unit-testable. */\n/* -------------------------------------------------------------------------- */\n\n/** A minimal env shape so the config resolver is pure and testable. */\ntype HttpEnv = Record<string, string | undefined>;\n\n/** Resolved, validated HTTP transport configuration. */\ninterface HttpConfig {\n host: string;\n port: number;\n /** The bearer token, or \"\" when auth is disabled. */\n token: string;\n /** Whether the filesystem tools are exposed over HTTP. */\n allowFs: boolean;\n /** Whether the networked probe_endpoint tool is exposed over HTTP (default off). */\n allowNetwork: boolean;\n timeoutMs: number;\n maxResponseBytes: number;\n /** True when the host is a loopback interface (safe without auth). */\n loopback: boolean;\n /**\n * Extra origin hosts to allow on `POST /mcp` (lower-cased hostnames), beyond\n * the implicit loopback set and the bind host. From QUANTAKRYPTO_MCP_ALLOW_ORIGIN.\n */\n allowedOrigins: string[];\n}\n\n/**\n * Resolve the HTTP transport config from env + explicit overrides. Pure: does\n * no I/O and never reads `process` directly. Overrides win over env.\n */\nexport function resolveHttpConfig(env: HttpEnv, options: HttpServerOptions = {}): HttpConfig {\n const host = options.host ?? env.QUANTAKRYPTO_MCP_HOST ?? env.HOST ?? \"127.0.0.1\";\n const port = options.port ?? toInt(env.PORT, 3000);\n const token = (options.token ?? env.QUANTAKRYPTO_MCP_TOKEN ?? \"\").trim();\n const allowFs =\n options.allowFs ??\n (env.QUANTAKRYPTO_MCP_ALLOW_FS === \"1\" || env.QUANTAKRYPTO_MCP_ALLOW_FS === \"true\");\n const allowNetwork =\n options.allowNetwork ??\n (env.QUANTAKRYPTO_MCP_ALLOW_NETWORK === \"1\" || env.QUANTAKRYPTO_MCP_ALLOW_NETWORK === \"true\");\n const timeoutMs = options.timeoutMs ?? toInt(env.QUANTAKRYPTO_MCP_TIMEOUT_MS, DEFAULT_TIMEOUT_MS);\n const maxResponseBytes =\n options.maxResponseBytes ??\n toInt(env.QUANTAKRYPTO_MCP_MAX_RESPONSE_BYTES, DEFAULT_MAX_RESPONSE_BYTES);\n const allowedOrigins = parseOriginList(env.QUANTAKRYPTO_MCP_ALLOW_ORIGIN);\n return {\n host,\n port,\n token,\n allowFs,\n allowNetwork,\n timeoutMs,\n maxResponseBytes,\n loopback: isLoopbackHost(host),\n allowedOrigins,\n };\n}\n\n/**\n * Parse the comma-separated origin allow-list into lower-cased hostnames. Each\n * entry may be a bare host or a full origin URL; either way only the hostname is\n * retained (the value compared against `new URL(Origin).hostname`).\n */\nfunction parseOriginList(value: string | undefined): string[] {\n const out = new Set<string>();\n for (const raw of (value ?? \"\").split(\",\")) {\n const entry = raw.trim().toLowerCase();\n if (entry.length === 0) continue;\n try {\n out.add(new URL(entry).hostname);\n } catch {\n out.add(entry); // bare hostname (no scheme).\n }\n }\n return [...out];\n}\n\n/** Parse a positive integer from an env string, falling back on bad input. */\nfunction toInt(value: string | undefined, fallback: number): number {\n if (value === undefined) return fallback;\n const n = Number(value);\n return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;\n}\n\n/** True when binding to `host` keeps the server private to this machine. */\nexport function isLoopbackHost(host: string): boolean {\n return LOOPBACK_HOSTS.has(host.trim().toLowerCase());\n}\n\n/**\n * Decide whether a non-loopback bind is permitted. A server reachable from the\n * network MUST require auth; binding wide-open without a token would be an open,\n * unauthenticated arbitrary-tool relay. Pure decision used at startup.\n */\nexport function startupDecision(config: HttpConfig): {\n ok: boolean;\n reason?: string;\n} {\n if (!config.loopback && config.token.length === 0) {\n return {\n ok: false,\n reason:\n `refusing to bind to non-loopback host \"${config.host}\" without a token. ` +\n `Set QUANTAKRYPTO_MCP_TOKEN to require Bearer auth, or bind to 127.0.0.1.`,\n };\n }\n return { ok: true };\n}\n\n/**\n * Decide whether a request's `Origin` / `Host` is acceptable, to defend the\n * default no-token loopback config against DNS-rebinding and localhost-CSRF: a\n * malicious web page can POST to `http://127.0.0.1:<port>/mcp`, but the browser\n * stamps a foreign `Origin` we can reject. Pure and testable.\n *\n * Policy:\n * - No `Origin` header (curl, a native MCP client, same-origin GET) → allow;\n * the header is a browser artifact and absence is not an attack signal here.\n * - An `Origin` present → its host must be a configured loopback host (or a\n * host explicitly added to the allow-list). A foreign origin is rejected.\n * - When the server itself binds a non-loopback interface, a token is already\n * mandatory (see {@link startupDecision}); the loopback allow-list still\n * applies but auth is the primary control there.\n */\nexport function originDecision(\n allowedHosts: ReadonlySet<string>,\n originHeader: string | undefined,\n): { ok: boolean; reason?: string } {\n const origin = (originHeader ?? \"\").trim();\n if (origin.length === 0) return { ok: true }; // non-browser client; nothing to check.\n if (origin.toLowerCase() === \"null\") {\n return { ok: false, reason: \"opaque/null Origin is not allowed\" };\n }\n let host: string;\n try {\n host = new URL(origin).hostname.toLowerCase();\n } catch {\n return { ok: false, reason: \"malformed Origin header\" };\n }\n if (allowedHosts.has(host)) return { ok: true };\n return { ok: false, reason: `Origin host \"${host}\" is not in the allow-list` };\n}\n\n/** The hosts an `Origin` may name. Loopback by default, plus the bind host. */\nexport function allowedOriginHosts(config: HttpConfig): Set<string> {\n const hosts = new Set<string>([\"127.0.0.1\", \"::1\", \"localhost\"]);\n // Allow the interface the server is actually bound to (e.g. a LAN address with\n // a token), so a same-host browser client keeps working.\n hosts.add(config.host.trim().toLowerCase());\n for (const extra of config.allowedOrigins) hosts.add(extra);\n return hosts;\n}\n\n/** A request-authorization outcome. */\ninterface AuthDecision {\n authorized: boolean;\n /** HTTP status to use when not authorized. */\n status?: number;\n message?: string;\n}\n\n/**\n * Decide whether a request is authorized given the configured token and the\n * incoming Authorization header. When no token is configured, all requests are\n * allowed (the loopback / trusted-edge case). Pure and testable.\n */\nexport function authorizeRequest(\n token: string,\n authorizationHeader: string | undefined,\n): AuthDecision {\n if (token.length === 0) return { authorized: true };\n const header = (authorizationHeader ?? \"\").trim();\n const match = /^Bearer\\s+(.+)$/i.exec(header);\n const presented = match?.[1]?.trim();\n if (!presented) {\n return { authorized: false, status: 401, message: \"missing bearer token\" };\n }\n if (!timingSafeEqualStr(presented, token)) {\n return { authorized: false, status: 401, message: \"invalid bearer token\" };\n }\n return { authorized: true };\n}\n\n/**\n * Constant-time string compare. Both inputs are hashed with the same algorithm\n * to fixed-length digests before {@link timingSafeEqual}, so the comparison runs\n * over equal-length buffers and the early length-mismatch return (which leaked\n * the configured token's length) is gone. The hashing is a domain-separation /\n * length-equalization step, not a secrecy measure: `timingSafeEqual` still does\n * the constant-time work and rejects unequal digests.\n */\nexport function timingSafeEqualStr(a: string, b: string): boolean {\n const ha = createHash(\"sha256\").update(a, \"utf8\").digest();\n const hb = createHash(\"sha256\").update(b, \"utf8\").digest();\n // Same algorithm ⇒ identical digest length ⇒ timingSafeEqual never throws.\n return timingSafeEqual(ha, hb);\n}\n\n/**\n * Select the tools to expose over HTTP for a given policy. The knowledge tools\n * are always returned; the filesystem tools are included only when `allowFs` is\n * true, and the networked probe tool only when `allowNetwork` is true (both off by\n * default). Pure function of its inputs — the single source of truth for HTTP tool\n * gating, so tools/list and tools/call stay consistent.\n */\nexport function gateHttpTools(\n tools: readonly ToolDefinition[],\n allowFs: boolean,\n allowNetwork = false,\n): ToolDefinition[] {\n const fsNames = new Set(FS_TOOL_NAMES);\n const networkNames = new Set(NETWORK_TOOL_NAMES);\n return tools.filter(\n (t) =>\n (allowNetwork || !networkNames.has(t.name)) && // networked tools: opt-in on HTTP\n (allowFs || !fsNames.has(t.name)), // fs tools: opt-in on HTTP\n );\n}\n\n/* -------------------------------------------------------------------------- */\n/* HTTP plumbing */\n/* -------------------------------------------------------------------------- */\n\n/**\n * Error thrown by {@link readBody} when the request body exceeds the size cap.\n * Distinguished from a transport/I/O error so the caller can map it to HTTP 413\n * specifically, while genuine read failures map to 400/500 (M1).\n */\nexport class BodyTooLargeError extends Error {\n override readonly name = \"BodyTooLargeError\";\n constructor(message = \"request body too large\") {\n super(message);\n }\n}\n\n/**\n * Read a request body fully, enforcing the size cap. Resolves to the raw string.\n * Rejects with a {@link BodyTooLargeError} when the cap is exceeded, and with the\n * original transport error (an I/O failure) otherwise — the two are mapped to\n * different HTTP statuses by the caller (M1).\n */\nfunction readBody(req: IncomingMessage): Promise<string> {\n return new Promise((resolve, reject) => {\n let size = 0;\n let done = false;\n const chunks: Buffer[] = [];\n req.on(\"data\", (chunk: Buffer) => {\n if (done) return;\n size += chunk.length;\n if (size > MAX_BODY_BYTES) {\n // Stop consuming and reject. We deliberately do NOT destroy the socket\n // here: the caller still needs to flush a 413 response, so it owns the\n // teardown (resume-and-drain) once that response is written.\n done = true;\n req.pause();\n reject(new BodyTooLargeError());\n return;\n }\n chunks.push(chunk);\n });\n req.on(\"end\", () => {\n if (!done) resolve(Buffer.concat(chunks).toString(\"utf8\"));\n });\n req.on(\"error\", (err: Error) => {\n if (!done) {\n done = true;\n reject(err);\n }\n });\n });\n}\n\n/** Write a JSON response with the given status and optional extra headers. */\nfunction sendJson(\n res: ServerResponse,\n status: number,\n body: unknown,\n headers: Record<string, string> = {},\n): void {\n const payload = JSON.stringify(body);\n res.writeHead(status, {\n \"content-type\": \"application/json; charset=utf-8\",\n \"content-length\": Buffer.byteLength(payload).toString(),\n ...headers,\n });\n res.end(payload);\n}\n\n/** Error raised by {@link withTimeout} when the request deadline elapses. */\nclass RequestTimeoutError extends Error {\n override readonly name = \"RequestTimeoutError\";\n constructor(message = \"request timed out\") {\n super(message);\n }\n}\n\n/**\n * Race a handler against a deadline. On timeout the returned promise rejects with\n * a {@link RequestTimeoutError} AND the supplied {@link AbortController} is\n * aborted, so the in-flight work (a `scan()` wired to `controller.signal`) stops\n * cooperatively instead of running unbounded in the background after the 504.\n *\n * The controller is always aborted once the race settles (success, failure, or\n * timeout) so no scan keeps running past its response.\n */\nfunction withTimeout<T>(promise: Promise<T>, ms: number, controller: AbortController): Promise<T> {\n if (!(ms > 0)) return promise;\n return new Promise<T>((resolve, reject) => {\n const timer = setTimeout(() => {\n controller.abort(); // cancel the underlying scan — no leaked background work.\n reject(new RequestTimeoutError());\n }, ms);\n timer.unref?.();\n promise.then(\n (value) => {\n clearTimeout(timer);\n controller.abort(); // settle the signal so nothing lingers post-response.\n resolve(value);\n },\n (err: unknown) => {\n clearTimeout(timer);\n controller.abort();\n reject(err instanceof Error ? err : new Error(String(err)));\n },\n );\n });\n}\n\n/**\n * Build (but do not start) the HTTP server wrapping an {@link McpServer}.\n * Exposed for testing and for embedding in a larger process. The `config`\n * carries the resolved auth / timeout / response-cap policy.\n */\nexport function createHttpServer(server: McpServer, config: HttpConfig): Server {\n return createServer((req, res) => {\n void handleRequest(server, config, req, res).catch((err: unknown) => {\n // Last-resort handler: log the detail locally, return a generic message so\n // a remote caller never sees server internals (paths, stack traces).\n logServerError(\"unhandled request error\", err);\n if (!res.headersSent) {\n sendJson(res, 500, makeFailure(null, ErrorCode.InternalError, \"internal error\"));\n } else {\n res.end();\n }\n });\n });\n}\n\n/** Log a server-side error detail to stderr (never sent to the remote caller). */\nfunction logServerError(context: string, err: unknown): void {\n const detail = err instanceof Error ? (err.stack ?? err.message) : String(err);\n process.stderr.write(`quantakrypto MCP (http): ${context}: ${detail}\\n`);\n}\n\n/** Extract the (single) `Origin` request header, if present. */\nfunction originHeader(req: IncomingMessage): string | undefined {\n const value = req.headers.origin;\n return Array.isArray(value) ? value[0] : value;\n}\n\n/** Route and handle a single HTTP request. */\nasync function handleRequest(\n server: McpServer,\n config: HttpConfig,\n req: IncomingMessage,\n res: ServerResponse,\n): Promise<void> {\n const url = req.url ?? \"/\";\n const method = req.method ?? \"GET\";\n const path = url.split(\"?\")[0];\n\n if (method === \"GET\" && path === \"/health\") {\n sendJson(res, 200, { status: \"ok\", server: \"quantakrypto\", transport: \"http\" });\n return;\n }\n\n if (path === \"/mcp\") {\n if (method !== \"POST\") {\n // The full transport also allows GET for an SSE stream; we don't here.\n sendJson(res, 405, makeFailure(null, ErrorCode.InvalidRequest, \"method not allowed\"), {\n allow: \"POST\",\n });\n return;\n }\n // DNS-rebinding / localhost-CSRF guard: reject a foreign browser Origin\n // BEFORE auth/body, so the default no-token loopback config is not a\n // confused deputy for a malicious web page. (P0 — origin validation.)\n const origin = originDecision(allowedOriginHosts(config), originHeader(req));\n if (!origin.ok) {\n sendJson(\n res,\n 403,\n makeFailure(null, ErrorCode.InvalidRequest, origin.reason ?? \"forbidden origin\"),\n );\n return;\n }\n // Authenticate BEFORE reading the body or dispatching (Q-02).\n const auth = authorizeRequest(config.token, req.headers.authorization);\n if (!auth.authorized) {\n sendJson(\n res,\n auth.status ?? 401,\n makeFailure(null, ErrorCode.InvalidRequest, auth.message ?? \"unauthorized\"),\n { \"www-authenticate\": \"Bearer\" },\n );\n return;\n }\n await handleMcpPost(server, config, req, res);\n return;\n }\n\n sendJson(res, 404, makeFailure(null, ErrorCode.MethodNotFound, \"not found\"));\n}\n\n/** Handle `POST /mcp`: parse one JSON-RPC message, dispatch, return the result. */\nasync function handleMcpPost(\n server: McpServer,\n config: HttpConfig,\n req: IncomingMessage,\n res: ServerResponse,\n): Promise<void> {\n // Session handling (stateless here): echo a provided session id, else mint one.\n // A production server would look this up in a session store — see HOSTING.md.\n const incomingSession = req.headers[SESSION_HEADER];\n const sessionId =\n (Array.isArray(incomingSession) ? incomingSession[0] : incomingSession) ?? randomUUID();\n const sessionHeaders = { [SESSION_HEADER]: sessionId };\n\n let raw: string;\n try {\n raw = await readBody(req);\n } catch (err) {\n // M1: only the size-cap rejection is a 413. A genuine transport/I/O error\n // (the client aborted, a socket reset) is a 400 — it is the request that\n // failed, not a server fault — and never echoes the raw error text.\n if (err instanceof BodyTooLargeError) {\n // Flush the 413 first, then tear down the connection so a still-uploading\n // client stops sending. Destroying on `finish` guarantees the response is\n // written before the socket goes away (avoids a connection-reset race).\n res.on(\"finish\", () => req.destroy());\n sendJson(res, 413, makeFailure(null, ErrorCode.InvalidRequest, \"request body too large\"), {\n ...sessionHeaders,\n connection: \"close\",\n });\n } else {\n logServerError(\"error reading request body\", err);\n sendJson(\n res,\n 400,\n makeFailure(null, ErrorCode.InvalidRequest, \"error reading request body\"),\n sessionHeaders,\n );\n }\n return;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n sendJson(res, 400, makeFailure(null, ErrorCode.ParseError, \"parse error\"), sessionHeaders);\n return;\n }\n\n // Cancellation: the controller's signal is threaded into the tool handler (and\n // thus into `scan()`); withTimeout aborts it on deadline so a timed-out\n // request stops the underlying work instead of leaking it (P0 — timeout/abort).\n const controller = new AbortController();\n let response: JsonRpcResponse | null;\n try {\n response = await withTimeout(\n server.handle(parsed, { signal: controller.signal }),\n config.timeoutMs,\n controller,\n );\n } catch (err) {\n if (err instanceof RequestTimeoutError) {\n sendJson(\n res,\n 504,\n makeFailure(null, ErrorCode.InternalError, \"request timed out\"),\n sessionHeaders,\n );\n } else {\n // server.handle never rejects in practice (it catches internally), but be\n // defensive and never leak a raw message.\n logServerError(\"error dispatching request\", err);\n sendJson(\n res,\n 500,\n makeFailure(null, ErrorCode.InternalError, \"internal error\"),\n sessionHeaders,\n );\n }\n return;\n }\n\n if (response === null) {\n // Notification — acknowledge with 202 and no body.\n res.writeHead(202, sessionHeaders);\n res.end();\n return;\n }\n\n // Enforce the response-size cap (Q-03): never stream back an unbounded body.\n const serialized = JSON.stringify(response);\n if (Buffer.byteLength(serialized) > config.maxResponseBytes) {\n sendJson(\n res,\n 500,\n makeFailure(null, ErrorCode.InternalError, \"response too large\"),\n sessionHeaders,\n );\n return;\n }\n sendJson(res, 200, response, sessionHeaders);\n}\n\n/**\n * Build the HTTP-facing {@link McpServer}: the same server used over stdio but\n * with the filesystem tools gated per policy. Exposed for tests.\n */\nexport function createHttpMcpServer(config: HttpConfig): McpServer {\n return createQuantakryptoServer({\n tools: gateHttpTools(quantakryptoTools, config.allowFs, config.allowNetwork),\n });\n}\n\n/** Start the HTTP server, resolving once it is listening. */\nfunction startHttpServer(options: HttpServerOptions = {}): Promise<Server> {\n const config = resolveHttpConfig(process.env, options);\n\n const decision = startupDecision(config);\n if (!decision.ok) {\n return Promise.reject(new Error(decision.reason ?? \"refusing to start\"));\n }\n if (!config.loopback) {\n process.stderr.write(\n `quantakrypto MCP (http): WARNING binding to non-loopback host ${config.host}; ` +\n `Bearer auth is required and active.\\n`,\n );\n }\n\n const mcp = createHttpMcpServer(config);\n const httpServer = createHttpServer(mcp, config);\n return new Promise((resolve) => {\n httpServer.listen(config.port, config.host, () => {\n const auth = config.token ? \"bearer-auth\" : \"no-auth\";\n const fs = config.allowFs ? \"fs-tools:on\" : \"fs-tools:off\";\n const probe = config.allowNetwork ? \"probe:on\" : \"probe:off\";\n process.stderr.write(\n `quantakrypto MCP server (http) listening on http://${config.host}:${config.port} ` +\n `[${auth}, ${fs}, ${probe}]\\n`,\n );\n process.stderr.write(\n ` POST http://${config.host}:${config.port}/mcp ` +\n `GET http://${config.host}:${config.port}/health\\n`,\n );\n resolve(httpServer);\n });\n });\n}\n\n/** Entry point when run directly. */\nfunction main(): void {\n startHttpServer().catch((err: unknown) => {\n const messageText = err instanceof Error ? err.message : String(err);\n process.stderr.write(`quantakrypto MCP (http) failed to start: ${messageText}\\n`);\n process.exitCode = 1;\n });\n}\n\n/** True when this module is the program's entry point (handles symlinks). */\nfunction isMainModule(): boolean {\n const argv1 = process.argv[1];\n if (!argv1) return false;\n const thisPath = fileURLToPath(import.meta.url);\n try {\n return realpathSync(argv1) === realpathSync(thisPath);\n } catch {\n return argv1 === thisPath;\n }\n}\n\n// Only auto-run when invoked as a script, not when imported by tests.\nif (isMainModule()) {\n main();\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export type { JsonRpcRequest, JsonRpcResponse, JsonRpcSuccess, JsonRpcFailure, T
|
|
|
16
16
|
/** The MCP server name advertised to clients. */
|
|
17
17
|
export declare const SERVER_NAME = "quantakrypto";
|
|
18
18
|
/** The version reported by the server (kept in sync with @quantakrypto/core). */
|
|
19
|
-
export declare const SERVER_VERSION = "0.
|
|
19
|
+
export declare const SERVER_VERSION = "0.5.0";
|
|
20
20
|
export interface CreateServerOptions {
|
|
21
21
|
/** Override the advertised server version (defaults to @quantakrypto/core VERSION). */
|
|
22
22
|
version?: string;
|