Skip to content

refactor(app): construct the MCP broker in one place behind its contract - #2686

Merged
ChrisJBurns merged 2 commits into
mainfrom
refactor/mcpbroker-construction-site
Nov 5, 2026
Merged

ChrisJBurns merged 2 commits into
mainfrom
refactor/mcpbroker-construction-site

Conversation

@ChrisJBurns

@ChrisJBurns ChrisJBurns commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2531. Part of #2422.

Problem

Composition and the command roots depended on the MCP broker adapter's concrete types instead of its contract:

  • internal/app/build.go held *mcpbroker.Runtime, *mcpbroker.Process and the concrete callers, chose between the bundled ToolHive process and the configured runtime inline, and repeated the broker's rollback in five places.
  • cmd/mecated and cmd/mecak8s took the concrete mcpbroker.HandlerBundle to mount the broker's HTTP surface.
  • On the bundled path, composition parsed the callback URL a second time with a looser parser than the adapter's, after the adapter had already validated it and thrown the path away.

Swapping in a different implementation, or changing how the broker is constructed, meant editing all of these. Slice 4 of agent identity changes construction twice: #2533 reconfigures the embedded ToolHive authorization server as AS-2, and #2536 passes the identity broker into the MCP broker on both construction paths.

Summary

The adapter is now constructed in one file, internal/app/mcp_broker_construction.go, and everything else in composition and the command roots uses the internal/mcpbroker contract. #2533 and #2536 become changes inside that file. Behavior is unchanged.

Contract (internal/mcpbroker)

Type What it owns
HTTPHandlers (new, http_handlers.go) The broker's HTTP surface: Mount(mux, callbackPath). Nil means the broker has no surface. Replaces HandlerBundle in the command roots
Service (existing) Built.MCPBroker, replacing *mcpbroker.Runtime
ConnectorInspector (existing) Wired only for the bundled process, as before

Service and Attachment are unchanged, so this does not overlap with #2530.

The construction site

Piece What it does
ConfiguredMCPBroker The five configured-path inputs, embedded in Config with the same field names, so existing callers compile unchanged
selectsConfiguredMCPBroker() The unchanged path-selection rule: a non-empty MCPBrokerOptions, MCPBrokerCaller, MCPBrokerAuthorizedCaller, MCPBrokerQueryCaller or MCPBrokerDiscovered selects the configured path instead of the bundled ToolHive one
buildMCPBroker Builds either path and returns contract values only (mcpBrokerParts). It releases what it built on failure, with the same error strings as before

build.go now makes one call, no longer imports the adapter, and keeps its teardown order.

Other changes

  • The ToolHive process reports the callback path it validated (Process.CallbackPath), and composition's second parser is deleted.
  • An empty bundled handler bundle is stored as nil, so every mount site checks one thing: handlers == nil.
  • Fix: the bundled broker's closer also closes the auth Redis client. Before, an anonymous-only bundled broker with RedisURL set never built the embedded auth server, so nothing closed the client composition created for it. This was already the case in build.go before this PR.
  • A new depguard rule, internal-adapter-mcpbroker-importers, keeps non-test files in internal/app and cmd off the adapter.
  • docs/architecture/extensibility.md gets an in-place correction of the broker paragraph.

What stays the same, and why

  • Mounted routes, the verified-identity and loopback guards in both command roots, startup and shutdown order, rollback and error strings are unchanged.
  • The configured-path inputs keep adapter types. Only tests and the SDK fixture daemon set them, so hiding them behind contract types would be speculative. ConfiguredMCPBroker is documented as a test and fixture seam.
  • Nothing from slice 4 is included. The identity broker option belongs to MCP broker authorizes gateway calls and sends them to the configured gateway #2536.

API compatibility

Assessment: no change. No proto, server mapper, engine behavior, mounted HTTP route or server broker configuration changes, so no conforming client is affected. Only internal Go types changed: the app.Built field types, and the app.Config layout, through an embedded struct with unchanged promoted field names. Stored data is untouched. No major bump or feature identifier. No upgrade implications.

