π The fast, Pythonic way to build MCP servers and clients.
Reject positional-only parameters in prompts and resource templates (#5505)
Asem ahmed77b585ae1948
diff --git a/fastmcp_slim/fastmcp/prompts/function_prompt.py b/fastmcp_slim/fastmcp/prompts/function_prompt.pyindex e17d17d4e..525eaae74 100644--- a/fastmcp_slim/fastmcp/prompts/function_prompt.py+++ b/fastmcp_slim/fastmcp/prompts/function_prompt.py@@ -131,9 +131,17 @@ class FunctionPrompt(Prompt):if func_name == "<lambda>":raise ValueError("You must provide a name for lambda functions")- # Reject functions with *args or **kwargs+ # Reject signatures that cannot be represented by MCP's named+ # prompt argumentssig = inspect.signature(fn)for param in sig.parameters.values():+ if param.kind == inspect.Parameter.POSITIONAL_ONLY:+ raise ValueError(+ "Functions with positional-only parameters are not "+ "supported as prompts because MCP passes prompt arguments "+ "by name. Replace them with standard parameters that can be "+ "passed as keywords."+ )if param.kind == inspect.Parameter.VAR_POSITIONAL:raise ValueError("Functions with *args are not supported as prompts")if param.kind == inspect.Parameter.VAR_KEYWORD:diff --git a/fastmcp_slim/fastmcp/resources/template.py b/fastmcp_slim/fastmcp/resources/template.pyindex c76dd7ed8..f72d31d4a 100644--- a/fastmcp_slim/fastmcp/resources/template.py+++ b/fastmcp_slim/fastmcp/resources/template.py@@ -659,10 +659,17 @@ class FunctionResourceTemplate(ResourceTemplate):if func_name == "<lambda>":raise ValueError("You must provide a name for lambda functions")- # Reject functions with *args+ # Reject functions with positional-only parameters or *args# (**kwargs is allowed because the URI will define the parameter names)sig = inspect.signature(fn)for param in sig.parameters.values():+ if param.kind == inspect.Parameter.POSITIONAL_ONLY:+ raise ValueError(+ "Functions with positional-only parameters are not "+ "supported as resource templates because URI template "+ "parameters are passed by name. Replace them with standard "+ "parameters that can be passed as keywords."+ )if param.kind == inspect.Parameter.VAR_POSITIONAL:raise ValueError("Functions with *args are not supported as resource templates"diff --git a/tests/prompts/test_prompt.py b/tests/prompts/test_prompt.pyindex 841c173da..66991f8d4 100644--- a/tests/prompts/test_prompt.py+++ b/tests/prompts/test_prompt.py@@ -64,6 +64,27 @@ class TestRenderPrompt:with pytest.raises(ValueError):await prompt.render(arguments=dict(age=40))+ async def test_fn_with_positional_only_parameters_not_allowed(self):+ def fn(name: str, /, age: int = 30) -> str:+ return f"Hello, {name}! You're {age} years old."++ with pytest.raises(+ ValueError,+ match=(+ "Functions with positional-only parameters are not supported as "+ "prompts.*standard parameters"+ ),+ ):+ Prompt.from_function(fn)++ async def test_fn_with_keyword_only_parameters(self):+ def fn(name: str, *, age: int = 30) -> str:+ return f"Hello, {name}! You're {age} years old."++ prompt = Prompt.from_function(fn)+ result = await prompt.render(arguments=dict(name="World", age=40))+ assert result.messages == [Message("Hello, World! You're 40 years old.")]+async def test_fn_returns_message_list(self):async def fn() -> list[Message]:return [Message("Hello, world!")]diff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.pyindex 14f453b36..6fca11cf0 100644--- a/tests/resources/test_resource_template.py+++ b/tests/resources/test_resource_template.py@@ -375,6 +375,35 @@ class TestResourceTemplate:name="test",)+ async def test_function_with_positional_only_not_allowed(self):+ def func(x: int, /) -> int:+ return x++ with pytest.raises(+ ValueError,+ match=(+ "Functions with positional-only parameters are not supported as "+ "resource templates.*standard parameters"+ ),+ ):+ ResourceTemplate.from_function(+ fn=func,+ uri_template="test://{x}",+ name="test",+ )++ async def test_function_with_keyword_only_ok(self):+ def func(x: int, *, y: int = 1) -> int:+ return x + y++ template = ResourceTemplate.from_function(+ fn=func,+ uri_template="test://{x}{?y}",+ name="test",+ )++ assert await template.read({"x": "2", "y": "3"}) == 5+async def test_function_with_varkwargs_ok(self):def func(x: int, **kwargs: int) -> int:return x + sum(kwargs.values())