Skip to content

feat: OpenAI-compat /v1 proxy + managed remote MCP server entry#82

Merged
chrysb merged 3 commits into
chrysb:mainfrom
luckyPipewrench:feat/openai-compat-and-remote-mcp
Jun 1, 2026
Merged

feat: OpenAI-compat /v1 proxy + managed remote MCP server entry#82
chrysb merged 3 commits into
chrysb:mainfrom
luckyPipewrench:feat/openai-compat-and-remote-mcp

Conversation

@luckyPipewrench

Copy link
Copy Markdown
Contributor

Summary

Adds two opt-in features that let AlphaClaw front an OpenClaw deployment for OpenAI-compatible clients and for OpenClaw-managed remote MCP servers, without anyone hand-editing openclaw.json.

Both default off. Existing deployments are unaffected until the relevant env vars are set.

1. OpenAI-compatible /v1/* proxy

Bearer-auth proxy from AlphaClaw's public Express to the loopback OpenClaw gateway:

  • POST /v1/chat/completions
  • POST /v1/responses
  • POST /v1/embeddings
  • GET /v1/models, GET /v1/models/<id>

Lets a self-hosted backend use this AlphaClaw deployment as its OpenAI-compatible endpoint without exposing OpenClaw's gateway port directly.

  • Requires Authorization: Bearer <OPENCLAW_GATEWAY_TOKEN>. AlphaClaw enforces the header is present; OpenClaw validates the value.
  • Setup cookies stripped before forwarding.
  • Hop-by-hop response headers and Set-Cookie stripped on the way back out.
  • Content-Encoding stripped on forwarded requests because Express has already inflated the body.
  • gateway.http.endpoints.chatCompletions.enabled and responses.enabled are set in managed-onboarding writes AND backfilled on every gateway start, so existing managed deployments pick up the flags on image bump.

2. Env-driven managed mcp.servers.<name> entry

When REMOTE_MCP_URL + REMOTE_MCP_API_TOKEN are set, AlphaClaw writes a streamable-http MCP server block to openclaw.json and keeps it in sync at every gateway start.

Env var Default Purpose
REMOTE_MCP_URL unset Upstream MCP endpoint.
REMOTE_MCP_API_TOKEN unset Bearer token. Persisted as ${REMOTE_MCP_API_TOKEN}, not plaintext.
REMOTE_MCP_NAME remote Key under mcp.servers.<name>. Validated ^[A-Za-z0-9_-]{1,64}$; __proto__/constructor/prototype rejected.
REMOTE_MCP_PROXY_URL unset If set, OpenClaw connects here instead of REMOTE_MCP_URL. For same-host scanning proxies.
  • Idempotent: only rewrites when something actually changes.
  • Plaintext rewrite: any existing plaintext Authorization value gets re-scrubbed to the ${REMOTE_MCP_API_TOKEN} placeholder on next run.
  • Rename cleanup: managed entries are tagged _alphaclawManaged: true. If REMOTE_MCP_NAME changes, the prior managed entry is removed. Unmarked (user-managed) entries are never touched.
  • When the env vars are unset, the managed entry is removed.

Security boundary

/v1/chat/completions is an operator-access surface on OpenClaw's side: anyone holding OPENCLAW_GATEWAY_TOKEN can run any tool the agent profile allows. The README's "Security Notes" section is updated to call this out and to recommend the surface be used only for trusted server-to-server callers.

Tests

Test count moves from 619 to 637. New cases cover the /v1/* proxy (bearer-auth gate, body re-serialize, model-list path, Set-Cookie strip, hop-by-hop strip, Content-Encoding strip) and the managed MCP entry (write with placeholder, route via proxy URL, plaintext scrub, name validation + reserved-key rejection, rename cleanup, user-entry preservation, idempotency, remove-on-unset, gateway-start backfill).

npm test clean against Node 22. npm run build:ui unaffected.

Backwards compatibility

Defaults unchanged: /v1/* route only registers when configured, no managed MCP entry without env vars, no schema breaks. Existing tests still pass. The only addition to managed config writes is enabling the chat-completions and responses endpoints (the precondition for #1 to be useful).

@chrysb chrysb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review notes from the AlphaClaw pass. The focused and full test suites pass locally, but I think the two P1 items should be addressed before merge.

Comment thread lib/server/gateway.js Outdated
Comment thread lib/server/routes/proxy.js Outdated
Comment thread lib/server/routes/proxy.js
Two opt-in features for AlphaClaw, both default off; existing
deployments unaffected.

- /v1/chat/completions, /v1/responses, /v1/embeddings, /v1/models[/<id>]
  bearer-auth proxy to the loopback gateway. Strips setup cookies +
  hop-by-hop response headers + Set-Cookie + Content-Encoding. Enables
  gateway.http.endpoints.chatCompletions and responses at managed
  onboarding AND backfills on every gateway start.

- mcp.servers.<REMOTE_MCP_NAME> entry driven by REMOTE_MCP_URL +
  REMOTE_MCP_API_TOKEN + REMOTE_MCP_PROXY_URL. Token persisted as
  ${REMOTE_MCP_API_TOKEN} reference, not plaintext. Name validated
  ([A-Za-z0-9_-]{1,64}, reserved keys rejected). Stale managed
  entries cleaned up on rename via _alphaclawManaged marker.

89 test files, 637 passing.
@luckyPipewrench luckyPipewrench force-pushed the feat/openai-compat-and-remote-mcp branch from 9ed614e to 6b96231 Compare May 26, 2026 10:18
@luckyPipewrench

Copy link
Copy Markdown
Contributor Author

Addressed review notes

@chrysb chrysb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good after the explicit API exposure toggle and local verification.

@chrysb chrysb merged commit 80a078d into chrysb:main Jun 1, 2026
@luckyPipewrench luckyPipewrench deleted the feat/openai-compat-and-remote-mcp branch June 1, 2026 18:29
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.

2 participants