-
Notifications
You must be signed in to change notification settings - Fork 12
refactor(agent): serve root-mounted handlers through one registry #1875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nbouliol
wants to merge
2
commits into
feature/prd-1076-5-invalidate
Choose a base branch
from
feature/prd-1076-6-root-middleware
base: feature/prd-1076-5-invalidate
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−144
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import type { HttpCallback, RootHandler } from './types'; | ||
| import type { Logger } from '@forestadmin/datasource-toolkit'; | ||
| import type Koa from 'koa'; | ||
|
|
||
| import expressToKoa from './utils/express-to-koa'; | ||
|
|
||
| /** | ||
| * Handlers the agent serves at the root of the host application rather than under its own router | ||
| * prefix — the MCP server, the embedded BFF. Each one claims a set of paths through its matcher; | ||
| * anything else falls through to the host, untouched. | ||
| * | ||
| * Registered by name so a handler can be replaced or removed on a restart without the mount points | ||
| * having to know what is registered. | ||
| */ | ||
| export default class RootMiddleware { | ||
| private readonly handlers = new Map<string, RootHandler>(); | ||
| private readonly logger: Logger; | ||
|
|
||
| constructor(logger: Logger) { | ||
| this.logger = logger; | ||
| } | ||
|
|
||
| set(name: string, handler: RootHandler | null): void { | ||
| if (handler) this.handlers.set(name, handler); | ||
| else this.handlers.delete(name); | ||
| } | ||
|
|
||
| private entryFor(url: string): [string, RootHandler] | null { | ||
| for (const entry of this.handlers) { | ||
| if (entry[1].matches(url)) return entry; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * First registered matcher wins. Overlapping matchers cannot be caught at registration — the | ||
| * predicates are opaque — so the winner is named here instead: a request answered by the wrong | ||
| * handler is otherwise indistinguishable from one whose route was never registered. | ||
| */ | ||
| private handlerFor(url: string): HttpCallback | null { | ||
| const entry = this.entryFor(url); | ||
|
|
||
| if (!entry) return null; | ||
|
|
||
| this.logger('Debug', `Root middleware: '${entry[0]}' claims ${url}`); | ||
|
|
||
| return entry[1].callback; | ||
| } | ||
|
|
||
| /** | ||
| * Connect-style middleware. A handler that decides not to answer calls `next()`, which passes the | ||
| * request on to the host exactly as if nothing had been registered. | ||
| */ | ||
| getExpressMiddleware(): HttpCallback { | ||
| return (req, res, next) => { | ||
| const handler = this.handlerFor(req.url ?? '/'); | ||
|
|
||
| if (handler) { | ||
| handler(req, res, next); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| next?.(); | ||
| }; | ||
| } | ||
|
|
||
| getKoaMiddleware(): Koa.Middleware { | ||
| return expressToKoa(this.getExpressMiddleware(), url => this.entryFor(url) !== null); | ||
| } | ||
|
|
||
| /** The combined callback, or null when nothing is registered and the host needs no detour. */ | ||
| getCallback(): HttpCallback | null { | ||
| if (this.handlers.size === 0) return null; | ||
|
|
||
| return this.getExpressMiddleware(); | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude Opus 5 (claude-opus-5): Preferential
Two small contract-versus-code mismatches in this file, neither reachable today.
The defensive 404 disappeared. At the merge base an unclaimed request still reached the MCP callback, which owned the decision and had an explicit last resort when
nextwas missing —res.writeHead(404, …)under a "this shouldn't happen in normal usage" comment.next?.()replaces that with a no-op: no status, no log, the socket open until the client times out. I enumerated all five mount points plus the nested standalone path and every one supplies anext, so this is not observable — it is worth a line only because the optional-call is now the only thing between a future next-less caller and a hung connection.The class comment claims a removal path no code takes. It says a handler can be removed on "a restart, a
stop()".restart()only ever replaces, andFrameworkMounter.stop()runs itsonStoptasks and nothing else — no path callsset(name, null, …)on stop, so afteragent.stop()a still-serving Express or NestJS host keeps routing/mcpinto a stopped MCP callback. The behaviour is unchanged from the merge base; only the comment asserting otherwise is new, and it sits in the one file a maintainer will read while diagnosing exactly that. Either wirestop()to deregister, or cut the parenthetical.Worth a sentence in the PR body either way: if a handler is ever deregistered while the host keeps the middleware, its requests fall through to the host's 404 — a reasonable contract, but indistinguishable from "the route was never registered", so the deregistration wants a log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment fixed — the parenthetical now says "on a restart" only, since
stop()runs itsonStoptasks and never deregisters. Not restoring the 404: the middleware's contract is that an unclaimed url is not its business, and the old 404 belonged to the MCP callback, not to the dispatcher — havingRootMiddlewareanswer a request no handler claimed is exactly the behaviour this PR removes from the connect path. All five mount points plus the nested standalone path supply anext; if a terminal, next-less caller is ever added it needs to say so at construction rather than have the middleware guess per request.