Is it possible to load mcp tools on demand in conversation to save tokens? #5
Replies: 1 comment 3 replies
-
That's a great idea. I am currently working on something similar. Let's say you have 10 mcp servers configured in your config file. let's say each server have 5 tools. That's 50 tools which makes the system prompt massive. In order to disable unnecessary servers you currently have to disable some servers from the config file and restart. But this is not ideal. Instead, What we can do is open the UI, press "t" on a server name to disable that server. Similarly press "t" on a tool to disable that particular tool. So If you dont need a server or tools on a server like Please let me know if this solves your case? This functionality should be available in a day. If this isn't for you, another way could be to extend current tool: tools = {
["mcp"] = {
-- callback = vim.fn.stdpath("config") .. "/lua/user/codecompanion/tools/mcp.lua",
-- callback = require("mcphub.extensions.codecompanion"),
callback = vim.tbl_deep_extend("force", require("mcphub.extensions.codecompanion"), {
system_prompt = function(schema)
local prompt_utils = require("mcphub.utils.prompt")
local xml2lua = require("codecompanion.utils.xml.xml2lua")
local servers = require("mcphub.state").server_state.servers or {}
local allowed_servers = {
fetch = {},
todoist = {
disable_tools = { "todoist_delete_task" }
},
filesystem = {
disable_tools = { "get_file_info", "list_directory" }
}
}
-- Filter out servers and tools that are not allowed
local filtered_servers = vim.tbl_map(function(s)
-- Filter out tools that are not allowed for the server
s.capabilities.tools = vim.tbl_filter(function(t)
return not vim.tbl_contains(allowed_servers[s.name].disable_tools or {}, t.name)
end, s.capabilities.tools)
return s
end, vim.tbl_filter(function(s)
return vim.tbl_contains(vim.tbl_keys(allowed_servers), s.name)
end, servers))
return string.format([[### MCP Tool
⚠️ **CRITICAL INSTRUCTIONS - READ CAREFULLY** ⚠️
The Model Context Protocol (MCP) enables communication with locally running MCP servers that provide additional tools and resources to extend your capabilities.
1. **ONE TOOL CALL PER RESPONSE**:
- YOU MUST MAKE ONLY ONE TOOL CALL PER RESPONSE
- NEVER chain multiple tool calls in a single response
- For tasks requiring multiple tools, you MUST wait for the result of each tool before proceeding
2. **ONLY USE AVAILABLE SERVERS AND TOOLS**:
- ONLY use the servers and tools listed in the "Connected MCP Servers" section below
- DO NOT invent or hallucinate server names, tool names, or resource URIs
- If a requested server or tool is not listed in "Connected MCP Servers", inform the user it's not available
3. **GATHER REQUIRED INFORMATION FIRST**:
- NEVER use placeholder values for parameters e.g {"id": "YOUR_ID_HERE"}
- NEVER guess or make assumptions about parameters like IDs, or file paths etc
- Before making tool calls:
* CALL other tools to get the required information first e.g listing available files or database pages before writing to them.
* ASK the user for needed information if not provided
4. **Dependent Operations Workflow**:
- Step 1: Make ONE tool call
- Step 2: WAIT for the user to show you the result
- Step 3: Only THEN, in a NEW response, make the next tool call
5. **Forbidden Pattern Examples**:
❌ DO NOT DO THIS: Multiple <tools> blocks in one response
❌ DO NOT DO THIS: Using placeholder values or made-up while calling tools e.g {"id": "YOUR_ID_HERE"}
6. **Correct Pattern Examples**:
✅ DO THIS: List available resources first → Wait for result → Use correct parameters
✅ DO THIS: Verify parameters are correct before making tool calls
✅ DO THIS: Ask for clarification when user requests are unclear
7. **XML Structure Requirements**:
- Format: ```xml<tools><tool name="mcp"><action type="...">...</action></tool></tools>```
- ALWAYS use name="mcp" for the tool tag
- Inside the tool must be exactly ONE <action> tag with type="use_mcp_tool" OR type="access_mcp_resource"
- Except for optional attributes, ALL required parameters must be provided for actions.
8. **Available Actions**:
The only valid action types are "use_mcp_tool" and "access_mcp_resource":
%s
%s
%s]], prompt_utils.get_use_mcp_tool_prompt(xml2lua.toXml({
tools = { schema[1] }
})), -- gets the prompt for the use_mcp_tool action
prompt_utils.get_access_mcp_resource_prompt(xml2lua.toXml({
tools = { schema[2] }
})), -- gets the prompt for the access_mcp_resource action
prompt_utils.get_active_servers_prompt(filtered_servers)
)
end,
}),
description = "Call tools and resources from the MCP Servers",
opts = {
user_approval = true,
},
},
}
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For example, besides setting a single
mcp
tool, export the ability to export a banch of tools by something likerequire("mcphub.extensions.codecompanion").get("mcp_server_name")
, so that we don't have to load full prompts that we actually don't access all of them.In my case, the configuration below + default system prompts costs about 10K tokens:
Although I am a copilot user, but to save token is to speed up responses from LLM :)
Beta Was this translation helpful? Give feedback.
All reactions