Repository navigation
✨ feat(http): single shared-instance HTTP MCP server (PR1 — HTTP core) - #13
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
♻️ refactor(core)— extractSharedCore(shared components + lifecycle:initialize / startBackgroundServices / shutdown / killChildrenSync) and a free
registerTools(core, server, clientId)out ofCodeExecutorServer. Pure,behavior-preserving; index.ts 1001 → ~290 lines.
✨ feat(http)—src/http/http-server.ts: Streamable HTTP MCP server on127.0.0.1:<port>/mcp, selected byCODE_EXECUTOR_ROLE=http(env, not argv —bun --compile shifts argv). One
SharedCoreshared across N sessions; each hostgets its own
McpServerbound to a per-sessionStreamableHTTPServerTransport(id isolation + server→client requests like sampling route back to the right host
automatically). Bearer-token auth on
/mcp(on by default), SDK DNS-rebindingprotection (Host allowlist), unauthenticated
GET /health, signal-drivenshutdown that closes sessions then drains the core.
✨ feat(core)— thread a per-session client id into the audit log and rate-limitbucket (was hardcoded
'default') so multiple hosts on the one process areattributed / limited independently; stdio passes
'stdio'.Config (new)
CODE_EXECUTOR_ROLE=http,CODE_EXECUTOR_HTTP_PORT(default 39273),CODE_EXECUTOR_HTTP_AUTH(on unlessoff),CODE_EXECUTOR_HTTP_TOKEN. Host isloopback-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=offon a trusted single-user box (the server refuses tostart with auth on but no token).
Verification
typecheckclean.filesystem.test.tsenv failure remains (1192 passed).orphan-cleanupstdio lifecycle2/2; new
http-serverintegration 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)
brew services.cectlcontrol utility (install/start/stop/status/health/update/logs +token management + host
urlwiring).Note: the repo-wide
claude-reviewcheck is red for every PR due to a missingANTHROPIC_API_KEY/CLAUDE_CODE_OAUTH_TOKENsecret (infra, not this PR). Theauthoritative
Test & Lintgate is what matters.