Skip to content

Commit 2f13119

Browse files
strawgateCopilot
andauthored
Add Documentation for FastMCP Server Testing (#2244)
* Add doc with recommendations for server testing * Updates to testing doc * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * add to sidebar under patterns * Update docs/patterns/testing.mdx Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent c002bc3 commit 2f13119

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

docs/docs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@
236236
"patterns/tool-transformation",
237237
"patterns/decorating-methods",
238238
"patterns/cli",
239-
"patterns/contrib"
239+
"patterns/contrib",
240+
"patterns/testing"
240241
]
241242
},
242243
{

docs/patterns/testing.mdx

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Testing your FastMCP Server
3+
sidebarTitle: Testing
4+
description: How to test your FastMCP server.
5+
icon: vial
6+
---
7+
8+
The best way to ensure a reliable and maintainable FastMCP Server is to test it! The FastMCP Client combined with Pytest provides a simple and powerful way to test your FastMCP servers.
9+
10+
Using Pytest Fixtures, you can wrap your FastMCP Server in a Client instance that makes interacting with your server fast and easy. This is especially useful when building your own MCP Servers and enables a tight development loop by allowing you to avoid using a separate tool like MCP Inspector during development:
11+
12+
```python
13+
import pytest
14+
from fastmcp.client import Client
15+
from fastmcp.client.transports import FastMCPTransport
16+
17+
from my_project.main import mcp
18+
19+
@pytest.fixture
20+
async def main_mcp_client():
21+
async with Client(transport=mcp) as mcp_client:
22+
yield mcp_client
23+
24+
async def test_list_tools(main_mcp_client: Client[FastMCPTransport]):
25+
list_tools = await main_mcp_client.list_tools()
26+
27+
assert len(list_tools) == 5
28+
```
29+
30+
We recommend the [inline-snapshot library](https://github.com/15r10nk/inline-snapshot) for asserting complex data structures coming from your MCP Server. This library allows you to write tests that are easy to read and understand, and are also easy to update when the data structure changes.
31+
32+
```python
33+
from inline_snapshot import snapshot
34+
35+
async def test_list_tools(main_mcp_client: Client[FastMCPTransport]):
36+
list_tools = await main_mcp_client.list_tools()
37+
38+
assert list_tools == snapshot()
39+
```
40+
41+
Simply run `pytest --inline-snapshot=fix,create` to fill in the `snapshot()` with actual data.
42+
43+
<Tip>
44+
For values that change you can leverage the [dirty-equals](https://github.com/samuelcolvin/dirty-equals) library to perform flexible equality assertions on dynamic or non-deterministic values.
45+
</Tip>
46+
47+
Using the pytest `parametrize` decorator, you can easily test your tools with a wide variety of inputs.
48+
49+
```python
50+
import pytest
51+
from my_project.main import mcp
52+
53+
from fastmcp.client import Client
54+
from fastmcp.client.transports import FastMCPTransport
55+
@pytest.fixture
56+
async def main_mcp_client():
57+
async with Client(mcp) as client:
58+
yield client
59+
60+
61+
@pytest.mark.parametrize(
62+
"first_number, second_number, expected",
63+
[
64+
(1, 2, 3),
65+
(2, 3, 5),
66+
(3, 4, 7),
67+
],
68+
)
69+
async def test_add(
70+
first_number: int,
71+
second_number: int,
72+
expected: int,
73+
main_mcp_client: Client[FastMCPTransport],
74+
):
75+
result = await main_mcp_client.call_tool(
76+
name="add", arguments={"x": first_number, "y": second_number}
77+
)
78+
assert result.data is not None
79+
assert isinstance(result.data, int)
80+
assert result.data == expected
81+
```
82+
83+
<Tip>
84+
The [FastMCP Repository contains thousands of tests](https://github.com/jlowin/fastmcp/tree/main/tests) for the FastMCP Client and Server. Everything from connecting to remote MCP servers, to testing tools, resources, and prompts is covered, take a look for inspiration!
85+
</Tip>

0 commit comments

Comments
 (0)