Skip to content

test: run the OAuth client steps on the MCP client SDK - #446

Merged
Reilley64 merged 2 commits into
mainfrom
test/mcp-sdk-oauth-client
Oct 20, 2026
Merged

Reilley64 merged 2 commits into
mainfrom
test/mcp-sdk-oauth-client

Conversation

@Reilley64

@Reilley64 Reilley64 commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

The database tests and the MCP pre-flight now run the OAuth steps of an AI client with the OAuth functions of @modelcontextprotocol/client 2.3.1. Nothing changes for users or AI clients.

 supabase/mcp-connection.ts
+  discoverAuthorizationServer(supabaseUrl)          discoverAuthorizationServerMetadata, once per run
-  registerAiClient: fetch POST /oauth/clients/register
+  registerAiClient(metadata, ...)                   registerClient
-  authorize: hand-written PKCE and authorize URL
+  authorize(metadata, resource, client)             startAuthorization, then fetch with redirect: 'manual'
   approve(user, authorization)                      supabase-js approveAuthorization
+                                                    compares the redirect state, returns code and iss
-  requestTokens: hand-written token request, Basic header or client_id
+  exchangeCode(metadata, resource, ..., approval)   exchangeAuthorization, with iss
+  refreshTokens(metadata, resource, ...)            refreshAuthorization
   allowAiClient(metadata, resource, user, ...)      takes the metadata, so it reads none per test user

 supabase/tests/ai-clients.ts
+  authorizationServerMetadata()                     discovers the metadata once and reuses it

 supabase/mcp-preflight/main.ts
-  /resource_metadata="..."/ regex, fetch of the metadata URL
+  extractWWWAuthenticateParams, discoverOAuthProtectedResourceMetadata
   metadata check: --hosted still fetches the RFC 8414 URL and checks its issuer,
+  then discoverAuthorizationServer, which the client checks reuse

How each acceptance criterion is met:

  • Registration, authorize, the code exchange, and the refresh call registerClient, startAuthorization, exchangeAuthorization, and refreshAuthorization. No hand-written PKCE, authorize URL, or token request is left.
  • authorize still finds the consent page with a fetch that does not follow the redirect. It still throws when the redirect does not go to /oauth/consent with an authorization_id. approve is still a Supabase Auth call. It now also checks that the redirect has the state of the authorize request, and it returns iss for the code exchange, as an SDK client does.
  • allowAiClient takes the discovered metadata as an argument, so it cannot read the metadata again for each test user. authorizationServerMetadata() in tests/ai-clients.ts discovers it once per test run.
  • A new database test, "connecting an AI client", runs a public client and a confidential client through allowAiClient and refreshTokens. It checks the token endpoint auth method that each client registered (none and client_secret_basic), the audience, the client_id, and that refresh issues a new access token. The pre-flight still runs both kinds of client.
  • The pre-flight reads WWW-Authenticate with extractWWWAuthenticateParams. It reads the protected resource metadata with discoverOAuthProtectedResourceMetadata. The hosted RFC 8414 check is still a plain fetch, and it checks the issuer in that response.
  • The local stack passes the SDK's token endpoint check. assertSecureTokenEndpoint exempts 127.0.0.1. The SDK refused nothing that the tests did before. It changes three things on the wire:
    • startAuthorization adds prompt=consent because the scope has offline_access. Supabase Auth accepts it and still redirects to the consent page.
    • discoverAuthorizationServerMetadata checks that the metadata's issuer is the issuer that the URL came from (RFC 8414 §3.3). The pre-flight's own issuer comparison is gone, because the SDK does it. The local stack answers only at /auth/v1/.well-known/openid-configuration. That is the third URL that the SDK tries, after two 404s.
    • The approval redirect has code and state but no iss. exchangeCode passes iss through to exchangeAuthorization when it is present. The metadata does not set authorization_response_iss_parameter_supported, so the SDK's RFC 9207 iss check passes with no iss. The pre-flight prints iss not sent.
  • The pre-flight's metadata check no longer prints which discovery URL answered, because the SDK does not return it. With --hosted, the check still names the RFC 8414 URL when it fails. README says this.

Client ID Metadata Documents

Supabase Auth does not support Client ID Metadata Documents yet. The local stack's authorization server metadata has no client_id_metadata_document_supported. The Supabase OAuth 2.1 server docs (getting started) cover only dynamic client registration and clients that the owner registers. The feature request supabase/auth#2850 (opened 2026-10-02) is open, with no reply. The discussion supabase/supabase#41695 has no answer from the Supabase team. The latest Supabase Auth release, v2.197.0 (2026-09-09), does not add it. MCP spec 2026-07-28 deprecates dynamic client registration in favor of them. The SDK's registerClient doc says it keeps working until at least 2027-07-28. So the tests keep registerClient.

Evidence

  • Before: bun test tests/ai-client-tokens.test.ts -t 'connecting an AI client' failed with Export named 'authorizationServer' not found. allowAiClient took a Supabase URL and supported only a public client.
    After: both cases pass.
(pass) connecting an AI client > a public AI client gets tokens for the MCP server and refreshes them
(pass) connecting an AI client > a confidential AI client gets tokens for the MCP server and refreshes them
  • bun test tests/ai-client-tokens.test.ts tests/mcp-server.test.ts tests/mcp-calls.test.ts: 78 pass, 0 fail. These files get tokens through connectAiClient.
  • bun test tests/mcp-preflight.test.ts: 8 pass, 0 fail.
  • bun run mcp-preflight on the local stack, after the catalog fixture import and bun run test-user:
PASS  A call with no token gets 401 and the protected resource metadata
PASS  The authorization server metadata has the issuer, its endpoints, PKCE with S256, and dynamic client registration
      issuer http://127.0.0.1:55321/auth/v1, registration http://127.0.0.1:55321/auth/v1/oauth/clients/register, ...
PASS  Public client: dynamic client registration
      client_id ef1f9ede-..., token endpoint auth method none
PASS  Public client: the token exchange with PKCE and resource issues an access token and a refresh token
PASS  Public client: refresh issues a new access token
PASS  Revoke refuses the AI client's refresh token
PASS  Confidential client: dynamic client registration
      client_id 352d4139-..., token endpoint auth method client_secret_basic
PASS  Confidential client: refresh issues a new access token
...
39 passed, 0 failed.
  • bun run typecheck in supabase passes (tsc, and deno check on both functions).

Merge Danger

Door: two-way

The change touches only test and pre-flight code and README.

Blast Radius: tests

If the SDK's OAuth functions break against Supabase Auth, the database tests that connect an AI client fail, and so does the MCP pre-flight. The app, the MCP server, and the consent page do not change.

Closes #418

The database tests and the MCP pre-flight now register, authorize,
exchange the code, and refresh with the OAuth functions of
@modelcontextprotocol/client. The pre-flight reads WWW-Authenticate
and the protected resource metadata with the SDK too. The database
tests read the authorization server metadata once and cover both a
public and a confidential AI client.

Refs #418
…the pre-flight

Approval compares the redirect state with the authorize request, and the
code exchange passes iss to exchangeAuthorization. The OAuth steps take
the authorization server metadata and the resource apart. The pre-flight
calls discoverAuthorizationServer, checks the issuer at the hosted
RFC 8414 URL, and takes every earlier result through fromEarlierCheck.

Refs #418
@Reilley64
Reilley64 merged commit 55c9df3 into main Oct 20, 2026
6 checks passed
@Reilley64
Reilley64 deleted the test/mcp-sdk-oauth-client branch October 20, 2026 04:08
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