Skip to content

Commit eab5244

Browse files
committed
test tool_call
1 parent 21ef750 commit eab5244

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/mcpservertemplate/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def add_numbers(
1313
b: Annotated[float, Field(description="The second number")],
1414
) -> str:
1515
"""Add two numbers and return the result as a string."""
16-
return f"{a} + {b} = {a+b} from MCP Template"
16+
return str(a + b)
1717

1818

1919
def main():

tests/test_server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from mcp import ClientSession
33
from mcp import StdioServerParameters
44
from mcp import stdio_client
5+
from mcp.types import TextContent
6+
7+
from mcpservertemplate.server import add_numbers
58

69

710
@pytest.fixture
@@ -16,3 +19,17 @@ async def test_list_tools(server_params: StdioServerParameters) -> None:
1619

1720
result = await session.list_tools()
1821
assert len(result.tools) > 0
22+
23+
24+
@pytest.mark.asyncio
25+
async def test_call_tool(server_params: StdioServerParameters) -> None:
26+
async with stdio_client(server_params) as (read, write), ClientSession(read, write) as session:
27+
await session.initialize()
28+
29+
a = 1234
30+
b = 5678
31+
result = await session.call_tool("add_numbers", {"a": a, "b": b})
32+
33+
assert len(result.content) == 1
34+
assert isinstance(result.content[0], TextContent)
35+
assert float(result.content[0].text) == a + b

0 commit comments

Comments
 (0)