Skip to content

MCP Hub Architecture

The MCP Hub (apps/mcp-hub) is a single Railway service that hosts many MCP servers as subprocesses. This page is the technical reference — for the admin guide, see MCP Hub (portal).

The problem it solves

Before the hub, every new MCP needed:

  • Its own Railway service (railway add)
  • Its own Dockerfile
  • Its own env vars
  • Its own deploy / logs / monitoring
  • Developer team involvement

The operational cost scaled linearly with the number of MCPs. Most OSS MCPs (pandoc, time, filesystem, sqlite, fetch) are tiny stdio-based packages that don’t need isolated containers — they just need a process to spawn.

The architecture

Customer (Claude Desktop)
│ OAuth + X-API-Key: ltg_*
▼
LTG Gateway (apps/mcp-server) ← unchanged
│
│ routes by toolPrefix. For hub-hosted entries,
│ calls http://mcp-hub.railway.internal:4000/hosted/<slug>/mcp
▼
MCP Hub (apps/mcp-hub) ← single Railway service
│
│ streamable-HTTP IN → bridge → stdio OUT
│
├─ subprocess 1: uvx mcp-pandoc (stdio)
├─ subprocess 2: npx @mcp/server-fs ... (stdio)
├─ subprocess 3: npx @mcp/server-sqlite (stdio)
└─ ...

The gateway doesn’t know or care whether an upstream is a dedicated Railway service or a hub-hosted slug — it just sees an entry in the MCP Servers registry and calls serverUrl.

Request lifecycle

A customer tool call to fs_list_directory goes through:

  1. Customer → Gateway: POST https://mcp-server-production-ba1f.up.railway.app/mcp with OAuth + ltg_* key
  2. Gateway → Portal /api/gateway/validate: per-tool permission check (mcpServerId + toolName)
  3. Gateway → Hub: POST http://mcp-hub.railway.internal:4000/hosted/filesystem/mcp with a tools/call JSON-RPC message
  4. Hub bridge: receives the streamable-HTTP request, gets or spawns the filesystem subprocess
  5. Hub → subprocess: forwards the tools/call message to the subprocess’s stdin
  6. Subprocess → Hub: writes response to stdout
  7. Hub → Gateway: streamable-HTTP response
  8. Gateway → Customer: final response

Steps 3–7 all happen inside Railway’s internal network.

Subprocess pool

Implemented in apps/mcp-hub/src/pool.ts. Key properties:

Property Value
Idle timeout 15 min (subprocess killed after no calls)
Cold start ~200ms for cached packages, 2–30s on first-ever npx/uvx resolve
Crash threshold 3 non-zero exits in 60s → marks MCP as crashed, requires admin restart
stderr capture Last 4KB kept per subprocess, included in lastError when crashed
Concurrent requests Serialized per subprocess — the MCP SDK’s stdio transport is not concurrent
interface HostedRecord {
slug: string;
name: string;
installSpec: { type, package, args?, env? };
env?: Record<string, string>; // supports @credential:<uuid> sentinels
status: 'installing' | 'running' | 'stopped' | 'crashed';
crashes: number[]; // timestamps, windowed
lastStarted?: number;
lastUsed?: number;
lastError?: string; // includes stderr tail on failure
}

The stdio ↔ streamable-HTTP bridge

Implemented in apps/mcp-hub/src/bridge.ts.

When a gateway request hits /hosted/<slug>/mcp:

  1. Build a low-level MCP Server on the fly
  2. Register raw request handlers for ListToolsRequestSchema and CallToolRequestSchema — no high-level .tool() API (which validates against a schema and can drop unknown args)
  3. Get or spawn the upstream subprocess via the pool, which returns an MCP Client connected over stdio
  4. Forward tools/list and tools/call requests from the client-facing server to the upstream client verbatim
  5. Stream the response back

Why raw handlers: some upstream MCPs declare an empty inputSchema.properties: {} but enforce required params server-side (mcp-pandoc does this). The high-level .tool(name, desc, schema, handler) API of the MCP SDK validates against schema and strips unknown args before invoking the handler. Raw request handlers bypass that validation and pass args through to the subprocess verbatim.

Credential resolution

Hosted MCPs often need vendor tokens (GitHub PAT, Brave API key) as env vars. The hub never stores plaintext secrets — it stores sentinels that resolve to vault entries at spawn time.

