fastmcp/tests/server/providers/proxy/test_proxy_client.py
Jeremiah Lowin 3a9717e6be
Publish docs for v3.2.0 (#3713)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Jeremiah Lowin <jlowin@users.noreply.github.com>
Co-authored-by: Marvin Context Protocol <41898282+Marvin Context Protocol@users.noreply.github.com>
Co-authored-by: voidborne-d <voidborne-d@users.noreply.github.com>
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: d 🔹 <258577966+voidborne-d@users.noreply.github.com>
Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
Co-authored-by: nightcityblade <nightcityblade@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Bill Easton <strawgate@users.noreply.github.com>
Co-authored-by: Sumanshu Nankana <sumanshunankana@gmail.com>
Co-authored-by: Eric Robinson <ericrobinson@indeed.com>
Co-authored-by: Martim Santos <martimfasantos@gmail.com>
Co-authored-by: d 🔹 <liusway405@gmail.com>
Co-authored-by: Matthieu B <66959271+mtthidoteu@users.noreply.github.com>
Co-authored-by: Sascha Buehrle <47737812+saschabuehrle@users.noreply.github.com>
Co-authored-by: Hakancan <142545736+hkc5@users.noreply.github.com>
Co-authored-by: nightcityblade <jackchen@haloailabs.com>
Co-authored-by: Matt Hallowell <17804673+mhallo@users.noreply.github.com>
Co-authored-by: nate nowack <thrast36@gmail.com>
Co-authored-by: Bill Easton <williamseaston@gmail.com>
Co-authored-by: Marcus Shu <46469249+shulkx@users.noreply.github.com>
Co-authored-by: Rushabh Doshi <radoshi@gmail.com>
Co-authored-by: AIKAWA Shigechika <shige@aikawa.jp>
Co-authored-by: Jeremy Simon <simonjer805@gmail.com>
Co-authored-by: Miguel Miranda Dias <7780875+pandego@users.noreply.github.com>
Co-authored-by: Anthony James Padavano <padavano.anthony@gmail.com>
Co-authored-by: Mostafa Kamal <hiremostafa@gmail.com>
Fix auto-close MRE script posting comment without closing (#3386)
Fix WorkOS token scope verification bypass 🤖 Generated with Codex (#3407)
Fix initialize McpError fallthrough 🤖 Generated with Codex (#3413)
Fix transform arg collisions with passthrough params (#3431)
Fix get_* returning None when latest version is disabled (#3439)
Fix get_* returning None when latest version is disabled (#3421)
Fix server lifespan overlap teardown (#3415)
Fix $ref output schema object detection regression (#3420)
resolved annotations (#3429)
Fix async partial callables rejected by iscoroutinefunction (#3438)
Fix async partial callables rejected by iscoroutinefunction (#3423)
fix: add version to components (#3458)
fix: use intent-based flag for OIDC scope patch in load_access_token (#3465)
Fixes #3461
fix: normalize Google scope shorthands and surface valid_scopes (#3477)
fix: resolve ty 0.0.23 type-checking errors and bump pin (#3481)
fix: shield lifespan teardown from cancellation (#3480)
fix: forward custom_route endpoints from mounted servers (#3462)
fix updates _get_additional_http_routes() to traverse providers,
Fixes #3457
fix: remove hardcoded version from CLI help text (#3456)
fix: monty 0.0.8 compatibility, drop external_functions from constructor (#3468)
fix: task test teardown hanging 5s per test (#3499)
Closes #3498
fix: validate workspace path is a directory before cursor install (#3440)
Fixes #3426
fix: handle re.error from malformed URI templates in build_regex (#3501)
fix: reject empty/OIDC-only required_scopes in AzureProvider (#3503)
fix: restrict $ref resolution to local refs only (SSRF/LFI) (#3502)
fix warnings and timeouts (#3504)
close upgrade check issue when build passes (#3505)
Closes #3484
fix: URL-encode path params to prevent SSRF/path traversal (GHSA-vv7q-7jx5-f767) (#3507)
fix: prevent path traversal in skill download (#3493)
fix: prefer IdP-granted scopes over client-requested scopes in OAuthProxy (#3492)
fix: remove unrelated transform and http.py changes from PR scope
fix: remove forced follow_redirects from httpx_client_factory calls (#3496)
fix: stop passing follow_redirects to httpx_client_factory
fix: restore follow_redirects=True for custom httpx client factories
Closes #3509
fix: CSRF double-submit cookie check in consent flow (#3519)
fix: validate server names in install commands (#3522)
fix: use raw strings for regex in pytest.raises match (#3523)
fix: reject refresh tokens used as Bearer access tokens (#3524)
fix: route ResourcesAsTools/PromptsAsTools through server middleware (#3495)
fix: resolve Pyright "Module is not callable" on @tool, @resource, @prompt decorators (#3540)
fix: filter warnings by message in KEY_PREFIX test (#3549)
fix: suppress output schema for ToolResult subclass annotations (#3548)
fix: increase sleep duration in proxy cache tests (#3567)
fix: store absolute token expiry to prevent stale expires_in on reload (#3572)
fix: preserve tool properties named 'title' during schema compression (#3582)
Fix loopback redirect URI port matching per RFC 8252 §7.3 (#3589)
Fix app tool routing: visibility check and middleware propagation (#3591)
Fix query parameter serialization to respect OpenAPI explode/style settings (#3595)
Fix dev apps form: union types, textarea support, JSON parsing (#3597)
fix(google): replace deprecated /oauth2/v1/tokeninfo with /oauth2/v3/userinfo (#3603)
fix: resolve EntraOBOToken dependency injection through MultiAuth (#3609)
fix(docs): correct misleading stateless_http header (#3622)
fix: filesystem provider import machinery (#3626)
Closes #3625 (issues 2, 3, 6)
fix: recover StdioTransport after subprocess exits (#3630)
fix(server): preserve mounted tool task metadata (#3632)
fix: scope deprecation warning filter to FastMCPDeprecationWarning (#3649)
fix imports, add PrefabAppConfig (#3650)
fix: resolve CurrentFastMCP/ctx.fastmcp to child server in mounted background tasks (#3651)
Fix blocking docs issues: chart imports, Select API, Rx consistency (#3652)
closed by default (#3657)
Fix prompt caching middleware missing wrap/unwrap round-trip (#3666)
fix: serialize object query params per OpenAPI style/explode rules (#3662)
Fixes #2857
fix: HTTP request headers not accessible in background task workers (#3631)
fix: restore HTTP headers in worker execution path for background tasks (#3681)
fix: strip discriminator after dereferencing schemas (#3682)
fix: remove stale ty:ignore directives for ty 0.0.26 (#3684)
Fix docs gaps in app provider pages (#3690)
fix: dev apps log panel UX improvements (#3698)
fix dev server empty string args (#3700)
2026-03-30 16:48:30 -04:00

438 lines
16 KiB
Python

from dataclasses import dataclass
import pytest
from anyio import create_task_group
from mcp.types import (
ElicitRequestFormParams,
LoggingLevel,
ModelHint,
ModelPreferences,
TextContent,
)
from pydantic import BaseModel, Field
from fastmcp import Client, Context, FastMCP
from fastmcp.client.elicitation import ElicitRequestParams, ElicitResult
from fastmcp.client.logging import LogMessage
from fastmcp.client.sampling import RequestContext, SamplingMessage, SamplingParams
from fastmcp.exceptions import ToolError
from fastmcp.server.elicitation import AcceptedElicitation
from fastmcp.server.providers.proxy import ProxyClient, _create_client_factory
@pytest.fixture
def fastmcp_server():
mcp = FastMCP("TestServer")
@mcp.tool(tags={"echo"})
def echo(message: str) -> str:
return f"echo: {message}"
@mcp.tool
async def list_roots(context: Context) -> list[str]:
roots = await context.list_roots()
return [str(r.uri) for r in roots]
@mcp.tool
async def sampling(
context: Context,
) -> str:
result = await context.sample(
"Hello, world!",
system_prompt="You love FastMCP",
temperature=0.5,
max_tokens=100,
model_preferences="gpt-4o",
)
return result.text or ""
@dataclass
class Person:
name: str
@mcp.tool
async def elicit(context: Context) -> str:
result = await context.elicit(
message="What is your name?",
response_type=Person,
)
if result.action == "accept":
assert isinstance(result, AcceptedElicitation)
assert isinstance(result.data, Person)
return f"Hello, {result.data.name}!"
else:
return "No name provided."
@mcp.tool
async def log(
message: str, level: LoggingLevel, logger: str, context: Context
) -> None:
await context.log(message=message, level=level, logger_name=logger)
@mcp.tool
async def report_progress(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
@pytest.fixture
async def proxy_server(fastmcp_server: FastMCP):
"""
A proxy server that forwards interactions with the proxy client to the given fastmcp server.
"""
return FastMCP.as_proxy(ProxyClient(fastmcp_server))
class TestProxyClient:
async def test_forward_tool_meta(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `echo` tool meta.
"""
async with Client(proxy_server) as client:
tools = await client.list_tools()
echo_tool = next(t for t in tools if t.name == "echo")
assert echo_tool.meta == {"fastmcp": {"tags": ["echo"]}}
async def test_forward_error_response(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards an error response.
"""
async with Client(proxy_server) as client:
with pytest.raises(ToolError, match="Elicitation not supported"):
await client.call_tool("elicit", {})
async def test_forward_list_roots_request(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `list_roots` request.
"""
roots_handler_called = False
async def roots_handler(ctx: RequestContext):
nonlocal roots_handler_called
roots_handler_called = True
return []
async with Client(proxy_server, roots=roots_handler) as client:
await client.call_tool("list_roots", {})
assert roots_handler_called
async def test_forward_list_roots_response(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `list_roots` response.
"""
async with Client(proxy_server, roots=["file://x/y/z"]) as client:
result = await client.call_tool("list_roots", {})
assert result.data == ["file://x/y/z"]
async def test_forward_sampling_request(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `sampling` request.
"""
sampling_handler_called = False
def sampling_handler(
messages: list[SamplingMessage],
params: SamplingParams,
ctx: RequestContext,
) -> str:
nonlocal sampling_handler_called
sampling_handler_called = True
assert messages == [
SamplingMessage(
role="user",
content=TextContent(type="text", text="Hello, world!"),
)
]
assert params.systemPrompt == "You love FastMCP"
assert params.temperature == 0.5
assert params.maxTokens == 100
assert params.modelPreferences == ModelPreferences(
hints=[ModelHint(name="gpt-4o")]
)
return ""
async with Client(proxy_server, sampling_handler=sampling_handler) as client:
await client.call_tool("sampling", {})
assert sampling_handler_called
async def test_forward_sampling_response(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `sampling` response.
"""
async with Client(
proxy_server, sampling_handler=lambda *args: "I love FastMCP"
) as client:
result = await client.call_tool("sampling", {})
assert result.data == "I love FastMCP"
async def test_elicit_request(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `elicit` request.
"""
elicitation_handler_called = False
async def elicitation_handler(
message, response_type, params: ElicitRequestParams, ctx
):
nonlocal elicitation_handler_called
elicitation_handler_called = True
assert message == "What is your name?"
assert "Person" in str(response_type)
assert isinstance(params, ElicitRequestFormParams)
assert params.requestedSchema == {
"title": "Person",
"type": "object",
"properties": {"name": {"title": "Name", "type": "string"}},
"required": ["name"],
}
return ElicitResult(action="accept", content=response_type(name="Alice"))
async with Client(
proxy_server, elicitation_handler=elicitation_handler
) as client:
await client.call_tool("elicit", {})
assert elicitation_handler_called
async def test_elicit_accept_response(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `elicit` accept response.
"""
async def elicitation_handler(
message, response_type, params: ElicitRequestParams, ctx
):
return ElicitResult(action="accept", content=response_type(name="Alice"))
async with Client(
proxy_server,
elicitation_handler=elicitation_handler,
) as client:
result = await client.call_tool("elicit", {})
assert result.data == "Hello, Alice!"
async def test_elicit_decline_response(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `elicit` decline response.
"""
async def elicitation_handler(
message, response_type, params: ElicitRequestParams, ctx
):
return ElicitResult(action="decline")
async with Client(
proxy_server, elicitation_handler=elicitation_handler
) as client:
result = await client.call_tool("elicit", {})
assert result.data == "No name provided."
async def test_log_request(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `log` request.
"""
log_handler_called = False
async def log_handler(message: LogMessage) -> None:
nonlocal log_handler_called
log_handler_called = True
assert message.data == "Hello, world!"
assert message.level == "info"
assert message.logger == "test"
async with Client(proxy_server, log_handler=log_handler) as client:
await client.call_tool(
"log", {"message": "Hello, world!", "level": "info", "logger": "test"}
)
assert log_handler_called
async def test_report_progress_request(self, proxy_server: FastMCP):
"""
Test that the proxy client correctly forwards the `report_progress` request.
"""
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"),
]
PROGRESS_MESSAGES = []
async def progress_handler(
progress: float, total: float | None, message: str | None
) -> None:
PROGRESS_MESSAGES.append(
dict(progress=progress, total=total, message=message)
)
async with Client(proxy_server, progress_handler=progress_handler) as client:
await client.call_tool("report_progress", {})
assert PROGRESS_MESSAGES == EXPECTED_PROGRESS_MESSAGES
async def test_concurrent_log_requests_no_mixing(self, proxy_server: FastMCP):
"""Test that concurrent log requests don't mix handlers (fixes #1068)."""
results: dict[str, LogMessage] = {}
async def log_handler_a(message: LogMessage) -> None:
results["logger_a"] = message
async def log_handler_b(message: LogMessage) -> None:
results["logger_b"] = message
async with (
Client(proxy_server, log_handler=log_handler_a) as client_a,
Client(proxy_server, log_handler=log_handler_b) as client_b,
):
async with create_task_group() as tg:
tg.start_soon(
client_a.call_tool,
"log",
{"message": "Hello, world!", "level": "info", "logger": "a"},
)
tg.start_soon(
client_b.call_tool,
"log",
{"message": "Hello, world!", "level": "info", "logger": "b"},
)
assert results["logger_a"].logger == "a"
assert results["logger_b"].logger == "b"
async def test_concurrent_elicitation_no_mixing(self, proxy_server: FastMCP):
"""Test that concurrent elicitation requests don't mix handlers (fixes #1068)."""
results = {}
async def elicitation_handler_a(
message: str,
response_type: type,
params: ElicitRequestParams,
ctx: RequestContext,
) -> ElicitResult:
return ElicitResult(action="accept", content=response_type(name="Alice"))
async def elicitation_handler_b(
message: str,
response_type: type,
params: ElicitRequestParams,
ctx: RequestContext,
) -> ElicitResult:
return ElicitResult(action="accept", content=response_type(name="Bob"))
async def get_and_store(name, coro):
result = await coro
results[name] = result.data
async with (
Client(proxy_server, elicitation_handler=elicitation_handler_a) as client_a,
Client(proxy_server, elicitation_handler=elicitation_handler_b) as client_b,
):
async with create_task_group() as tg:
tg.start_soon(
get_and_store,
"elicitation_a",
client_a.call_tool("elicit", {}),
)
tg.start_soon(
get_and_store,
"elicitation_b",
client_b.call_tool("elicit", {}),
)
assert results["elicitation_a"] == "Hello, Alice!"
assert results["elicitation_b"] == "Hello, Bob!"
async def test_elicit_with_default_values(self, fastmcp_server: FastMCP):
"""
Test that the proxy client correctly handles elicitation with default values (fixes #1167).
"""
@fastmcp_server.tool
async def elicit_with_defaults(context: Context) -> str:
class TestModel(BaseModel):
content: str = Field(description="Your reply content")
acknowledge: bool = Field(
default=False, description="Send immediately or save as draft"
)
result = await context.elicit(
"Please provide input:", response_type=TestModel
)
if result.action == "accept":
assert isinstance(result, AcceptedElicitation)
assert isinstance(result.data, TestModel)
return f"Content: {result.data.content}, Acknowledge: {result.data.acknowledge}"
else:
return f"Elicitation {result.action}"
proxy_server = FastMCP.as_proxy(ProxyClient(fastmcp_server))
# Test that elicitation works correctly through the proxy
async def elicitation_handler(
message: str,
response_type: type,
params: ElicitRequestParams,
ctx: RequestContext,
):
# Verify the schema is correct - acknowledge should have default=False, not be nullable
assert isinstance(params, ElicitRequestFormParams)
schema = params.requestedSchema
assert schema["properties"]["acknowledge"]["type"] == "boolean"
assert schema["properties"]["acknowledge"]["default"] is False
return {"content": "Test content", "acknowledge": True}
async with Client(
proxy_server, elicitation_handler=elicitation_handler
) as client:
result = await client.call_tool("elicit_with_defaults", {})
assert result.data == "Content: Test content, Acknowledge: True"
async def test_client_factory_creates_fresh_sessions(self, fastmcp_server: FastMCP):
"""Test that the client factory pattern creates fresh sessions for each request."""
from fastmcp.server.providers.proxy import FastMCPProxy
# Create a disconnected client (should use fresh sessions per request)
base_client = Client(fastmcp_server)
# Test both as_proxy convenience method and direct client_factory usage
proxy_via_as_proxy = FastMCP.as_proxy(base_client)
proxy_via_factory = FastMCPProxy(client_factory=base_client.new)
# Verify the proxies are created successfully - this tests the client factory pattern
assert proxy_via_as_proxy is not None
assert proxy_via_factory is not None
# Verify they have the expected client factory behavior
assert hasattr(proxy_via_as_proxy, "_local_provider")
assert hasattr(proxy_via_factory, "_local_provider")
async def test_connected_proxy_client_uses_fresh_sessions(
self, fastmcp_server: FastMCP
):
"""Connected ProxyClient targets should create fresh sessions to avoid stale context."""
async with ProxyClient(fastmcp_server) as connected_client:
factory = _create_client_factory(connected_client)
client_a = factory()
client_b = factory()
assert isinstance(client_a, Client)
assert isinstance(client_b, Client)
assert client_a is not connected_client
assert client_b is not connected_client
assert client_a is not client_b
assert not client_a.is_connected()
assert not client_b.is_connected()