Skip to content

feat(http): /mcp as an OAuth protected resource, metadata and challenge (ADR-0022 slice 15b) - #787

Merged
ajianaz merged 3 commits into
developfrom
feat/agent-metadata-challenge
Dec 5, 2026
Merged

ajianaz merged 3 commits into
developfrom
feat/agent-metadata-challenge

Conversation

@ajianaz

@ajianaz ajianaz commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What

ADR-0022 slice 15b. /mcp becomes an OAuth protected resource. With a public origin and an issuer configured, the server serves the RFC 9728 metadata document at /.well-known/oauth-protected-resource/mcp (and at the root form), and a 401 of /mcp carries WWW-Authenticate: Bearer resource_metadata="<origin>/.well-known/oauth-protected-resource/mcp". whoami gains kind and tiers. Stacked on #783 (slice 15a): the base is feat/principal-aware-policy.

Why

An agent that calls /mcp with no credential has to be told where to authenticate (RFC 9728 and RFC 6750), or an MCP client cannot start the OAuth flow. This is the second third of ADR-0022 slice 15; 15c adds the bearer resolver, so nothing here validates a token yet. whoami says which kind of principal the credential is and which risk tiers it holds, so an agent can tell what the server will let it do before it calls a tool.

The rules that write each byte (the document, the challenges, the quoting, the scope words) are the functions of meldframe-oidc from slice 6-3. This slice builds them once at start from three variables and serves what they wrote, so no request header can change them.

Decisions where the ADR was silent:

  • The metadata sends no Access-Control-* header and a preflight is a 405 (Claude Code is not a browser, as for /mcp).
  • It is cached public, max-age=300 with nosniff and no validator. A HEAD is a GET without a body; every other method is 405 with Allow: GET,HEAD. It spends the per-address budget, and a 429 is never a challenge.
  • Both address forms are served, because the resource has a path.
  • A request with no bearer credential to judge (no header, empty, another scheme, a repeated header, a cookie alone) gets a challenge with no error; a token that was read and refused gets error="invalid_token". The body is the same for both.
  • With the tiers required the challenge asks for offline_access and the three tier words; with off no scope is written.
  • A 503, a 429 and a path outside /mcp carry no metadata challenge. The gate of /mcp is also the fallback of the merged router, so a path no route has is refused by it (401 with no credential, 404 with one) and has been since before this slice; those keep the bare Bearer.
  • With neither variable set there is no resource and the 401 keeps the bare Bearer. One variable without the other, an empty value, or a value that is not valid stops the start and names the variable without repeating the value. A build without the mcp feature does not read the variables.
  • Protected::insufficient_scope writes the 403 challenge and nothing calls it: a refusal of a tool travels inside a 200 of MCP, and 15c decides where a status can carry it.
  • Today a development token is an agent with every tier, so whoami says agent and all three tiers for it, a token named user:bea included: the kind is the credential's, and only a session makes a person.

Changes

  • meldframe-http-agents: Protected (protected.rs: the resource, the document, both challenges and the two routes) and the reading of MELDFRAME_PUBLIC_ORIGIN, MELDFRAME_OIDC_ISSUER and MELDFRAME_TIER_SCOPES (settings.rs). It now depends on meldframe-http, meldframe-oidc, meldframe-auth and axum.
  • meldframe-http: Challenger and Unauthenticated (challenge.rs), McpConfig::challenge, and the 401 split in two (Reply::unauthenticated() and Reply::token_refused()) so the gate can tell the two challenges apart. Status and body do not change.
  • meldframe-principal: Kind::as_str, Tiers::iter and Tiers::names.
  • whoami adds kind and tiers; docs/mcp-tools.json is regenerated and skills/meldframe/SKILL.md follows.
  • apps/server: main.rs reads the configuration, passes the challenger to /mcp and merges the two routes, and prints mcp resource and mcp metadata. Five new test files, two shared helper modules, goldens under tests/golden/agent/, and the test world of slice 15a takes a Wiring with the protected resource, an authenticator and a config.
  • Docs: ADR-0022 built note (with the silences and what 15c must know), docs/epics.md status, docs/operations.md (three variables), docs/architecture.md, AGENTS.md.

Testing

