fix(fastmcp): accept zone_url as valid audience in AuthProvider#123
Merged
Conversation
Keycard PKCE access tokens carry aud=zone_url rather than aud=resource_url (the MCP resource is indicated by a separate `resource` claim). The previous implementation set audience to only the MCP resource URL, causing token validation to fail with invalid_token for all Keycard-issued PKCE tokens. Fix: set audience to a list containing both the resource URL and zone_url, so the JWTVerifier accepts either form.
The grant() decorator built auth_info with resource_client_id set to self.client.config.client_id, which for WebIdentity is the DCR-registered ua:<hash> identifier. This changes on every restart and cannot be pre-registered as a Keycard application credential. Fix: when application_credential has an identity_manager (WebIdentity), use its stable key_id instead. This matches the identifier registered via keycard agent api application-credentials --type public-key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
keycardai-fastmcp'sAuthProvidersetsself.audienceto the MCP resource URL (e.g.https://my-server.fly.dev/mcp). But Keycard PKCE access tokens haveaud = zone_url(e.g.https://abc123.keycard.cloud) — the actual MCP resource is in a separateresourceclaim, not inaud.This causes token validation to always fail with
invalid_token/ "audience mismatch" for any MCP server usingkeycardai-fastmcp'sAuthProviderwith PKCE-issued tokens.The older
keycardai-mcp-fastmcppackage works correctly because it usesAuthSettings.issuer_urlfor validation, which doesn't checkaudagainst the resource URL.Fix
Accept both
audforms by settingaudienceto a list:The
JWTVerifieralready handles list audiences by checkingaud in self.audience, so a token with eitheraud = resource_urloraud = zone_urlwill pass validation.Repro
Deploy any MCP server using
keycardai-fastmcp'sAuthProvider+WebIdentity, connect via Keycard PKCE, observeBearer token rejected: audience mismatchin server logs.🤖 Generated with Claude Code