mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-22 05:24:18 +02:00
Update lambda handling
This commit is contained in:
parent
156ecc8bae
commit
4ef0f61753
3 changed files with 21 additions and 5 deletions
|
|
@ -30,6 +30,10 @@ class Tool(BaseModel):
|
|||
) -> "Tool":
|
||||
"""Create a Tool from a function."""
|
||||
func_name = name or func.__name__
|
||||
|
||||
if func_name == "<lambda>":
|
||||
raise ValueError("You must provide a name for lambda functions")
|
||||
|
||||
func_doc = description or func.__doc__ or ""
|
||||
is_async = inspect.iscoroutinefunction(func)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,22 +10,27 @@ class TestServer:
|
|||
assert server.name == "FastMCPServer"
|
||||
|
||||
|
||||
def tool_fn(x: int, y: int) -> int:
|
||||
return x + y
|
||||
|
||||
|
||||
class TestServerTools:
|
||||
async def test_add_tool(self):
|
||||
server = FastMCPServer()
|
||||
server.add_tool(lambda x: x)
|
||||
server.add_tool(tool_fn)
|
||||
server.add_tool(tool_fn)
|
||||
assert len(server._tool_manager.list_tools()) == 1
|
||||
|
||||
async def test_list_tools(self):
|
||||
server = FastMCPServer()
|
||||
server.add_tool(lambda x: x)
|
||||
server.add_tool(tool_fn)
|
||||
async with client_session(server._mcp_server) as client:
|
||||
tools = await client.list_tools()
|
||||
assert len(tools.tools) == 1
|
||||
|
||||
async def test_call_tool(self):
|
||||
server = FastMCPServer()
|
||||
server.add_tool(lambda x: x)
|
||||
server.add_tool(tool_fn)
|
||||
async with client_session(server._mcp_server) as client:
|
||||
result = await client.call_tool("my_tool", {"arg1": "value"})
|
||||
assert "error" not in result
|
||||
|
|
|
|||
|
|
@ -73,8 +73,15 @@ class TestAddTools:
|
|||
|
||||
def test_add_lambda(self):
|
||||
manager = ToolManager()
|
||||
manager.add_tool(lambda x: x)
|
||||
assert len(manager.list_tools()) == 1
|
||||
tool = manager.add_tool(lambda x: x, name="my_tool")
|
||||
assert tool.name == "my_tool"
|
||||
|
||||
def test_add_lambda_with_no_name(self):
|
||||
manager = ToolManager()
|
||||
with pytest.raises(
|
||||
ValueError, match="You must provide a name for lambda functions"
|
||||
):
|
||||
manager.add_tool(lambda x: x)
|
||||
|
||||
def test_warn_on_duplicate_tools(self):
|
||||
"""Test warning on duplicate tools."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue