60e0ffc959
Upgrade checks / Notify on failure (push) Has been cancelled
Upgrade checks / Close issue on success (push) Has been cancelled
Schema Crash Test / Real-world schema crash test (232K schemas) (push) Has been cancelled
Run static analysis / static_analysis (push) Has been cancelled
Tests / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Tests / Tests with lowest-direct dependencies (push) Has been cancelled
Tests / MCP conformance tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
Tests / Package install smoke (push) Has been cancelled
Upgrade checks / Static analysis (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Upgrade checks / Integration tests (push) Has been cancelled
Update MCPServerConfig Schema / update-config-schema (push) Has been cancelled
Update SDK Documentation / update-sdk-docs (push) Has been cancelled
77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
import pytest
|
|
|
|
from fastmcp import Client, Context, FastMCP
|
|
|
|
PROGRESS_MESSAGES = []
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clear_progress_messages():
|
|
PROGRESS_MESSAGES.clear()
|
|
yield
|
|
PROGRESS_MESSAGES.clear()
|
|
|
|
|
|
@pytest.fixture
|
|
def fastmcp_server():
|
|
mcp = FastMCP()
|
|
|
|
@mcp.tool
|
|
async def progress_tool(context: Context) -> int:
|
|
for i in range(3):
|
|
await context.report_progress(
|
|
progress=i + 1,
|
|
total=3,
|
|
message=f"{(i + 1) / 3 * 100:.2f}% complete",
|
|
)
|
|
return 100
|
|
|
|
return mcp
|
|
|
|
|
|
EXPECTED_PROGRESS_MESSAGES = [
|
|
dict(progress=1, total=3, message="33.33% complete"),
|
|
dict(progress=2, total=3, message="66.67% complete"),
|
|
dict(progress=3, total=3, message="100.00% complete"),
|
|
]
|
|
|
|
|
|
async def progress_handler(
|
|
progress: float, total: float | None, message: str | None
|
|
) -> None:
|
|
PROGRESS_MESSAGES.append(dict(progress=progress, total=total, message=message))
|
|
|
|
|
|
async def test_progress_handler(fastmcp_server: FastMCP):
|
|
async with Client(fastmcp_server, progress_handler=progress_handler) as client:
|
|
await client.call_tool("progress_tool", {})
|
|
|
|
assert PROGRESS_MESSAGES == EXPECTED_PROGRESS_MESSAGES
|
|
|
|
|
|
async def test_progress_handler_can_be_supplied_on_tool_call(fastmcp_server: FastMCP):
|
|
async with Client(fastmcp_server) as client:
|
|
await client.call_tool("progress_tool", {}, progress_handler=progress_handler)
|
|
|
|
assert PROGRESS_MESSAGES == EXPECTED_PROGRESS_MESSAGES
|
|
|
|
|
|
async def test_progress_handler_supplied_on_tool_call_overrides_default(
|
|
fastmcp_server: FastMCP,
|
|
):
|
|
async def bad_progress_handler(
|
|
progress: float, total: float | None, message: str | None
|
|
) -> None:
|
|
raise Exception("This should not be called")
|
|
|
|
async with Client(fastmcp_server, progress_handler=bad_progress_handler) as client:
|
|
await client.call_tool("progress_tool", {}, progress_handler=progress_handler)
|
|
|
|
assert PROGRESS_MESSAGES == EXPECTED_PROGRESS_MESSAGES
|
|
|
|
|
|
async def test_default_progress_handler_handles_zero_total() -> None:
|
|
from fastmcp.client.progress import default_progress_handler
|
|
|
|
await default_progress_handler(progress=1, total=0, message="starting")
|