Skip to content

refactor(mcpbroker): move authorization orchestration behind the broker contract - #2685

Merged
ChrisJBurns merged 1 commit into
mainfrom
refactor/mcpbroker-authorization-contract
Nov 5, 2026
Merged

ChrisJBurns merged 1 commit into
mainfrom
refactor/mcpbroker-authorization-contract

Conversation

@ChrisJBurns

Copy link
Copy Markdown
Contributor

Closes #2530. Part of #2422.

Summary

The server now calls explicit MCP broker contract operations for MCP authorization. It no longer type-asserts on broker attachments or orchestrates broker state itself. Slice 4 of agent identity (#2536) can then add the identity-broker call check, per-call gateway tokens and resumed-call checks inside the MCP broker, without spreading into the server.

Contract (internal/mcpbroker)

Operation What it owns
Attachment embeds WorkspaceEnrollmentAttachment Replaces the type assertion in server/workspace_enrollment.go
Attachment.CallMcpWithQueryTool() Replaces the anonymous-interface assertion in server/mcp_broker.go
PresentPendingAuthorization(ctx, tx, auth) Status read, pending check, present (ErrAuthorizationNotPending)
SettleAuthorization(ctx, tx, auth, settled) Cancel, then reconcile the outcome (previously reconcileAuthorizationCancellation in the server). A failed cancel is reported as *CancelFailedError
ExpireAuthorization(ctx, tx, auth) The expiry timer's broker steps
Attachment.PrepareGrantedAuthorization(ctx, pending) Catalogue refresh, query tool, parked-argument check against the authenticated schema. A non-nil ArgumentsErr means the host must not run the call

The settle/present/expire operations are package functions over the small AuthorizationTransactions interface, which Attachment satisfies. They are not attachment methods, because every implementation would just forward to the same logic.

What stays in the server, and why

These are server-owned:

  • per-session run-entry locks, leases and the broker lock;
  • session load/save, claim and restore;
  • event append, backfill and publish;
  • engine rebuilds and starting the continuation run;
  • expiry timers, retries and shutdown/restart handling;
  • request/response and error mapping.

openIdentityBrokerSession stays in the server and runs at the same point as before: before any settle, save or append, so a refused access token leaves the authorization pending and the session unchanged. Recheck therefore still reads status and settles in two steps, so the identity-broker open can sit between them.

The connector inventory is unchanged and stays behind ConnectorInspector, which #1699 can reuse.

API compatibility

Assessment: no change. No proto, route, event or stored-data changes. Error codes and messages match the previous behaviour, including the two failed-precondition messages for invalid cancellation outcomes and the not-pending error. Persisted PendingAuthorization / ExternalAuthorization and restart interruption (ErrStateUnavailable → interrupted) are unchanged. Only the internal Go contract in internal/mcpbroker changed. No major bump or feature identifier. No upgrade implications.

Tests

  • New contract tests: settle reconciliation (moved from the server), cancel-failure signalling, present and expire.
  • Real-attachment tests: PrepareGrantedAuthorization returns the catalogue plus the query tool, accepts a valid parked CallMcpWithQuery call and refuses a forged one. With no query tool it returns only the catalogue and refuses the parked query call.
  • New server tests:
    • an invalid outcome stays ErrFailedPrecondition with the exact message on cancel, recheck and the expiry timer;
    • a failed cancel on lost state resolves as interrupted;
    • a failed read after the cancel is surfaced;
    • presenting a non-pending authorization keeps its error.
  • Fakes: the server fake now returns a set argument verdict. The real validator is unexported and tested in the adapter package.
  • Mutation checks: breaking the cancel-failure check, the interrupted branch or the message wording each fails a new test.

Commands run, all passing:

go build ./...
go vet ./internal/mcpbroker/ ./internal/adapter/mcpbroker/ ./internal/adapter/server/ ./internal/app/
go test -race -count=1 ./internal/mcpbroker/ ./internal/adapter/mcpbroker/
go test -race -count=1 ./internal/adapter/server/ -run 'Authorization|Enrollment|Connector|McpBroker|MCPBroker|Broker|Rebind|Authenticated|Cancellation|Presentation'
go test -count=1 ./internal/app/ -run 'MCPBroker|McpBroker|Broker|CallMcpWithQuery|EmptyBroker'
golangci-lint run (pinned) on the four package trees: 0 issues

The full task test:race was not run to completion on macOS. Failures it showed on the earlier working tree, e.g. the ACP placement tests and microvm/gitexec, reproduce on a clean main checkout; one failing package (microvmmanager) was not re-checked on main. CI covers the full suite.

Follow-ups

…er contract

The server now calls explicit MCP broker contract operations instead of
orchestrating MCP authorization itself or type-asserting on broker
attachments. Present, settle (cancel + reconcile), expiry, and granted
continuation (catalogue refresh, query tool, parked-argument check) live
in internal/mcpbroker and internal/adapter/mcpbroker. The server keeps
locks, leases, persistence, engine rebuilds, events, error mapping, and
the identity broker session open, in the same order as before.

Behavior and the wire API are unchanged.

Refs #2530, #2422

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns
ChrisJBurns requested a review from jhrozek November 5, 2026 15:47

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I checked that each authorization path behaves exactly as it did before:

  • Cancel: only a failed cancel on lost state resolves as interrupted, as before. A failed status read after CancelAlreadyResolved still surfaces. CancelFailedError unwraps, so brokerStateLost and the errors.Is mapping still match.
  • Recheck / expiry timer: the status-read/cancel/reconcile order is unchanged, and an expiry-timer status-read error on lost state still resolves as interrupted. The ErrFailedPrecondition messages are the same after authorizationOutcomeError.
  • Granted continuation: refresh, add query tool, rebuild, then reject on bad args: same order and same tool set. Validation now runs under the broker lock, but it is cheap.
  • Workspace enrollment: the removed "not configured" branch was unreachable. *mcpbroker.Attachment is the only production Attachment, and it already implemented enrollment.

CI is green.

@ChrisJBurns
ChrisJBurns merged commit 5e3591e into main Nov 5, 2026
50 checks passed
@ChrisJBurns
ChrisJBurns deleted the refactor/mcpbroker-authorization-contract branch November 5, 2026 16:27
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Nov 18, 2026
2 tasks
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.

Move authorization orchestration out of the server

2 participants