mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 23:29:10 +02:00
| .. | ||
| tests | ||
| pyproject.toml | ||
| README.md | ||
| server.py | ||
| uv.lock | ||
FastMCP Testing Demo
A comprehensive example demonstrating FastMCP testing patterns with pytest-asyncio.
Overview
This example shows how to:
- Set up pytest-asyncio configuration in
pyproject.toml - Write async test fixtures for MCP clients
- Test tools, resources, and prompts
- Use parametrized tests for multiple scenarios
- Leverage inline-snapshot and dirty-equals for assertions
Project Structure
testing_demo/
├── pyproject.toml # Project config with pytest-asyncio setup
├── server.py # Simple MCP server with tools/resources/prompts
├── tests/
│ └── test_server.py # Comprehensive test suite
└── README.md # This file
Key Features
pyproject.toml Configuration
The pyproject.toml includes the critical pytest-asyncio configuration:
[tool.pytest.ini_options]
asyncio_mode = "auto"
This eliminates the need for @pytest.mark.asyncio decorators on every async test.
Server Components
The demo server (server.py) includes:
- Tools:
add,greet,async_multiply - Resources:
demo://info,demo://greeting/{name} - Prompts:
hello,explain
Test Patterns
The test suite demonstrates:
- Async fixture pattern: Proper client fixture using
async with - Tool testing: Calling tools and checking results via
.dataattribute - Resource testing: Reading static and templated resources
- Prompt testing: Getting prompts with different arguments
- Parametrized tests: Testing multiple scenarios efficiently
- Schema validation: Verifying tool schemas and structure
- Pattern matching: Using dirty-equals for flexible assertions
Running the Tests
# Install dependencies
uv sync
# Run all tests
uv run pytest
# Run with verbose output
uv run pytest -v
# Run a specific test
uv run pytest tests/test_server.py::test_add_tool
Running the Server
# Run the server
uv run fastmcp run server.py
# Inspect the server
uv run fastmcp inspect server.py
Learning More
For detailed information about testing FastMCP servers, see the Testing Documentation.