Tests

  • Path selection: TestConfiguredMCPBrokerSelectsConfiguredPath checks that each of the five fields alone selects the configured path, and that none selects bundled.
  • Identity broker does not change the path: TestBuildKeepsBundledMCPBrokerWithIdentityBroker and TestBuildKeepsConfiguredMCPBrokerWithIdentityBroker run a full Build with the identity broker enabled.
  • No broker without broker authority: TestConfiguredMCPBrokerWithoutBrokerAuthorityBuildsNoBroker.
  • Error strings: TestBuildMCPBrokerFailuresKeepTheirErrors covers catalogue, handler and bundled construction failures.
  • What each path hands the server: TestBuildMCPBrokerHandsOffPathSpecificParts checks the connector inspector, workspace enrollment and HTTP handlers for the bundled path (with and without an HTTP surface) and the configured path (with and without a callback).
  • Auth Redis client: TestBundledMCPBrokerClosesUnusedAuthRedisClient checks that closing an anonymous-only bundled broker drops its Redis connections (miniredis).
  • Nil handlers: they mount nothing on an unauthenticated non-loopback listener, in both mecak8s and mecated.
  • Mutation checks:
    • the bundled test fails if selection reads IdentityBroker, or if the callback path is dropped;
    • each of these fails a test: dropping the Redis close, skipping the empty-handlers-to-nil conversion, giving the configured path an inspector, forcing enrollment off;
    • lint rejects an adapter import planted in cmd/mecated.

Commands run on this branch:

go build ./...
go vet ./internal/app/ ./cmd/mecak8s/ ./cmd/mecated/ ./internal/mcpbroker/ ./internal/adapter/mcpbroker/
go test -race -count=1 ./cmd/mecak8s/ ./internal/adapter/mcpbroker/ ./internal/mcpbroker/   # pass
go test -race -count=1 ./internal/app/ ./cmd/mecated/   # only failures are listed below
golangci-lint run (pinned) ./... on the root module: 0 issues
go run ./cmd/mecademo
task docs:check   # 0 broken links, anchors or orphans

The full task lint stops after the root, engine, adaptersupport, driver, adapters and authn/oidc modules (all 0 issues). It halts on a gosec G703 in the darwin-only environment/microvm/launch_ownership_darwin.go:754, which also fails on a clean main, so the provider-module lints after it did not run locally.

The full task test:race was run on macOS. Every failure it showed also fails on a clean main checkout, because the sandboxed shell has no bash and puts a unix socket under /dev/fd:

  • internal/app: TestYoloRelativeEscape*, the *GitInWorktree* tests, TestCanonicalConfiguredDir_AllowsMissingDefaultParents, TestBuildSubagentToolRealWiringForksChildShellWhenShell;
  • cmd/mecated: TestMecatedCLICompositionValidatesMicroVMDefaultAfterReadiness;
  • four cmd/mecatui startup and handoff tests, and TestStatusLine_Scenario4_…;
  • environment/microvm/gitexec.

CI covers the full suite.

Follow-ups

ChrisJBurns and others added 2 commits November 5, 2026 15:55
Composition and the cmd mains used the MCP broker adapter's concrete
types (*mcpbroker.Runtime, *mcpbroker.Process, HandlerBundle). Construct
the adapter only in internal/app/mcp_broker_construction.go and hand the
rest of composition and the command roots contract values:

- internal/mcpbroker.HTTPHandlers is the broker's HTTP surface; nil
  means none. Built.MCPBroker is mcpbroker.Service and
  Built.MCPBrokerHandlers is mcpbroker.HTTPHandlers.
- ConfiguredMCPBroker (embedded in Config, same field names) holds the
  configured-path inputs; selectsConfiguredMCPBroker is the unchanged
  path-selection rule.
- The ToolHive process reports the callback path it validated, so
  composition no longer re-parses the callback URL.
- depguard rule internal-adapter-mcpbroker-importers keeps internal/app
  and cmd on the contract.

Behavior, routes, startup/shutdown order, rollback, and error strings are
unchanged.

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

An anonymous-only bundled broker never builds the embedded auth server,
so process.Close never closed the auth Redis client composition created
for it. The broker's closer now closes the client after the process;
go-redis Close is idempotent.

Pin what each construction path hands the server: the connector
inspector and workspace enrollment come only from the bundled process,
and a broker without an HTTP surface hands over nil handlers. Document
ConfiguredMCPBroker as a test and fixture seam.

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns
ChrisJBurns requested a review from jhrozek November 5, 2026 16:57
@ChrisJBurns
ChrisJBurns merged commit 835163b into main Nov 5, 2026
50 checks passed
@ChrisJBurns
ChrisJBurns deleted the refactor/mcpbroker-construction-site branch November 5, 2026 20:56
@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.

Wire the broker through its contract

2 participants