mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-21 21:14:17 +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)
606 lines
20 KiB
Python
606 lines
20 KiB
Python
from datetime import timedelta
|
|
|
|
import pytest
|
|
from dirty_equals import HasName
|
|
from inline_snapshot import snapshot
|
|
from mcp.types import (
|
|
AudioContent,
|
|
ImageContent,
|
|
ToolExecution,
|
|
)
|
|
from pydantic import BaseModel
|
|
|
|
from fastmcp.tools.base import Tool, ToolResult
|
|
from fastmcp.utilities.types import Audio, File, Image
|
|
|
|
|
|
class TestToolFromFunction:
|
|
def test_basic_function(self):
|
|
"""Test registering and running a basic function."""
|
|
|
|
def add(a: int, b: int) -> int:
|
|
"""Add two numbers."""
|
|
return a + b
|
|
|
|
tool = Tool.from_function(add)
|
|
|
|
assert tool.model_dump(exclude_none=True) == snapshot(
|
|
{
|
|
"name": "add",
|
|
"description": "Add two numbers.",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"additionalProperties": False,
|
|
"properties": {
|
|
"a": {"type": "integer"},
|
|
"b": {"type": "integer"},
|
|
},
|
|
"required": ["a", "b"],
|
|
"type": "object",
|
|
},
|
|
"output_schema": {
|
|
"properties": {"result": {"type": "integer"}},
|
|
"required": ["result"],
|
|
"type": "object",
|
|
"x-fastmcp-wrap-result": True,
|
|
},
|
|
"fn": HasName("add"),
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
def test_meta_parameter(self):
|
|
"""Test that meta parameter is properly handled."""
|
|
|
|
def multiply(a: int, b: int) -> int:
|
|
"""Multiply two numbers."""
|
|
return a * b
|
|
|
|
meta_data = {"version": "1.0", "author": "test"}
|
|
tool = Tool.from_function(multiply, meta=meta_data)
|
|
|
|
assert tool.meta == meta_data
|
|
mcp_tool = tool.to_mcp_tool()
|
|
|
|
# MCP tool includes fastmcp meta, so check that our meta is included
|
|
assert mcp_tool.meta is not None
|
|
assert meta_data.items() <= mcp_tool.meta.items()
|
|
|
|
async def test_async_function(self):
|
|
"""Test registering and running an async function."""
|
|
|
|
async def fetch_data(url: str) -> str:
|
|
"""Fetch data from URL."""
|
|
return f"Data from {url}"
|
|
|
|
tool = Tool.from_function(fetch_data)
|
|
|
|
assert tool.model_dump(exclude_none=True) == snapshot(
|
|
{
|
|
"name": "fetch_data",
|
|
"description": "Fetch data from URL.",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"additionalProperties": False,
|
|
"properties": {"url": {"type": "string"}},
|
|
"required": ["url"],
|
|
"type": "object",
|
|
},
|
|
"output_schema": {
|
|
"properties": {"result": {"type": "string"}},
|
|
"required": ["result"],
|
|
"type": "object",
|
|
"x-fastmcp-wrap-result": True,
|
|
},
|
|
"fn": HasName("fetch_data"),
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
def test_callable_object(self):
|
|
class Adder:
|
|
"""Adds two numbers."""
|
|
|
|
def __call__(self, x: int, y: int) -> int:
|
|
"""ignore this"""
|
|
return x + y
|
|
|
|
tool = Tool.from_function(Adder())
|
|
|
|
assert tool.model_dump(exclude_none=True, exclude={"fn"}) == snapshot(
|
|
{
|
|
"name": "Adder",
|
|
"description": "Adds two numbers.",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"additionalProperties": False,
|
|
"properties": {
|
|
"x": {"type": "integer"},
|
|
"y": {"type": "integer"},
|
|
},
|
|
"required": ["x", "y"],
|
|
"type": "object",
|
|
},
|
|
"output_schema": {
|
|
"properties": {"result": {"type": "integer"}},
|
|
"required": ["result"],
|
|
"type": "object",
|
|
"x-fastmcp-wrap-result": True,
|
|
},
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
def test_async_callable_object(self):
|
|
class Adder:
|
|
"""Adds two numbers."""
|
|
|
|
async def __call__(self, x: int, y: int) -> int:
|
|
"""ignore this"""
|
|
return x + y
|
|
|
|
tool = Tool.from_function(Adder())
|
|
|
|
assert tool.model_dump(exclude_none=True, exclude={"fn"}) == snapshot(
|
|
{
|
|
"name": "Adder",
|
|
"description": "Adds two numbers.",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"additionalProperties": False,
|
|
"properties": {
|
|
"x": {"type": "integer"},
|
|
"y": {"type": "integer"},
|
|
},
|
|
"required": ["x", "y"],
|
|
"type": "object",
|
|
},
|
|
"output_schema": {
|
|
"properties": {"result": {"type": "integer"}},
|
|
"required": ["result"],
|
|
"type": "object",
|
|
"x-fastmcp-wrap-result": True,
|
|
},
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
def test_pydantic_model_function(self):
|
|
"""Test registering a function that takes a Pydantic model."""
|
|
|
|
class UserInput(BaseModel):
|
|
name: str
|
|
age: int
|
|
|
|
def create_user(user: UserInput, flag: bool) -> dict:
|
|
"""Create a new user."""
|
|
return {"id": 1, **user.model_dump()}
|
|
|
|
tool = Tool.from_function(create_user)
|
|
|
|
assert tool.model_dump(exclude_none=True) == snapshot(
|
|
{
|
|
"name": "create_user",
|
|
"description": "Create a new user.",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"$defs": {
|
|
"UserInput": {
|
|
"properties": {
|
|
"name": {"type": "string"},
|
|
"age": {"type": "integer"},
|
|
},
|
|
"required": ["name", "age"],
|
|
"type": "object",
|
|
},
|
|
},
|
|
"additionalProperties": False,
|
|
"properties": {
|
|
"user": {"$ref": "#/$defs/UserInput"},
|
|
"flag": {"type": "boolean"},
|
|
},
|
|
"required": ["user", "flag"],
|
|
"type": "object",
|
|
},
|
|
"output_schema": {"additionalProperties": True, "type": "object"},
|
|
"fn": HasName("create_user"),
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
async def test_tool_with_image_return(self):
|
|
def image_tool(data: bytes) -> Image:
|
|
return Image(data=data)
|
|
|
|
tool = Tool.from_function(image_tool)
|
|
assert tool.parameters["properties"]["data"]["type"] == "string"
|
|
assert tool.output_schema is None
|
|
|
|
result = await tool.run({"data": "test.png"})
|
|
assert isinstance(result.content[0], ImageContent)
|
|
|
|
async def test_tool_with_audio_return(self):
|
|
def audio_tool(data: bytes) -> Audio:
|
|
return Audio(data=data)
|
|
|
|
tool = Tool.from_function(audio_tool)
|
|
assert tool.parameters["properties"]["data"]["type"] == "string"
|
|
assert tool.output_schema is None
|
|
|
|
result = await tool.run({"data": "test.wav"})
|
|
assert isinstance(result.content[0], AudioContent)
|
|
|
|
async def test_tool_with_file_return(self):
|
|
from pydantic import AnyUrl
|
|
|
|
def file_tool(data: bytes) -> File:
|
|
return File(data=data, format="octet-stream")
|
|
|
|
tool = Tool.from_function(file_tool)
|
|
assert tool.parameters["properties"]["data"]["type"] == "string"
|
|
assert tool.output_schema is None
|
|
|
|
result: ToolResult = await tool.run({"data": "test.bin"})
|
|
assert result.content[0].model_dump(exclude_none=True) == snapshot(
|
|
{
|
|
"type": "resource",
|
|
"resource": {
|
|
"uri": AnyUrl("file:///resource.octet-stream"),
|
|
"mimeType": "application/octet-stream",
|
|
"blob": "dGVzdC5iaW4=",
|
|
},
|
|
}
|
|
)
|
|
|
|
def test_non_callable_fn(self):
|
|
with pytest.raises(TypeError, match="not a callable object"):
|
|
Tool.from_function(1) # type: ignore
|
|
|
|
def test_lambda(self):
|
|
tool = Tool.from_function(lambda x: x, name="my_tool")
|
|
assert tool.model_dump(exclude_none=True, exclude={"fn"}) == snapshot(
|
|
{
|
|
"name": "my_tool",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"additionalProperties": False,
|
|
"properties": {"x": {"title": "X"}},
|
|
"required": ["x"],
|
|
"type": "object",
|
|
},
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
def test_lambda_with_no_name(self):
|
|
with pytest.raises(
|
|
ValueError, match="You must provide a name for lambda functions"
|
|
):
|
|
Tool.from_function(lambda x: x)
|
|
|
|
def test_private_arguments(self):
|
|
def add(_a: int, _b: int) -> int:
|
|
"""Add two numbers."""
|
|
return _a + _b
|
|
|
|
tool = Tool.from_function(add)
|
|
|
|
assert tool.model_dump(
|
|
exclude_none=True, exclude={"output_schema", "fn"}
|
|
) == snapshot(
|
|
{
|
|
"name": "add",
|
|
"description": "Add two numbers.",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"additionalProperties": False,
|
|
"properties": {
|
|
"_a": {"type": "integer"},
|
|
"_b": {"type": "integer"},
|
|
},
|
|
"required": ["_a", "_b"],
|
|
"type": "object",
|
|
},
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
def test_tool_with_varargs_not_allowed(self):
|
|
def func(a: int, b: int, *args: int) -> int:
|
|
"""Add two numbers."""
|
|
return a + b
|
|
|
|
with pytest.raises(
|
|
ValueError, match=r"Functions with \*args are not supported as tools"
|
|
):
|
|
Tool.from_function(func)
|
|
|
|
def test_tool_with_varkwargs_not_allowed(self):
|
|
def func(a: int, b: int, **kwargs: int) -> int:
|
|
"""Add two numbers."""
|
|
return a + b
|
|
|
|
with pytest.raises(
|
|
ValueError, match=r"Functions with \*\*kwargs are not supported as tools"
|
|
):
|
|
Tool.from_function(func)
|
|
|
|
async def test_instance_method(self):
|
|
class MyClass:
|
|
def add(self, x: int, y: int) -> int:
|
|
"""Add two numbers."""
|
|
return x + y
|
|
|
|
obj = MyClass()
|
|
|
|
tool = Tool.from_function(obj.add)
|
|
assert "self" not in tool.parameters["properties"]
|
|
|
|
assert tool.model_dump(exclude_none=True, exclude={"fn"}) == snapshot(
|
|
{
|
|
"name": "add",
|
|
"description": "Add two numbers.",
|
|
"tags": set(),
|
|
"parameters": {
|
|
"additionalProperties": False,
|
|
"properties": {
|
|
"x": {"type": "integer"},
|
|
"y": {"type": "integer"},
|
|
},
|
|
"required": ["x", "y"],
|
|
"type": "object",
|
|
},
|
|
"output_schema": {
|
|
"properties": {"result": {"type": "integer"}},
|
|
"required": ["result"],
|
|
"type": "object",
|
|
"x-fastmcp-wrap-result": True,
|
|
},
|
|
"task_config": {
|
|
"mode": "forbidden",
|
|
"poll_interval": timedelta(seconds=5),
|
|
},
|
|
}
|
|
)
|
|
|
|
async def test_instance_method_with_varargs_not_allowed(self):
|
|
class MyClass:
|
|
def add(self, x: int, y: int, *args: int) -> int:
|
|
"""Add two numbers."""
|
|
return x + y
|
|
|
|
obj = MyClass()
|
|
|
|
with pytest.raises(
|
|
ValueError, match=r"Functions with \*args are not supported as tools"
|
|
):
|
|
Tool.from_function(obj.add)
|
|
|
|
async def test_instance_method_with_varkwargs_not_allowed(self):
|
|
class MyClass:
|
|
def add(self, x: int, y: int, **kwargs: int) -> int:
|
|
"""Add two numbers."""
|
|
return x + y
|
|
|
|
obj = MyClass()
|
|
|
|
with pytest.raises(
|
|
ValueError, match=r"Functions with \*\*kwargs are not supported as tools"
|
|
):
|
|
Tool.from_function(obj.add)
|
|
|
|
async def test_classmethod(self):
|
|
class MyClass:
|
|
x: int = 10
|
|
|
|
@classmethod
|
|
def call(cls, x: int, y: int) -> int:
|
|
"""Add two numbers."""
|
|
return x + y
|
|
|
|
tool = Tool.from_function(MyClass.call)
|
|
assert tool.name == "call"
|
|
assert tool.description == "Add two numbers."
|
|
assert "x" in tool.parameters["properties"]
|
|
assert "y" in tool.parameters["properties"]
|
|
|
|
|
|
class TestToolNameValidation:
|
|
"""Tests for tool name validation per MCP specification (SEP-986)."""
|
|
|
|
@pytest.fixture
|
|
def caplog_for_mcp_validation(self, caplog):
|
|
"""Capture logs from the MCP SDK's tool name validation logger."""
|
|
import logging
|
|
|
|
caplog.set_level(logging.WARNING)
|
|
logger = logging.getLogger("mcp.shared.tool_name_validation")
|
|
original_level = logger.level
|
|
logger.setLevel(logging.WARNING)
|
|
logger.addHandler(caplog.handler)
|
|
try:
|
|
yield caplog
|
|
finally:
|
|
logger.removeHandler(caplog.handler)
|
|
logger.setLevel(original_level)
|
|
|
|
@pytest.mark.parametrize(
|
|
"name",
|
|
[
|
|
"valid_tool",
|
|
"valid-tool",
|
|
"valid.tool",
|
|
"ValidTool",
|
|
"tool123",
|
|
"a",
|
|
"a" * 128,
|
|
],
|
|
)
|
|
def test_valid_tool_names_no_warnings(self, name, caplog_for_mcp_validation):
|
|
"""Valid tool names should not produce warnings."""
|
|
|
|
def fn() -> str:
|
|
return "test"
|
|
|
|
tool = Tool.from_function(fn, name=name)
|
|
assert tool.name == name
|
|
assert "Tool name validation warning" not in caplog_for_mcp_validation.text
|
|
|
|
def test_tool_name_with_spaces_warns(self, caplog_for_mcp_validation):
|
|
"""Tool names with spaces should produce a warning."""
|
|
|
|
def fn() -> str:
|
|
return "test"
|
|
|
|
tool = Tool.from_function(fn, name="my tool")
|
|
assert tool.name == "my tool"
|
|
assert "Tool name validation warning" in caplog_for_mcp_validation.text
|
|
assert "contains spaces" in caplog_for_mcp_validation.text
|
|
|
|
def test_tool_name_with_invalid_chars_warns(self, caplog_for_mcp_validation):
|
|
"""Tool names with invalid characters should produce a warning."""
|
|
|
|
def fn() -> str:
|
|
return "test"
|
|
|
|
tool = Tool.from_function(fn, name="tool@name!")
|
|
assert tool.name == "tool@name!"
|
|
assert "Tool name validation warning" in caplog_for_mcp_validation.text
|
|
assert "invalid characters" in caplog_for_mcp_validation.text
|
|
|
|
def test_tool_name_too_long_warns(self, caplog_for_mcp_validation):
|
|
"""Tool names exceeding 128 characters should produce a warning."""
|
|
|
|
def fn() -> str:
|
|
return "test"
|
|
|
|
long_name = "a" * 129
|
|
tool = Tool.from_function(fn, name=long_name)
|
|
assert tool.name == long_name
|
|
assert "Tool name validation warning" in caplog_for_mcp_validation.text
|
|
assert "exceeds maximum length" in caplog_for_mcp_validation.text
|
|
|
|
def test_tool_name_with_leading_dash_warns(self, caplog_for_mcp_validation):
|
|
"""Tool names starting with dash should produce a warning."""
|
|
|
|
def fn() -> str:
|
|
return "test"
|
|
|
|
tool = Tool.from_function(fn, name="-tool")
|
|
assert tool.name == "-tool"
|
|
assert "Tool name validation warning" in caplog_for_mcp_validation.text
|
|
assert "starts or ends with a dash" in caplog_for_mcp_validation.text
|
|
|
|
def test_tool_still_created_despite_warnings(self, caplog_for_mcp_validation):
|
|
"""Tools with invalid names should still be created (SHOULD not MUST)."""
|
|
|
|
def add(a: int, b: int) -> int:
|
|
return a + b
|
|
|
|
tool = Tool.from_function(add, name="invalid tool name!")
|
|
assert tool.name == "invalid tool name!"
|
|
assert tool.parameters is not None
|
|
assert "a" in tool.parameters["properties"]
|
|
assert "b" in tool.parameters["properties"]
|
|
|
|
|
|
class TestToolExecutionField:
|
|
"""Tests for the execution field on the base Tool class."""
|
|
|
|
def test_tool_with_execution_field(self):
|
|
"""Test that Tool can store and return execution metadata."""
|
|
tool = Tool(
|
|
name="my_tool",
|
|
description="A tool with execution",
|
|
parameters={"type": "object", "properties": {}},
|
|
execution=ToolExecution(taskSupport="optional"),
|
|
)
|
|
|
|
mcp_tool = tool.to_mcp_tool()
|
|
assert mcp_tool.execution is not None
|
|
assert mcp_tool.execution.taskSupport == "optional"
|
|
|
|
def test_tool_without_execution_field(self):
|
|
"""Test that Tool without execution returns None."""
|
|
tool = Tool(
|
|
name="my_tool",
|
|
description="A tool without execution",
|
|
parameters={"type": "object", "properties": {}},
|
|
)
|
|
|
|
mcp_tool = tool.to_mcp_tool()
|
|
assert mcp_tool.execution is None
|
|
|
|
def test_execution_override_takes_precedence(self):
|
|
"""Test that explicit override takes precedence over field value."""
|
|
tool = Tool(
|
|
name="my_tool",
|
|
description="A tool",
|
|
parameters={"type": "object", "properties": {}},
|
|
execution=ToolExecution(taskSupport="optional"),
|
|
)
|
|
|
|
override_execution = ToolExecution(taskSupport="required")
|
|
mcp_tool = tool.to_mcp_tool(execution=override_execution)
|
|
assert mcp_tool.execution is not None
|
|
assert mcp_tool.execution.taskSupport == "required"
|
|
|
|
async def test_function_tool_task_config_still_works(self):
|
|
"""FunctionTool should still derive execution from task_config."""
|
|
|
|
async def my_fn() -> str:
|
|
return "hello"
|
|
|
|
tool = Tool.from_function(my_fn, task=True)
|
|
mcp_tool = tool.to_mcp_tool()
|
|
|
|
# FunctionTool sets execution from task_config
|
|
assert mcp_tool.execution is not None
|
|
assert mcp_tool.execution.taskSupport == "optional"
|
|
|
|
def test_tool_execution_required_mode(self):
|
|
"""Test that Tool can store required execution mode."""
|
|
tool = Tool(
|
|
name="my_tool",
|
|
description="A tool with required execution",
|
|
parameters={"type": "object", "properties": {}},
|
|
execution=ToolExecution(taskSupport="required"),
|
|
)
|
|
|
|
mcp_tool = tool.to_mcp_tool()
|
|
assert mcp_tool.execution is not None
|
|
assert mcp_tool.execution.taskSupport == "required"
|
|
|
|
def test_tool_execution_forbidden_mode(self):
|
|
"""Test that Tool can store forbidden execution mode."""
|
|
tool = Tool(
|
|
name="my_tool",
|
|
description="A tool with forbidden execution",
|
|
parameters={"type": "object", "properties": {}},
|
|
execution=ToolExecution(taskSupport="forbidden"),
|
|
)
|
|
|
|
mcp_tool = tool.to_mcp_tool()
|
|
assert mcp_tool.execution is not None
|
|
assert mcp_tool.execution.taskSupport == "forbidden"
|