Skip to content

Conversation

@edmundhighcock
Copy link
Contributor

This PR adds support for lazy loading of MCP tools. Apart from a set of critical tools, MCP tools are not added to the prompt unless requested. A short description of MCP tools is added to the system prompt, and a tool is provided (load_mcp_tool) to load the full tool spec into the tools section of the prompt.

This commit adds a lazy loading mechanism for MCP tools to reduce token usage
and improve performance. The implementation includes:

- A tool summarizer module that extracts concise descriptions
- A new load_mcp_tool function to request detailed tool information on demand
- Updated system prompt to explain the lazy loading mechanism

Benefits:

- Reduced token usage in system prompts
- More context available for actual conversations
- Improved performance by loading tool details only when needed

The implementation is compatible with mcphub.nvim and includes documentation

for the required integration changes.
@edmundhighcock edmundhighcock force-pushed the feature/lazy_loading_new branch from 16bf665 to 11c6329 Compare November 25, 2025 09:58
@edmundhighcock
Copy link
Contributor Author

Hi All,

I have fixed the lint and type issues. Thanks for looking at this PR!

@yetone
Copy link
Owner

yetone commented Nov 29, 2025

I took a look at the code, and this PR actually makes not only the MCP tools but also the regular tools lazy-loaded, right?

@edmundhighcock
Copy link
Contributor Author

edmundhighcock commented Nov 29, 2025

I took a look at the code, and this PR actually makes not only the MCP tools but also the regular tools lazy-loaded, right?

Yes, apart from (1) those that are defined in the list of critical tools

  -- Define critical tools that should always be eagerly loaded regardless of user configuration
  local critical_tools = {
    "think",
    "attempt_completion",
    "load_mcp_tool",
    "use_mcp_tool",
    "add_todos",
    "update_todo_status",
    "list_tools",
    "dispatch_agent",
  }

and (2) those that are in Config.lazy_loading.always_eager. We could change the value of this to default to having all the internal tools as always eager, allowing the users to drop the ones they want (apart from the critical tools).

Are there other tools that should be in the list of critical tools?

@yetone
Copy link
Owner

yetone commented Dec 1, 2025

I took a look at the code, and this PR actually makes not only the MCP tools but also the regular tools lazy-loaded, right?

Yes, apart from (1) those that are defined in the list of critical tools

  -- Define critical tools that should always be eagerly loaded regardless of user configuration
  local critical_tools = {
    "think",
    "attempt_completion",
    "load_mcp_tool",
    "use_mcp_tool",
    "add_todos",
    "update_todo_status",
    "list_tools",
    "dispatch_agent",
  }

and (2) those that are in Config.lazy_loading.always_eager. We could change the value of this to default to having all the internal tools as always eager, allowing the users to drop the ones they want (apart from the critical tools).

Are there other tools that should be in the list of critical tools?

Thank you for the explanation, I think this is enough.

@yetone
Copy link
Owner

yetone commented Dec 1, 2025

Would it be more appropriate to call it load_tool or search_tool, because it's not just the tools of mcp that are being lazy loaded?

Regarding why it's called a search tool, I feel this feature is very similar to the tool search tool mentioned in this document by Anthropic: https://www.anthropic.com/engineering/advanced-tool-use

@edmundhighcock
Copy link
Contributor Author

Would it be more appropriate to call it load_tool or search_tool, because it's not just the tools of mcp that are being lazy loaded?

Regarding why it's called a search tool, I feel this feature is very similar to the tool search tool mentioned in this document by Anthropic: https://www.anthropic.com/engineering/advanced-tool-use

Thanks for the link that's very interesting to hear what Anthropic are doing. I vote for load_tool because it is not doing any searching. I'll make the change ASAP.

@edmundhighcock
Copy link
Contributor Author

I'll get to this tomorrow hopefully.. it has been a busy week!

@edmundhighcock
Copy link
Contributor Author

Changed load_mcp_tool to load_tool... ready for review again! Thansk in advance

@github-actions
Copy link

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the Stale label Dec 24, 2025
@edmundhighcock
Copy link
Contributor Author

Bump

@github-actions github-actions bot removed the Stale label Dec 27, 2025
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