Skip to content

feat: support context decorator in MCP controllers#458

Merged
akitaSummer merged 2 commits into
masterfrom
feat/mcp-context-decorator
Jun 17, 2026
Merged

feat: support context decorator in MCP controllers#458
akitaSummer merged 2 commits into
masterfrom
feat/mcp-context-decorator

Conversation

@akitaSummer

@akitaSummer akitaSummer commented Jun 17, 2026

Copy link
Copy Markdown
Contributor
Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

Summary by CodeRabbit

Release Notes

  • New Features

    • Added context parameter support for MCP tools, resources, and prompts—handlers can now inject and access request context via the @Context() decorator.
  • Tests

    • Updated MCP tests to validate context parameter injection and retrieval in handler operations.

@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 introduces context parameter injection support for MCP (Model Context Protocol) prompts, tools, and resources. It updates metadata builders, metadata models, and the MCPServerHelper to resolve and inject the context parameter index. Additionally, it integrates AsyncLocalStorage in the service worker register to store and retrieve the fetch context. The feedback recommends adding error handling to the unawaited transport.handleRequest call to prevent unhandled promise rejections, and using optional chaining when validating the ctx object in test fixtures to avoid potential runtime TypeErrors.

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 thread standalone/service-worker/src/mcp/MCPControllerRegister.ts
Comment thread standalone/service-worker/test/fixtures/mcp/MCPTestController.ts Outdated
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c0b57f01-d35f-464b-86fa-a2219bd76289

📥 Commits

Reviewing files that changed from the base of the PR and between bf89468 and a3088a8.

📒 Files selected for processing (16)
  • core/controller-decorator/src/impl/mcp/MCPControllerPromptMetaBuilder.ts
  • core/controller-decorator/src/impl/mcp/MCPControllerResourceMetaBuilder.ts
  • core/controller-decorator/src/impl/mcp/MCPControllerToolMetaBuilder.ts
  • core/controller-decorator/src/model/MCPPromptMeta.ts
  • core/controller-decorator/src/model/MCPResourceMeta.ts
  • core/controller-decorator/src/model/MCPToolMeta.ts
  • core/controller-decorator/test/MCPMeta.test.ts
  • core/controller-decorator/test/fixtures/MCPController.ts
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts
  • plugin/controller/lib/impl/mcp/MCPServerHelper.ts
  • plugin/controller/test/fixtures/apps/mcp-app/app/controller/McpController.ts
  • plugin/controller/test/fixtures/apps/mcp-app/app/controller/TestController.ts
  • plugin/controller/test/mcp/helper.test.ts
  • standalone/service-worker/src/mcp/MCPControllerRegister.ts
  • standalone/service-worker/src/mcp/MCPServerHelper.ts
  • standalone/service-worker/test/fixtures/mcp/MCPTestController.ts

📝 Walkthrough

Walkthrough

This PR adds @Context() parameter injection support to MCP (tool, prompt, resource) handlers. MCPToolMeta, MCPPromptMeta, and MCPResourceMeta now implement MethodMeta and carry a contextParamIndex field populated by the meta builders. Both the plugin and service-worker MCPServerHelper classes gain centralized build*Args helpers that insert the request context at the declared index, wired via a getContext callback.

Changes

MCP Context Parameter Injection

