Add ergonomic Client class for testing MCP servers (#1870)
This commit is contained in:
parent
8adb5bdce8
commit
df039bf97c
1 changed files with 1298 additions and 526 deletions
|
|
@ -12,18 +12,16 @@ import pytest
|
|||
from pydantic import AnyUrl
|
||||
from pytest_examples import CodeExample, EvalExample, find_examples
|
||||
|
||||
from examples.fastmcp.complex_inputs import mcp as complex_inputs_mcp
|
||||
from examples.fastmcp.desktop import mcp as desktop_mcp
|
||||
from examples.fastmcp.direct_call_tool_result_return import mcp as direct_call_tool_result_mcp
|
||||
from examples.fastmcp.simple_echo import mcp as simple_echo_mcp
|
||||
from mcp.shared.memory import create_connected_server_and_client_session as client_session
|
||||
from mcp import Client
|
||||
from mcp.types import TextContent, TextResourceContents
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_simple_echo():
|
||||
"""Test the simple echo server"""
|
||||
async with client_session(simple_echo_mcp._mcp_server) as client:
|
||||
from examples.fastmcp.simple_echo import mcp
|
||||
|
||||
async with Client(mcp) as client:
|
||||
result = await client.call_tool("echo", {"text": "hello"})
|
||||
assert len(result.content) == 1
|
||||
content = result.content[0]
|
||||
|
|
@ -34,7 +32,9 @@ async def test_simple_echo():
|
|||
@pytest.mark.anyio
|
||||
async def test_complex_inputs():
|
||||
"""Test the complex inputs server"""
|
||||
async with client_session(complex_inputs_mcp._mcp_server) as client:
|
||||
from examples.fastmcp.complex_inputs import mcp
|
||||
|
||||
async with Client(mcp) as client:
|
||||
tank = {"shrimp": [{"name": "bob"}, {"name": "alice"}]}
|
||||
result = await client.call_tool("name_shrimp", {"tank": tank, "extra_names": ["charlie"]})
|
||||
assert len(result.content) == 3
|
||||
|
|
@ -49,7 +49,9 @@ async def test_complex_inputs():
|
|||
@pytest.mark.anyio
|
||||
async def test_direct_call_tool_result_return():
|
||||
"""Test the CallToolResult echo server"""
|
||||
async with client_session(direct_call_tool_result_mcp._mcp_server) as client:
|
||||
from examples.fastmcp.direct_call_tool_result_return import mcp
|
||||
|
||||
async with Client(mcp) as client:
|
||||
result = await client.call_tool("echo", {"text": "hello"})
|
||||
assert len(result.content) == 1
|
||||
content = result.content[0]
|
||||
|
|
@ -69,7 +71,9 @@ async def test_desktop(monkeypatch: pytest.MonkeyPatch):
|
|||
monkeypatch.setattr(Path, "iterdir", lambda self: mock_files) # type: ignore[reportUnknownArgumentType]
|
||||
monkeypatch.setattr(Path, "home", lambda: Path("/fake/home"))
|
||||
|
||||
async with client_session(desktop_mcp._mcp_server) as client:
|
||||
from examples.fastmcp.desktop import mcp
|
||||
|
||||
async with Client(mcp) as client:
|
||||
# Test the sum function
|
||||
result = await client.call_tool("sum", {"a": 1, "b": 2})
|
||||
assert len(result.content) == 1
|
||||
|
|
|
|||
Loading…
Reference in a new issue