Skip to content

workers/mcp: connect an agent with a FacturX account, no key to paste - #18

Merged
euphyra-dev merged 2 commits into
mainfrom
workers/mcp-agent-connect
Nov 26, 2026
Merged

euphyra-dev merged 2 commits into
mainfrom
workers/mcp-agent-connect

Conversation

@euphyra-dev

@euphyra-dev euphyra-dev commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

What this does

On the hosted MCP server's consent page, a user who already has a FacturX account can authorize an agent in one click: « Se connecter avec mon compte FacturX ». The API creates a dedicated key for the agent. The key never appears in the browser, in a URL or on a page. Pasting an API key stays available as a fallback.

The button is shown only when WEB_ORIGIN and both secrets below are configured. Without them, the page is unchanged.

Flow

  1. POST /authorize with decision=facturx:
    • the Worker approves the consent (mcp:read required);
    • it calls the OAuth library's beginUpstream, which creates a 256-bit state, stores the request encrypted in KV for 600 s, and sets a __Host- cookie holding the state's hash;
    • it signs a consent token (HMAC-SHA256, 600 s) with the state, client id, client name, redirect host and scopes;
    • it answers 303 to the fixed address https://facturxapi.com/connecter-agent#consent=<token>. The token is in the fragment.
  2. facturxapi.com shows only what the token covers, records the user's consent, and returns to the fixed path /authorize/retour?state=…&code=….
  3. GET /authorize/retour:
    • finishUpstream refuses any browser that did not start this flow, before anything else;
    • only then is the single-use code exchanged server to server (/api/v1/internal/agent-connections/exchange, dedicated secret);
    • the API creates the agent key at that exchange only, so a refused return leaves no key behind;
    • the authorization is then completed as with a pasted key.

Security properties

  • Browser binding. A link started by someone else and opened by a signed-in victim is refused at the return, and the code is never exchanged.
  • Trusted display. Everything the web page shows comes from the signed token, never from URL parameters.
  • Fixed redirects. The web page and return URLs are constants; no request parameter changes them.
  • Fail closed.
    • Identifiers longer than the API accepts (200 characters) are refused before anything is bound.
    • The scopes given to the agent are the intersection of what the user granted, what the client asked for, and what the API recorded for the key.
    • Every failure ends on the same French page (« Relancez-la depuis votre agent. Aucune clé n'a été créée. »), with no detail.
  • No key echo. The existing guards that refuse a page or redirect containing the key still apply.

Configuration

  • WEB_ORIGIN (var): https://facturxapi.com. It must be a bare https origin; anything else disables the button.
  • AGENT_CONNECT_SIGNING_SECRET and AGENT_CONNECT_EXCHANGE_SECRET (wrangler secrets, at least 32 characters): the same values as the API's.
  • The wrangler.jsonc comment now states the actual rule: no deployment from a branch or pull request; the Worker is deployed only at a release, after review. This PR deploys nothing.

Tests

node --experimental-strip-types --test test/*.test.ts: 32 passed. That is 12 new, plus the 20 existing ones (the paste fallback is unchanged). tsc --noEmit: clean.

The new tests cover:

  • the token format and 600 s lifetime;
  • the origin and config checks;
  • that the button is shown only when configured;
  • the 303 to the fixed page, with a ?web= parameter ignored;
  • a return from another browser, refused with 0 exchange calls;
  • the exchange with the dedicated secret;
  • an API refusal (400, 403, 409, 500);
  • missing parameters;
  • the checks after finishUpstream;
  • over-long identifiers;
  • scope intersection;
  • the French error page.

Mutants, each run and then reverted. Every one makes at least one test fail:

  • browser binding removed;
  • token lifetime set to 1 h;
  • token moved to the query string;
  • wrong exchange secret;
  • a state not bound to the browser;
  • identifiers truncated instead of refused;
  • the API's scopes ignored;
  • the checks after finishUpstream removed.

End-to-end proof (local)

This ran against a local FacturX API and web site, and this Worker under wrangler dev. A browser reached them through their real https host names (Playwright routing), so the __Host- cookies behaved as in production. No request reached a real host.

Step Result
Browser A starts the flow binding cookie __Host-oauth-upstream-…: Secure, HttpOnly, SameSite=Lax, host-only
facturxapi.com page shows the client name and redirect host from the signed token, which the API verified
A authorizes the callback receives an OAuth code, with no key in the URL
token exchange 200, scope: mcp:read mcp:write
MCP tools/list with that token 200, the 4 FacturX tools
A's agent keys one new key per run (3 runs: 3 created, 2 replaced, 1 active)
A starts a flow, browser B (another signed-in user) opens the link and authorizes return refused with 400: « La connexion n'a pas abouti… Aucune clé n'a été créée. »
B's agent keys 0 before, 0 after. B's consents were recorded but never consumed

The local run used compatibility_date 2026-10-13, the newest supported by the local workerd. The committed config is unchanged.

euphyra-dev and others added 2 commits November 26, 2026 16:29
The consent page offers « Se connecter avec mon compte FacturX » when WEB_ORIGIN and both
agent-connect secrets are set. The Worker approves the consent, binds the flow to the browser
with the OAuth library's beginUpstream (__Host- cookie), signs a consent token and sends the
browser to facturxapi.com/connecter-agent. On the fixed return /authorize/retour,
finishUpstream refuses any other browser before the code is exchanged server to server.
The paste field stays as a fallback. The wrangler comment states the real deploy rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t the API's scopes, tests

Identifiers the API would refuse (client_id or redirect host over 200 characters) are refused
before anything is bound to the browser; only the displayed name is shortened. The scopes given
to the agent are also limited to those the API recorded for the key. Tests for the checks after
finishUpstream and for the French restart page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@euphyra-dev
euphyra-dev merged commit 6c58209 into main Nov 26, 2026
12 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