Skip to content

feat: lazy loading mcp#2

Merged
neooriginal merged 2 commits intomainfrom
lazy-loading-mcp
Jun 24, 2025
Merged

feat: lazy loading mcp#2
neooriginal merged 2 commits intomainfrom
lazy-loading-mcp

Conversation

@neooriginal
Copy link
Owner

@neooriginal neooriginal commented Jun 24, 2025

This pull request introduces a significant enhancement to the MCP (Model Context Protocol) tools by implementing a lazy-loading architecture. This approach optimizes resource usage by deferring server connections until tools are explicitly invoked. The changes span multiple files and include updates to tool discovery, execution, and metadata handling.

Lazy Loading for MCP Tools:

  • tools/mcp/main.js: Added lazy-loading functionality to allow static tool discovery without starting servers. Introduced methods like getAvailableToolsLazy and ensureConnection to manage tool retrieval and server connections on-demand. Updated tool metadata to include isStatic and adjusted discovery logic to differentiate between connected and configured servers. [1] [2] [3]

  • tools/mcp/main.js: Enhanced DynamicMcpExecutor to support lazy-loading during capability discovery. Tools are now retrieved from static configurations or connected servers as needed. Updated logging and metadata to reflect lazy-loading mode. [1] [2]

  • tools/mcp/main.js: Modified getMcpToolsForAI and getAvailableTools to support lazy-loading, allowing tools to be retrieved without forcing server connections. Added metadata fields like lazyMode and updated summary outputs to indicate lazy-loading status. [1] [2]

Updates to Tool Metadata and Execution:

  • tools/mcp/main.js: Updated tool metadata to include isStatic and adjusted the execution logic in callTool to ensure server connections are established lazily when required. [1] [2]

  • tools/AI/prompts.js: Updated the global prompt generator to include lazy-loading notes and display tool statuses (e.g., static or connected).

Documentation and Cleanup:

  • todo.MD: Removed outdated notes about MCP tool execution and added clarity to the "Nice to have" section.

This update significantly improves the efficiency and scalability of MCP tools by introducing lazy-loading, reducing unnecessary server connections, and providing clearer tool metadata.

Summary by CodeRabbit

  • New Features

    • Enhanced tool integration with lazy loading, allowing tools to be discovered without starting all servers.
    • Tools are now labeled as "static" or "connected" for clearer status visibility.
    • Servers will only start when a tool is actually invoked, improving resource efficiency.
  • Documentation

    • Updated documentation to reflect the new lazy-loading architecture and tool discovery process.
  • Chores

    • Removed a completed item from the "Urgent" task list.

@entelligence-ai-pr-reviews
Copy link

Unable to Process PR Review

The author of this PR does not exist on Entelligence Dashboard. Please add the user to Entelligence AI here to enable reviews for this user.

@coderabbitai
Copy link

coderabbitai bot commented Jun 24, 2025

Walkthrough

The changes implement a lazy-loading architecture for MCP (Modular Command Protocol) tools, allowing tool discovery without starting all servers upfront. Tool metadata now indicates whether tools are static or connected. The AI prompt is updated to reflect this lazy-loading behavior, and a related task is removed from the todo list.

Changes

File(s) Change Summary
todo.MD Removed a task related to MCP tool lazy loading from the "Urgent" list.
tools/AI/prompts.js Updated AI prompt generation: passes lazy-loading flag, annotates tool status, and adds a note about MCP tool lazy loading.
tools/mcp/main.js Introduced lazy-loading for MCP tools, added static tool configs, updated client/manager classes, and refactored tool discovery.

Sequence Diagram(s)

sequenceDiagram
    participant AI_Agent
    participant Prompt_Generator
    participant McpClientManager
    participant MCP_Server (optional)

    AI_Agent->>Prompt_Generator: Request available MCP tools
    Prompt_Generator->>McpClientManager: getMcpToolsForAI(forceConnect=false)
    McpClientManager->>McpClientManager: Gather static tools from config
    McpClientManager->>McpClientManager: Gather tools from connected servers
    McpClientManager-->>Prompt_Generator: Return tool list (static/connected)
    Prompt_Generator-->>AI_Agent: Return annotated tool list and note

    AI_Agent->>McpClientManager: Call a specific tool
    McpClientManager->>McpClientManager: ensureConnection(serverName)
    alt Server not connected
        McpClientManager->>MCP_Server: Start/connect on-demand
    end
    McpClientManager->>MCP_Server: Execute tool
    MCP_Server-->>McpClientManager: Return result
    McpClientManager-->>AI_Agent: Return tool result
Loading

Poem

