Jeremiah Lowin commited on
Commit
b693865
·
1 Parent(s): 6d5c6bc

add testing doc

Browse files
Files changed (2) hide show
  1. docs/docs.json +2 -1
  2. docs/patterns/testing.mdx +38 -0
docs/docs.json CHANGED
@@ -62,7 +62,8 @@
62
  "patterns/decorating-methods",
63
  "patterns/openapi",
64
  "patterns/fastapi",
65
- "patterns/contrib"
 
66
  ]
67
  },
68
  {
 
62
  "patterns/decorating-methods",
63
  "patterns/openapi",
64
  "patterns/fastapi",
65
+ "patterns/contrib",
66
+ "patterns/testing"
67
  ]
68
  },
69
  {
docs/patterns/testing.mdx ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Testing MCP Servers
3
+ sidebarTitle: Testing
4
+ description: Learn how to test your FastMCP servers effectively
5
+ icon: vial
6
+ ---
7
+
8
+
9
+ Testing your MCP servers thoroughly is essential for ensuring they work correctly when deployed. FastMCP makes this easy through a variety of testing patterns.
10
+
11
+ ## In-Memory Testing
12
+
13
+ The most efficient way to test an MCP server is to pass your FastMCP server instance directly to a Client. This enables in-memory testing without having to start a separate server process, which is particularly useful because managing an MCP server programmatically can be challenging.
14
+
15
+ Here is an example of using a `Client` to test a server with pytest:
16
+
17
+ ```python
18
+ import pytest
19
+ from fastmcp import FastMCP, Client
20
+
21
+ @pytest.fixture
22
+ def mcp_server():
23
+ server = FastMCP("TestServer")
24
+
25
+ @server.tool()
26
+ def greet(name: str) -> str:
27
+ return f"Hello, {name}!"
28
+
29
+ return server
30
+
31
+ async def test_tool_functionality(mcp_server):
32
+ # Pass the server directly to the Client constructor
33
+ async with Client(mcp_server) as client:
34
+ result = await client.call_tool("greet", {"name": "World"})
35
+ assert "Hello, World!" in str(result[0])
36
+ ```
37
+
38
+ This pattern creates a direct connection between the client and server, allowing you to test your server's functionality efficiently.