Skip to content

Consent-page domain sign-in, stateless MCP transport, unified II instance - #8

Merged
aterga merged 2 commits into
mainfrom
claude/zealous-archimedes-qi8b42
Jun 17, 2026
Merged

aterga merged 2 commits into
mainfrom
claude/zealous-archimedes-qi8b42

Conversation

@aterga

@aterga aterga commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Three fixes found while getting the ChatGPT connector working end-to-end (Claude already worked).

1. Consent-page domain sign-in

The domain sign-in link was bound to the browser that did OAuth via an mcp_session cookie. Connector clients (ChatGPT, Claude) run OAuth in an isolated/ephemeral in-app browser, so when the user later opens the generated /signin/<link> in their normal browser it has no matching cookie → "this sign-in link belongs to a different session."

New flow:

  • GET /signin/<link> consumes the single-use link and redirects to II from any browser (no cookie check).
  • After II login, the verified delegation is staged pending rather than stored.
  • New /signin/confirm page shows the verified principal + target domain and requires an explicit "Confirm and connect" before the delegation lands in the requesting session.

Informed consent replaces the cookie as the anti-phishing control: a victim phished into opening someone else's link must knowingly approve connecting their identity to the requesting assistant session. The SameSite=None flow cookie still keeps the II round-trip + confirm in one browser.

2. Stateless + JSON MCP transport

rmcp's Streamable HTTP defaults to stateful_mode: true + SSE responses. ChatGPT's connector client doesn't complete that SSE/session handshake — sessions closed immediately (input stream terminated → quit_reason=Closed) and the UI hung on "Connecting to app". Our two tools are pure request/response, so the server now runs with_stateful_mode(false).with_json_response(true) — valid per the MCP spec and compatible with both clients.

3. Single Internet Identity instance

The connector login (authorize.html) used https://id.ai while the app delegation flow used a different II canister. Both now derive from one II_URL origin (ii_mcp_url() = {II_URL}/mcp), and the authorize page is server-templated (__II_URL__) instead of hardcoded. Overridable via II_URL / II_MCP_URL.

⚠️ Open item: II_URL_DEFAULT is currently the previously-used app-delegation II origin as a working interim default. It should be switched to the II staging-B origin (pending the exact URL), or set via II_URL in the systemd unit.

Verification

cargo test passes (9/9); cargo check clean (only the pre-existing tool_router dead-code warning).

🤖 Generated with Claude Code


Generated by Claude Code

…ance

Three connector-compatibility fixes:

- Domain sign-in: drop the mcp_session cookie binding (connector clients
  run OAuth in an isolated browser, so the later sign-in link opened
  elsewhere never carried the cookie -> "this sign-in link belongs to a
  different session"). The link now opens in any browser; after II login
  the delegation is staged pending, and a new /signin/confirm page shows
  the verified principal + target domain and requires an explicit approve
  before it lands in the session. Informed consent replaces the cookie as
  the anti-phishing control.

- MCP transport: run rmcp Streamable HTTP in stateless + json_response
  mode. The stateful SSE/session handshake the defaults require is not
  completed by ChatGPT's connector client (sessions closed immediately,
  endless "Connecting to app"); our tools are pure request/response.

- II instance: unify the connector login (authorize.html, now templated)
  and the app delegation flow behind a single II_URL origin; ii_mcp_url
  derives {II_URL}/mcp. Override via II_URL / II_MCP_URL env.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011whdm4khV3aoLP3F3DbdZP

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts the MCP server’s authentication and transport behavior to make the end-to-end connector flow compatible with clients that run OAuth in an isolated in-app browser (notably ChatGPT), while also unifying Internet Identity (II) configuration across the connector login page and the II /mcp delegation flow.

Changes:

  • Reworks /signin/<link> to be browser-agnostic and adds a /signin/confirm step that stages a verified delegation until explicit user confirmation.
  • Switches rmcp Streamable HTTP to stateless mode with plain JSON responses for broader MCP client compatibility.
  • Unifies the II origin by templating static/authorize.html with a server-provided II_URL (with optional env overrides).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
static/authorize.html Replaces hardcoded II origin with a server-substituted placeholder.
src/auth.rs Injects the configured II origin into the authorize page at request time.
src/main.rs Implements the new /signin confirmation flow and switches MCP transport to stateless JSON.
src/identities.rs Introduces staged delegations pending confirmation and derives II /mcp URL from a unified II origin.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/identities.rs Resolved
Comment thread src/identities.rs Resolved
Comment thread src/auth.rs Resolved
Comment thread src/main.rs Resolved
Comment thread src/main.rs Resolved
- Set II staging-B as the explicit default (frontend canister
  uhh2r-oyaaa-aaaad-agbva-cai); ii_url() now normalizes a trailing slash.
