Skip to content

Commit 140e8a5

Browse files
committed
code review
1 parent 09934af commit 140e8a5

3 files changed

Lines changed: 24 additions & 49 deletions

File tree

src/mcp/server/fastmcp/server.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
asynccontextmanager,
1111
)
1212
from itertools import chain
13-
from typing import Any, Generic, Literal, overload
13+
from typing import Any, Generic, Literal
1414

1515
import anyio
1616
import pydantic_core
@@ -315,42 +315,31 @@ async def read_resource(self, uri: AnyUrl | str) -> Iterable[ReadResourceContent
315315
logger.error(f"Error reading resource {uri}: {e}")
316316
raise ResourceError(str(e))
317317

318-
@overload
319-
def add_tool(self, fn: Tool) -> None: ...
318+
def add_tool_instance(self, tool: Tool) -> None:
319+
"""Add a Tool instance to the server."""
320+
self._tool_manager.add_tool_instance(tool)
320321

321-
@overload
322322
def add_tool(
323323
self,
324324
fn: AnyFunction,
325325
name: str | None = None,
326326
description: str | None = None,
327327
annotations: ToolAnnotations | None = None,
328-
) -> None: ...
329-
330-
def add_tool(
331-
self,
332-
fn: AnyFunction | Tool,
333-
name: str | None = None,
334-
description: str | None = None,
335-
annotations: ToolAnnotations | None = None,
336328
) -> None:
337329
"""Add a tool to the server.
338330
339331
The tool function can optionally request a Context object by adding a parameter
340332
with the Context type annotation. See the @tool decorator for examples.
341333
342334
Args:
343-
fn: The function to register as a tool or a Tool instance
335+
fn: The function to register as a tool
344336
name: Optional name for the tool (defaults to function name)
345337
description: Optional description of what the tool does
346338
annotations: Optional ToolAnnotations providing additional tool information
347339
"""
348-
if isinstance(fn, Tool):
349-
self._tool_manager.add_tool(fn)
350-
else:
351-
self._tool_manager.add_tool(
352-
fn, name=name, description=description, annotations=annotations
353-
)
340+
self._tool_manager.add_tool(
341+
fn, name=name, description=description, annotations=annotations
342+
)
354343

355344
def tool(
356345
self,

src/mcp/server/fastmcp/tools/tool_manager.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations as _annotations
22

33
from collections.abc import Callable
4-
from typing import TYPE_CHECKING, Any, overload
4+
from typing import TYPE_CHECKING, Any
55

66
from mcp.server.fastmcp.exceptions import ToolError
77
from mcp.server.fastmcp.tools.base import Tool
@@ -31,42 +31,28 @@ def list_tools(self) -> list[Tool]:
3131
"""List all registered tools."""
3232
return list(self._tools.values())
3333

34-
@overload
35-
def add_tool(
36-
self,
37-
fn: Tool,
38-
) -> Tool: ...
34+
def add_tool_instance(self, tool: Tool) -> Tool:
35+
"""Add a Tool instance to the server."""
36+
existing = self._tools.get(tool.name)
37+
if existing:
38+
if self.warn_on_duplicate_tools:
39+
logger.warning(f"Tool already exists: {tool.name}")
40+
return existing
41+
self._tools[tool.name] = tool
42+
return tool
3943

40-
@overload
4144
def add_tool(
4245
self,
4346
fn: Callable[..., Any],
4447
name: str | None = None,
4548
description: str | None = None,
4649
annotations: ToolAnnotations | None = None,
47-
) -> Tool: ...
48-
49-
def add_tool(
50-
self,
51-
fn: Callable[..., Any] | Tool,
52-
name: str | None = None,
53-
description: str | None = None,
54-
annotations: ToolAnnotations | None = None,
5550
) -> Tool:
5651
"""Add a tool to the server."""
57-
if isinstance(fn, Tool):
58-
tool = fn
59-
else:
60-
tool = Tool.from_function(
61-
fn, name=name, description=description, annotations=annotations
62-
)
63-
existing = self._tools.get(tool.name)
64-
if existing:
65-
if self.warn_on_duplicate_tools:
66-
logger.warning(f"Tool already exists: {tool.name}")
67-
return existing
68-
self._tools[tool.name] = tool
69-
return tool
52+
tool = Tool.from_function(
53+
fn, name=name, description=description, annotations=annotations
54+
)
55+
return self.add_tool_instance(tool)
7056

7157
async def call_tool(
7258
self,

tests/server/fastmcp/test_tool_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def add(a: int, b: int) -> int:
3232
assert tool.parameters["properties"]["a"]["type"] == "integer"
3333
assert tool.parameters["properties"]["b"]["type"] == "integer"
3434

35-
def test_add_tool_directly(self):
35+
def test_add_tool_instance(self):
3636
manager = ToolManager()
3737

3838
def add(a: int, b: int) -> int:
@@ -54,7 +54,7 @@ class AddArguments(ArgModelBase):
5454
context_kwarg=None,
5555
annotations=None,
5656
)
57-
manager.add_tool(original_tool)
57+
manager.add_tool_instance(original_tool)
5858
saved_tool = manager.get_tool("add")
5959
assert saved_tool == original_tool
6060

0 commit comments

Comments
 (0)