|
| 1 | +import anyio |
| 2 | +import pytest |
| 3 | +from mcp.client.client import Client |
| 4 | +from mcp.server.mcpserver import MCPServer |
| 5 | +from mcp.shared._callable_inspection import split_positional_args |
| 6 | + |
| 7 | + |
| 8 | +def test_split_positional_args(): |
| 9 | + # 1. Pure positional-only |
| 10 | + def f1(x: str, /): |
| 11 | + return x |
| 12 | + |
| 13 | + args, kwargs = split_positional_args(f1, {"x": "hello"}) |
| 14 | + assert args == ("hello",) |
| 15 | + assert kwargs == {} |
| 16 | + |
| 17 | + # 2. Positional-only with defaults |
| 18 | + def f2(x: int, y: str = "default_y", /): |
| 19 | + return x, y |
| 20 | + |
| 21 | + # Default not overridden |
| 22 | + args, kwargs = split_positional_args(f2, {"x": 42}) |
| 23 | + assert args == (42,) |
| 24 | + assert kwargs == {} |
| 25 | + |
| 26 | + # Default overridden |
| 27 | + args, kwargs = split_positional_args(f2, {"x": 42, "y": "custom"}) |
| 28 | + assert args == (42, "custom") |
| 29 | + assert kwargs == {} |
| 30 | + |
| 31 | + # 3. Mixed positional-only, regular, and kw-only |
| 32 | + def f3(a: str, /, b: int, *, c: bool = True): |
| 33 | + return a, b, c |
| 34 | + |
| 35 | + args, kwargs = split_positional_args(f3, {"a": "pos", "b": 10, "c": False}) |
| 36 | + assert args == ("pos",) |
| 37 | + assert kwargs == {"b": 10, "c": False} |
| 38 | + |
| 39 | + # 4. No positional-only parameters |
| 40 | + def f4(a: str, b: int = 1): |
| 41 | + return a, b |
| 42 | + |
| 43 | + args, kwargs = split_positional_args(f4, {"a": "val", "b": 2}) |
| 44 | + assert args == () |
| 45 | + assert kwargs == {"a": "val", "b": 2} |
| 46 | + |
| 47 | + # 5. Method |
| 48 | + class Greeter: |
| 49 | + def greet(self, msg: str, /): |
| 50 | + return f"hi {msg}" |
| 51 | + |
| 52 | + g = Greeter() |
| 53 | + args, kwargs = split_positional_args(g.greet, {"msg": "there"}) |
| 54 | + assert args == ("there",) |
| 55 | + assert kwargs == {} |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.anyio |
| 59 | +async def test_mcpserver_posonly_tools_prompts_resources(): |
| 60 | + mcp = MCPServer("test_posonly") |
| 61 | + |
| 62 | + @mcp.tool() |
| 63 | + def sync_tool(msg: str, /) -> str: |
| 64 | + return f"sync:{msg}" |
| 65 | + |
| 66 | + @mcp.tool() |
| 67 | + async def async_tool(msg: str, /) -> str: |
| 68 | + return f"async:{msg}" |
| 69 | + |
| 70 | + @mcp.tool() |
| 71 | + def mixed_tool(x: int, /, y: str = "world") -> str: |
| 72 | + return f"{x}:{y}" |
| 73 | + |
| 74 | + @mcp.prompt() |
| 75 | + def sync_prompt(name: str, /) -> str: |
| 76 | + return f"hello {name}" |
| 77 | + |
| 78 | + @mcp.prompt() |
| 79 | + async def async_prompt(name: str, /) -> str: |
| 80 | + return f"async hello {name}" |
| 81 | + |
| 82 | + @mcp.resource("test://{item}") |
| 83 | + def sync_res(item: str, /) -> str: |
| 84 | + return f"content:{item}" |
| 85 | + |
| 86 | + @mcp.resource("async://{item}") |
| 87 | + async def async_res(item: str, /) -> str: |
| 88 | + return f"async_content:{item}" |
| 89 | + |
| 90 | + async with Client(mcp) as client: |
| 91 | + # Tool tests |
| 92 | + t1 = await client.call_tool("sync_tool", {"msg": "apple"}) |
| 93 | + assert not t1.is_error |
| 94 | + assert t1.content[0].text == "sync:apple" |
| 95 | + |
| 96 | + t2 = await client.call_tool("async_tool", {"msg": "banana"}) |
| 97 | + assert not t2.is_error |
| 98 | + assert t2.content[0].text == "async:banana" |
| 99 | + |
| 100 | + t3 = await client.call_tool("mixed_tool", {"x": 7, "y": "universe"}) |
| 101 | + assert not t3.is_error |
| 102 | + assert t3.content[0].text == "7:universe" |
| 103 | + |
| 104 | + t3_default = await client.call_tool("mixed_tool", {"x": 9}) |
| 105 | + assert not t3_default.is_error |
| 106 | + assert t3_default.content[0].text == "9:world" |
| 107 | + |
| 108 | + # Prompt tests |
| 109 | + p1 = await client.get_prompt("sync_prompt", {"name": "carol"}) |
| 110 | + assert p1.messages[0].content.text == "hello carol" |
| 111 | + |
| 112 | + p2 = await client.get_prompt("async_prompt", {"name": "dave"}) |
| 113 | + assert p2.messages[0].content.text == "async hello dave" |
| 114 | + |
| 115 | + # Resource tests |
| 116 | + r1 = await client.read_resource("test://gadget") |
| 117 | + assert r1.contents[0].text == "content:gadget" |
| 118 | + |
| 119 | + r2 = await client.read_resource("async://widget") |
| 120 | + assert r2.contents[0].text == "async_content:widget" |
0 commit comments