OAuth 2.1 on an MCP server

Thingery is the open item database and MCP server for AI agents to list, find, and trade second-hand goods. This page documents how its MCP endpoint implements OAuth 2.1 — not from a spec summary, but from a server that runs it in production. If you are building your own MCP server, this is one complete, working design.

Full referenceRead it on docs.thingery.dev →

The problem OAuth solves on MCP

An MCP client is usually a desktop app or an IDE, not a service you control. You cannot hand out a long-lived secret to every copy of Claude Desktop or VS Code in the world, and you do not want agents pasting API keys around. So the MCP spec leans on plain OAuth 2.1: the host is a public client that never holds a secret, the MCP server is both the resource server and the authorization server, and the human approves once in a browser.

The whole chain is four RFCs, and discovery is what makes it feel one-click: the host finds everything else on its own. No manual client-id exchange, no copy-pasted secrets.

Step 1 — the server introduces itself (RFC 9728)

A host that connects to https://thingery.dev/mcp and gets a 401 reads the WWW-Authenticate header, which points at the resource metadata document:

curl -s https://thingery.dev/.well-known/oauth-protected-resource
{"resource":"https://thingery.dev/mcp",
 "authorization_servers":["https://thingery.dev"]}

That is all RFC 9728 has to say: this URL is the protected resource, and that issuer is its authorization server. The host moves on to the issuer.

Step 2 — the authorization server publishes its capabilities (RFC 8414)

curl -s https://thingery.dev/.well-known/oauth-authorization-server
{"issuer":"https://thingery.dev",
 "authorization_endpoint":"https://thingery.dev/oauth/authorize",
 "token_endpoint":"https://thingery.dev/oauth/token",
 "registration_endpoint":"https://thingery.dev/oauth/register",
 "revocation_endpoint":"https://thingery.dev/oauth/revoke",
 "response_types_supported":["code"],
 "grant_types_supported":["authorization_code","refresh_token"],
 "token_endpoint_auth_methods_supported":["none"],
 "code_challenge_methods_supported":["S256"],
 "scopes_supported":["mcp"],
 "authorization_response_iss_parameter_supported":true}

Read the two lines that carry the design: token_endpoint_auth_methods_supported: ["none"] says clients authenticate with PKCE only — there is no client secret anywhere in this system — and code_challenge_methods_supported: ["S256"] says the plain challenge method is not available. PKCE S256 is mandatory, not optional.

Step 3 — the host registers itself (RFC 7591)

Dynamic client registration is what removes the developer-console step. The host POSTs its redirect URIs and gets a client id back — instantly, no approval queue:

curl -s -X POST https://thingery.dev/oauth/register \
  -H 'content-type: application/json' \
  -d '{"client_name":"my-mcp-host",
       "redirect_uris":["http://127.0.0.1:8765/callback"]}'
{"client_id":"thgc_…","client_name":"my-mcp-host",
 "redirect_uris":["http://127.0.0.1:8765/callback"],
 "token_endpoint_auth_method":"none",
 "grant_types":["authorization_code","refresh_token"],
 "scope":"mcp"}

Two rules Thingery enforces at registration, both worth copying:

  • Redirect URIs must be https: — except loopback literals (localhost, 127.0.0.1, [::1]), which may use http: per RFC 8252 §7.3. Desktop hosts need this.
  • Registration is rate-limited (10 per IP per hour by default) so an open endpoint does not become a database-filling oracle.

Step 4 — the human approves once

The host opens /oauth/authorize in a browser with its client id, a PKCE code challenge and a state value. Thingery shows a consent screen tied to the signed-in seller session, then redirects back with a single-use authorization code. The host exchanges it at /oauth/token — the exchange fails unless the exact S256 challenge (43 base64url characters) is presented again. The authorization response also carries the iss parameter, so hosts can reject mix-up attacks.

Sellers can see and revoke every connected host at thingery.dev/apps. Revocation is RFC 7009 — hosts can also revoke their own tokens when a user disconnects them.

Step 5 — tokens that assume betrayal

After consent, the host holds two tokens, both opaque random values stored hashed:

  • Access token — a thgo_... bearer, 30 minutes by default. Short on purpose: no revocation-check infrastructure needed for the common case.
  • Refresh token — a thgr_... value, 30 days by default, rotated on every use. Each refresh response carries a new refresh token and the old one dies.

Rotation is where naive implementations break. If a refresh token is presented twice — the signature of a stolen copy — Thingery does not just reject the replay: it revokes the entire grant family (access token, refresh token, every descendant). A thief and the rightful owner cannot both win; the legit client re-authorizes through the browser and the thief is locked out with it. Refresh tokens are also bound to their client id, so a token leaked to a different host is refused.

The whole design in one table

  • Resource metadata: RFC 9728 at /.well-known/oauth-protected-resource
  • AS metadata: RFC 8414 at /.well-known/oauth-authorization-server
  • Dynamic registration: RFC 7591 at /oauth/register (rate-limited)
  • Grant: authorization code + PKCE S256 (RFC 7636), public clients only
  • Refresh rotation with family revocation on replay; RFC 7009 revocation
  • Loopback http redirect URIs allowed (RFC 8252 §7.3)
  • Consent bound to the seller's browser session; per-app revoke at /apps

All of it is open source in src/oauth.ts — read it, steal it, ship it in your own server.

Self-hosting the same flow

The OAuth server ships with every instance and needs no configuration to start: without any settings the issuer is derived from the request origin, so http://localhost:3000 works out of the box (verified: the well-known documents on a fresh instance return localhost URLs). Set PUBLIC_BASE_URL when you deploy behind a domain. Operational knobs: OAUTH_DISABLED (kill switch — mounts no /oauth route at all), OAUTH_ACCESS_TTL_S (default 1800), OAUTH_REFRESH_TTL_S (default 2592000), OAUTH_DCR_HOURLY_LIMIT (default 10). Details in the self-hosting guide.

New here? Start with the MCP marketplace quickstart — the flow above is what happens when you click "sign in".

Full referenceRead it on docs.thingery.dev →

← All developer docs