Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605import jsonfrom typing import Any
from mcp.types import TextContent
from fastmcp import FastMCPfrom fastmcp.server.plugins.code_mode import ( GetTags, ListTools, Search,)from fastmcp.server.plugins.code_mode.sandbox import _ensure_asyncfrom fastmcp.server.plugins.code_mode.transform import CodeModeTransformfrom fastmcp.tools.base import ToolResult
def _unwrap_result(result: ToolResult) -> Any: """Extract the logical return value from a ToolResult.""" if result.structured_content is not None: return result.structured_content
text_blocks = [ content.text for content in result.content if isinstance(content, TextContent) ] if not text_blocks: return None
if len(text_blocks) == 1: try: return json.loads(text_blocks[0]) except json.JSONDecodeError: return text_blocks[0]
values: list[Any] = [] for text in text_blocks: try: values.append(json.loads(text)) except json.JSONDecodeError: values.append(text) return values
def _unwrap_string_result(result: ToolResult) -> str: """Extract a string result from a ToolResult.""" data = _unwrap_result(result) if isinstance(data, dict) and "result" in data: return data["result"] assert isinstance(data, str) return data
class _UnsafeTestSandboxProvider: """UNSAFE: Uses exec() for testing only. Never use in production."""
async def run( self, code: str, *, inputs: dict[str, Any] | None = None, external_functions: dict[str, Any] | None = None, ) -> Any: namespace: dict[str, Any] = {} if inputs: namespace.update(inputs) if external_functions: namespace.update( {key: _ensure_async(value) for key, value in external_functions.items()} )
wrapped = "async def __test_main__():\n" for line in code.splitlines(): wrapped += f" {line}\n" if not code.strip(): wrapped += " return None\n"
exec(wrapped, namespace, namespace) return await namespace["__test_main__"]()
async def _run_tool( server: FastMCP, name: str, arguments: dict[str, Any]) -> ToolResult: return await server.call_tool(name, arguments)
# ---------------------------------------------------------------------------# Tags discovery tool# ---------------------------------------------------------------------------
async def test_categories_brief_shows_tag_counts() -> None: mcp = FastMCP("Tags Brief")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: return x + y
@mcp.tool(tags={"math"}) def multiply(x: int, y: int) -> int: return x * y
@mcp.tool(tags={"text"}) def greet(name: str) -> str: return f"Hello, {name}!"
mcp.add_transform( CodeModeTransform( discovery_tools=[GetTags()], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "tags", {}) text = _unwrap_string_result(result) assert "math (2 tools)" in text assert "text (1 tool)" in text
async def test_categories_full_lists_tools_per_tag() -> None: mcp = FastMCP("Tags Full")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
@mcp.tool(tags={"text"}) def greet(name: str) -> str: """Say hello.""" return f"Hello, {name}!"
mcp.add_transform( CodeModeTransform( discovery_tools=[GetTags(default_detail="full")], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "tags", {}) text = _unwrap_string_result(result) assert "### math" in text assert "- add: Add two numbers." in text assert "### text" in text assert "- greet: Say hello." in text
async def test_categories_includes_untagged() -> None: mcp = FastMCP("Tags Untagged")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: return x + y
@mcp.tool def ping() -> str: return "pong"
mcp.add_transform( CodeModeTransform( discovery_tools=[GetTags()], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "tags", {}) text = _unwrap_string_result(result) assert "math" in text assert "untagged (1 tool)" in text
async def test_categories_tool_in_multiple_tags() -> None: mcp = FastMCP("Tags Multi-tag")
@mcp.tool(tags={"math", "core"}) def add(x: int, y: int) -> int: return x + y
mcp.add_transform( CodeModeTransform( discovery_tools=[GetTags(default_detail="full")], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "tags", {}) text = _unwrap_string_result(result) assert "### core" in text assert "### math" in text # Tool appears under both tags assert text.count("- add") == 2
async def test_categories_detail_override_per_call() -> None: """LLM can override default_detail on a per-call basis.""" mcp = FastMCP("Tags Override")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: """Add numbers.""" return x + y
mcp.add_transform( CodeModeTransform( discovery_tools=[GetTags()], # default_detail="brief" sandbox_provider=_UnsafeTestSandboxProvider(), ) )
# Override to full result = await _run_tool(mcp, "tags", {"detail": "full"}) text = _unwrap_string_result(result) assert "### math" in text assert "- add: Add numbers." in text
async def test_get_tags_empty_catalog() -> None: """GetTags with no tools returns 'No tools available.'.""" mcp = FastMCP("CodeMode Empty Tags")
mcp.disable(names={"ping"}, components={"tool"}) mcp.add_transform( CodeModeTransform( discovery_tools=[GetTags()], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "tags", {}) text = _unwrap_string_result(result) assert "No tools available" in text
# ---------------------------------------------------------------------------# Search with tags filtering# ---------------------------------------------------------------------------
async def test_search_with_tags_filter() -> None: mcp = FastMCP("Search Tags")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
@mcp.tool(tags={"text"}) def greet(name: str) -> str: """Say hello.""" return f"Hello, {name}!"
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "search", {"query": "add hello", "tags": ["math"]}) text = _unwrap_string_result(result) assert "add" in text assert "greet" not in text
async def test_search_with_tags_filter_no_matches() -> None: mcp = FastMCP("Search Tags Empty")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "search", {"query": "add", "tags": ["nonexistent"]}) text = _unwrap_string_result(result) assert "add" not in text or "No tools" in text
async def test_search_without_tags_returns_all() -> None: """Search without tags parameter searches the full catalog.""" mcp = FastMCP("Search No Tags")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
@mcp.tool(tags={"text"}) def greet(name: str) -> str: """Say hello.""" return f"Hello, {name}!"
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "search", {"query": "add hello"}) text = _unwrap_string_result(result) assert "add" in text assert "greet" in text
async def test_search_with_untagged_filter() -> None: """Search with tags=["untagged"] matches tools that have no tags.""" mcp = FastMCP("Search Untagged")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
@mcp.tool def ping() -> str: """Ping.""" return "pong"
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "search", {"query": "ping add", "tags": ["untagged"]}) text = _unwrap_string_result(result) assert "ping" in text assert "add" not in text
async def test_search_default_detail_detailed_skips_get_schema() -> None: """Two-stage pattern: Search(default_detail='detailed') returns schemas inline.""" mcp = FastMCP("CodeMode Two-Stage")
@mcp.tool def square(x: int) -> int: """Compute the square.""" return x * x
mcp.add_transform( CodeModeTransform( discovery_tools=[Search(default_detail="detailed")], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "search", {"query": "square"}) text = _unwrap_string_result(result) assert "### square" in text assert "**Parameters**" in text assert "`x` (integer, required)" in text
async def test_search_full_detail_empty_results_returns_json() -> None: """Search with detail=full and no matches returns valid JSON, not plain text.""" mcp = FastMCP("CodeMode Empty Full")
@mcp.tool(tags={"math"}) def add(x: int, y: int) -> int: return x + y
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool( mcp, "search", {"query": "nonexistent", "tags": ["nonexistent"], "detail": "full"}, ) text = _unwrap_string_result(result) parsed = json.loads(text) assert parsed == []
async def test_get_schema_empty_tools_list() -> None: """get_schema with an empty tools list returns no-match message.""" mcp = FastMCP("CodeMode Empty Schema")
@mcp.tool def ping() -> str: return "pong"
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "get_schema", {"tools": []}) text = _unwrap_string_result(result) assert "No tools matched" in text
async def test_get_schema_full_partial_match_returns_valid_json() -> None: """get_schema with detail=full and missing tools returns valid JSON.""" mcp = FastMCP("Schema Full Partial")
@mcp.tool def square(x: int) -> int: """Compute the square.""" return x * x
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool( mcp, "get_schema", {"tools": ["square", "nonexistent"], "detail": "full"} ) text = _unwrap_string_result(result) parsed = json.loads(text) assert isinstance(parsed, list) assert parsed[0]["name"] == "square" assert parsed[-1] == {"not_found": ["nonexistent"]}
# ---------------------------------------------------------------------------# Search catalog size annotation# ---------------------------------------------------------------------------
async def test_search_shows_catalog_size_when_results_are_subset() -> None: """Search annotates results with 'N of M tools' when not all tools match.""" mcp = FastMCP("Search Annotation")
@mcp.tool def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
@mcp.tool def multiply(x: int, y: int) -> int: """Multiply two numbers.""" return x * y
@mcp.tool def greet(name: str) -> str: """Say hello.""" return f"Hello, {name}!"
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "search", {"query": "add numbers"}) text = _unwrap_string_result(result) # Should show partial result count out of total catalog assert "of 3 tools:" in text
async def test_search_omits_annotation_when_all_tools_returned() -> None: """Search does not annotate when results include every tool.""" mcp = FastMCP("Search All Match")
@mcp.tool def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "search", {"query": "add numbers"}) text = _unwrap_string_result(result) assert "of" not in text or "tools:" not in text
# ---------------------------------------------------------------------------# Search limit# ---------------------------------------------------------------------------
async def test_search_limit_caps_results() -> None: """Search with limit returns at most that many results.""" mcp = FastMCP("Search Limit")
@mcp.tool def add(x: int, y: int) -> int: """Add numbers.""" return x + y
@mcp.tool def subtract(x: int, y: int) -> int: """Subtract numbers.""" return x - y
@mcp.tool def multiply(x: int, y: int) -> int: """Multiply numbers.""" return x * y
mcp.add_transform(CodeModeTransform(sandbox_provider=_UnsafeTestSandboxProvider()))
result = await _run_tool(mcp, "search", {"query": "numbers", "limit": 1}) text = _unwrap_string_result(result) assert "1 of 3 tools:" in text # Only one tool line (starts with "- ") tool_lines = [line for line in text.splitlines() if line.startswith("- ")] assert len(tool_lines) == 1
async def test_search_default_limit_from_constructor() -> None: """Search(default_limit=N) caps results by default.""" mcp = FastMCP("Search Default Limit")
@mcp.tool def a() -> str: """Tool A.""" return "a"
@mcp.tool def b() -> str: """Tool B.""" return "b"
@mcp.tool def c() -> str: """Tool C.""" return "c"
mcp.add_transform( CodeModeTransform( discovery_tools=[Search(default_limit=2)], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "search", {"query": "tool"}) text = _unwrap_string_result(result) assert "2 of 3 tools:" in text
# ---------------------------------------------------------------------------# ListTools discovery tool# ---------------------------------------------------------------------------
async def test_list_tools_brief() -> None: """ListTools at brief detail lists all tool names and descriptions.""" mcp = FastMCP("ListTools Brief")
@mcp.tool def add(x: int, y: int) -> int: """Add two numbers.""" return x + y
@mcp.tool def multiply(x: int, y: int) -> int: """Multiply two numbers.""" return x * y
mcp.add_transform( CodeModeTransform( discovery_tools=[ListTools()], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "list_tools", {}) text = _unwrap_string_result(result) assert "add" in text assert "multiply" in text assert "Add two numbers" in text # brief should not include parameter details assert "**Parameters**" not in text
async def test_list_tools_detailed() -> None: """ListTools at detailed shows parameter schemas.""" mcp = FastMCP("ListTools Detailed")
@mcp.tool def square(x: int) -> int: """Compute the square.""" return x * x
mcp.add_transform( CodeModeTransform( discovery_tools=[ListTools(default_detail="detailed")], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "list_tools", {}) text = _unwrap_string_result(result) assert "### square" in text assert "**Parameters**" in text assert "`x` (integer, required)" in text
async def test_list_tools_full_returns_json() -> None: """ListTools at full returns valid JSON with schemas.""" mcp = FastMCP("ListTools Full")
@mcp.tool def ping() -> str: """Ping.""" return "pong"
mcp.add_transform( CodeModeTransform( discovery_tools=[ListTools()], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "list_tools", {"detail": "full"}) text = _unwrap_string_result(result) parsed = json.loads(text) assert isinstance(parsed, list) assert parsed[0]["name"] == "ping"
async def test_list_tools_empty_catalog() -> None: """ListTools with no tools returns no-match message.""" mcp = FastMCP("ListTools Empty")
mcp.add_transform( CodeModeTransform( discovery_tools=[ListTools()], sandbox_provider=_UnsafeTestSandboxProvider(), ) )
result = await _run_tool(mcp, "list_tools", {}) text = _unwrap_string_result(result) assert "No tools matched" in text