Skip to content

✨ feat(http): single shared-instance HTTP MCP server (PR1 — HTTP core) - #13

Merged
SWSAmor merged 3 commits into
mainfrom
feat/single-instance-http-server
Jul 24, 2026
Merged

SWSAmor merged 3 commits into
mainfrom
feat/single-instance-http-server

Conversation

@SWSAmor

@SWSAmor SWSAmor commented Jul 24, 2026

Copy link
Copy Markdown
Owner

PR1 of the single-shared-instance effort — the HTTP core. Draft: first of a
multi-PR feature; stdio remains the default so merging changes no current behavior.

Why

Every MCP host (Claude Code, Claude Desktop, Hermes) currently spawns its own
code-executor stdio child, and each one independently spawns the whole downstream
MCP pool (~15 children — the "thundering herd" the fix/mcp-* work has been fighting).
This adds an opt-in HTTP mode so ONE resident process serves every host over a
loopback URL, building the downstream pool once.

Commits

  1. ♻️ refactor(core) — extract SharedCore (shared components + lifecycle:
    initialize / startBackgroundServices / shutdown / killChildrenSync) and a free
    registerTools(core, server, clientId) out of CodeExecutorServer. Pure,
    behavior-preserving; index.ts 1001 → ~290 lines.
  2. ✨ feat(http) — src/http/http-server.ts: Streamable HTTP MCP server on
    127.0.0.1:<port>/mcp, selected by CODE_EXECUTOR_ROLE=http (env, not argv —
    bun --compile shifts argv). One SharedCore shared across N sessions; each host
    gets its own McpServer bound to a per-session StreamableHTTPServerTransport
    (id isolation + server→client requests like sampling route back to the right host
    automatically). Bearer-token auth on /mcp (on by default), SDK DNS-rebinding
    protection (Host allowlist), unauthenticated GET /health, signal-driven
    shutdown that closes sessions then drains the core.
  3. ✨ feat(core) — thread a per-session client id into the audit log and rate-limit
    bucket (was hardcoded 'default') so multiple hosts on the one process are
    attributed / limited independently; stdio passes 'stdio'.

Config (new)

CODE_EXECUTOR_ROLE=http, CODE_EXECUTOR_HTTP_PORT (default 39273),
CODE_EXECUTOR_HTTP_AUTH (on unless off), CODE_EXECUTOR_HTTP_TOKEN. Host is
loopback-only by design.

Security

Loopback bind only; a code-execution endpoint on a TCP port is reachable by any
local process (and browsers via DNS-rebinding), so auth is on by default and the
SDK's DNS-rebinding Host check is enabled. Disable auth only via
CODE_EXECUTOR_HTTP_AUTH=off on a trusted single-user box (the server refuses to
start with auth on but no token).

Verification

  • typecheck clean.
  • Full suite (sandbox-free) shows no regression vs main — only the pre-existing
    filesystem.test.ts env failure remains (1192 passed).
  • Rebuilt binary: stdio closed-stdin still exits 0; orphan-cleanup stdio lifecycle
    2/2; new http-server integration 5/5 (readiness, 401 without/with wrong token,
    authenticated initialize opens a session, session-less non-initialize rejected).

Not in this PR (planned follow-ups)

  • PR2: launchd lifecycle (resident service) + optional lazy/idle downstream pool.
  • PR3: Homebrew tap + prebuilt release-asset binary + brew services.
  • PR4: cectl control utility (install/start/stop/status/health/update/logs +
    token management + host url wiring).
  • User-facing docs (README / config reference) land with the packaging PRs.

Note: the repo-wide claude-review check is red for every PR due to a missing
ANTHROPIC_API_KEY/CLAUDE_CODE_OAUTH_TOKEN secret (infra, not this PR). The
authoritative Test & Lint gate is what matters.

SWSAmor and others added 3 commits July 24, 2026 11:20
Pure, behavior-preserving extraction to create a transport-agnostic seam
for the upcoming shared HTTP server mode. No stdio behavior changes.

