Skip to content

feat(session): mcpServers attaches MCP servers to one session on claude and codex - #178

Merged
xxchan merged 1 commit into
mainfrom
session-mcp-servers
Oct 7, 2026
Merged

xxchan merged 1 commit into
mainfrom
session-mcp-servers

Conversation

@xxchan

@xxchan xxchan commented Oct 7, 2026

Copy link
Copy Markdown
Member

First PR for #170, following the shape Cindy decided: the contract, plus claude and codex. Cursor, pi and the ACP runtimes come in PR 2. Until then they refuse the option.

Contract

  • McpServer: stdio {name, command, args?, env?} or http {name, type: "http", url, headers?}, the exact union from the issue. It lives in the new contracts/session-options.ts together with SessionOptions, because contracts/session.ts had reached the 300-line lint limit. It is still re-exported from session.ts and the root.
  • SessionOptions.mcpServers?: readonly McpServer[] applies to that session only, on top of the runtime's own servers. It is not remembered, so it has to be passed again on resume. refuseSessionOptions / refusedSessionOptions now cover mcpServers (a non-empty list): cursor, grok, kimi, opencode, antigravity, pi and morph refuse it with UnsupportedOptionError.
  • The open fails with a plain error, before anything starts, for a list with an empty or repeated name.

claude

  • Passes --mcp-config <file> and no --strict-mcp-config. The same flag goes on --resume.
  • The file is 0600, in a fresh temp directory, and is removed when the process exits (new claude/launch.ts).
  • Measured: system/init lists each server as source: "dynamic". A user-scope server with the same name is replaced for that process, and the user's other servers still load. A resume without the flag lists no servers.

codex

  • Sends config.mcp_servers on both thread/start and thread/resume, nested under one key, because a dotted key would split a name that contains ..
  • Measured: codex merges overrides into config.toml field by field. A user entry with enabled = false silently disabled the session's server, and a user url under a session command failed the open. oar therefore always sets args and enabled = true. Names outside ^[a-zA-Z0-9_:@/.-]+$ are refused before codex starts.
  • A resume that carries the override kept the thread's own model and effort (0.160.1, aimock).
  • Error texts (codex messages, stderr tail) go through a redactor.

Evidence

  • tests/fixtures/echo-mcp-server.mjs (stdio, or --http) answers echo:<text> via=<transport> token=<sha256 prefix of its credential>. Only that process writes this string.
  • sea-trial/vendor/mcp-servers.vendor.test.ts runs the real claude and codex CLIs against aimock. The model calls a stdio and an http server, on open and on resume, and the provider receives the server's echo. The same tests cover the name clash and check that no record contains a credential.
  • codex offers MCP tools as a Responses namespace tool, which aimock cannot script, so the test proxy rewrites flat mcp__<server>__<tool> calls (namespaceMcpToolCalls).
  • Replay fixtures: claude-mcp-echo and codex-mcp-echo, recorded through oar's own session (pnpm sea-trial:record <rt>-aimock mcp-echo -).
  • Docs: the spec README, the runtime matrix (refusals and channels), claude.md, codex.md and both READMEs.

CI=1 pnpm run check is green, and the claude-aimock and codex-aimock behavior and vendor suites pass locally.

🤖 Generated with Claude Code

…de and codex

Hosts (Ferry's `ferry mcp`, `oar mcp` itself) need a tool server in every
agent session without editing each runtime's config files. SessionOptions
gains `mcpServers?: readonly McpServer[]` in ACP's shape (stdio
{name, command, args?, env?} | http {name, type: "http", url, headers?}),
for that session only, on top of the runtime's own servers, given again on
resume. This is the first PR of #170: the contract, claude and codex.

- claude: `--mcp-config <file>`, no `--strict-mcp-config`, also on
  `--resume`. The file is 0600 in a fresh temp directory and removed when
  the process exits: argv would show the credentials to `ps`.
- codex: `config.mcp_servers` on thread/start and thread/resume, nested
  under one key (a dotted key would split a name with a `.`). codex merges
  overrides into config.toml field by field, so oar sets `args` and
  `enabled = true` to keep a same-named user entry from disabling or
  changing the session's server. Names codex would not start are refused
  before it starts.
- Every other runtime refuses a non-empty list (UnsupportedOptionError,
  declared in refusedSessionOptions) until its channel is verified.
- env and headers values never reach a record or an error: neither CLI
  echoes them in its frames, and codex's error texts pass a redactor.

Evidence: a tiny echo MCP server (tests/fixtures/echo-mcp-server.mjs) whose
tool result only it can write; vendor tests run the real CLIs against aimock
with the model calling a stdio and an http server, on open and on resume,
plus the name-clash behaviour; replay fixtures claude/codex-mcp-echo. codex
offers MCP tools as a Responses `namespace` tool, which aimock cannot
script, so the test proxy namespaces flat `mcp__<server>__<tool>` calls.

