Files
Netcatty/domain/externalMcpHttp.test.ts
T
0ccafbabf7 feat: optional Streamable HTTP for External MCP (#3790)
* feat: add optional Streamable HTTP for External MCP

Serve the existing catalog tools and approval path on a loopback HTTP
endpoint so clients that cannot spawn the stdio launcher, such as Codex
in WSL, can connect with a bearer token. The listener stays off until
the user enables it.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: close HTTP sockets before stop and export the Codex bearer

Destroy tracked connections before awaiting server.close so disable does not hang on a half-open request. Prefix the Codex snippet with export so the child process receives the token.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: advertise a real client host and return 413 intact

Wildcard binds no longer copy 127.0.0.1 into client configs; the URL stays empty until a reachable client address is set. Oversized POSTs receive a JSON-RPC 413 before the socket is closed.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: recheck the bearer token after the HTTP body arrives

A half-open POST already passed auth before readBody. Recheck the live token before dispatch so a rotation during that wait cannot still run a tool.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: expire idle Streamable HTTP sessions

JSON responses do not keep a socket open, so a client that never sends DELETE used to pin an MCP session until the listener stopped. Sweep sessions that have been idle for 15 minutes, and skip any session with a request still in flight.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: return 404 when an HTTP session id is unknown

GET and DELETE with a nonempty session id that is no longer active now return 404, matching POST and the Streamable HTTP session contract. A missing session id stays 400.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: accept bracketed IPv6 origins

Node reports URL.hostname as [::1], so an otherwise valid loopback or wildcard Origin was rejected. Compare the address with the brackets removed.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: release rejected HTTP handshakes that never open a session

A 406 or 415 from the SDK returns normally before onsessioninitialized. Detach the approval client and close the MCP server when that happens so rejected initializes do not accumulate.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-20 16:39:56 +08:00

55 lines
2.1 KiB
TypeScript

import assert from "node:assert/strict";
import { createRequire } from "node:module";
import test from "node:test";
import {
DEFAULT_EXTERNAL_MCP_HTTP_PORT,
buildClaudeHttpAddCommand,
buildCodexHttpEnvLine,
buildCodexHttpToml,
buildExternalMcpHttpUrl,
buildGenericHttpMcpJson,
} from "./externalMcpHttp.ts";
const require = createRequire(import.meta.url);
const security = require("../electron/bridges/externalMcp/httpSecurity.cjs");
test("HTTP client URL stays on loopback unless a specific listen address is set", () => {
assert.equal(DEFAULT_EXTERNAL_MCP_HTTP_PORT, security.DEFAULT_EXTERNAL_MCP_HTTP_PORT);
assert.equal(
buildExternalMcpHttpUrl("127.0.0.1", 47391),
"http://127.0.0.1:47391/mcp",
);
assert.equal(buildExternalMcpHttpUrl("0.0.0.0", 47391), "");
assert.equal(
buildExternalMcpHttpUrl("0.0.0.0", 47391, "172.22.0.1"),
"http://172.22.0.1:47391/mcp",
);
assert.equal(
buildExternalMcpHttpUrl("172.22.0.1", 47391),
"http://172.22.0.1:47391/mcp",
);
});
test("HTTP snippets use Codex env bearer form, Claude http transport, and generic JSON", () => {
const url = "http://127.0.0.1:47391/mcp";
const token = "abc123";
const toml = buildCodexHttpToml(url);
assert.match(toml, /\[mcp_servers\.netcatty-http\]/);
assert.match(toml, /url = "http:\/\/127\.0\.0\.1:47391\/mcp"/);
assert.match(toml, /bearer_token_env_var = "NETCATTY_MCP_BEARER_TOKEN"/);
assert.equal(buildCodexHttpEnvLine(token), "export NETCATTY_MCP_BEARER_TOKEN='abc123'");
assert.equal(
buildCodexHttpEnvLine("a'b"),
"export NETCATTY_MCP_BEARER_TOKEN='a'\\''b'",
);
assert.equal(
buildClaudeHttpAddCommand(url, token),
"claude mcp add --transport http netcatty-http 'http://127.0.0.1:47391/mcp' --header 'Authorization: Bearer abc123'",
);
const generic = JSON.parse(buildGenericHttpMcpJson(url, token));
assert.equal(generic.mcpServers["netcatty-http"].type, "http");
assert.equal(generic.mcpServers["netcatty-http"].url, url);
assert.equal(generic.mcpServers["netcatty-http"].headers.Authorization, "Bearer abc123");
});