Skip to content

🚀 The fast, Pythonic way to build MCP servers and clients.

restore mcp tool generation to server

Jeremiah Lowin8d922d995afe
diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.py
index 55116159f..e7b267375 100644
--- a/src/fastmcp/server/server.py
+++ b/src/fastmcp/server/server.py
@@ -188,10 +188,6 @@ class FastMCP(Generic[LifespanResultT]):
"""Get all registered tools, indexed by registered key."""
return self._tool_manager.get_tools()
- def list_tools(self) -> list[Tool]:
- """List all registered tools."""
- return self._tool_manager.list_tools()
-
async def _mcp_list_tools(self) -> list[MCPTool]:
"""
List all available tools, in the format expected by the low-level MCP
@@ -199,7 +195,7 @@ class FastMCP(Generic[LifespanResultT]):
See `list_tools` for a more ergonomic way to list tools.
"""
- return self._tool_manager.list_mcp_tools()
+ return [tool.to_mcp_tool(name=key) for key, tool in self.get_tools().items()]
def get_context(self) -> "Context[ServerSession, LifespanResultT]":
"""
diff --git a/src/fastmcp/tools/tool_manager.py b/src/fastmcp/tools/tool_manager.py
index f11d98e22..f82c9f5be 100644
--- a/src/fastmcp/tools/tool_manager.py
+++ b/src/fastmcp/tools/tool_manager.py
@@ -7,7 +7,7 @@ from mcp.shared.context import LifespanContextT
from fastmcp.exceptions import ToolError
from fastmcp.settings import DuplicateBehavior
-from fastmcp.tools.tool import MCPTool, Tool
+from fastmcp.tools.tool import Tool
from fastmcp.utilities.logging import get_logger
if TYPE_CHECKING:
@@ -48,10 +48,6 @@ class ToolManager:
"""List all registered tools."""
return list(self.get_tools().values())
- def list_mcp_tools(self) -> list[MCPTool]:
- """List all registered tools in the format expected by the low-level MCP server."""
- return [tool.to_mcp_tool(name=key) for key, tool in self._tools.items()]
-
def add_tool_from_fn(
self,
fn: Callable[..., Any],
diff --git a/tests/server/test_proxy.py b/tests/server/test_proxy.py
index 1c1e23cd4..75e9b7a1f 100644
--- a/tests/server/test_proxy.py
+++ b/tests/server/test_proxy.py
@@ -80,9 +80,11 @@ async def test_create_proxy(fastmcp_server):
class TestTools:
- async def test_list_tools(self, proxy_server):
- tools = proxy_server.list_tools()
- assert [t.name for t in tools] == Contains("greet", "add", "error_tool")
+ async def test_get_tools(self, proxy_server):
+ tools = proxy_server.get_tools()
+ assert "greet" in tools
+ assert "add" in tools
+ assert "error_tool" in tools
async def test_list_tools_same_as_original(self, fastmcp_server, proxy_server):
assert (
diff --git a/tests/server/test_server.py b/tests/server/test_server.py
index 219c249f1..c5cef1fee 100644
--- a/tests/server/test_server.py
+++ b/tests/server/test_server.py
@@ -57,6 +57,34 @@ class TestCreateServer:
assert "¡Hola, 世界! 👋" == content.text
+class TestTools:
+ async def test_mcp_tool_name(self):
+ """Test MCPTool name for add_tool (key != tool.name)."""
+
+ mcp = FastMCP()
+
+ @mcp.tool()
+ def fn(x: int) -> int:
+ return x + 1
+
+ mcp_tools = await mcp._mcp_list_tools()
+ assert len(mcp_tools) == 1
+ assert mcp_tools[0].name == "fn"
+
+ async def test_mcp_tool_custom_name(self):
+ """Test MCPTool name for add_tool (key != tool.name)."""
+
+ mcp = FastMCP()
+
+ @mcp.tool(name="custom_name")
+ def fn(x: int) -> int:
+ return x + 1
+
+ mcp_tools = await mcp._mcp_list_tools()
+ assert len(mcp_tools) == 1
+ assert mcp_tools[0].name == "custom_name"
+
+
class TestToolDecorator:
async def test_no_tools_before_decorator(self):
mcp = FastMCP()
@@ -216,9 +244,8 @@ class TestToolDecorator:
mcp.add_tool(multiply, name="custom_multiply")
# Check that the tool is registered with the custom name
- tools = mcp.list_tools()
- tool_names = [t.name for t in tools]
- assert "custom_multiply" in tool_names
+ tools = mcp.get_tools()
+ assert "custom_multiply" in tools
# Call the tool by its custom name
result = await mcp.call_tool("custom_multiply", {"a": 5, "b": 3})
@@ -226,7 +253,7 @@ class TestToolDecorator:
assert result[0].text == "15"
# Original name should not be registered
- assert "multiply" not in tool_names
+ assert "multiply" not in tools
class TestResourceDecorator:
diff --git a/tests/tools/test_tool_manager.py b/tests/tools/test_tool_manager.py
index 5b8d79e7c..2c3639b56 100644
--- a/tests/tools/test_tool_manager.py
+++ b/tests/tools/test_tool_manager.py
@@ -612,35 +612,6 @@ class TestCustomToolNames:
with pytest.raises(ToolError):
await manager.call_tool("multiply", {"a": 5, "b": 3})
- def test_tool_to_mcp_tool(self):
- """Test that to_mcp_tool uses the key, not the internal name."""
-
- def some_function(x: int) -> int:
- return x
-
- manager = ToolManager()
- tool = Tool.from_function(some_function, name="api_function")
- manager.add_tool(tool)
-
- mcp_tools = manager.list_mcp_tools()
- assert len(mcp_tools) == 1
- assert mcp_tools[0].name == "api_function"
-
- def test_tool_to_mcp_tool_with_custom_key(self):
- """Test that to_mcp_tool uses the key, not the internal name."""
-
- def some_function(x: int) -> int:
- return x
-
- manager = ToolManager()
- tool = Tool.from_function(some_function, name="api_function")
- manager.add_tool(tool, key="custom-key")
-
- # When listing tools for MCP, the key should be used
- mcp_tools = manager.list_mcp_tools()
- assert len(mcp_tools) == 1
- assert mcp_tools[0].name == "custom-key"
-
def test_import_tools_with_custom_names(self):
"""Test importing tools with custom names."""
@@ -688,28 +659,3 @@ class TestCustomToolNames:
# But the function is different
assert stored_tool.fn.__name__ == "replacement_fn"
-
- def test_mcp_tool_name_for_add_tool(self):
- """Test MCPTool name for add_tool (key != tool.name)."""
-
- def fn(x: int) -> int:
- return x + 1
-
- tool = Tool.from_function(fn, name="my_tool")
- manager = ToolManager()
- manager.add_tool(tool, key="proxy_tool")
- mcp_tools = manager.list_mcp_tools()
- assert len(mcp_tools) == 1
- assert mcp_tools[0].name == "proxy_tool"
-
- def test_mcp_tool_name_for_add_tool_from_fn(self):
- """Test MCPTool name for add_tool_from_fn (key == tool.name)."""
-
- def fn(x: int) -> int:
- return x + 1
-
- manager = ToolManager()
- manager.add_tool_from_fn(fn, name="custom_fn")
- mcp_tools = manager.list_mcp_tools()
- assert len(mcp_tools) == 1
- assert mcp_tools[0].name == "custom_fn"