8000 refactor: mcp server helper by akitaSummer · Pull Request #321 · eggjs/tegg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: mcp server helper #321

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

Merged
merged 3 commits into from
May 14, 2025

Conversation

akitaSummer
Copy link
Contributor
@akitaSummer akitaSummer commented May 14, 2025
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

  • Refactor
    • Introduced helper components to streamline registration of prompts, tools, and resources on MCP servers.
    • Improved delegation and consolidation of server initialization and registration processes.
    • Enhanced maintainability and scalability of server-side resource handling.

No changes to user-facing features or functionality.

Copy link
coderabbitai bot commented May 14, 2025

"""

Walkthrough

The MCPControllerRegister class is refactored to delegate all MCP server registration logic to new MCPServerHelper instances, removing its own registration methods and direct server management. A new MCPServerHelper class is introduced to encapsulate the registration of prompts, tools, and resources, centralizing initialization and control flow for MCP server interactions.

Changes

Files/Paths Change Summary
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts Refactored to remove direct MCP server usage; replaced with MCPServerHelper instances. Removed in-class registration methods and delegated registration to helpers. Simplified register() method.
plugin/controller/lib/impl/mcp/MCPServerHelper.ts Introduced new MCPServerHelper class to handle registration of resources, tools, and prompts on MCP servers. Provides encapsulated methods for each registration type and manages controller hooks.
plugin/controller/test/mcp/helper.test.ts Added new test suite for MCPServerHelper covering registration and invocation of MCP resources, tools, and prompts with client-server interaction verification.
plugin/controller/package.json Modified test script by removing NODE_OPTIONS='--no-deprecation' environment variable setting.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant MCPControllerRegister
    participant MCPServerHelper
    participant McpServer
    participant Controller

    App->>MCPControllerRegister: register()
    MCPControllerRegister->>MCPServerHelper: mcpPromptRegister / mcpToolRegister / mcpResourceRegister
    MCPServerHelper->>McpServer: register prompt/tool/resource
    McpServer->>Controller: invoke method with prepared args
Loading

Suggested reviewers

  • killagu

Poem

In the warren of code, helpers leap in,
Old methods retire, new flows begin.
Controllers now delegate, less to recall,
Helpers take charge, registering all.
With every refactor, the garden grows neat—
🐇 A hop to the future, code tidy and sweet!
"""

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.

plugin/controller/test/mcp/helper.test.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-eggache".

