Repository navigation
refactor(app): construct the MCP broker in one place behind its contract - #2686
Merged
Merged
Conversation
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]>
jhrozek
approved these changes
Nov 5, 2026
Merged
2 tasks
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.
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.goheld*mcpbroker.Runtime,*mcpbroker.Processand the concrete callers, chose between the bundled ToolHive process and the configured runtime inline, and repeated the broker's rollback in five places.cmd/mecatedandcmd/mecak8stook the concretemcpbroker.HandlerBundleto mount the broker's HTTP surface.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 theinternal/mcpbrokercontract. #2533 and #2536 become changes inside that file. Behavior is unchanged.Contract (
internal/mcpbroker)HTTPHandlers(new,http_handlers.go)Mount(mux, callbackPath). Nil means the broker has no surface. ReplacesHandlerBundlein the command rootsService(existing)Built.MCPBroker, replacing*mcpbroker.RuntimeConnectorInspector(existing)ServiceandAttachmentare unchanged, so this does not overlap with #2530.The construction site
ConfiguredMCPBrokerConfigwith the same field names, so existing callers compile unchangedselectsConfiguredMCPBroker()MCPBrokerOptions,MCPBrokerCaller,MCPBrokerAuthorizedCaller,MCPBrokerQueryCallerorMCPBrokerDiscoveredselects the configured path instead of the bundled ToolHive onebuildMCPBrokermcpBrokerParts). It releases what it built on failure, with the same error strings as beforebuild.gonow makes one call, no longer imports the adapter, and keeps its teardown order.Other changes
Process.CallbackPath), and composition's second parser is deleted.handlers == nil.RedisURLset never built the embedded auth server, so nothing closed the client composition created for it. This was already the case inbuild.gobefore this PR.internal-adapter-mcpbroker-importers, keeps non-test files ininternal/appandcmdoff the adapter.docs/architecture/extensibility.mdgets an in-place correction of the broker paragraph.What stays the same, and why
ConfiguredMCPBrokeris documented as a test and fixture seam.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.Builtfield types, and theapp.Configlayout, through an embedded struct with unchanged promoted field names. Stored data is untouched. No major bump or feature identifier. No upgrade implications.Tests
TestConfiguredMCPBrokerSelectsConfiguredPathchecks that each of the five fields alone selects the configured path, and that none selects bundled.TestBuildKeepsBundledMCPBrokerWithIdentityBrokerandTestBuildKeepsConfiguredMCPBrokerWithIdentityBrokerrun a fullBuildwith the identity broker enabled.Built.Closeshuts the process down.TestConfiguredMCPBrokerWithoutBrokerAuthorityBuildsNoBroker.TestBuildMCPBrokerFailuresKeepTheirErrorscovers catalogue, handler and bundled construction failures.TestBuildMCPBrokerHandsOffPathSpecificPartschecks 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).TestBundledMCPBrokerClosesUnusedAuthRedisClientchecks that closing an anonymous-only bundled broker drops its Redis connections (miniredis).IdentityBroker, or if the callback path is dropped;cmd/mecated.Commands run on this branch:
The full
task lintstops after the root, engine, adaptersupport, driver, adapters and authn/oidc modules (all 0 issues). It halts on a gosec G703 in the darwin-onlyenvironment/microvm/launch_ownership_darwin.go:754, which also fails on a cleanmain, so the provider-module lints after it did not run locally.The full
task test:racewas run on macOS. Every failure it showed also fails on a cleanmaincheckout, because the sandboxed shell has nobashand puts a unix socket under/dev/fd:internal/app:TestYoloRelativeEscape*, the*GitInWorktree*tests,TestCanonicalConfiguredDir_AllowsMissingDefaultParents,TestBuildSubagentToolRealWiringForksChildShellWhenShell;cmd/mecated:TestMecatedCLICompositionValidatesMicroVMDefaultAfterReadiness;cmd/mecatuistartup and handoff tests, andTestStatusLine_Scenario4_…;environment/microvm/gitexec.CI covers the full suite.
Follow-ups
buildBundledMCPBrokerandtoolHiveBrokerConfig. Giving the broker AS-2's endpoints also touchesbuildIdentityBroker.buildMCPBrokerparameter passed to both builders, never throughConfiguredMCPBroker, so it cannot change the selected path.Mount(mux)). This would removeBuilt.MCPBrokerCallbackPathand the extra argument in both command roots. It was left out because it changes many mecak8s tests.AuthorizationStatus/CancelAuthorizationdirectly. That is server-side authorization orchestration, not construction wiring, so it is not part of this PR.