Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Merge remote-tracking branch 'origin/main' into modern-merge

Jeremiah Lowin1593257f2aa7
diff --cc fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py
index 747d1ab69,747d1ab69..0ee24ef8d
--- a/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py
+++ b/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py
@@@ -75,7 -75,7 +75,7 @@@ class AnthropicSamplingHandler
Example:
```python
from anthropic import AsyncAnthropic
-- from fastmcp import FastMCP
++ from fastmcp import Client
from fastmcp.client.sampling.handlers.anthropic import AnthropicSamplingHandler
handler = AnthropicSamplingHandler(
@@@ -83,7 -83,7 +83,9 @@@
client=AsyncAnthropic(),
)
-- server = FastMCP(sampling_handler=handler)
++ # Sampling is a server-initiated request, so it only exists on the
++ # handshake era; pass `mode="legacy"` to answer one.
++ client = Client(server_url, sampling_handler=handler, mode="legacy")
```
"""
diff --cc fastmcp_slim/fastmcp/client/sampling/handlers/google_genai.py
index 7bfbbb626,7bfbbb626..c49c77635
--- a/fastmcp_slim/fastmcp/client/sampling/handlers/google_genai.py
+++ b/fastmcp_slim/fastmcp/client/sampling/handlers/google_genai.py
@@@ -60,18 -60,18 +60,20 @@@ class GoogleGenaiSamplingHandler
Example:
```python
-- from google.genai import Client
-- from fastmcp import FastMCP
++ from google.genai import Client as GoogleGenaiClient
++ from fastmcp import Client as FastMCPClient
from fastmcp.client.sampling.handlers.google_genai import (
GoogleGenaiSamplingHandler,
)
handler = GoogleGenaiSamplingHandler(
default_model="gemini-2.0-flash",
-- client=Client(),
++ client=GoogleGenaiClient(),
)
-- server = FastMCP(sampling_handler=handler)
++ # Sampling is a server-initiated request, so it only exists on the
++ # handshake era; pass `mode="legacy"` to answer one.
++ client = FastMCPClient(server_url, sampling_handler=handler, mode="legacy")
```
"""
diff --cc tests/client/test_roots.py
index 9d87202c0,8874ced05..10fd47632
--- a/tests/client/test_roots.py
+++ b/tests/client/test_roots.py
@@@ -1,5 -1,6 +1,7 @@@
+ import functools
+
import pytest
+from mcp_types import Root
from fastmcp import Client, Context, FastMCP
@@@ -52,16 -46,39 +54,53 @@@ class TestClientRoots
"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"]