- Require the mcp_flow cookie on GET /signin/confirm too (not just the
  POST), so the staged principal/domain isn't disclosable to anyone who
  obtains the confirm URL — restores the same-browser binding.
- Bound the pending-confirmation map with a 10-min TTL and opportunistic
  GC on insert; finalize/confirm_info reject expired entries.
- Escape II_URL for the JS string literal in authorize.html (guards
  against a stray quote/backslash/</script> in config).
- Add referrer policy (no-referrer) to the confirm page so the c token
  can't leak via Referer.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011whdm4khV3aoLP3F3DbdZP
@aterga
aterga marked this pull request as ready for review June 17, 2026 22:36
@aterga
aterga merged commit c9fe014 into main Jun 17, 2026
aterga added a commit that referenced this pull request Jul 17, 2026
…origin for OQL reads (Tiers 0–1) (#61)

* Make per-app reads self-correcting: surface auth as the missing ingredient (Tiers 0-1)

Anonymous per-app reads come back empty because the data is gated by the
caller's principal, and nothing in the old tool surface signalled that
authentication — not "no data" — was the missing ingredient. An agent asked a
"my data in this app" question would read empty, guess bespoke method/entity
names, and thrash before stumbling onto passing the app_url. This reworks the
return payloads, error messages, capability flags, and the server-instructions
playbook so the authenticated path is the obvious one and an empty read is
self-explaining.

Tier 0
 #1 Empty reads now carry an auth signal. get_canister_oql_schema,
    run_canister_oql_query, and call_canister query results always echo
    is_anonymous (alongside acted_as_principal / derived_for_origin). When a read
    is anonymous AND empty, they append a note: empty here most likely means "not
    authenticated as your account", so re-run adding the origin. Computed only
    from local facts (anonymous && empty) — never by probing whether
    authenticated data exists — and the origin stays a placeholder the agent
    fills, never one guessed via app_url_default.

Tier 1
 #2 Rewrote the server-instructions playbook: open_app FIRST for a named app; a
    "my/our…" question is an authenticated read (pass the origin); anonymous
    per-app reads usually return empty and that's an auth artifact; call
    get_canister_api_doc only when candid says it's available; oql:true => reads
    via the OQL tools, call_canister is updates-only. Dropped the old
    "discover_app_canisters first" / "get_canister_api_doc FIRST" advice.
 #3 open_app / discover_app_canisters now probe each app-owned canister's Candid
    once (enrich_capabilities) and attach oql / api_doc_available flags plus a
    caller-gated data-access note bound to the resolved derivation_origin. Scoped
    by is_app_data_candidate to the app's own data canisters — never
    II/NNS/ledger/frontend — per the origin-resolution trust boundary.
 #4 get_canister_candid reports api_doc_available, computed with the same
    predicate get_canister_api_doc uses, so the api-doc call is gated on it.
 #5 The call_canister OQL-rejection message names the replacement path
    (icp_oql_guide -> get_canister_oql_schema -> run_canister_oql_query) and the
    auth hint (pass the origin for the user's own data).
 #6 get_canister_api_doc returns a structured result in every case
    (available/expected/retry/next), distinguishing an expected absence from an
    unreachable canister instead of one opaque error.
 #7 run_canister_oql_query validates the query's start entity on an empty result
    (validate-on-empty, not preflight) by re-reading the schema for the SAME
    principal, returning valid_entities + a did_you_mean repair (booking ->
    bookings). Hard-validates start only.
 #8 get_canister_oql_schema emits a ready-to-run run_canister_oql_query example
    per entity, each preserving the identity the schema was read under so copying
    one doesn't silently drop to anonymous.
 #9 Rewrote the OQL tool descriptions (guide->schema->query; entity names are
    often plural and differ from Candid; my-data => pass origin) and the OQL primer.

Tests cover the new pure helpers (empty detection, entity extraction,
did_you_mean, examples, conservative empty-reply detection, app-data-candidate
scoping) and the enriched OQL-rejection message. Tier 2 (auto-authenticated
reads / open_app_as_me) is left to a follow-up.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRpq9CvXFNhkr2roH7j22d

* Address adversarial review: fix empty-diagnosis on error paths, app_url mis-hints, unparsed-reply note

Folds in the confirmed findings from an adversarial review of the Tier 0-1 diff
(4 dimensions x per-finding verification). No high-severity findings; the
security-guardrail dimension found nothing.

 - diagnose_empty_oql now takes an EmptyContext. On a FAILED result (trap / OQL
   error arm) it emits ONLY the #7 unknown-`start` repair, never the "came back
   empty => likely not authenticated" / "no entities here" notes — those describe
   an EMPTY result and would contradict a genuine error message. (Previously an
   anonymous query with a valid start that ERRORED got a misleading re-authenticate
   hint.)
 - diagnose_empty_oql distinguishes a FAILED schema re-read (entities: None) from a
   schema that genuinely lists no entities (Some([])), so a transient schema-read
   failure on an authenticated empty-table result no longer mislabels it "this
   account sees no entities" — it falls back to the benign "0 rows" note.
 - Dropped the "(or app_url)" hint for the OQL read tools in three places (the
   open_app data-access handle, the open_app description, and the server-instructions
   MY/OUR block): get_canister_oql_schema / run_canister_oql_query take only
   `derivation_origin`; `app_url` is call_canister's. Offering it sent the agent to
   a param those tools silently ignore -> an anonymous empty read.
 - The OQL Unrecognized-result arm now carries a structured `note` ("not an empty
   result; raw reply is in the text block"), so a structured-only consumer doesn't
   misread a non-tabular reply as "no data".
 - Fixed the candid_reply_is_empty doc to match the implementation: it recognizes
   `variant { none }` only; an explicit `variant { err = … }` is a real error, not
   "empty" (with a test assertion locking that contract in).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRpq9CvXFNhkr2roH7j22d

* Build OQL example query with serde_json so untrusted entity names are escaped

Addresses a Copilot review comment on PR #61: oql_query_examples built the
example `query` JSON string with format!, so a schema entity name containing a
quote or backslash — the schema is canister-supplied, hence untrusted — would
produce invalid JSON in the (advisory) example. Build the query object via
serde_json::json!(...).to_string() instead, which escapes it. Added a test with
a quoted entity name asserting both the args and the inner query string parse as
valid JSON and the name round-trips.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRpq9CvXFNhkr2roH7j22d

* Require an origin for OQL reads: disable anonymous per-app reads for now

Per maintainer direction, replace the soft "anonymous empty read" auth note on
the OQL read tools with a hard precondition: get_canister_oql_schema and
run_canister_oql_query now REJECT a call with no derivation_origin, since they
read per-app data gated by the caller's principal (an anonymous read is signed as
2vxsx-fae and returns nothing useful). The rejection names what to pass and points
at open_app / resolve_app so the agent recovers in one step. A hard reject can't
be misread as "no data" the way an empty result can.

Rationale for no anonymous escape hatch: authenticating never hurts a public read
— the canister serves the request regardless of principal, so a caller with no
account there simply isn't blocked. So there's no case that needs anonymous.

Scope:
 - Applies to the two OQL read tools only (the definitively caller-gated per-app
   data surface). The guards are a two-line origin.is_none() check, so re-enabling
   anonymous later is a trivial revert — the is_anonymous field and the empty-read
   diagnostics are left intact and simply aren't reached while the guards stand.
 - call_canister stays permissive (keeps its soft empty-read note): it's the
   general-purpose tool used for genuinely public canisters like ledgers, where
   there's often no app origin to pass and hard-blocking would break common public
   reads for no gain.
 - Public-metadata tools (get_canister_candid, get_canister_api_doc,
   discover_app_canisters, resolve_app) are unaffected — they read public data and
   take no origin.

Updated the OQL tool descriptions, the server-instructions playbook, the
get_canister_candid OQL note, open_app's description + data-access note, and the
OQL primer to say the OQL reads REQUIRE the origin (not "anonymous comes back
empty"). Added a test that the rejection message is actionable.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRpq9CvXFNhkr2roH7j22d

* Sync README with the tool changes (capability flags, structured api-doc, require-origin OQL reads)

The README tool table + flow drifted from the code. Brought them current:
 - get_canister_candid now advertises both `oql` and `api_doc_available` flags.
 - get_canister_api_doc is call-only-when-available and returns a structured
   available/expected/retry/next result (not "call first for an unfamiliar app").
 - get_canister_oql_schema / run_canister_oql_query now REQUIRE derivation_origin
   (anonymous per-app reads are rejected for now); noted the per-entity examples
   and the empty-result start validation / did_you_mean.
 - open_app / discover_app_canisters now report per-canister oql/api_doc_available
   capability flags + the caller-gated data-access note.
 - Typical flow + OQL section updated to say the OQL read tools require the origin
   while public metadata and call_canister public reads stay anonymous.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRpq9CvXFNhkr2roH7j22d

---------

Co-authored-by: Claude <noreply@anthropic.com>
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