Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201import asyncioimport jsonimport sys
import pytestfrom mcp import McpErrorfrom mcp.types import TextResourceContents
from fastmcp.client import Clientfrom fastmcp.client.transports import SSETransportfrom fastmcp.server.dependencies import get_http_requestfrom fastmcp.server.http import create_sse_appfrom fastmcp.server.server import FastMCPfrom fastmcp.utilities.tests import run_server_async
def create_test_server() -> FastMCP: """Create a FastMCP server with tools, resources, and prompts.""" server = FastMCP("TestServer")
@server.tool def greet(name: str) -> str: """Greet someone by name.""" return f"Hello, {name}!"
@server.tool def add(a: int, b: int) -> int: """Add two numbers together.""" return a + b
@server.tool async def sleep(seconds: float) -> str: """Sleep for a given number of seconds.""" await asyncio.sleep(seconds) return f"Slept for {seconds} seconds"
@server.resource(uri="data://users") async def get_users() -> str: import json
return json.dumps(["Alice", "Bob", "Charlie"])
@server.resource(uri="data://user/{user_id}") async def get_user(user_id: str) -> str: import json
return json.dumps({"id": user_id, "name": f"User {user_id}", "active": True})
@server.resource(uri="request://headers") async def get_headers() -> str: import json
request = get_http_request() return json.dumps(dict(request.headers))
@server.prompt def welcome(name: str) -> str: """Example greeting prompt.""" return f"Welcome to FastMCP, {name}!"
return server
@pytest.fixtureasync def sse_server(): """Start a test server with SSE transport and return its URL.""" server = create_test_server() async with run_server_async(server, transport="sse") as url: yield url
async def test_ping(sse_server: str): """Test pinging the server.""" async with Client(transport=SSETransport(sse_server)) as client: result = await client.ping() assert result is True
async def test_http_headers(sse_server: str): """Test getting HTTP headers from the server.""" async with Client( transport=SSETransport(sse_server, headers={"X-DEMO-HEADER": "ABC"}) ) as client: raw_result = await client.read_resource("request://headers") assert isinstance(raw_result[0], TextResourceContents) json_result = json.loads(raw_result[0].text) assert "x-demo-header" in json_result assert json_result["x-demo-header"] == "ABC"
@pytest.fixtureasync def sse_server_custom_path(): """Start a test server with SSE on a custom path.""" server = create_test_server() async with run_server_async(server, transport="sse", path="/help") as url: yield url
@pytest.fixtureasync def nested_sse_server(): """Test nested server mounts with SSE.""" import uvicorn from starlette.applications import Starlette from starlette.routing import Mount
from fastmcp.utilities.http import find_available_port
server = create_test_server() sse_app = create_sse_app( server=server, message_path="/mcp/messages", sse_path="/mcp/sse/" )
# Nest the app under multiple mounts to test URL resolution inner = Starlette(routes=[Mount("/nest-inner", app=sse_app)]) outer = Starlette(routes=[Mount("/nest-outer", app=inner)])
# Run uvicorn with the nested ASGI app port = find_available_port()
config = uvicorn.Config( app=outer, host="127.0.0.1", port=port, log_level="critical", ws="websockets-sansio", )
uvicorn_server = uvicorn.Server(config) server_task = asyncio.create_task(uvicorn_server.serve()) await asyncio.sleep(0.1)
try: yield f"http://127.0.0.1:{port}/nest-outer/nest-inner/mcp/sse/" finally: # Graceful shutdown - required for uvicorn 0.39+ due to context isolation uvicorn_server.should_exit = True try: await server_task except asyncio.CancelledError: pass
async def test_run_server_on_path(sse_server_custom_path: str): """Test running server on a custom path.""" async with Client(transport=SSETransport(sse_server_custom_path)) as client: result = await client.ping() assert result is True
async def test_nested_sse_server_resolves_correctly(nested_sse_server: str): """Test patch for https://github.com/modelcontextprotocol/python-sdk/pull/659""" async with Client(transport=SSETransport(nested_sse_server)) as client: result = await client.ping() assert result is True
@pytest.mark.skipif( sys.platform == "win32", reason="Timeout tests are flaky on Windows. Timeouts *are* supported but the tests are unreliable.",)class TestTimeout: async def test_timeout(self, sse_server: str): with pytest.raises( McpError, match="Timed out while waiting for response to ClientRequest. Waited 0.03 seconds", ): async with Client( transport=SSETransport(sse_server), timeout=0.03, ) as client: await client.call_tool("sleep", {"seconds": 0.1})
async def test_timeout_tool_call(self, sse_server: str): async with Client(transport=SSETransport(sse_server)) as client: with pytest.raises(McpError, match="Timed out"): await client.call_tool("sleep", {"seconds": 0.1}, timeout=0.03)
async def test_timeout_tool_call_overrides_client_timeout_if_lower( self, sse_server: str ): async with Client( transport=SSETransport(sse_server), timeout=2, ) as client: with pytest.raises(McpError, match="Timed out"): await client.call_tool("sleep", {"seconds": 0.1}, timeout=0.03)
async def test_timeout_client_timeout_does_not_override_tool_call_timeout_if_lower( self, sse_server: str ): """ With SSE, the tool call timeout always takes precedence over the client.
Note: on Windows, the behavior appears unpredictable. """ async with Client( transport=SSETransport(sse_server), timeout=0.5, ) as client: await client.call_tool("sleep", {"seconds": 0.8}, timeout=2)