fastmcp/tests/client/test_roots.py
nate nowack 56c5ae01ae
Make Client callback setters reach a live session (#6717)
Co-authored-by: Diogo Santos <12731907+dgenio@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QCgr8md9CNeZCywsn9adrG
2026-11-23 08:35:13 +00:00

133 lines
5.1 KiB
Python

import functools
import pytest
from mcp_types import Root
from fastmcp import Client, Context, FastMCP
from fastmcp.exceptions import ToolError
@pytest.fixture
def fastmcp_server():
"""A server that issues a handshake-era `roots/list` request.
`Context` has no `list_roots()` — server-initiated requests are not part of
FastMCP's server API. This server reaches the SDK session directly to stand
in for a legacy upstream, so the client's `roots=` handling stays covered.
"""
mcp = FastMCP()
@mcp.tool
async def list_roots(context: Context) -> list[str]:
result = await context.session.list_roots() # ty: ignore[deprecated]
return [str(r.uri) for r in result.roots]
return mcp
class TestClientRoots:
@pytest.mark.parametrize("roots", [["x"], ["x", "y"]])
async def test_invalid_roots(self, fastmcp_server: FastMCP, roots: list[str]):
"""
Roots must be URIs
"""
with pytest.raises(ValueError, match="Input should be a valid URL"):
async with Client(fastmcp_server, roots=roots):
pass
@pytest.mark.parametrize("roots", [["https://x.com"]])
async def test_invalid_urls(self, fastmcp_server: FastMCP, roots: list[str]):
"""
At this time, root URIs must start with file://
"""
with pytest.raises(ValueError, match="URL scheme should be 'file'"):
async with Client(fastmcp_server, roots=roots):
pass
@pytest.mark.parametrize("roots", [["file://x/y/z", "file://x/y/z"]])
async def test_valid_roots(self, fastmcp_server: FastMCP, roots: list[str]):
# `roots/list` is a server-initiated request, so it only exists on the
# handshake era; SEP-2577 removed it from the modern protocol.
async with Client(fastmcp_server, mode="legacy", roots=roots) as client:
result = await client.call_tool("list_roots", {})
assert result.data == [
"file://x/y/z",
"file://x/y/z",
]
async def test_roots_handler_answers_a_legacy_server(self, fastmcp_server: FastMCP):
"""A callable `roots=` handler still answers a legacy server's request."""
calls: list[object] = []
async def roots_handler(ctx) -> list[Root]:
calls.append(ctx)
return [Root(uri="file://from/handler")]
async with Client(fastmcp_server, mode="legacy", roots=roots_handler) as client:
result = await client.call_tool("list_roots", {})
assert len(calls) == 1
assert result.data == ["file://from/handler"]
async def test_bound_method_roots_handler(self, fastmcp_server: FastMCP):
class RootsProvider:
async def get_roots(self, _context: object) -> list[str]:
return ["file:///bound-method"]
provider = RootsProvider()
async with Client(
fastmcp_server, mode="legacy", roots=provider.get_roots
) as client:
result = await client.call_tool("list_roots", {})
assert result.data == ["file:///bound-method"]
async def test_partial_roots_handler(self, fastmcp_server: FastMCP):
async def get_roots(prefix: str, _context: object) -> list[str]:
return [f"file:///{prefix}"]
handler = functools.partial(get_roots, "partial")
async with Client(fastmcp_server, mode="legacy", roots=handler) as client:
result = await client.call_tool("list_roots", {})
assert result.data == ["file:///partial"]
async def test_callable_object_roots_handler(self, fastmcp_server: FastMCP):
class RootsProvider:
async def __call__(self, _context: object) -> list[str]:
return ["file:///callable-object"]
async with Client(
fastmcp_server, mode="legacy", roots=RootsProvider()
) as client:
result = await client.call_tool("list_roots", {})
assert result.data == ["file:///callable-object"]
async def test_set_roots_updates_a_live_session(self, fastmcp_server: FastMCP):
"""`set_roots` while connected takes effect without reconnecting."""
async with Client(
fastmcp_server, mode="legacy", roots=["file:///before"]
) as client:
before = await client.call_tool("list_roots", {})
client.set_roots(["file:///after"])
after = await client.call_tool("list_roots", {})
assert before.data == ["file:///before"]
assert after.data == ["file:///after"]
async def test_set_roots_without_roots_at_connect_waits_for_reconnect(
self, fastmcp_server: FastMCP
):
"""A session negotiated without roots does not gain them mid-session."""
client = Client(fastmcp_server, mode="legacy")
async with client:
client.set_roots(["file:///later"])
with pytest.raises(ToolError, match="List roots not supported"):
await client.call_tool("list_roots", {})
async with client:
result = await client.call_tool("list_roots", {})
assert result.data == ["file:///later"]