POST /admin/install
{
"catalogSlug": "github",
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "@credential:2a4f-…"
}
}

At spawn time, credentials.ts:

  1. Iterates the env map
  2. For each value starting with @credential:, extracts the UUID
  3. Calls GET <portal>/api/credentials/<uuid>/reveal with the hub’s PORTAL_INTERNAL_TOKEN (internal-only endpoint)
  4. Substitutes the plaintext secret into the env
  5. Passes the resolved env to the subprocess

Resolved secrets are cached in memory for 2 minutes per subprocess spawn. On credential rotation, the cache automatically picks up the new value on the next cold spawn.

Vault reveal endpoint

The portal exposes GET /api/credentials/:id/reveal — requires X-Internal-Token matching INTERNAL_SERVICE_TOKEN. Every call is audit-logged as credential.revealed.

Token model

The hub holds two separate tokens:

Var Purpose
INTERNAL_SERVICE_TOKEN Inbound — validates admin callers hitting /admin/*
PORTAL_INTERNAL_TOKEN Outbound — used when the hub calls portal endpoints (/api/mcp-servers, /api/credentials/:id/reveal)

These were originally the same token, but per-service isolation is safer: if hub creds leak, the attacker can’t also impersonate the portal to other services.

Install pipeline

install.ts pre-downloads packages so first-request latency is low:

Install spec type Command
uvx / pip uv tool install <package>
npx / npm npm install --prefix /var/cache/mcp-hub/npm-global -g <package>
github git clone --depth 1 <url> /var/cache/mcp-hub/repos/<name>

Pre-install is best-effort. Even if it fails, npx -y and uvx will still resolve on demand at spawn time. Install results include a 2KB stdout + stderr tail for debugging.

Auto-registration with the portal

After a successful install + test spawn, the hub POSTs to the portal’s MCP Servers registry:

POST <portal>/api/mcp-servers
Headers: X-Internal-Token: <PORTAL_INTERNAL_TOKEN>
Body:
{
"name": "Pandoc",
"description": "Hosted via MCP Hub — slug: pandoc",
"serverUrl": "http://mcp-hub.railway.internal:4000/hosted/pandoc/mcp",
"transport": "streamable-http",
"auth": { "type": "none" },
"gatewayEnabled": true,
"toolPrefix": "pandoc_"
}

Then triggers discover via POST /api/mcp-servers/:id/discover. From this point on, the hub-hosted MCP is indistinguishable from a dedicated Railway MCP in the registry.

Dockerfile runtime stack

The hub image (apps/mcp-hub/Dockerfile) pre-installs:

  • Bun 1.3 (for running the hub itself)
  • Node 20 (for npm/npx-based MCPs)
  • Python 3 + uv + uvx (for PyPI-based MCPs)
  • Pandoc, ImageMagick, sqlite3 (system binaries used by common MCPs)
  • Git + curl + ca-certificates (for GitHub installs, healthchecks, HTTPS)

Persistent volume mounts at /var/cache/mcp-hub/ contain uv, npm, sandbox, sqlite, and repos subdirectories — preserved across deploys.

Security boundary

  • Public internet → Hub: the hub should have no public Railway domain in production. All traffic is via mcp-hub.railway.internal:4000 from the gateway and portal.
  • Hub → Portal: uses PORTAL_INTERNAL_TOKEN, revokable independently.
  • Subprocess → Outside world: subprocesses inherit the hub container’s network. If you need per-MCP egress policy, use a dedicated Railway service instead.
  • Subprocess → Filesystem: subprocesses share the hub’s filesystem. Sandboxed by MCP choice — e.g. filesystem MCP enforces its own --allowed-dirs argument.
  • Subprocess crash isolation: a crashed subprocess doesn’t affect others because each call gets its own connection. The pool’s exit handler updates state; future requests respawn cleanly.

Deployment

Because the hub is a separate service in the same monorepo, Railway can accidentally pick the wrong Dockerfile. To prevent this:

  • apps/mcp-hub/railway.json pins builder: DOCKERFILE and dockerfilePath: Dockerfile
  • RAILWAY_ROOT_DIRECTORY=apps/mcp-hub on the service
  • Always deploy via cd apps/mcp-hub && railway up --service mcp-hub -d — not from the repo root

If the hub service suddenly serves portal-shaped /health ({authDb, postgres, s3}), the wrong Dockerfile was used — see DEPLOY.md in apps/mcp-hub/.