Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cookbook/tools/mcp/tool_name_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""This example demonstrates how to add a prefix to the name of your MCP tools.
This is useful to avoid name collisions with other tools, especially when using multiple MCP servers."""

import asyncio

from agno.agent.agent import Agent
from agno.tools.mcp import MCPTools


async def run_agent():
# Development environment tools
dev_tools = MCPTools(
transport="streamable-http",
url="https://docs.agno.com/mcp",
# By providing this tool_name_prefix, all the tool names will be prefixed with "dev_"
tool_name_prefix="dev",
)
await dev_tools.connect()

agent = Agent(tools=[dev_tools])
await agent.aprint_response("Which tools do you have access to? List them all.")

await dev_tools.close()


if __name__ == "__main__":
asyncio.run(run_agent())
9 changes: 8 additions & 1 deletion libs/agno/agno/tools/mcp/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
include_tools: Optional[list[str]] = None,
exclude_tools: Optional[list[str]] = None,
refresh_connection: bool = False,
tool_name_prefix: Optional[str] = "",
**kwargs,
):
"""
Expand Down Expand Up @@ -71,6 +72,7 @@ def __init__(
self.include_tools = include_tools
self.exclude_tools = exclude_tools
self.refresh_connection = refresh_connection
self.tool_name_prefix = tool_name_prefix

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

# Get tool name prefix if available
tool_name_prefix = ""
if self.tool_name_prefix is not None:
tool_name_prefix = self.tool_name_prefix + "_"

# Register the tools with the toolkit
for tool in filtered_tools:
try:
# Get an entrypoint for the tool
entrypoint = get_entrypoint_for_tool(tool, self.session) # type: ignore
# Create a Function for the tool
f = Function(
name=tool.name,
name=tool_name_prefix + tool.name,
description=tool.description,
parameters=tool.inputSchema,
entrypoint=entrypoint,
Expand Down