mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-22 21:44:18 +02:00
feat(tool): validate and enforce default values
- Added validation to ensure all parameters listed in `exclude_args` exist in the function. - Ensured parameters in `exclude_args` have default values to avoid runtime errors. - Added test.
This commit is contained in:
parent
138316715e
commit
2233b5844b
2 changed files with 28 additions and 0 deletions
|
|
@ -76,6 +76,18 @@ class Tool(BaseModel):
|
|||
if param.kind == inspect.Parameter.VAR_KEYWORD:
|
||||
raise ValueError("Functions with **kwargs are not supported as tools")
|
||||
|
||||
if exclude_args:
|
||||
for arg_name in exclude_args:
|
||||
if arg_name not in sig.parameters:
|
||||
raise ValueError(
|
||||
f"Parameter '{arg_name}' in exclude_args does not exist in function."
|
||||
)
|
||||
param = sig.parameters[arg_name]
|
||||
if param.default == inspect.Parameter.empty:
|
||||
raise ValueError(
|
||||
f"Parameter '{arg_name}' in exclude_args must have a default value."
|
||||
)
|
||||
|
||||
func_name = name or getattr(fn, "__name__", None) or fn.__class__.__name__
|
||||
|
||||
if func_name == "<lambda>":
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from mcp.types import TextContent
|
||||
|
||||
from fastmcp import Client, FastMCP
|
||||
|
|
@ -24,6 +25,21 @@ async def test_tool_exclude_args_in_tool_manager():
|
|||
assert args not in tools[0].parameters
|
||||
|
||||
|
||||
async def test_tool_exclude_args_without_default_value_raises_error():
|
||||
"""Test that excluding args without default values raises ValueError"""
|
||||
mcp = FastMCP("Test Server")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
@mcp.tool(exclude_args=["state"])
|
||||
def echo(message: str, state: dict[str, Any] | None) -> str:
|
||||
"""Echo back the message provided."""
|
||||
if state:
|
||||
# State was read
|
||||
pass
|
||||
return message
|
||||
|
||||
|
||||
async def test_add_tool_method_exclude_args():
|
||||
"""Test that tool exclude_args work with the add_tool method."""
|
||||
mcp = FastMCP("Test Server")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue