Skip to content

fix(controller): resolve MCP global middleware lazily on first request#460

Closed
elrrrrrrr wants to merge 2 commits into
eggjs:masterfrom
elrrrrrrr:fix/mcp-global-middleware-lazy
Closed

fix(controller): resolve MCP global middleware lazily on first request#460
elrrrrrrr wants to merge 2 commits into
eggjs:masterfrom
elrrrrrrr:fix/mcp-global-middleware-lazy

Conversation

@elrrrrrrr

@elrrrrrrr elrrrrrrr commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Problem

MCPControllerRegister.register() runs during the tegg load-unit init (postCreate) and eagerly called getGlobalMiddleware(), which resolves the names in config.mcp.middleware against app.middlewares. But app.middlewares is only populated later by loadMiddleware, so booting an app whose config.mcp.middleware references any middleware throws at startup:

TypeError: Middleware <name> not found
  at MCPControllerRegister.getGlobalMiddleware
  at MCPControllerRegister.register
  at AppLoadUnitControllerHook.postCreate

Surfaced under egg4 when a controller app configures mcp.middleware (e.g. a tracing middleware from another plugin).

Fix

Defer global-middleware resolution to the first request, when app.middlewares is ready:

  • composeGlobalMiddleware(mw) wraps the base middleware and builds/composes the global chain on first invocation.
  • getGlobalMiddleware() is now idempotent (builds once).
  • The eager call in register() is removed.

Middleware applied at request time is unchanged; it just no longer requires app.middlewares to be populated during controller registration.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of global middleware so it is applied reliably across request types.
    • Middleware is now loaded lazily on first use, helping avoid missing setup during registration.
    • Requests now use the base middleware even when no global middleware is configured.

MCPControllerRegister.register() runs during the tegg load-unit init
(postCreate), and eagerly called getGlobalMiddleware() which looks up the
configured middleware names in app.middlewares. But app.middlewares is only
populated later by loadMiddleware, so registering a controller whose
config.mcp.middleware references any middleware throws
'TypeError: Middleware xxx not found' at boot.

Defer the resolution: build the global middleware chain lazily on the first
request (composeGlobalMiddleware wraps the base middleware), make
getGlobalMiddleware idempotent, and drop the eager call in register(). The
installed middleware is identical at request time; it just no longer requires
app.middlewares to be ready during registration.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@elrrrrrrr, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 47 minutes and 56 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 338eb748-b696-4a4e-91f2-1649d050464c

📥 Commits

Reviewing files that changed from the base of the PR and between 01c0def and 49bc715.

📒 Files selected for processing (2)
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts
  • plugin/controller/test/mcp/mcpGlobalMiddleware.test.ts
📝 Walkthrough

Walkthrough

MCP controller registration now defers global middleware resolution until the first request. The same lazy wrapper is applied to stateless stream, stateful stream, SSE, and MCP POST paths, and eager global-middleware initialization is removed.

Changes

Lazy MCP middleware composition

Layer / File(s) Summary
Lazy resolution helper
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts
getGlobalMiddleware() now returns early when globals are already built, composeGlobalMiddleware(mw) lazily resolves and composes the middleware chain, and register() stops forcing eager global-middleware initialization.
Middleware wrapping at MCP entry points
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts
The stateless stream, stateful stream, SSE context storage, and MCP POST registration paths now call composeGlobalMiddleware(mw) before request handling.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • eggjs/tegg#335: Introduces the MCP global-middleware plumbing that this PR changes from eager composition to lazy composition.

Suggested reviewers

  • killagu

Poem

