Skip to content

Configure the operational-files directory via McpConfig - #138

Merged
aterga merged 2 commits into
security/dcr-limits-atomic-storefrom
feat/mcpconfig-state-dir
Aug 18, 2026
Merged

aterga merged 2 commits into
security/dcr-limits-atomic-storefrom
feat/mcpconfig-state-dir

Conversation

@aterga

@aterga aterga commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

What

imcp2's only persistent operational file — the dynamic-client-registration store — was located by reading OAUTH_CLIENTS_FILE deep inside the auth module (clients_file()), a process-global env lookup the library did for itself. Wrong layer: the embedding application should decide where imcp2 writes its state, the way it already injects the Agent.

This adds McpConfig.state_dir: PathBuf — the directory imcp2 creates its operational files in — and threads it through so the location has one configured home (and future operational state isn't scattered).

Changes

  • SharedClients::load(state_dir) loads/persists {state_dir}/oauth-clients.json. ClientStore now owns its resolved file path, so the coalesced write-through needs no global/env lookup.
  • McpServer stores state_dir and exposes state_dir(), so an embedder can place its own operational files under the same home.
  • The library reads no path from the environment. The imcp2 binary reads the directory from IMCP2_STATE_DIR (default: the working directory, preserving the old ./oauth-clients.json default). The systemd unit points it at StateDirectory — so the store still lands at exactly /var/lib/imcp2/oauth-clients.json (no data migration).
  • persist_clients_to / load_clients_from take &Path (non-UTF-8 safe); the atomic-replace temp path is derived on OsString.
  • The e2e handshake test and router tests inject a throwaway directory instead of overriding a process-global env (the e2e test's env guard becomes a temp-dir guard).
  • README, module docs, the directory-submission doc, and the unit file move to the directory model.

Behavior / compatibility

  • Deployed store path is unchanged (/var/lib/imcp2/oauth-clients.json).
  • The env var renames OAUTH_CLIENTS_FILE (full file path) → IMCP2_STATE_DIR (directory); the unit file is updated in the same change, and a bare local run still defaults to ./oauth-clients.json.

Tests

176 lib + router + integration tests pass; --features e2e compiles; clippy clean on the changed files.

Note on base

Stacked on #137 (security/dcr-limits-atomic-store) because it builds directly on that PR's atomic, path-taking persistence helpers — basing on main would duplicate and conflict with them. Retarget to main once #137 merges. Happy to make it independent instead if you'd prefer.

🤖 Generated with Claude Code

imcp2's only persistent operational file — the dynamic-client-registration
store — was located by reading `OAUTH_CLIENTS_FILE` deep inside the auth
module (`clients_file()`), a process-global env lookup the library
performed for itself. That is the wrong layer: the embedding application,
not a global env var, should decide where imcp2 writes its state.

Introduce `McpConfig.state_dir: PathBuf` — the directory imcp2 creates its
operational files in — and thread it through:

  * `SharedClients::load(state_dir)` loads/persists
    `{state_dir}/oauth-clients.json`; `ClientStore` now owns its resolved
    file path, so the coalesced writer needs no global lookup.
  * `McpServer` stores `state_dir` and exposes `state_dir()`, so an
    embedder can place its own operational files under the same home and
    future imcp2 state has one configured location (not scattered files).
  * The library no longer reads any path from the environment. The `imcp2`
    binary reads the directory from `IMCP2_STATE_DIR` (default: the working
    directory, preserving the old `./oauth-clients.json` default) and the
    systemd unit points it at `StateDirectory` (`/var/lib/imcp2`), so the
    store still lands at the same `/var/lib/imcp2/oauth-clients.json`.

`persist_clients_to`/`load_clients_from` take `&Path` (non-UTF-8 safe) and
the atomic-replace temp path is derived on `OsString`. The e2e handshake
test and router tests inject a throwaway directory instead of overriding a
process-global env. README, the module docs, the directory-submission doc,
and the unit file are updated to the directory model.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@aterga
aterga requested a review from a team as a code owner August 18, 2026 12:08
@aterga
aterga requested a balanced review from Copilot August 18, 2026 12:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Configures imcp2 operational storage through McpConfig.state_dir, removing library-level environment lookup.

Changes:

  • Persists OAuth clients under <state_dir>/oauth-clients.json.
  • Exposes the configured state directory through McpServer.
  • Updates deployment configuration, tests, and documentation.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/auth.rs Makes client persistence path-based.
src/lib.rs Adds and exposes state_dir.
src/main.rs Reads IMCP2_STATE_DIR.
src/e2e_handshake.rs Uses an isolated state directory.
src/metrics.rs Supplies state configuration in tests.
tests/routers.rs Migrates router tests to directory storage.
README.md Documents state-directory configuration.
docs/anthropic-directory-submission.md Updates storage disclosure.
deploy/native/imcp2.service Points state storage at systemd’s directory.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/lib.rs Resolved
Comment thread src/main.rs Resolved
Comment thread src/auth.rs Resolved
Addresses the PR review:

* `state_dir` and `clients` were two independent fields that could
  disagree — a caller could load `clients` from directory A while setting
  `state_dir` to B, and `McpServer::state_dir()` would then report B while
  files went to A. Fixed by making the client store the single source of
  truth: `ClientStore` now owns the operational DIRECTORY (not a file
  path), `SharedClients::state_dir()` surfaces it, and
  `McpServer::state_dir()` reports it straight from the store. Construction
  asserts `config.clients` was loaded from `config.state_dir`, so a
  mismatch fails loudly instead of letting the getter lie. New tests cover
  both the happy path and the panic.

* Reworded the `IMCP2_STATE_DIR` default comment: the default is the
  process's current working directory, not necessarily the executable's
  directory ("beside the binary" was inaccurate).

* Fixed the indentation of the edited client-store test seam. (The tree is
  formatted with a different rustfmt line width than this toolchain's
  default, so `cargo fmt --all` would rewrite unrelated files; hand-matched
  the surrounding style instead. CI does not gate `cargo fmt --check`.)

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@aterga
aterga merged commit a8e3040 into security/dcr-limits-atomic-store Aug 18, 2026
2 checks passed
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.

3 participants