In the warren of code, the tools now awake,
Only when needed, for efficiency’s sake!
Static or connected, the prompt knows the way,
With lazy discovery, we all hop and play.
No more urgent tasks in our burrow today!
🐇✨

✨ 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: 0

🧹 Nitpick comments (2)
tools/mcp/main.js (2)

154-178: Consider refining the lazy loading logic.

The getAvailableToolsLazy() method will automatically connect to the server if no static tools are available, which might not be the expected "lazy" behavior. Consider these approaches:

  1. Only connect if explicitly requested via forceConnect=true
  2. Return empty array if no static tools and no forced connection
  3. Add a separate parameter to control fallback behavior
-    async getAvailableToolsLazy(forceConnect = false) {
-        if (!forceConnect && this.staticTools.length > 0) {
-            return [...this.staticTools];
-        }
-        
-        if (!this.isConnected) {
-            await this.connect();
-        }
-        
-        return [...this.availableTools];
-    }
+    async getAvailableToolsLazy(forceConnect = false) {
+        if (!forceConnect) {
+            return [...this.staticTools];
+        }
+        
+        if (!this.isConnected) {
+            await this.connect();
+        }
+        
+        return [...this.availableTools];
+    }

814-814: Apply optional chaining as suggested by static analysis.

The code can be simplified using optional chaining for better readability and consistency with modern JavaScript practices.

-            const isConnected = client && client.isServerConnected();
+            const isConnected = client?.isServerConnected();
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0b8a34a and 35d5bd5.

📒 Files selected for processing (3)
  • todo.MD (0 hunks)
  • tools/AI/prompts.js (1 hunks)
  • tools/mcp/main.js (15 hunks)
💤 Files with no reviewable changes (1)
  • todo.MD
🧰 Additional context used
🧬 Code Graph Analysis (1)
tools/AI/prompts.js (1)
tools/mcp/main.js (2)
  • require (6-6)
  • require (7-7)
🪛 Biome (1.9.4)
tools/mcp/main.js

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

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (9)
tools/mcp/main.js (6)

22-22: LGTM! Clean addition for lazy-loading support.

The staticTools property enables tool discovery without server startup, which is essential for the lazy-loading architecture.


812-838: Excellent implementation of lazy tool discovery.

This method effectively implements the core lazy-loading strategy by:

  • Prioritizing tools from already connected servers
  • Falling back to static tool configurations without connecting
  • Implementing proper caching for static tools
  • Maintaining clean separation between connected and static tools

The logic is sound and supports the architecture's goals.


840-854: Well-implemented on-demand connection logic.

The ensureConnection() method properly implements the lazy connection pattern by:

  • Checking existing client state before connecting
  • Reusing existing connections when available
  • Providing a clean interface for on-demand server startup

This is a key component of the lazy-loading architecture.


872-885: Perfect implementation of lazy execution.

The updated callTool() method now properly implements lazy loading by using ensureConnection() to establish server connections only when tools are actually invoked. This is exactly the behavior promised by the lazy-loading architecture.


2021-2101: Excellent backward-compatible enhancement.

The enhanced getAvailableTools() method successfully bridges legacy and new functionality by:

  • Maintaining backward compatibility with forceConnect=true
  • Defaulting to lazy loading for improved performance
  • Providing comprehensive metadata about tool and server states
  • Including clear indicators for static vs connected tools

The implementation maintains clean separation between the two modes while providing rich information for debugging and monitoring.


2220-2266: Outstanding documentation of the lazy-loading architecture.

The comprehensive documentation clearly explains:

  • The three lazy-loading modes and their use cases
  • Parameter handling and type coercion strategies
  • Usage patterns and expected behaviors
  • The benefits of the lazy-loading approach

This level of documentation is crucial for a complex architecture like this and will significantly help maintainers and users understand the system.

tools/AI/prompts.js (3)

17-17: Correct integration with lazy-loading MCP API.

The explicit false parameter properly enables lazy loading mode, ensuring MCP servers are not started during prompt generation. This aligns perfectly with the performance goals of the lazy-loading architecture.


25-26: Great user experience enhancement.

Adding status indicators for " (static)" vs " (connected)" tools provides valuable transparency to users about tool availability and whether server startup will be required. This helps users understand the lazy-loading behavior.


31-31: Important user education about lazy loading.

The explanatory note helps users understand the lazy loading behavior and sets appropriate expectations about when servers will start. This prevents confusion about tool availability and potential initial delays.

@neooriginal neooriginal merged commit 89d53de into main Jun 24, 2025
2 checks passed
@neooriginal neooriginal deleted the lazy-loading-mcp branch June 24, 2025 02:52
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