mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-29 02:32:09 +02:00
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)
434 lines
15 KiB
Python
434 lines
15 KiB
Python
import base64
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
try:
|
|
from google.genai import Client as GoogleGenaiClient
|
|
from google.genai.types import (
|
|
Candidate,
|
|
FunctionCall,
|
|
FunctionCallingConfigMode,
|
|
GenerateContentResponse,
|
|
ModelContent,
|
|
Part,
|
|
UserContent,
|
|
)
|
|
from mcp.types import (
|
|
AudioContent,
|
|
CreateMessageResult,
|
|
ImageContent,
|
|
ModelHint,
|
|
ModelPreferences,
|
|
SamplingMessage,
|
|
TextContent,
|
|
ToolChoice,
|
|
ToolResultContent,
|
|
ToolUseContent,
|
|
)
|
|
|
|
from fastmcp.client.sampling.handlers.google_genai import (
|
|
GoogleGenaiSamplingHandler,
|
|
_convert_messages_to_google_genai_content,
|
|
_convert_tool_choice_to_google_genai,
|
|
_response_to_create_message_result,
|
|
_response_to_result_with_tools,
|
|
_sampling_content_to_google_genai_part,
|
|
)
|
|
|
|
GOOGLE_GENAI_AVAILABLE = True
|
|
except ImportError:
|
|
GOOGLE_GENAI_AVAILABLE = False
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
not GOOGLE_GENAI_AVAILABLE, reason="google-genai not installed"
|
|
)
|
|
|
|
|
|
def test_convert_sampling_messages_to_google_genai_content():
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user", content=TextContent(type="text", text="hello")
|
|
),
|
|
SamplingMessage(
|
|
role="assistant", content=TextContent(type="text", text="ok")
|
|
),
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 2
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert isinstance(msgs[1], ModelContent)
|
|
assert msgs[0].parts[0].text == "hello"
|
|
assert msgs[1].parts[0].text == "ok"
|
|
|
|
|
|
def test_convert_single_image_content_to_google_genai():
|
|
part = _sampling_content_to_google_genai_part(
|
|
ImageContent(type="image", data="YWJj", mimeType="image/png")
|
|
)
|
|
|
|
assert part.inline_data is not None
|
|
assert part.inline_data.data == base64.b64decode("YWJj")
|
|
assert part.inline_data.mime_type == "image/png"
|
|
|
|
|
|
def test_convert_single_audio_content_to_google_genai():
|
|
part = _sampling_content_to_google_genai_part(
|
|
AudioContent(type="audio", data="YWJj", mimeType="audio/wav")
|
|
)
|
|
|
|
assert part.inline_data is not None
|
|
assert part.inline_data.data == base64.b64decode("YWJj")
|
|
assert part.inline_data.mime_type == "audio/wav"
|
|
|
|
|
|
def test_convert_image_message_to_google_genai_content():
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=ImageContent(type="image", data="YWJj", mimeType="image/jpeg"),
|
|
)
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 1
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert msgs[0].parts[0].inline_data is not None
|
|
assert msgs[0].parts[0].inline_data.mime_type == "image/jpeg"
|
|
|
|
|
|
def test_convert_audio_message_to_google_genai_content():
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=AudioContent(type="audio", data="YWJj", mimeType="audio/mp3"),
|
|
)
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 1
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert msgs[0].parts[0].inline_data is not None
|
|
assert msgs[0].parts[0].inline_data.mime_type == "audio/mp3"
|
|
|
|
|
|
def test_convert_list_content_with_image_and_text():
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=[
|
|
TextContent(type="text", text="What is in this image?"),
|
|
ImageContent(type="image", data="YWJj", mimeType="image/png"),
|
|
],
|
|
)
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 1
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert len(msgs[0].parts) == 2
|
|
assert msgs[0].parts[0].text == "What is in this image?"
|
|
assert msgs[0].parts[1].inline_data is not None
|
|
assert msgs[0].parts[1].inline_data.mime_type == "image/png"
|
|
|
|
|
|
def test_convert_list_content_with_audio_and_text():
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=[
|
|
TextContent(type="text", text="Transcribe this audio"),
|
|
AudioContent(type="audio", data="YWJj", mimeType="audio/wav"),
|
|
],
|
|
)
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 1
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert len(msgs[0].parts) == 2
|
|
assert msgs[0].parts[0].text == "Transcribe this audio"
|
|
assert msgs[0].parts[1].inline_data is not None
|
|
assert msgs[0].parts[1].inline_data.mime_type == "audio/wav"
|
|
|
|
|
|
def test_get_model():
|
|
mock_client = MagicMock(spec=GoogleGenaiClient)
|
|
handler = GoogleGenaiSamplingHandler(
|
|
default_model="fallback-model", client=mock_client
|
|
)
|
|
|
|
# Test with Gemini model hint
|
|
prefs = ModelPreferences(hints=[ModelHint(name="gemini-2.0-flash-exp")])
|
|
assert handler._get_model(prefs) == "gemini-2.0-flash-exp"
|
|
|
|
# Test with None
|
|
assert handler._get_model(None) == "fallback-model"
|
|
|
|
# Test with empty hints
|
|
prefs_empty = ModelPreferences(hints=[])
|
|
assert handler._get_model(prefs_empty) == "fallback-model"
|
|
|
|
# Test with non-Gemini hint falls back to default
|
|
prefs_other = ModelPreferences(hints=[ModelHint(name="gpt-4o")])
|
|
assert handler._get_model(prefs_other) == "fallback-model"
|
|
|
|
# Test with mixed hints selects first Gemini model
|
|
prefs_mixed = ModelPreferences(
|
|
hints=[ModelHint(name="claude-3.5-sonnet"), ModelHint(name="gemini-2.0-flash")]
|
|
)
|
|
assert handler._get_model(prefs_mixed) == "gemini-2.0-flash"
|
|
|
|
|
|
async def test_response_to_create_message_result():
|
|
# Create a mock response
|
|
mock_response = MagicMock(spec=GenerateContentResponse)
|
|
mock_response.text = "HELPFUL CONTENT FROM GEMINI"
|
|
|
|
result: CreateMessageResult = _response_to_create_message_result(
|
|
response=mock_response, model="gemini-2.0-flash-exp"
|
|
)
|
|
assert result == CreateMessageResult(
|
|
content=TextContent(type="text", text="HELPFUL CONTENT FROM GEMINI"),
|
|
role="assistant",
|
|
model="gemini-2.0-flash-exp",
|
|
)
|
|
|
|
|
|
def test_convert_tool_choice_to_google_genai():
|
|
# Test auto mode
|
|
result = _convert_tool_choice_to_google_genai(ToolChoice(mode="auto"))
|
|
assert result.function_calling_config is not None
|
|
assert result.function_calling_config.mode == FunctionCallingConfigMode.AUTO
|
|
|
|
# Test required mode
|
|
result = _convert_tool_choice_to_google_genai(ToolChoice(mode="required"))
|
|
assert result.function_calling_config is not None
|
|
assert result.function_calling_config.mode == FunctionCallingConfigMode.ANY
|
|
|
|
# Test none mode
|
|
result = _convert_tool_choice_to_google_genai(ToolChoice(mode="none"))
|
|
assert result.function_calling_config is not None
|
|
assert result.function_calling_config.mode == FunctionCallingConfigMode.NONE
|
|
|
|
# Test None (defaults to auto)
|
|
result = _convert_tool_choice_to_google_genai(None)
|
|
assert result.function_calling_config is not None
|
|
assert result.function_calling_config.mode == FunctionCallingConfigMode.AUTO
|
|
|
|
|
|
def test_sampling_content_to_google_genai_part_tool_use():
|
|
"""Test converting ToolUseContent to Google GenAI Part with FunctionCall."""
|
|
content = ToolUseContent(
|
|
type="tool_use",
|
|
id="get_weather_abc123",
|
|
name="get_weather",
|
|
input={"city": "London"},
|
|
)
|
|
|
|
part = _sampling_content_to_google_genai_part(content)
|
|
|
|
assert part.function_call is not None
|
|
assert part.function_call.name == "get_weather"
|
|
assert part.function_call.args == {"city": "London"}
|
|
|
|
|
|
def test_sampling_content_to_google_genai_part_tool_result():
|
|
"""Test converting ToolResultContent to Google GenAI Part with FunctionResponse."""
|
|
content = ToolResultContent(
|
|
type="tool_result",
|
|
toolUseId="get_weather_abc123",
|
|
content=[TextContent(type="text", text="Weather is sunny")],
|
|
)
|
|
|
|
part = _sampling_content_to_google_genai_part(content)
|
|
|
|
assert part.function_response is not None
|
|
# Function name is extracted from toolUseId by removing the UUID suffix
|
|
assert part.function_response.name == "get_weather"
|
|
assert part.function_response.response == {"result": "Weather is sunny"}
|
|
|
|
|
|
def test_sampling_content_to_google_genai_part_tool_result_empty():
|
|
"""Test converting empty ToolResultContent to Google GenAI Part."""
|
|
content = ToolResultContent(
|
|
type="tool_result",
|
|
toolUseId="my_tool_xyz789",
|
|
content=[],
|
|
)
|
|
|
|
part = _sampling_content_to_google_genai_part(content)
|
|
|
|
assert part.function_response is not None
|
|
assert part.function_response.name == "my_tool"
|
|
assert part.function_response.response == {"result": ""}
|
|
|
|
|
|
def test_sampling_content_to_google_genai_part_tool_result_no_underscore():
|
|
"""Test ToolResultContent when toolUseId has no underscore (fallback)."""
|
|
content = ToolResultContent(
|
|
type="tool_result",
|
|
toolUseId="simplefunction",
|
|
content=[TextContent(type="text", text="Result")],
|
|
)
|
|
|
|
part = _sampling_content_to_google_genai_part(content)
|
|
|
|
# When no underscore, the full ID is used as the name
|
|
assert part.function_response is not None
|
|
assert part.function_response.name == "simplefunction"
|
|
|
|
|
|
def test_convert_messages_with_tool_use():
|
|
"""Test converting messages containing ToolUseContent."""
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=TextContent(type="text", text="What's the weather?"),
|
|
),
|
|
SamplingMessage(
|
|
role="assistant",
|
|
content=ToolUseContent(
|
|
type="tool_use",
|
|
id="get_weather_123",
|
|
name="get_weather",
|
|
input={"city": "NYC"},
|
|
),
|
|
),
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 2
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert isinstance(msgs[1], ModelContent)
|
|
assert msgs[1].parts[0].function_call is not None
|
|
assert msgs[1].parts[0].function_call.name == "get_weather"
|
|
|
|
|
|
def test_convert_messages_with_tool_result():
|
|
"""Test converting messages containing ToolResultContent."""
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=ToolResultContent(
|
|
type="tool_result",
|
|
toolUseId="get_weather_123",
|
|
content=[TextContent(type="text", text="Sunny, 72F")],
|
|
),
|
|
),
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 1
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert msgs[0].parts[0].function_response is not None
|
|
assert msgs[0].parts[0].function_response.name == "get_weather"
|
|
|
|
|
|
def test_convert_messages_with_multiple_content_blocks():
|
|
"""Test converting messages with multiple content blocks (list content)."""
|
|
msgs = _convert_messages_to_google_genai_content(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=[
|
|
TextContent(type="text", text="I need weather info."),
|
|
ToolResultContent(
|
|
type="tool_result",
|
|
toolUseId="get_weather_xyz",
|
|
content=[TextContent(type="text", text="Cloudy")],
|
|
),
|
|
],
|
|
),
|
|
],
|
|
)
|
|
|
|
assert len(msgs) == 1
|
|
assert isinstance(msgs[0], UserContent)
|
|
assert len(msgs[0].parts) == 2
|
|
assert msgs[0].parts[0].text == "I need weather info."
|
|
assert msgs[0].parts[1].function_response is not None
|
|
|
|
|
|
def test_response_to_result_with_tools_text_only():
|
|
"""Test _response_to_result_with_tools with a text-only response."""
|
|
mock_candidate = MagicMock(spec=Candidate)
|
|
mock_candidate.content = MagicMock()
|
|
mock_candidate.content.parts = [Part(text="Here's the answer")]
|
|
mock_candidate.finish_reason = "STOP"
|
|
|
|
mock_response = MagicMock(spec=GenerateContentResponse)
|
|
mock_response.candidates = [mock_candidate]
|
|
|
|
result = _response_to_result_with_tools(mock_response, model="gemini-2.0-flash")
|
|
|
|
assert result.role == "assistant"
|
|
assert result.model == "gemini-2.0-flash"
|
|
assert result.stopReason == "endTurn"
|
|
assert isinstance(result.content, list)
|
|
assert len(result.content) == 1
|
|
assert result.content[0].type == "text"
|
|
assert isinstance(result.content[0], TextContent)
|
|
assert result.content[0].text == "Here's the answer"
|
|
|
|
|
|
def test_response_to_result_with_tools_function_call():
|
|
"""Test _response_to_result_with_tools with a function call response."""
|
|
mock_candidate = MagicMock(spec=Candidate)
|
|
mock_candidate.content = MagicMock()
|
|
mock_candidate.content.parts = [
|
|
Part(function_call=FunctionCall(name="get_weather", args={"city": "Paris"}))
|
|
]
|
|
mock_candidate.finish_reason = "STOP"
|
|
|
|
mock_response = MagicMock(spec=GenerateContentResponse)
|
|
mock_response.candidates = [mock_candidate]
|
|
|
|
result = _response_to_result_with_tools(mock_response, model="gemini-2.0-flash")
|
|
|
|
assert result.stopReason == "toolUse"
|
|
assert isinstance(result.content, list)
|
|
assert len(result.content) == 1
|
|
tool_use = result.content[0]
|
|
assert isinstance(tool_use, ToolUseContent)
|
|
assert tool_use.type == "tool_use"
|
|
assert tool_use.name == "get_weather"
|
|
assert tool_use.input == {"city": "Paris"}
|
|
# ID should be in format "get_weather_{uuid}"
|
|
assert tool_use.id.startswith("get_weather_")
|
|
|
|
|
|
def test_response_to_result_with_tools_mixed_content():
|
|
"""Test _response_to_result_with_tools with text and function call."""
|
|
mock_candidate = MagicMock(spec=Candidate)
|
|
mock_candidate.content = MagicMock()
|
|
mock_candidate.content.parts = [
|
|
Part(text="Let me check that for you."),
|
|
Part(function_call=FunctionCall(name="search", args={"query": "test"})),
|
|
]
|
|
mock_candidate.finish_reason = "STOP"
|
|
|
|
mock_response = MagicMock(spec=GenerateContentResponse)
|
|
mock_response.candidates = [mock_candidate]
|
|
|
|
result = _response_to_result_with_tools(mock_response, model="gemini-2.0-flash")
|
|
|
|
assert result.stopReason == "toolUse"
|
|
assert isinstance(result.content, list)
|
|
assert len(result.content) == 2
|
|
text_content = result.content[0]
|
|
assert isinstance(text_content, TextContent)
|
|
assert text_content.type == "text"
|
|
assert text_content.text == "Let me check that for you."
|
|
tool_use = result.content[1]
|
|
assert isinstance(tool_use, ToolUseContent)
|
|
assert tool_use.type == "tool_use"
|
|
assert tool_use.name == "search"
|