What this thread does
Do first: Use async with Client(mcp) as client around calls to your imported FastMCP server.
Short answer
The client can connect directly to the server object. Use an async pytest fixture to share the connection pattern, then assert tool discovery and the returned data.
What to do
| Value | Action / meaning |
|---|---|
pip install pytest-asyncio | Install the dependency for async test functions and fixtures. |
pyproject.toml; [tool.pytest.ini_options]; asyncio_mode = "auto" | Recommended pytest configuration; avoids needing @pytest.mark.asyncio on every async test. |
from fastmcp.client import Client | Import the client; Client(mcp) and Client(transport=mcp) use the server instance. |
@pytest.fixture; async with Client(transport=mcp) | An async fixture yields the connected client and closes its context afterward. |
from fastmcp.client.transports import FastMCPTransport | The example annotates the fixture consumer as Client[FastMCPTransport]. |
await client.list_tools() | Inspect tool discovery; the documented fixture example expects 5 tools in its own sample server. |
call_tool(name="add", arguments={"x":1,"y":2}); result.data=3 | The add example calls the named tool, checks non-None and integer data, then checks the expected sum. |
@pytest.mark.parametrize; (1,2,3); (2,3,5); (3,4,7) | The documented input/output cases for the add example. |
inline-snapshot; snapshot(); pytest --inline-snapshot=fix,create | Optional complex-result assertions; this command fills snapshots from actual data. |
dirty-equals | Optional flexible equality assertions for dynamic values. |
With the documented sample add tool registered on mcp:
from fastmcp.client import Client
async with Client(mcp) as client:
result = await client.call_tool(name="add", arguments={"x": 1, "y": 2})
assert result.data == 3
Limits and open questions
- The 5-tool count and add cases belong to the documentation sample; use expectations for your own server.
- This direct connection does not exercise a deployed HTTP endpoint. The page gives no package version or release date.
Not for: MCP Inspector interactive inspection or remote HTTP deployment checks.
Sources
- Testing entry page and redirect, FastMCP.
- Testing your FastMCP Server, FastMCP.
- Testing Markdown, FastMCP.
Checked
2026-11-17; read the linked Testing entry page and redirect, Testing your FastMCP Server, Testing Markdown; no command was run.
Started from a post by @FastMCP, packaged by the editor. Original post