Layer / File(s) Summary
MCP meta models: contextParamIndex and MethodMeta
core/controller-decorator/src/model/MCPToolMeta.ts, core/controller-decorator/src/model/MCPPromptMeta.ts, core/controller-decorator/src/model/MCPResourceMeta.ts
MCPToolMeta, MCPPromptMeta, and MCPResourceMeta each implement MethodMeta and gain an optional contextParamIndex: number | undefined property and constructor option.
Meta builders: read contextParamIndex
core/controller-decorator/src/impl/mcp/MCPControllerToolMetaBuilder.ts, .../MCPControllerPromptMetaBuilder.ts, .../MCPControllerResourceMetaBuilder.ts
All three builder build() methods now call MethodInfoUtil.getMethodContextIndex(...) and pass the result into the meta constructor.
Plugin MCPServerHelper: getContext and centralized arg builders
plugin/controller/lib/impl/mcp/MCPServerHelper.ts, plugin/controller/lib/impl/mcp/MCPControllerRegister.ts
MCPServerHelperOptions gains optional getContext; three private build*Args helpers centralize argument cloning and context injection at contextParamIndex. Registration handlers delegate to these builders. MCPControllerRegister wires getContext to app.currentContext.
Service-worker MCPServerHelper and MCPControllerRegister: context via AsyncLocalStorage
standalone/service-worker/src/mcp/MCPServerHelper.ts, standalone/service-worker/src/mcp/MCPControllerRegister.ts
Mirrors the plugin helper changes; MCPControllerRegister creates an AsyncLocalStorage<ServiceWorkerFetchContext>, wraps stream request handling in contextStorage.run(ctx, ...), and passes getContext returning the stored context.
Tests and fixtures: @Context() usage and assertions
core/controller-decorator/test/fixtures/MCPController.ts, core/controller-decorator/test/MCPMeta.test.ts, plugin/controller/test/mcp/helper.test.ts, plugin/controller/test/fixtures/apps/mcp-app/app/controller/McpController.ts, plugin/controller/test/fixtures/apps/mcp-app/app/controller/TestController.ts, standalone/service-worker/test/fixtures/mcp/MCPTestController.ts
Fixture controllers inject @Context() into MCP handlers; tests assert contextParamIndex values on metadata and verify handlers receive the correct context object.

Sequence Diagram(s)

sequenceDiagram
  rect rgba(173, 216, 230, 0.5)
    Note over MCPControllerRegister,MethodInfoUtil: Metadata build time
    MCPControllerRegister->>MCPControllerToolMetaBuilder: build(clazz, method)
    MCPControllerToolMetaBuilder->>MethodInfoUtil: getMethodContextIndex(clazz, method)
    MethodInfoUtil-->>MCPControllerToolMetaBuilder: contextParamIndex
    MCPControllerToolMetaBuilder-->>MCPControllerRegister: MCPToolMeta { contextParamIndex }
  end
  rect rgba(144, 238, 144, 0.5)
    Note over MCPControllerRegister,ControllerMethod: Request invocation time
    MCPControllerRegister->>MCPServerHelper: new MCPServerHelper({ getContext: () => currentContext })
    MCPServerHelper->>MCPServerHelper: buildToolArgs(args, toolMeta, hasSchema)
    MCPServerHelper->>MCPServerHelper: getContext?.() → ctx
    MCPServerHelper->>MCPServerHelper: newArgs[contextParamIndex] = ctx
    MCPServerHelper->>ControllerMethod: Reflect.apply(method, target, newArgs)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • eggjs/tegg#307: Introduced the MCP controller metadata and server registration plumbing that this PR extends with contextParamIndex.
  • eggjs/tegg#321: Refactored MCPServerHelper and MCPControllerRegister in the plugin, directly in the same files this PR modifies.
  • eggjs/tegg#436: Introduced the standalone service-worker MCP stack (MCPServerHelper, MCPControllerRegister) that this PR extends with getContext/AsyncLocalStorage support.

Suggested labels

enhancement

Suggested reviewers

  • killagu

🐇 Hops through context with glee,
@Context() injected, plain to see!
contextParamIndex marks the spot,
AsyncLocalStorage ties the knot.
Now MCP handlers know who's there —
A rabbit's gift, passed with care! 🎉

🚥 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 'feat: support context decorator in MCP controllers' directly aligns with the main changes, which add contextParamIndex support and @Context() decorator functionality across all MCP controller metadata builders and handlers.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/mcp-context-decorator

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

core/controller-decorator/src/impl/mcp/MCPControllerPromptMetaBuilder.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

core/controller-decorator/src/impl/mcp/MCPControllerResourceMetaBuilder.ts

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.

core/controller-decorator/src/impl/mcp/MCPControllerToolMetaBuilder.ts

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.

  • 13 others

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 and usage tips.

@gxkl gxkl 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.

LGTM

@akitaSummer akitaSummer merged commit 283a331 into master Jun 17, 2026
12 checks passed
@akitaSummer akitaSummer deleted the feat/mcp-context-decorator branch June 17, 2026 11:32
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.

2 participants