A bunny hopped through middleware dew,
Waited for requests, then the globals came through.
One lazy hop here, one lazy hop there,
MCP danced softly in first-request air.
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: deferring MCP global middleware resolution until first request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors MCPControllerRegister to lazily resolve and compose global middlewares on the first request rather than at controller registration time, preventing errors when middlewares are not yet fully loaded. Feedback on the changes suggests caching the composed middleware within composeGlobalMiddleware to avoid redundant allocations and improve performance on every request.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +830 to +837
composeGlobalMiddleware(mw: compose.Middleware<EggContext>): compose.Middleware<EggContext> {
const self = this;
return async (ctx, next) => {
self.getGlobalMiddleware();
const composed = self.globalMiddlewares ? compose([ mw, self.globalMiddlewares ]) : mw;
return composed(ctx, next);
};
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Calling compose([ mw, self.globalMiddlewares ]) on every request allocates a new composed middleware function and array on every invocation, which is inefficient. Since self.globalMiddlewares is resolved once and mw is constant for each endpoint, we can cache the composed middleware locally inside composeGlobalMiddleware to avoid redundant allocations.

  composeGlobalMiddleware(mw: compose.Middleware<EggContext>): compose.Middleware<EggContext> {
    const self = this;
    let composedCache: compose.Middleware<EggContext> | undefined;
    return async (ctx, next) => {
      self.getGlobalMiddleware();
      if (!composedCache) {
        composedCache = self.globalMiddlewares ? compose([ mw, self.globalMiddlewares ]) : mw;
      }
      return composedCache(ctx, next);
    };
  }

@elrrrrrrr elrrrrrrr requested a review from akitaSummer June 25, 2026 09:43

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (1)

827-837: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Cache the composed pipeline after lazy resolution.

getGlobalMiddleware() is guarded, but Line 834 still rebuilds the composed middleware function on every request. Cache it in the closure so each registered MCP route composes once after app.middlewares is ready.

♻️ Proposed fix
   composeGlobalMiddleware(mw: compose.Middleware<EggContext>): compose.Middleware<EggContext> {
     const self = this;
+    let composed: compose.Middleware<EggContext> | undefined;
     return async (ctx, next) => {
-      self.getGlobalMiddleware();
-      const composed = self.globalMiddlewares ? compose([ mw, self.globalMiddlewares ]) : mw;
+      if (!composed) {
+        self.getGlobalMiddleware();
+        composed = self.globalMiddlewares ? compose([ mw, self.globalMiddlewares ]) : mw;
+      }
       return composed(ctx, next);
     };
   }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@plugin/controller/lib/impl/mcp/MCPControllerRegister.ts` around lines 827 -
837, The composeGlobalMiddleware wrapper in MCPControllerRegister still rebuilds
the composed middleware on every request even after getGlobalMiddleware() has
resolved app.middlewares; cache the composed result in the returned closure so
it is created once and reused. Update composeGlobalMiddleware to keep a
per-route cached composed middleware (initialized after the first lazy
resolution) and only fall back to mw until the global stack is available,
ensuring each MCP route composes once after app.middlewares is ready.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@plugin/controller/lib/impl/mcp/MCPControllerRegister.ts`:
- Around line 827-837: The composeGlobalMiddleware wrapper in
MCPControllerRegister still rebuilds the composed middleware on every request
even after getGlobalMiddleware() has resolved app.middlewares; cache the
composed result in the returned closure so it is created once and reused. Update
composeGlobalMiddleware to keep a per-route cached composed middleware
(initialized after the first lazy resolution) and only fall back to mw until the
global stack is available, ensuring each MCP route composes once after
app.middlewares is ready.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 16940fb5-9fd8-4ba0-aa7b-1a28ac44fb4b

📥 Commits

Reviewing files that changed from the base of the PR and between d68d6ad and 01c0def.

📒 Files selected for processing (1)
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts

Unit tests for the deferred resolution:
- composeGlobalMiddleware does not read app.middlewares at registration time
  (no throw, globalMiddlewares stays unbuilt) — the robustness the fix adds.
- the configured middleware is resolved and run on the first request, once
  app.middlewares has been populated by loadMiddleware.
- getGlobalMiddleware is idempotent (builds once).
- a genuinely unknown middleware name still throws at resolution time, so the
  deferral does not hide real misconfiguration.
- koa-compose onion ordering is preserved for multiple middlewares.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@elrrrrrrr elrrrrrrr force-pushed the fix/mcp-global-middleware-lazy branch from 5c3740b to 49bc715 Compare June 25, 2026 09:54
@elrrrrrrr

Copy link
Copy Markdown
Contributor Author

Wrong repo — tegg was merged into the eggjs/egg monorepo for egg4. Re-submitted as eggjs/egg#5994 (against the next branch). Closing this.

@elrrrrrrr elrrrrrrr closed this Jun 25, 2026
@elrrrrrrr elrrrrrrr deleted the fix/mcp-global-middleware-lazy branch June 25, 2026 10:08
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.

1 participant