Refs #170

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@xxchan
xxchan merged commit fa92104 into main Oct 7, 2026
13 checks passed
@xxchan
xxchan deleted the session-mcp-servers branch October 7, 2026 05:53
@xxchan xxchan mentioned this pull request Oct 7, 2026
xxchan added a commit that referenced this pull request Oct 7, 2026
Release OAR and CLI 0.31.0. This PR changes only the two package version
fields.

**New**
- `SessionOptions.mcpServers` (#178, part 1 of #170): MCP servers
attached to one session, in ACP's `McpServer` shape (stdio `{name,
command, args?, env?}` or http `{name, type: "http", url, headers?}`; no
SSE). They are added on top of the runtime's own servers, never written
to its configuration, and must be given again on `resume`. `env` and
`headers` values never reach a record, event or error.
- claude: `--mcp-config` from a 0600 file in a fresh 0700 directory,
deleted when the process ends; a same-named user server is replaced for
that session.
- codex: `thread/start` / `thread/resume` config overrides; codex merges
a same-named user entry field by field, so OAR always sets `args` and
`enabled = true`; names outside `^[\w:@/.-]+$` are refused before codex
starts.
- Every other runtime (cursor, pi, grok, kimi, opencode, antigravity,
morph) refuses a non-empty list with `UnsupportedOptionError` until its
channel is verified.
- opencode `systemPrompt` / `appendSystemPrompt` (#179, closes #171 for
opencode): carried through opencode's own `OPENCODE_CONFIG_CONTENT`.
`systemPrompt` replaces the prompt of the agent the session actually
uses (the saved agent on resume), verified against the mode opencode
reports; `appendSystemPrompt` adds a session-owned instructions file
after the user's own. Refused when `OPENCODE_CONFIG_CONTENT` is already
set, for an empty `systemPrompt`, and for a saved agent that no longer
exists. Prompt text is passed literally (`{env:…}` / `{file:…}` are not
expanded).

Validation: all 13 checks passed on the final heads of #178 (`da06902`)
and #179 (`41c9761`); claude and codex MCP attachment run against the
real CLIs with stdio and http echo servers. After this PR passes CI and
merges, tag its exact squash commit as `v0.31.0` to trigger npm trusted
publishing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: exe.dev user <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
xxchan added a commit that referenced this pull request Oct 7, 2026
…#181)

Second PR for #170, on top of #178 (the contract plus claude and codex)
and rebased onto #179 (opencode prompts). `mcpServers` now works on
grok, kimi, opencode, antigravity and pi. Antigravity refuses entries
that carry credentials. Cursor still refuses the option, because only a
paid run against Cursor's backend could show its agent calling the tool.

## ACP: grok, kimi, opencode, antigravity
- The entries go out as ACP `mcpServers` on `session/new`,
`session/load` and `session/resume` (`shared/acp/mcp-servers.ts`), where
oar used to send `[]`. Stdio is `{name, command, args, env: [{name,
value}]}` and http is `{type: "http", name, url, headers: [...]}`.
`args`, `env` and `headers` are always sent, because opencode reads them
without checking.
- An http entry is refused with `UnsupportedOptionError` on `mcpServers`
when `initialize` doesn't declare `mcpCapabilities.http`. All four
agents declare it.
- An empty or repeated name fails before the agent starts. When an open
fails, credentials are removed from the error's message and stack.
- No agent keeps the servers across a resume. Resumed without them, none
of the four attaches any; given again, they attach.
- grok's `--plugin-dir` launch flag also works, but it isn't used: the
ACP param does the job, and launch flags belong to #171.

| runtime | how the model reaches the tool | on a name clash with the
user's config |
|---|---|---|
| grok 1.0.46 | through its `use_tool` meta-tool, named
`<server>__<tool>` | the session's server wins; the user's other servers
stay |
| kimi 2.1.1 | `mcp__<server>__<tool>` | the session's server wins; the
user's other servers stay |
| opencode 1.18.30 | `<server>_<tool>` | the session's server replaces
the user's for that process. A session server that fails to start also
removes the user's (probe) |
| antigravity 1.3.0 | through `call_mcp_tool`; servers start at the
first prompt | the session's server wins; the user's other servers stay
|

**Antigravity refuses entries with credentials.** The server saves the
session's entries in plain text, `env` and header values included, in
`$GEMINI_HOME/antigravity-acp/conversations/<id>.db` (mode 0600). That
file outlives the session, and the host can't see it. So an entry with a
non-empty `env` or `headers` is refused with `UnsupportedOptionError` on
`mcpServers` before the server starts. Entries without credentials
attach as usual (`refuseAntigravityMcpCredentials`).

**opencode prompts together with MCP:** opencode no longer declares any
refused option. Its prompt path from #179 passes `mcpServers` to the ACP
open next to `OPENCODE_CONFIG_CONTENT`. #179's two "prompt and MCP
together are refused" regression tests now check that both reach the
open. The opencode-aimock vendor test runs a replaced prompt, an
appended prompt and both echo servers in one session: every agent
request carries both prompts, and both echoes arrive.

**Already there before this PR:** grok's `_x.ai/mcp/servers_updated`,
which oar records verbatim, includes the `env` values of stdio servers
from the user's own `config.toml`. Values passed through `mcpServers`
never show up there. Tracked separately (#182).

## pi
- pi's built-in MCP extension (`createMcpExtension`) is loaded with no
`mcp.json` of its own, plus one inline extension that calls
`pi.registerMcpServer` (`runtimes/pi/mcp.ts`). This is how the pi CLI
itself connects servers.
- I picked this over wrapping `@earendil-works/pi-mcp`'s client as
custom tools: pi keeps its own tool naming, rendering, reconnects and
lifecycle, and no new dependency is needed.
- Tools are declared to the model directly (`exposure: "direct"`). pi's
default exposure needs the codemode extension, which oar doesn't load.
- The SDK connects servers on `session_start` and closes them on
`session_shutdown`, and oar's opener emitted neither before. A session
given servers now calls `bindExtensions({})`, and emits
`session_shutdown` before dispose, so stdio servers exit (measured). One
side effect: in such a session the user's own extensions also receive
`session_start`. Emitting these events for every session is tracked
separately (#183).
- pi treats an `env` or header value starting with `!` as a shell
command and expands `$NAME`. oar escapes every `$` and `!` so the value
arrives literally; a unit test round-trips values through pi's own
resolver.
- A stdio server gets the host's environment, then `SessionOptions.env`,
then the entry's own `env` on top.
- Names: pi only accepts `[A-Za-z0-9_-]`, and it would merge `a-b` and
`a_b` into one tool namespace. Both cases are refused before the session
opens. A name another extension already registered makes the open fail,
rather than running without the server.
- oar's pi never loaded the user's `mcp.json`, and it still doesn't, so
there is no clash with it.

## cursor (still refused)
The refusal reason now says why. Cursor's agent runs on Cursor's
servers. Against a hand-written stand-in for that backend (not in the
repo), the SDK started the server at the first `send` and ran a
`tools/call` when the backend asked for it.

The missing evidence is a live run that shows Cursor's real agent
choosing the tool, and the `tool_call` deltas oar records for that call.
Getting it would take a login and paid tokens, so it wasn't done.
cursor.md lists what such a run has to show.

## Evidence
- `sea-trial/vendor/mcp-servers-acp.vendor.test.ts` runs the real grok,
kimi, opencode and agy_acp_server CLIs.
`sea-trial/vendor/mcp-servers-pi.vendor.test.ts` runs the bundled pi
SDK. Both use aimock, with no logins and no tokens.
- In each test, the model calls a stdio and an http echo server on open,
then calls the stdio one again after a resume in a new process. The
provider receives `echo:<text> via=<transport> token=<credential
fingerprint>`, which only the echo server writes.
- The same tests cover the name clash: the user's `echo` and `userecho`
against the session's `echo`, with each echo's fingerprint showing which
server answered. They also check that no record contains a credential.
- `sea-trial/harness/aimock-acp.ts` points each ACP CLI at aimock from a
fresh home. antigravity uses the public Gemini API with a dummy key and
`GOOGLE_GEMINI_BASE_URL`, and the test proxy hands its role-`model` tool
results to aimock as `user` (`geminiToolResultsAsUser`).
- The pi test runs in CI's pi-aimock job. The ACP tests are
`OAR_TEST`-gated and were run locally on all four
(`OAR_TEST=<rt>-aimock`; antigravity also needs `OAR_ANTIGRAVITY_BIN`),
because CI has no ACP aimock backend.
- `tests/acp/acp-session-mcp-servers.test.ts` covers the ACP wire shape
on new and resume, the http refusal, name checks, the redacted open
error and antigravity's credential refusal, against the fake ACP agent.
`tests/pi/pi-mcp-servers.test.ts` covers the pi config mapping and
escaping.
- Replay fixture `pi-mcp-echo`, recorded with `pnpm sea-trial:record
pi-aimock mcp-echo -`.
- The echo scenario helpers moved into `vendor/support/echo-mcp.ts`.
Prompts now match on the current turn's user messages, because kimi
follows the prompt with a `<system-reminder>`. I re-ran the
claude-aimock and codex-aimock MCP vendor tests and they pass.

Docs: the runtime matrix (refusals and channels), the contract comment,
both READMEs, and a "Session MCP servers" section on each of the grok,
kimi, opencode, antigravity, pi and cursor pages.

`CI=1 pnpm run check` is green. The pi-aimock behavior suite and vendor
suite pass, and so do the MCP vendor tests on all seven aimock backends.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: exe.dev user <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
xxchan added a commit that referenced this pull request Oct 7, 2026
…ows (#185)

The claude `mcpServers` name-clash vendor test (#178) removes its
`CLAUDE_CONFIG_DIR` scratch directory right after the session ends. On
the Windows claude-aimock job, claude and the user-scope MCP servers it
started can still be writing there as they exit, so `rmdir` failed with
`ENOTEMPTY` twice on the v0.32.0 release PR (#184, run 37586620255). The
assertions before it passed; only the cleanup failed.

This retries the removal (`maxRetries: 10, retryDelay: 300`), the same
as `sea-trial/harness/aimock.ts` does for codex's home. Test-only
change; no library code changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: exe.dev user <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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