- New src/core/shared-core.ts:
  - SharedCore owns the shared components (mcpClientPool, securityValidator,
    connectionPool, rateLimiter, denoAvailable, poolReady, healthCheckServer)
    and their lifecycle: initialize() (config + Deno + rate limiter),
    startBackgroundServices() (background pool init + health server),
    shutdown() (4-phase drain + timeout race, no process.exit — caller owns
    exit), killChildrenSync().
  - registerTools(core, server) as a free function so any McpServer instance
    (stdio today, per-session HTTP later) can register the tools; handlers
    take sampling from the passed server (server.server).
  - handleToolError / checkRateLimit(core) extracted; HealthCheckOutputSchema
    moved. Tool titles/descriptions/schemas relocated verbatim.
- index.ts CodeExecutorServer is now a thin stdio wrapper over SharedCore;
  bootstrap (signal handlers, exit backstop, arg parsing) unchanged. It keeps
  a wrapper-level guard so a duplicate shutdown trigger stays a no-op (matches
  the original P1 shutdownInProgress semantics).

Verified: typecheck; full suite shows no regression vs main (only the
pre-existing filesystem.test.ts env failure); rebuilt binary, closed-stdin
exits 0; orphan-cleanup integration (stdin + ppid) 2/2.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Adds an opt-in HTTP transport so one resident process can serve every MCP
host over a loopback URL, instead of each host spawning its own stdio child
(and its own downstream MCP pool). Selected by CODE_EXECUTOR_ROLE=http (env,
not argv — bun --compile shifts argv); stdio remains the default.

- src/http/http-server.ts: Streamable HTTP MCP server on 127.0.0.1:<port>.
  One SharedCore is built once and shared; each connecting host gets its own
  MCP session (a fresh McpServer bound to a per-session
  StreamableHTTPServerTransport), so id spaces are isolated and server->client
  requests (sampling/elicitation) route back to the originating host via the
  SDK. Bearer-token auth on every /mcp request (on by default), SDK DNS-
  rebinding protection (Host allowlist), unauthenticated GET /health probe,
  and a signal-driven shutdown that closes all sessions then drains the core.
- src/config/loader.ts: CODE_EXECUTOR_HTTP_PORT (default 39273),
  CODE_EXECUTOR_HTTP_AUTH (on unless "off"), CODE_EXECUTOR_HTTP_TOKEN;
  host fixed to loopback.
- src/index.ts: bootstrap dispatches http vs stdio; the stdio server and its
  signal/exit handlers now live in the stdio branch only.
- tests/integration/http-server.test.ts: real-binary test — readiness, auth
  (401 without/with wrong token), authenticated initialize opens a session
  (200 + Mcp-Session-Id), session-less non-initialize rejected (400).

Verified: typecheck; full suite no regression vs main (only pre-existing
filesystem.test.ts); rebuilt binary — stdio closed-stdin still exits 0,
orphan-cleanup 2/2, HTTP integration 5/5.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
registerTools now takes a clientId that is threaded into the audit-log
client id and the rate-limit bucket, replacing the hardcoded 'default'.
In HTTP mode each session passes a distinct id (pre-generated so it also
seeds the transport's sessionIdGenerator), so multiple hosts sharing the
one process are attributed and rate-limited independently; the stdio
transport passes 'stdio' (one host per process).

Verified: typecheck; full suite no regression (only pre-existing
filesystem.test.ts); rebuilt binary — stdio smoke exit 0, HTTP + orphan
integration 7/7.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@SWSAmor
SWSAmor marked this pull request as ready for review July 24, 2026 15:39
@SWSAmor
SWSAmor merged commit 3ee45f2 into main Jul 24, 2026
1 of 2 checks passed
@SWSAmor
SWSAmor deleted the feat/single-instance-http-server branch July 24, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant