Skip to content

Commit e6183a3

Browse files
hypen-codeHarsha Bandaramanuhortet
authored
feat: add tool_name_prefix param to MCPTools (#5271)
This PR adds support for namespacing MCP tools to avoid naming conflicts and improve environment distinction. Closes #5270 ## Changes - Added `tool_name_prefix` parameter to `MCPTools.__init__()` - Implemented automatic prefixing logic in tool registration - Maintained backward compatibility with empty string default --------- Co-authored-by: Harsha Bandara <Harsha.Bandara@cloudsolutions.com.sa> Co-authored-by: manu <manuhortet@gmail.com>
1 parent da7b099 commit e6183a3

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""This example demonstrates how to add a prefix to the name of your MCP tools.
2+
This is useful to avoid name collisions with other tools, especially when using multiple MCP servers."""
3+
4+
import asyncio
5+
6+
from agno.agent.agent import Agent
7+
from agno.tools.mcp import MCPTools
8+
9+
10+
async def run_agent():
11+
# Development environment tools
12+
dev_tools = MCPTools(
13+
transport="streamable-http",
14+
url="https://docs.agno.com/mcp",
15+
# By providing this tool_name_prefix, all the tool names will be prefixed with "dev_"
16+
tool_name_prefix="dev",
17+
)
18+
await dev_tools.connect()
19+
20+
agent = Agent(tools=[dev_tools])
21+
await agent.aprint_response("Which tools do you have access to? List them all.")
22+
23+
await dev_tools.close()
24+
25+
26+
if __name__ == "__main__":
27+
asyncio.run(run_agent())

libs/agno/agno/tools/mcp/mcp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(
4343
include_tools: Optional[list[str]] = None,
4444
exclude_tools: Optional[list[str]] = None,
4545
refresh_connection: bool = False,
46+
tool_name_prefix: Optional[str] = "",
4647
**kwargs,
4748
):
4849
"""
@@ -71,6 +72,7 @@ def __init__(
7172
self.include_tools = include_tools
7273
self.exclude_tools = exclude_tools
7374
self.refresh_connection = refresh_connection
75+
self.tool_name_prefix = tool_name_prefix
7476

7577
if session is None and server_params is None:
7678
if transport == "sse" and url is None:
@@ -279,14 +281,19 @@ async def build_tools(self) -> None:
279281
if self.include_tools is None or tool.name in self.include_tools:
280282
filtered_tools.append(tool)
281283

284+
# Get tool name prefix if available
285+
tool_name_prefix = ""
286+
if self.tool_name_prefix is not None:
287+
tool_name_prefix = self.tool_name_prefix + "_"
288+
282289
# Register the tools with the toolkit
283290
for tool in filtered_tools:
284291
try:
285292
# Get an entrypoint for the tool
286293
entrypoint = get_entrypoint_for_tool(tool, self.session) # type: ignore
287294
# Create a Function for the tool
288295
f = Function(
289-
name=tool.name,
296+
name=tool_name_prefix + tool.name,
290297
description=tool.description,
291298
parameters=tool.inputSchema,
292299
entrypoint=entrypoint,

0 commit comments

Comments
 (0)