refactor(agent): serve root-mounted handlers through one registry - #1875
Open
nbouliol wants to merge 1 commit into
Open
refactor(agent): serve root-mounted handlers through one registry#1875nbouliol wants to merge 1 commit into
nbouliol wants to merge 1 commit into
Conversation
`McpMiddleware` held a single callback, so the MCP server was the only thing the agent could serve at the root of a host application. The embedded BFF needs the same treatment, on paths of its own. `RootMiddleware` keeps named handlers, each with the matcher that says which urls it claims; anything unclaimed falls through to the host untouched. The five mount points are unchanged apart from the type they call, and the MCP matcher is now applied on the connect path too, where the callback used to be trusted to filter itself. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Coverage Impact This PR will not change total coverage. Modified Files with Diff Coverage (3)
🤖 Increase coverage with AI coding...🚦 See full report on Qlty Cloud » 🛟 Help
|
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.

Stacked on #1874. Touches only
packages/agent, so it can be reviewed independently of the agent-bff PRs below it in the stack.Why
McpMiddlewareheld one callback and one matcher. That was enough while the MCP server was the only thing the agent served at the root of a host application, next to its own/{prefix}/forestrouter. The embedded BFF needs the same treatment on/bff/*, and two singletons wired into five mount points is how mount points drift apart.What
RootMiddlewarekeeps handlers by name, each with the matcher declaring which urls it claims. Unclaimed urls fall through to the host untouched, exactly as before.The five mount points (
mountOnExpress,mountOnFastify,mountOnKoa,mountOnNestJs, and the connect callbackmountOnStandaloneServeruses) change only in the type they call.One behavior does tighten: on the connect and Express paths the MCP callback used to be handed every request and trusted to filter itself, while only the Koa path applied the matcher. The matcher now applies everywhere.
makeIsMcpRouteis what the MCP server filters on internally, so the claimed set is the same.Nothing registers a second handler yet — the BFF arrives in the next PR.
Tests
82 suites, 1502 tests in
@forestadmin/agent. The oldmcp-middlewaresuite becomesroot-middleware, plus coverage for what is new: dispatch to the right handler among several, and a null callback while nothing is registered so the host keeps its untouched path.Fixes PRD-1076
🤖 Generated with Claude Code
Note
Serve root-mounted handlers through one
RootMiddlewareregistryMcpMiddlewarewith a newRootMiddlewareclass in root-middleware.ts that hosts multiple named root-level handlers (e.g. MCP, BFF) keyed by URL matcher.isMcpRoute(url), which matches/.well-known/,/oauth/, and/mcpprefixes; MCP callback registration inFrameworkMounter.setMcpCallbacknow delegates toRootMiddleware.set('mcp', ...).mountOnExpress,mountOnFastify,mountOnKoa,mountOnNestJs) andgetConnectCallbackin framework-mounter.ts now dispatch throughRootMiddleware; unclaimed URLs fall through to the host unchanged.RootMiddleware.getCallback()returnsnullwhen no handlers are registered, unlike the old single-callback path; any out-of-tree callers relying onMcpMiddlewareor a non-null root callback will break.Macroscope summarized 46447d8.