Run with CARGO_INCREMENTAL=0 and a target directory of its own (the disk was short, so the test executables were cleared between runs):

  • cargo test -p meldframe-server: 115 binaries, 777 passed, 0 failed, 1 ignored. New: agent_metadata_routes (12), agent_challenge (9), agent_whoami (5), agent_metadata_env (4, the real binary), and the whoami assertions of surface.rs.
  • cargo test -p meldframe-server --no-default-features: 53 binaries, 358 passed, 0 failed (agent_metadata_off, 2, runs only there: no route, nothing printed, variables not read).
  • cargo test -p meldframe-principal -p meldframe-http -p meldframe-http-agents -p meldframe-mcp-defs -p meldframe-oidc -p meldframe-auth: 40 binaries, 302 passed, 0 failed. New: protected.rs (16), principal (2), auth.rs unit tests (3).
  • Against a real socket and a real rmcp client: 13 forms of a missing credential and 6 of a refused token, in both modes, with the exact header and its grammar; every method of /mcp; hostile Host, X-Forwarded-* and Forwarded headers; no CORS header for any origin or preflight; HEAD, 405 and the per-address budget; whoami for a person and five agents of different caps, called at the same time; the real binary's environment and 18 bad configurations; the golden documents and exchanges.
  • cargo fmt --all --check, cargo clippy -p meldframe-principal -p meldframe-http -p meldframe-http-agents -p meldframe-server -p meldframe-mcp-defs --all-targets -- -D warnings (also with --no-default-features), cargo xtask deps-check, mm-check (also with --base), size-check (also with --base), sh scripts/ci/principal-mint.sh, sh scripts/ci/selftest.sh: clean. tools/check-protocol.sh was not run: no protocol type changed.
  • Mutants run by hand, 6 of 6 caught: the challenge dropped, the challenge on a path outside /mcp, the document built from Host, the document built from X-Forwarded-Host, the challenge built from X-Forwarded-Host, and whoami listing every tier to everyone.
  • git log origin/feat/principal-aware-policy..HEAD --format=%B | sh scripts/ci/message-patterns.sh: clean.

Cora (agent, local): not run as an agent review. cora review --base feat/principal-aware-policy --no-llm found one critical (a static scanner read a doc comment that said "access token: ..." as a hardcoded secret), I reworded the comment in the second commit, and the rerun found no issues (49 files, 4 chunks). cora mcp starts and lists its tools, but cora.review_prepare on the 175 KB diff did not answer within 150 seconds, and no independent reviewer is available here, so no agent review was obtained. The CI Cora review runs on this pull request.

Not covered: nothing validates a bearer token yet (15c); no real client followed the challenge (the rmcp client of the tests only fails to connect, and what Claude Code does with the header is not tested, so E2.2 stays open); no real Gatehouse (the issuer is written into the document and never fetched); the public origin is the operator's word and is not compared with the address the server is reached at; no TLS or HTTP/2 was run; Protected::insufficient_scope has no caller; Postgres and the live sockets were not run through the new seam.


@ajianaz
ajianaz force-pushed the feat/principal-aware-policy branch 2 times, most recently from db16e81 to c7b9ad3 Compare December 5, 2026 01:28
…ge (ADR-0022 slice 15b)

An agent that calls /mcp with no usable credential is now told where to
authenticate. With MELDFRAME_PUBLIC_ORIGIN and MELDFRAME_OIDC_ISSUER set,
the server serves the RFC 9728 document at
/.well-known/oauth-protected-resource/mcp and at the root form, and a 401
of /mcp carries WWW-Authenticate: Bearer resource_metadata="...". A request
with no bearer credential to judge gets no error parameter; a token that
was read and refused gets error="invalid_token". With the tiers required
the scope parameter asks for offline_access and the three tier words.

The address and the document are built once from the configuration, so no
Host or forwarded header can change them. A 503, a 429 and a path outside
/mcp carry no metadata challenge. With nothing set there is no resource and
the 401 keeps the bare Bearer; a half or bad configuration stops the start
and names the variable.

whoami gains kind (person or agent) and tiers (read, change, delete held).
docs/mcp-tools.json and the skill follow.

Nothing validates a bearer token yet (slice 15c).

Signed-off-by: Anaz S. Aji <12648688+ajianaz@users.noreply.github.com>
…dential

Signed-off-by: Anaz S. Aji <12648688+ajianaz@users.noreply.github.com>
@ajianaz
ajianaz changed the base branch from feat/principal-aware-policy to develop December 5, 2026 08:37
@ajianaz
ajianaz force-pushed the feat/agent-metadata-challenge branch from 101a30b to 0e75af3 Compare December 5, 2026 08:37
…d one

Signed-off-by: Anaz S. Aji <12648688+ajianaz@users.noreply.github.com>
@ajianaz
ajianaz merged commit ab63618 into develop Dec 5, 2026
10 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.

1 participant