(The package "eslint-plugin-eggache" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-eggache@latest --save-dev

The plugin "eslint-plugin-eggache" was referenced from the config file in ".eslintrc » eslint-config-egg/typescript » ./index.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

plugin/controller/lib/impl/mcp/MCPControllerRegister.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-eggache".

(The package "eslint-plugin-eggache" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-eggache@latest --save-dev

The plugin "eslint-plugin-eggache" was referenced from the config file in ".eslintrc » eslint-config-egg/typescript » ./index.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🔭 Outside diff range comments (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (1)

118-146: ⚠️ Potential issue

statelessMcpServerHelper may be undefined and await is missing
connectStatelessStreamTransport() is async, but statelessMcpServerHelper.server.connect(...) is not awaited, and the helper itself is only instantiated later inside register().
If this static method is ever invoked before register(), the code will throw TypeError: Cannot read properties of undefined. A defensive check plus an await solves both issues:

-      MCPControllerRegister.instance.statelessMcpServerHelper.server.connect(
-        MCPControllerRegister.instance.statelessTransport
-      );
+      const helper = MCPControllerRegister.instance.statelessMcpServerHelper;
+      if (!helper) {
+        throw new Error('statelessMcpServerHelper has not been initialised. Call register() first.');
+      }
+      await helper.server.connect(MCPControllerRegister.instance.statelessTransport);
🧰 Tools
🪛 Biome (1.9.4)

[error] 118-118: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

♻️ Duplicate comments (1)
plugin/controller/lib/impl/mcp/MCPServerHelper.ts (1)

158-173: Same duplication bug as in mcpToolRegister
mcpPromptRegister repeats the exact newArgs = [...newArgs, ...args] logic. Apply the same fix here to prevent duplicated parameter lists.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a78565 and aa8fabe.

📒 Files selected for processing (2)
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (6 hunks)
  • plugin/controller/lib/impl/mcp/MCPServerHelper.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Runner-ubuntu (22)
  • GitHub Check: Runner-ubuntu (18)
  • GitHub Check: Runner-ubuntu (20)
  • GitHub Check: Runner-ubuntu (16)
  • GitHub Check: Runner-macos (18)
  • GitHub Check: Runner-macos (20)
  • GitHub Check: Runner-macos (16)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: Analyze (javascript)

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (1)

489-499: Asynchronous registration calls are now properly awaited

Good improvement! All the registration calls are now properly awaited, which fixes the potential race condition issue identified in the previous review.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2468b1b and b0819b7.

📒 Files selected for processing (2)
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (6 hunks)
  • plugin/controller/lib/impl/mcp/MCPServerHelper.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugin/controller/lib/impl/mcp/MCPServerHelper.ts
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Runner-ubuntu (22)
  • GitHub Check: Runner-ubuntu (20)
  • GitHub Check: Runner-ubuntu (18)
  • GitHub Check: Runner-macos (20)
  • GitHub Check: Runner-ubuntu (16)
  • GitHub Check: Runner-macos (16)
  • GitHub Check: Runner-macos (18)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Analyze (typescript)
🔇 Additional comments (5)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (5)

29-29: LGTM! Good addition of the MCPServerHelper import.

The import of MCPServerHelper is correctly added to support the refactoring.


78-79: LGTM! Refactoring to use helper classes.

The addition of these helper properties is a good architectural improvement. It encapsulates MCP server functionality and follows the single responsibility principle.


269-269: LGTM! Connect method is properly awaited.

Good use of await for the asynchronous connection operation.


360-360: LGTM! Connect method is properly awaited.

Good use of await for the asynchronous connection operation.


467-482: LGTM! Proper initialization of helper classes.

The initialization of helpers is well-structured and only happens once. Good practice to include both name and version from the controller metadata, with fallbacks to sensible defaults.

@akitaSummer akitaSummer force-pushed the refactor/mcp_server_helper branch from b0819b7 to b831d4b Compare May 14, 2025 07:44
Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (3)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts ( 10000 3)

119-119: Connect method is now properly awaited

Good fix! The async connect method is now properly awaited, which addresses a potential race condition mentioned in a previous review.


489-491: Asynchronous registration calls are now properly awaited

Great improvement! The asynchronous registration calls are now properly awaited, which prevents potential race conditions where the server might start handling requests before registration completes.


466-466: ⚠️ Potential issue

Method signature should be updated to match implementation

The register() method performs async operations but isn't marked as async. This creates a disconnect between the method signature and its implementation.

-  async register() {
+  async register() {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b0819b7 and b831d4b.

📒 Files selected for processing (2)
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (6 hunks)
  • plugin/controller/lib/impl/mcp/MCPServerHelper.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugin/controller/lib/impl/mcp/MCPServerHelper.ts
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Runner-ubuntu (20)
  • GitHub Check: Runner-ubuntu (22)
  • GitHub Check: Runner-ubuntu (18)
  • GitHub Check: Runner-macos (16)
  • GitHub Check: Runner-ubuntu (16)
  • GitHub Check: Runner-macos (20)
  • GitHub Check: Runner-macos (18)
🔇 Additional comments (7)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (7)

29-29: New dependency imported for the refactor

Good addition of the MCPServerHelper import to support the delegation pattern implemented in this refactor.


78-79: Public properties added for MCPServerHelper instances

Exposing these helpers as public properties makes the implementation more transparent and follows the delegation pattern correctly.


269-269: Connect method properly awaited

The connect method is now correctly awaited, which prevents potential race conditions when setting up the transport.


360-360: Connect method properly awaited

The connect method is now correctly awaited, which prevents potential race conditions when setting up the transport.


467-482: Good initialization of MCPServerHelper instances

The initialization logic properly creates the helper instances with appropriate configuration and only when needed. It also correctly initializes the MCP servers.


493-495: Asynchronous registration calls are now properly awaited

The asynchronous resource registration calls are now properly awaited, preventing race conditions.


497-499: Asynchronous registration calls are now properly awaited

The asynchronous tool registration calls are now properly awaited, preventing race conditions.

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (1)

483-502: Consider adding error handling for registration failures

The registration process delegates to helper methods but lacks proper error handling. If any registration fails, the error will propagate up and could potentially disrupt the entire registration process.

Consider adding try-catch blocks to handle potential registration failures:

  for (const proto of this.controllerProtos) {
    if (this.registeredControllerProtos.includes(proto)) {
      continue;
    }
    const metadata = proto.getMetaData(CONTROLLER_META_DATA) as MCPControllerMeta;
+   try {
      for (const prompt of metadata.prompts) {
        await this.mcpServerHelper.mcpPromptRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this), proto, prompt);
        await this.statelessMcpServerHelper.mcpPromptRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this), proto, prompt);
      }
      for (const resource of metadata.resources) {
        await this.mcpServerHelper.mcpResourceRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this), proto, resource);
        await this.statelessMcpServerHelper.mcpResourceRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this), proto, resource);
      }
      for (const tool of metadata.tools) {
        await this.mcpServerHelper.mcpToolRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this), proto, tool);
        await this.statelessMcpServerHelper.mcpToolRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this), proto, tool);
      }
      this.registeredControllerProtos.push(proto);
