fastmcp/examples/testing_demo/server.py
William Easton 8e0c6c8685
docs: clarify pytest-asyncio dependency and asyncio mode configuration (#2399)
* docs: clarify pytest-asyncio dependency and asyncio mode configuration

Added a Prerequisites section to the testing documentation explaining:
- pytest-asyncio is required for async test functions and fixtures
- Recommended configuration: asyncio_mode = 'auto' in pyproject.toml
- This eliminates need for @pytest.mark.asyncio decorators

Resolves #2372

Co-authored-by: William Easton <strawgate@users.noreply.github.com>

* feat: add testing_demo example with comprehensive test suite

Add a standalone example project demonstrating FastMCP testing patterns:
- Tools, resources, and prompts with full test coverage
- pytest-asyncio configuration in pyproject.toml
- 18 passing tests showing async fixtures, parametrized tests, and more
- Documentation explaining testing best practices

Co-authored-by: William Easton <strawgate@users.noreply.github.com>

---------

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: William Easton <strawgate@users.noreply.github.com>
2025-11-15 10:34:29 -05:00

61 lines
1.5 KiB
Python

"""
FastMCP Testing Demo Server
A simple MCP server demonstrating tools, resources, and prompts
with comprehensive test coverage.
"""
from fastmcp import FastMCP
# Create server
mcp = FastMCP("Testing Demo")
# Tools
@mcp.tool
def add(a: int, b: int) -> int:
"""Add two numbers together"""
return a + b
@mcp.tool
def greet(name: str, greeting: str = "Hello") -> str:
"""Greet someone with a customizable greeting"""
return f"{greeting}, {name}!"
@mcp.tool
async def async_multiply(x: float, y: float) -> float:
"""Multiply two numbers (async example)"""
return x * y
# Resources
@mcp.resource("demo://info")
def server_info() -> str:
"""Get server information"""
return "This is the FastMCP Testing Demo server"
@mcp.resource("demo://greeting/{name}")
def greeting_resource(name: str) -> str:
"""Get a personalized greeting resource"""
return f"Welcome to FastMCP, {name}!"
# Prompts
@mcp.prompt("hello")
def hello_prompt(name: str = "World") -> str:
"""Generate a hello world prompt"""
return f"Say hello to {name} in a friendly way."
@mcp.prompt("explain")
def explain_prompt(topic: str, detail_level: str = "medium") -> str:
"""Generate a prompt to explain a topic"""
if detail_level == "simple":
return f"Explain {topic} in simple terms for beginners."
elif detail_level == "detailed":
return f"Provide a detailed, technical explanation of {topic}."
else:
return f"Explain {topic} with moderate technical detail."