OAuth for MCP clients: dynamic client registration, safely
How Ostiary lets MCP clients and AI agents register themselves over OAuth: public PKCE clients only, scope limits, reused clients and RFC 9728.
An MCP client such as Claude, VS Code or Cursor does not wait for an administrator to create an OAuth client for it. It discovers your authorization server, registers itself, and starts an authorization code flow, with no one in between. That is convenient for the people connecting their tools, and it is also an open door: anyone on the internet can now create clients on your identity provider.
This post walks through how Ostiary handles that door: what MCP clients expect, what Ostiary lets them do, and the limits that keep anonymous registration from becoming a way to mint secrets or crowd your consent screen with look-alike apps.
Why MCP clients register themselves
The MCP authorization specification builds on OAuth. A protected MCP server acts as a resource server, and the client finds its authorization server through metadata. The specification says authorization servers and MCP clients should support Client ID Metadata Documents and may support the OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591). Ostiary supports both:
- Dynamic Client Registration: the client posts its metadata (name, redirect URIs, grant types) to
/api/auth/oauth2/registerand receives aclient_id. - Client ID Metadata Documents (IETF draft): the
client_idis itself an HTTPS URL pointing to the client's JSON metadata, for examplehttps://vscode.dev/oauth/client-metadata.json. Ostiary fetches it the first time the client signs someone in, and you can limit it to some hosts, such asclaude.aiorvscode.dev.
Both are off by default. You turn them on in the admin console under Applications > Self-registration. For Dynamic Client Registration you choose between Anyone, which is what MCP clients expect, and Signed-in users only. The setting lives in the database and reaches the auth server within a minute; there is nothing to redeploy.
Discovery: how clients find you
When registration is on, Ostiary's discovery documents advertise it: registration_endpoint for Dynamic Client Registration and client_id_metadata_document_supported for metadata documents, including on a freshly started serverless instance. With the issuer https://auth.example.com/api/auth, clients read the metadata at both the OAuth Authorization Server Metadata path (RFC 8414) and the OpenID Connect Discovery path:
https://auth.example.com/.well-known/oauth-authorization-server/api/auth
https://auth.example.com/api/auth/.well-known/openid-configuration
Browser-based MCP clients also call these endpoints from another origin, so Ostiary sends CORS headers on the discovery documents and the /oauth2/ endpoints; sign-in, session and SCIM routes stay same-origin.
The limits on self-registered clients
A self-registered client is never reviewed by a human. Ostiary treats it accordingly:
- Scopes you choose. Such clients may only request the scopes you tick in the self-registration settings: the OpenID Connect scopes by default, plus any API scopes you add. Narrowing the list also narrows clients registered earlier.
- Interactive sign-in only. Authorization code with PKCE, and nothing else: no client credentials, no device sign-in, no skipping the consent screen. Later updates cannot widen a client past these limits.
- A visible warning. The consent screen marks the app as unverified, says where the user will be sent, and hides the app's logo, so a registration called "Claude" cannot borrow Claude's branding.
- Rate limits. An hourly cap on new self-registered clients across all instances (30 by default), plus 5 registrations a minute per IP address in production.
- Careful fetching. Metadata documents are fetched only from public addresses: no private or reserved IPs, no redirects, at most 5 KB and a 5-second timeout.
Registrations and admin changes are written to the audit log.
Anonymous callers never get a secret
The most important rule concerns who receives a client secret. With Anyone selected, a request made without a signed-in session must register a public client: token_endpoint_auth_method: "none", which always uses PKCE. A confidential, secret-bearing client can only be registered with a session, by an admin or a signed-in user.
Many MCP clients simply omit token_endpoint_auth_method. Better Auth, the framework Ostiary is built on, would then default the registration to a confidential client and hand a freshly generated secret back to whoever made the anonymous request. Ostiary does not refuse these clients, which would break them, and does not let that secret out either: an anonymous registration that omits the method, or asks for a confidential one, is coerced to a public PKCE client. Public clients register freely; secrets never leave with an anonymous caller.
Here is what an MCP client sends for you:
curl -X POST https://auth.example.com/api/auth/oauth2/register \
-H 'content-type: application/json' \
-d '{
"client_name": "My Agent",
"redirect_uris": ["http://127.0.0.1:33418/callback"],
"token_endpoint_auth_method": "none",
"application_type": "native",
"grant_types": ["authorization_code", "refresh_token"],
"scope": "openid profile email offline_access"
}'
# 201 {"client_id": "...", "scope": "openid profile email offline_access", ...}
The client then runs the usual authorization code flow with PKCE (RFC 7636).
One client per app, not one per connection
MCP clients such as Claude register again every time someone connects them. Left alone, that fills the Applications page with identical "Claude" clients. Ostiary now reuses identical public clients: an anonymous registration of a public client whose metadata matches an existing, enabled public client gets that client back, with the same client_id and a 201 response carrying its stored metadata. Nothing is created, so it does not count against the hourly cap.
What counts as identical is deliberately broad: the name, redirect URIs, grant types, response types, scopes, application type, software id and version, website, terms, privacy policy and contacts. Every link the consent screen shows is part of the match. Otherwise, whoever registered a name first could put their own website, terms or privacy links in front of every later user of that name. The logo is not part of it, since the consent screen hides it anyway.
A public client has no secret, so handing its client_id out again gives the caller nothing it could not get by registering. Even so, reuse is never applied to confidential clients, clients registered with a session, disabled clients, or registrations that differ in any of those fields. It is on by default; Reuse identical public clients in the self-registration settings turns it off.
For the duplicates already in your database, the Applications page groups identical self-registered clients into one row (Claude ×12). Remove unused duplicates keeps the most recently used client of each group and deletes the others, except those still holding a live access or refresh token: they stay, so nobody is signed out, and the result tells you how many. Disabled clients are left alone. The same page can also delete self-registered clients that were never used (no token, no consent) after a number of days.
Protecting the MCP server itself
Registration is half the story; your MCP server also has to accept only tokens meant for it. In Ostiary you register the server under APIs, with its URL as identifier (for example https://mcp.example.com) and its scopes, and allow those scopes for self-registered clients.
The server then points clients to Ostiary with OAuth 2.0 Protected Resource Metadata (RFC 9728): it serves /.well-known/oauth-protected-resource and answers unauthenticated requests with 401 and a WWW-Authenticate header linking to that document. Clients request their token with resource=https://mcp.example.com, the resource indicator from RFC 8707, so the token's audience is your server and nothing else. The MCP and AI agents guide has a complete example using Better Auth's resource client, which verifies the JWT's signature, issuer, audience and scopes on every request.
Where to go next
- For the provider these clients talk to, read running an OpenID Connect provider on Better Auth.
- To see what Ostiary adds on top of the framework in general, read Ostiary as a self-hosted Auth0 alternative.
- To try it on your own domain, follow the deployment guide, or look at the agent features on the home page.
Self-registration is what makes "connect your MCP client" a one-click experience. With public PKCE clients, narrow scopes, an honest consent screen and one client per app, it does not have to cost you control of your identity provider.