+   } catch (error) {
+     this.app.logger.error('Failed to register controller %s: %o', proto.name, error);
+     // Optionally, consider whether to throw or continue with registration of other controllers
+   }
  }
🧹 Nitpick comments (4)
plugin/controller/test/mcp/helper.test.ts (4)

13-18: Consider adding more descriptive test names

While the current test name works, consider adding more descriptive test names that explain what specific aspects of the MCPServerHelper you're testing.

-describe('plugin/controller/test/mcp/mcp.test.ts', () => {
-  it('MCPServerHelper should work', async () => {
+describe('MCPServerHelper', () => {
+  it('should register and handle prompts, tools, and resources correctly', async () => {

58-92: Consider separating mock object creation into a helper function

The mock object creation could be extracted into a helper function to improve readability and maintainability.

+const createMockEggObject = (promptMeta, toolMeta, resourceMeta) => ({ obj: {
+  [promptMeta.name]: args => {
+    return {
+      messages: [
+        {
+          role: 'user',
+          content: {
+            type: 'text',
+            text: `Generate a concise but descriptive commit message for these changes:\n\n${args.name}`,
+          },
+        },
+      ],
+    };
+  },
+  [toolMeta.name]: args => {
+    return {
+      content: [
+        {
+          type: 'text',
+          text: `npm package: ${args.name} not found`,
+        },
+      ],
+    };
+  },
+  [resourceMeta.name]: uri => {
+    return {
+      contents: [
+        {
+          uri: uri.toString(),
+          text: 'MOCK TEXT',
+        },
+      ],
+    };
+  },
+} });
+
+// Then use it in the test
+const getOrCreateEggObject = () => createMockEggObject(promptMeta, toolMeta, resourceMeta);

94-96: Avoid using type assertions in tests

Using as any type assertions can hide type errors and reduce type safety. Consider creating proper mock objects that match the expected types.

-await helper.mcpToolRegister(getOrCreateEggObject as any, { getMetaData: () => ({}) } as any, toolMeta);
+// Create a properly typed mock
+const typedProto = {
+  getMetaData: () => ({}),
+  name: 'mockedProto'
+  // Add other required properties of EggPrototype
+};
+await helper.mcpToolRegister(getOrCreateEggObject, typedProto, toolMeta);

171-173: Add test for error handling scenarios

The test covers the happy path but doesn't test any error scenarios. Consider adding tests for how the helper handles registration failures or invalid requests.

+  it('should handle registration errors correctly', async () => {
+    const helper = new MCPServerHelper({
+      name: 'test-errors',
+      version: '1.0.0',
+      hooks: [],
+    });
+    
+    // Test with invalid metadata
+    const invalidResourceMeta = new MCPResourceMeta({
+      name: 'invalidResource',
+      needAcl: false,
+      middlewares: [],
+      // Missing both uri and template should cause an error
+    });
+    
+    try {
+      await helper.mcpResourceRegister(() => ({ obj: {} }), { getMetaData: () => ({}) } as any, invalidResourceMeta);
+      assert.fail('Should have thrown an error');
+    } catch (error) {
+      assert.ok(error.message.includes('must have uri or template'));
+    }
+  });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b831d4b and dc2e31e.

📒 Files selected for processing (3)
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (6 hunks)
  • plugin/controller/package.json (1 hunks)
  • plugin/controller/test/mcp/helper.test.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • plugin/controller/package.json
🧰 Additional context used
🧬 Code Graph Analysis (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (6)
core/runtime/src/factory/EggContainerFactory.ts (1)
  • EggContainerFactory (18-88)
core/types/metadata/model/EggPrototype.ts (1)
  • EggPrototype (117-154)
plugin/controller/lib/impl/mcp/MCPServerHelper.ts (1)
  • MCPServerHelper (20-184)
core/metadata/src/model/graph/GlobalGraph.ts (1)
  • proto (143-159)
core/types/controller-decorator/MetadataKey.ts (1)
  • CONTROLLER_META_DATA (9-9)
core/controller-decorator/src/model/MCPControllerMeta.ts (1)
  • MCPControllerMeta (7-70)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Runner-ubuntu (22)
  • GitHub Check: Runner-ubuntu (20)
  • GitHub Check: Runner-ubuntu (18)
  • GitHub Check: Runner-ubuntu (16)
  • GitHub Check: Runner-macos (16)
  • GitHub Check: Runner-macos (20)
  • GitHub Check: Runner-macos (18)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (14)
plugin/controller/test/mcp/helper.test.ts (7)

1-12: LGTM: Well-structured test imports and setup

The imports are properly organized, bringing in the necessary components for testing the MCPServerHelper class.


25-29: LGTM: Clean MCPServerHelper instantiation

Clean instantiation of the MCPServerHelper with proper configuration parameters.


31-56: LGTM: Proper MCP metadata setup

The test correctly sets up the necessary metadata for resources, tools, and prompts with appropriate configurations.


98-115: LGTM: Proper client-server transport setup

The test correctly sets up the in-memory transport pair and connects both client and server.


116-133: LGTM: Thorough tool registration and invocation testing

The test validates both listing of tools and calling tools with arguments, with proper assertions.


134-151: LGTM: Complete resource and prompt testing

The test covers listing and reading resources as well as listing prompts with their argument metadata.


153-169: LGTM: Proper prompt invocation testing

The test correctly validates prompt invocation and the expected response format.

plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (7)

29-29: LGTM: MCPServerHelper import added

The import of MCPServerHelper is appropriate for the refactoring.


72-80: Server implementation delegated to helper classes

The refactoring correctly moves the server management to dedicated helper classes, improving separation of concerns.


119-119: Connect method is properly awaited

Good job addressing the previous comment by ensuring the connect method is awaited, preventing potential race conditions.


269-269: Server reference correctly updated

The code now properly uses the server property from the helper instance.


360-360: Server reference correctly updated

The code now properly uses the server property from the helper instance.


467-482: LGTM: Helper instances initialization

The helper instances are properly initialized with appropriate configuration parameters.


488-499: Asynchronous registration calls are properly awaited

Good job addressing the previous comment by properly awaiting the registration calls to prevent race conditions.

@akitaSummer akitaSummer force-pushed the refactor/mcp_server_helper branch from dc2e31e to 0e113b8 Compare May 14, 2025 10:08
Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (1)

483-501: Consider add 8000 ing error handling for registration failures

The registration process delegates to helper methods but lacks proper error handling. If any registration fails, the error will propagate up and could potentially disrupt the entire registration process.

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

466-466: Add explicit async declaration to the register method

The register method contains await statements but isn't explicitly marked as async. While TypeScript automatically returns a Promise when await is used, explicit declaration improves code clarity.

- async register() {
+ async register(): Promise<void> {

467-482: Initialization logic could be more robust

The initialization only checks if mcpServerHelper exists but doesn't verify statelessMcpServerHelper. While they're always initialized together in the current implementation, a more robust check would verify both instances.

- if (!this.mcpServerHelper) {
+ if (!this.mcpServerHelper || !this.statelessMcpServerHelper) {

489-490: Optimize repetitive binding operations

The binding of getOrCreateEggObject is repeated within each loop iteration. It would be more efficient to create this bound function once before the loops.

  async register() {
    if (!this.mcpServerHelper) {
      this.mcpServerHelper = new MCPServerHelper({
        name: this.controllerMeta.name ?? `chair-mcp-${this.app.name}-server`,
        version: this.controllerMeta.version ?? '1.0.0',
        hooks: MCPControllerRegister.hooks,
      });
      this.statelessMcpServerHelper = new MCPServerHelper({
        name: this.controllerMeta.name ?? `chair-mcp-${this.app.name}-server`,
        version: this.controllerMeta.version ?? '1.0.0',
        hooks: MCPControllerRegister.hooks,
      });
      this.mcpStatelessStreamServerInit();
      this.mcpStreamServerInit();
      this.mcpServerInit();
      this.mcpServerRegister();
    }
+   const getOrCreateObject = this.eggContainerFactory.getOrCreateEggObject.bind(this.eggContainerFactory);
    for (const proto of this.controllerProtos) {
      if (this.registeredControllerProtos.includes(proto)) {
        continue;
      }
      const metadata = proto.getMetaData(CONTROLLER_META_DATA) as MCPControllerMeta;
      for (const prompt of metadata.prompts) {
-       await this.mcpServerHelper.mcpPromptRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this.eggContainerFactory), proto, prompt);
-       await this.statelessMcpServerHelper.mcpPromptRegister(this.eggContainerFactory.getOrCreateEggObject.bind(this.eggContainerFactory), proto, prompt);
+       await this.mcpServerHelper.mcpPromptRegister(getOrCreateObject, proto, prompt);
+       await this.statelessMcpServerHelper.mcpPromptRegister(getOrCreateObject, proto, prompt);
      }
      // Apply the same pattern to the other loops

29-29: Consider adding export for MCPServerHelper

The MCPServerHelper is only imported but not re-exported from this file. If this is meant to be used by external code, consider adding an export.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dc2e31e and 0e113b8.

📒 Files selected for processing (3)
  • plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (6 hunks)
  • plugin/controller/package.json (1 hunks)
  • plugin/controller/test/mcp/helper.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • plugin/controller/package.json
  • plugin/controller/test/mcp/helper.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: Runner-ubuntu (18)
  • GitHub Check: Runner-ubuntu (16)
  • GitHub Check: Runner-ubuntu (22)
  • GitHub Check: Runner-macos (20)
  • GitHub Check: Runner-macos (18)
  • GitHub Check: Runner-macos (16)
  • GitHub Check: Runner-ubuntu (20)
🔇 Additional comments (2)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (2)

119-119: LGTM! Connect method is now properly awaited

Excellent! I see you've fixed the issue from the previous review by properly awaiting the connect method. This prevents potential race conditions.


72-79: Good refactoring approach using helper classes

The refactoring to use helper classes improves separation of concerns by moving server management to dedicated MCPServerHelper instances instead of handling everything in the register class.

Copy link
Contributor
@killagu killagu left a comment

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 8d4cb0b into eggjs:master May 14, 2025
13 checks passed
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
0