Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
ee7b04a
1
Parent(s): 9afbf1e
Update lambda handling
Browse files- src/fastmcp/tools.py +4 -0
- tests/test_server.py +8 -3
- tests/test_tool_manager.py +9 -2
src/fastmcp/tools.py
CHANGED
|
@@ -30,6 +30,10 @@ class Tool(BaseModel):
|
|
| 30 |
) -> "Tool":
|
| 31 |
"""Create a Tool from a function."""
|
| 32 |
func_name = name or func.__name__
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
func_doc = description or func.__doc__ or ""
|
| 34 |
is_async = inspect.iscoroutinefunction(func)
|
| 35 |
|
|
|
|
| 30 |
) -> "Tool":
|
| 31 |
"""Create a Tool from a function."""
|
| 32 |
func_name = name or func.__name__
|
| 33 |
+
|
| 34 |
+
if func_name == "<lambda>":
|
| 35 |
+
raise ValueError("You must provide a name for lambda functions")
|
| 36 |
+
|
| 37 |
func_doc = description or func.__doc__ or ""
|
| 38 |
is_async = inspect.iscoroutinefunction(func)
|
| 39 |
|
tests/test_server.py
CHANGED
|
@@ -10,22 +10,27 @@ class TestServer:
|
|
| 10 |
assert server.name == "FastMCPServer"
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class TestServerTools:
|
| 14 |
async def test_add_tool(self):
|
| 15 |
server = FastMCPServer()
|
| 16 |
-
server.add_tool(
|
|
|
|
| 17 |
assert len(server._tool_manager.list_tools()) == 1
|
| 18 |
|
| 19 |
async def test_list_tools(self):
|
| 20 |
server = FastMCPServer()
|
| 21 |
-
server.add_tool(
|
| 22 |
async with client_session(server._mcp_server) as client:
|
| 23 |
tools = await client.list_tools()
|
| 24 |
assert len(tools.tools) == 1
|
| 25 |
|
| 26 |
async def test_call_tool(self):
|
| 27 |
server = FastMCPServer()
|
| 28 |
-
server.add_tool(
|
| 29 |
async with client_session(server._mcp_server) as client:
|
| 30 |
result = await client.call_tool("my_tool", {"arg1": "value"})
|
| 31 |
assert "error" not in result
|
|
|
|
| 10 |
assert server.name == "FastMCPServer"
|
| 11 |
|
| 12 |
|
| 13 |
+
def tool_fn(x: int, y: int) -> int:
|
| 14 |
+
return x + y
|
| 15 |
+
|
| 16 |
+
|
| 17 |
class TestServerTools:
|
| 18 |
async def test_add_tool(self):
|
| 19 |
server = FastMCPServer()
|
| 20 |
+
server.add_tool(tool_fn)
|
| 21 |
+
server.add_tool(tool_fn)
|
| 22 |
assert len(server._tool_manager.list_tools()) == 1
|
| 23 |
|
| 24 |
async def test_list_tools(self):
|
| 25 |
server = FastMCPServer()
|
| 26 |
+
server.add_tool(tool_fn)
|
| 27 |
async with client_session(server._mcp_server) as client:
|
| 28 |
tools = await client.list_tools()
|
| 29 |
assert len(tools.tools) == 1
|
| 30 |
|
| 31 |
async def test_call_tool(self):
|
| 32 |
server = FastMCPServer()
|
| 33 |
+
server.add_tool(tool_fn)
|
| 34 |
async with client_session(server._mcp_server) as client:
|
| 35 |
result = await client.call_tool("my_tool", {"arg1": "value"})
|
| 36 |
assert "error" not in result
|
tests/test_tool_manager.py
CHANGED
|
@@ -73,8 +73,15 @@ class TestAddTools:
|
|
| 73 |
|
| 74 |
def test_add_lambda(self):
|
| 75 |
manager = ToolManager()
|
| 76 |
-
manager.add_tool(lambda x: x)
|
| 77 |
-
assert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def test_warn_on_duplicate_tools(self):
|
| 80 |
"""Test warning on duplicate tools."""
|
|
|
|
| 73 |
|
| 74 |
def test_add_lambda(self):
|
| 75 |
manager = ToolManager()
|
| 76 |
+
tool = manager.add_tool(lambda x: x, name="my_tool")
|
| 77 |
+
assert tool.name == "my_tool"
|
| 78 |
+
|
| 79 |
+
def test_add_lambda_with_no_name(self):
|
| 80 |
+
manager = ToolManager()
|
| 81 |
+
with pytest.raises(
|
| 82 |
+
ValueError, match="You must provide a name for lambda functions"
|
| 83 |
+
):
|
| 84 |
+
manager.add_tool(lambda x: x)
|
| 85 |
|
| 86 |
def test_warn_on_duplicate_tools(self):
|
| 87 |
"""Test warning on duplicate tools."""
|