fix: resolve upstream OAuth per server across consent, runtime, and gateways

Merged
DA
1mo ago
+2747-585

AIM-586

Summary

Cuts issuer-gated serving over to per-server upstream OAuth clients, with no flag and no fallback. This merges only after the migration in #7659 shows every organization as migrated.

For user session credentials (user, agent, workload and anonymous sessions) on remote and tunneled servers:

  • Direct endpoints. The credential's recorded choice for the server (AIM-585) decides the upstream credential.
    • The token comes from the chosen client alone (ChallengeManager.ResolveClientAccessTokens). It routes, and identity chaining runs, by that client's own issuer instead of mcp_servers.remote_session_issuer_id. Chaining only uses a binding for the chosen client, so only that client's bindings count toward ambiguity, and a credential without a chosen client never chains.
    • A missing or unusable choice answers with the existing reconnect challenge (new rejection reason upstream_selection_unavailable). Reconnecting issues a credential that records a fresh choice.
    • A no-auth choice calls anonymously and never chains. There is never a fallback to another client the issuer binds, to the issuer's inventory, or to an inferred no-auth. A user, agent or workload credential whose auth context is missing or names another organization is refused. Anonymous sessions carry no auth context, so their choices are read in the endpoint's organization.
    • Refresh judges the same choice for every session kind. A missing or unusable choice refuses the refresh, and a no-auth choice needs no upstream. For a user, only the chosen client's stored grant decides a reconnect. A sibling client on the same user session issuer never does.
  • Gateways (stored and organization). Each proxied member's routing identity comes from the credential's choice for that member, applied once to the request's member snapshot, so routing, chaining and connection_status all follow it. Tokens resolve per member server and are cached per member for the request, not per project or issuer. Members resolve independently, so one slow token endpoint holds up only its own member. A member without a usable choice reports that the gateway must be signed in to again, and the other members keep serving.
  • Consent.
    • A direct server offers only its associated clients. A gateway offers only the clients associated with members the person reaches.
    • Gateway member status, member eligibility and the claiming-member lookups match by association instead of derived issuer. The metamcp claim queries become client lookups (ListMetaMCPMembersForRemoteSessionClient, ListOrganizationMCPServersForRemoteSessionClient).
    • Card routability and the verification probe route each card by its own client's issuer. Another server's grant for the same upstream is no longer a competing candidate, so the duplicate-grant refusal is removed.
    • A hosted server's consent page still offers every client its issuer binds, since hosted servers keep the issuer-derived path.
    • A gateway connect for a client no member uses is refused rather than qualified by the issuer's inventory. A client only a hosted member uses still connects, since hosted members keep the issuer-derived path. Gateway members' associations come from the gateway evidence the migration records (#7644).
    • A card borrows a gateway member's resource documentation only from members associated with its own client, never from members sharing its authorization server.
    • Identity chaining is judged per card through that card's own client (and, for a tunneled server, its issuer), at consent and in the Platform MCP connection status, matching the runtime.
  • Platform MCP get_my_mcp_connection_status picks the server's client by association and consults identity chaining only through that one client. A server with no associated client reports that no authorization is required.
  • Session-policy cache entries now carry the credential id (cache key bumped to v2), so no extra lookup runs per request.

Left on the issuer-derived path, unchanged: hosted toolsets, gateway hosted members, and credentials with no session row (principal credentials, agent keys, assistant tokens). Admission, mcp:connect, ownership, delegation, resource binding and inbound ID-JAG trust are untouched.

Sequencing. #7644 and #7647 record associations and choices as configuration and sign-ins happen. #7659 migrates existing organizations from the admin dashboard. This PR merges once every organization there shows as migrated. Credentials still asked to reconnect at that point are expected: no single client is decided for them, so reconnecting is the repair.

Rollback is reverting this PR. The derived column and issuer bindings keep being maintained, and choices keep being recorded at issuance, so the issuer-derived path still works after a revert. Removing the derived column is staged for after the excluded paths no longer read it.

Motivation

With several servers on one organization user session issuer, deriving one upstream provider per issuer either applies one server's provider to its siblings or leaves them all with none, and gateway routing, consent and member admission all key on that derived value (AIM-577). Resolving every reader from the credential's per-server choice and the server's own associations makes the provider a per-server fact. Cutting consent, direct runtime, gateways and status over together, in one change, avoids contradictory behavior between them. This replaces the earlier per-server issuer-column approach.


Summary by cubic

Resolves each MCP server's upstream OAuth client from the caller's recorded per-server choice instead of deriving one provider per shared sign-in, so servers sharing a sign-in no longer inherit a sibling's client or get none. Approving consent now records the client picked on the consent page as each covered server's choice, which issuance stores with the credential; paths without a consent page record only an unambiguous assignment. There is no rollout flag: the per-organization migration runs from the admin dashboard before this change merges, and serving reads associations and choices unconditionally after coverage.