Skip to content

feat(identitybroker): wire the embedded gateway's authorization server as AS-2 - #2744

Merged
ChrisJBurns merged 10 commits into
mainfrom
feat/identitybroker-embedded-as2
Nov 12, 2026
Merged

ChrisJBurns merged 10 commits into
mainfrom
feat/identitybroker-embedded-as2

Conversation

@ChrisJBurns

@ChrisJBurns ChrisJBurns commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2738.

Problem

The identity broker will swap each per-call ID-JAG for a gateway token at the embedded gateway's authorization server (AS-2). That server served only the MCP broker's own logins: it had no broker client, its vMCP resource was its issuer (so the design's ID-JAG audience could not be configured), and the identity broker had no way to reach it.

What

With the identity broker on, a broker client key, and at least one protected MCP route:

  • Resource: the vMCP's resource becomes AS-2's issuer + /mcp, its endpoint. The login client's audience and resource, the server's allowed audience, and the vMCP's incoming audience all use it. ID-JAGs are addressed to AS-2's issuer, as the design says; ToolHive refuses that audience while it is also the resource.
  • Broker client mecatl at AS-2: private_key_jwt with the broker client's public keys, written to a private temporary jwks_file and removed after construction; grants token exchange and JWT-bearer; scope openid; audience the vMCP resource; token exchange restricted to its own tokens, so it can't exchange users' login tokens.
  • Issuer: unchanged, still derived from the callback URL. No new setting, and provider redirect URIs don't change.
  • Trust: production trusts no ID-JAG issuer. ToolHive v0.61.0 only trusts listed subjects, so production trust of AS-1 waits for Delegated access: subject mapping at ID-JAG redemption toolhive#6887 (Configure the embedded gateway's authorization server as AS-2 #2533). Tests trust the offline harness's AS-1 through an unexported option.
  • Startup: buildIdentityBroker returns the broker client key set; buildMCPBroker takes it in a new mcpBrokerIdentity parameter (which MCP broker authorizes gateway calls and sends them to the configured gateway #2536 extends) and registers mecatl; startup then records AS-2's issuer, token endpoint, resource and an in-process client (10 s timeout) for Per-call grant and gateway token #2535 to use. Startup refuses an AS-2 issuer that names the same server as AS-1's: scheme and host compare case-insensitively, a default port equals none, and a trailing slash is ignored.
  • How the identity broker reaches AS-2: the MCP broker only serves AS-2. mcpbroker.AS2.Handler() answers a POST to AS-2's token endpoint and nothing else (other paths 404, other methods 405). internal/app wraps it in the identity broker's in-process client (as2InProcessClient), which admits only the token endpoint URL. The broker never sees the MCP broker.
  • No protected route: no AS-2, startup proceeds, nothing recorded.

Identity broker off, or an injected identity broker core with no key: nothing changes.

The ID-JAG harness gains AccessTokenSpec.Claims so it can sign ID-JAGs with resource and act.

Design and shortcuts

Design companion: #2773 records the end-state rules this review surfaced (AS-1 in-process calls, AS-2's anonymous routes, distinct AS-1/AS-2 issuers). Review the two side by side.

Tests

  • Adapter (internal/adapter/mcpbroker/as2_test.go): mecatl registration and the vMCP resource; the temporary key file is gone after construction; a real gateway login yields a token for issuer + /mcp that the vMCP accepts, and an issuer-audience token is refused; mecatl can't exchange a login token; assertion refusals (other audience, expired, exp > 5 min ahead, reused jti, unregistered key); a harness-signed ID-JAG redeems once for a gateway token with act, client_id mecatl and no refresh token, which passes an MCP initialize; production configuration refuses the same ID-JAG; AS2.Handler() serves only a POST to the token endpoint, while the embedded server does serve the other routes.
  • Startup (internal/app): AS-2 recorded through Build with the broker key accepted and another key refused; the recorded client refuses other URLs; nothing recorded with the broker off, an injected core, no protected route, or no MCP broker; AS-2/AS-1 issuer collision refused (hosted AS-1, external AS-1, and the same server spelled differently); sameIssuer table test; TestBuildKeeps{Bundled,Configured}MCPBrokerWithIdentityBroker unchanged.
  • In-process transport (internal/identitybroker/adapter/inprocesshttp): the client AS-1 and AS-2 are reached through, and the response Recorder as1server's discovery filter also uses, with its tests (served response, deadline, abandoned and refused request bodies closed, panic re-raised, no redirects); each caller tests its own admission rule.
  • Key-set file helpers (keysetfile_test.go in as1server and mcpbroker): the key check, 0700/0600 modes, removal, and no key material in errors, duplicated with the helpers.
  • Broker client validated before construction; an empty key set sets up no AS-2; key file removed when construction fails; AS-2 recorded with hosted, external, and no AS-1, signed with the broker's real client key.
  • Mutation-checked: resource reverted, unrestricted token exchange, key file left behind, context ignored by the in-process client, trust set in production configuration, issuer compared byte for byte, the in-process client's path check removed.

API compatibility

Assessment: no change to the versioned gRPC/HTTP API. With the identity broker on (an experimental mecak8s flag), the embedded MCP gateway's protected-resource resource and its login tokens' audience change from the gateway issuer to issuer + /mcp. The gateway is served on the server's mux but is not part of the versioned contract, and only Mecatl's own broker login client and mecatl obtain its tokens. No major or feature change. Upgrade: turning the identity broker flag on or off makes users log in to protected MCP servers again.

Follow-ups

…r as AS-2

With the identity broker on and a protected MCP route, register the
broker client mecatl at the embedded authorization server (private_key_jwt,
keys from a temporary jwks_file, token exchange restricted to its own
tokens), make the vMCP resource the issuer plus /mcp so ID-JAGs can be
addressed to the issuer, and record AS-2's issuer, token endpoint,
resource and an in-process client at startup for the identity broker.
Production trusts no ID-JAG issuer until toolhive#6887; tests trust the
offline harness through an unexported option. Startup refuses an AS-2
issuer equal to AS-1's, and sets up no AS-2 without a protected route.

Closes #2738

Co-authored-by: Mecatl <[email protected]>
…he embedded gateway in the design

Address review of the AS-2 wiring:

- Move the in-process HTTP transport that as1server and the MCP broker's
  AS-2 both used into internal/inprocesshttp, with its tests; each caller
  keeps only its admission rule.
- Validate the broker client before constructing the embedded server, and
  set up no AS-2 for an empty key set.
- Pass the broker's real client signer through startup, and test AS-2 with
  hosted AS-1, external AS-1, and no AS-1, and key-file cleanup when
  construction fails.
- Design: the embedded vMCP's resource is distinct from AS-2's issuer, and
  there is no embedded gateway without a protected MCP server.

Co-authored-by: Mecatl <[email protected]>

@ChrisJBurns ChrisJBurns left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • medium · maintainability — hasKeys/writeKeySet copied between packages instead of shared, though the PR extracts a shared package for the transport (internal/adapter/mcpbroker/as2.go:90)

    hasKeys and writeKeySet are byte-for-byte copies of as1server/delegate.go. Comments in both files say "fix both together". The same PR creates internal/inprocesshttp to remove a duplicated helper, so leaving this pair copied is inconsistent. A key-set helper that writes a private temp file for ToolHive's jwks_file is security-relevant code, and two copies can drift apart (for example in file mode or cleanup).

    Recommendation: Move both helpers into one stdlib-only internal package (or into inprocesshttp's sibling, e.g. internal/jwksfile). Allow it in as1server's depguard list the way inprocesshttp was allowed, and delete the "fix both together" comments.

ChrisJBurns and others added 5 commits November 9, 2026 17:04
Write AS-2's key set before creating ToolHive's auth store, and close a
store this function created when registration fails before ToolHive takes
ownership. Share the key-set file helpers in internal/jwksfile instead of
copying them, and pull the AS-2 setup out of newToolHiveProcess into
helpers.

Co-authored-by: Mecatl <[email protected]>
…by server

Export one Recorder from internal/inprocesshttp and use it in as1server's
discovery filter instead of a second copy of the buffered response.

Refuse an AS-2 issuer that names the same server as AS-1's even when spelled
differently: scheme and host compare case-insensitively, a default port
equals none, and a trailing slash is ignored.

Co-authored-by: Mecatl <[email protected]>
Move internal/inprocesshttp back under internal/identitybroker/adapter so
the identity broker still moves into its own service as one tree, and drop
as1server's depguard exception for it. The MCP broker no longer builds a
client for AS-2: AS2.Handler serves only a POST to AS-2's token endpoint,
and internal/app wraps it in the in-process client the identity broker
uses while it shares the process, admitting only the token endpoint.

Co-authored-by: Mecatl <[email protected]>
…ed package

Replace internal/jwksfile with a marked copy of hasKeys and writeKeySet in
as1server and in the MCP broker, and drop its depguard rule and as1server's
exception for it, so the identity broker tree imports nothing from Mecatl
outside it. The copies are temporary: #2538 replaces the generated broker
client key with an operator-supplied key-set file and deletes both.

Co-authored-by: Mecatl <[email protected]>
toolhive#7088 closed toolhive#6954 with a fixed 60-second skew and kept
the 5-minute bound on a client assertion's exp, with no cap on exp - iat;
the maintainer declined to make either configurable until there is a
consumer. The broker already signs 60-second assertions, and only the
broker client key's holder can sign one, so step (5) check 5 now states
the broker's lifetime and the server's fixed bound instead of a configured
60-second server maximum.

Move AS-2's "exp too far" case to 10 minutes so it stays past ToolHive's
bound once the 60-second skew arrives.

Co-authored-by: Mecatl <[email protected]>
The ID-JAGs the AS-2 tests mint now carry act as the design shapes it, the
instance's ag_ identifier with AS-1 as its issuer, rather than a SPIFFE
path, so later work does not copy the wrong form.

Note in the mecak8s operator guide that turning the identity broker on or
off with protected MCP servers changes the gateway's token audience, so
users sign in to those servers again.

Co-authored-by: Mecatl <[email protected]>
AS-2's in-process handler must refuse a POST to the embedded server's
consent and callback routes, not only a GET to its other routes, so a
handler that served any POST fails the test. Note that the in-process
client buffers whole responses and cannot carry a streamed one, and say in
the mecak8s guide that protected MCP servers must be enrolled again, as the
MCP OAuth page calls it.

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns

Copy link
Copy Markdown
Contributor Author

Re the hasKeys/writeKeySet duplication: 792e2b7 first shared them in internal/jwksfile, as suggested. b318898 deliberately went back to a marked duplicate, for the same reason the in-process client moved back under internal/identitybroker/adapter/ (f1ec529). The identity broker must move into its own service as one tree ("Modulith constraints"), so as1server can't import a shared package outside internal/identitybroker, and mcpbroker can't import a broker adapter.

The other two review threads are fixed and resolved.

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this reads well, and the shortcuts are easy to follow because each one names the ticket that removes it. Two questions and one test request inline; nothing blocking.

Comment thread internal/adapter/mcpbroker/as2_test.go Resolved
@ChrisJBurns
ChrisJBurns merged commit c920cc4 into main Nov 12, 2026
49 of 50 checks passed
@ChrisJBurns
ChrisJBurns deleted the feat/identitybroker-embedded-as2 branch November 12, 2026 22:31
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Nov 18, 2026
2 tasks

This branch was successfully deployed

1 active deployment
Preview — f47a33a0 Deployed Nov 12, 2026 by vercel[bot]
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.

Wire the embedded gateway's authorization server into startup as AS-2 Authorization server: allow clock skew on private_key_jwt client assertions

2 participants