π The fast, Pythonic way to build MCP servers and clients.
Update typing
Jeremiah Lowinb3799e7a7329
diff --git a/tests/server/test_server_interactions.py b/tests/server/test_server_interactions.pyindex b91b83b44..c48e5800a 100644--- a/tests/server/test_server_interactions.py+++ b/tests/server/test_server_interactions.py@@ -906,7 +906,7 @@ class TestToolOutputSchema:mcp = FastMCP()@mcp.tool- def f() -> annotation:+ def f() -> annotation: # type: ignore[valid-type]return {"name": "John", "age": 30}async with Client(mcp) as client:@@ -950,7 +950,7 @@ class TestToolOutputSchema:"""Test that output_schema=False works through full client/server handshake."""mcp = FastMCP()- @mcp.tool(output_schema=False)+ @mcp.tool(output_schema=False) # type: ignore[arg-type]def simple_tool() -> dict[str, str]:return {"message": "Hello from disabled schema"}@@ -964,9 +964,9 @@ class TestToolOutputSchema:result = await client.call_tool("simple_tool", {})assert result.structured_content is Noneassert result.data is None- assert json.loads(result.content[0].text) == {+ assert json.loads(result.content[0].text) == { # type: ignore[attr-defined]"message": "Hello from disabled schema"- } # type: ignore[attr-defined]+ }async def test_output_schema_explicit_object_full_handshake(self):"""Test explicit object output schema through full client/server handshake."""@@ -1076,7 +1076,9 @@ class TestToolOutputSchema:tool = next(t for t in tools if t.name == "dataclass_tool")expected_schema = TypeAdapter(User).json_schema()assert tool.outputSchema == expected_schema- assert "x-fastmcp-wrap-result" not in tool.outputSchema+ assert (+ tool.outputSchema and "x-fastmcp-wrap-result" not in tool.outputSchema+ )# Call tool and verify structured content is directresult = await client.call_tool("dataclass_tool", {})@@ -1131,7 +1133,7 @@ class TestToolOutputSchema:tool = next(t for t in tools if t.name == "edge_case_tool")# Tuples should be wrapped since they're not object type- assert "x-fastmcp-wrap-result" in tool.outputSchema+ assert tool.outputSchema and "x-fastmcp-wrap-result" in tool.outputSchemaresult = await client.call_tool("edge_case_tool", {})# Should be wrapped with result keydiff --git a/tests/tools/test_tool.py b/tests/tools/test_tool.pyindex 6a4b5ac24..24b10ce1f 100644--- a/tests/tools/test_tool.py+++ b/tests/tools/test_tool.py@@ -540,7 +540,7 @@ class TestToolFromFunctionOutputSchema:result = await tool.run({})assert result.structured_content is Noneassert len(result.content) == 1- assert result.content[0].text == '{\n "message": "Hello, world!"\n}'+ assert result.content[0].text == '{\n "message": "Hello, world!"\n}' # type: ignore[attr-defined]async def test_output_schema_none_disables_structured_content(self):"""Test that output_schema=None explicitly disables structured content."""@@ -554,7 +554,7 @@ class TestToolFromFunctionOutputSchema:result = await tool.run({})assert result.structured_content is Noneassert len(result.content) == 1- assert result.content[0].text == "42"+ assert result.content[0].text == "42" # type: ignore[attr-defined]async def test_output_schema_inferred_when_not_specified(self):"""Test that output schema is inferred when not explicitly specified."""@@ -587,12 +587,12 @@ class TestToolFromFunctionOutputSchema:}tool = Tool.from_function(func, output_schema=explicit_schema)assert tool.output_schema == explicit_schema # Schema not wrapped- assert "x-fastmcp-wrap-result" not in tool.output_schema+ assert tool.output_schema and "x-fastmcp-wrap-result" not in tool.output_schemaresult = await tool.run({})# Dict result with object schema is used directlyassert result.structured_content == {"value": 42}- assert result.content[0].text == '{\n "value": 42\n}'+ assert result.content[0].text == '{\n "value": 42\n}' # type: ignore[attr-defined]async def test_explicit_object_schema_with_non_dict_return_fails(self):"""Test that explicit object schemas fail when function returns non-dict."""@@ -621,7 +621,7 @@ class TestToolFromFunctionOutputSchema:tool = Tool.from_function(func)expected_schema = TypeAdapter(dict[str, int]).json_schema()assert tool.output_schema == expected_schema # Not wrapped- assert "x-fastmcp-wrap-result" not in tool.output_schema+ assert tool.output_schema and "x-fastmcp-wrap-result" not in tool.output_schemaresult = await tool.run({})assert result.structured_content == {"value": 42} # Direct value@@ -644,7 +644,7 @@ class TestToolFromFunctionOutputSchema:result = await tool.run({})# Unstructured contentassert len(result.content) == 1- assert result.content[0].text == "hello"+ assert result.content[0].text == "hello" # type: ignore[attr-defined]# Structured content should be wrappedassert result.structured_content == {"result": "hello"}@@ -706,7 +706,7 @@ class TestToolFromFunctionOutputSchema:assert result_none.structured_content is Noneassert result_false.structured_content is None- assert result_none.content[0].text == result_false.content[0].text == "123"+ assert result_none.content[0].text == result_false.content[0].text == "123" # type: ignore[attr-defined]async def test_non_object_output_schema_raises_error(self):"""Test that providing a non-object output schema raises a ValueError."""diff --git a/tests/tools/test_tool_transform.py b/tests/tools/test_tool_transform.pyindex ba25d19a4..9a31db81d 100644--- a/tests/tools/test_tool_transform.py+++ b/tests/tools/test_tool_transform.py@@ -131,7 +131,7 @@ async def test_hidden_arg_without_default_uses_parent_default(add_tool):async def test_mixed_hidden_args_with_custom_function(add_tool):"""Test custom function with both hidden constant and hidden default parameters."""- async def custom_fn(visible_x: int) -> int:+ async def custom_fn(visible_x: int) -> ToolResult:# This custom function should receive the transformed visible parameter# and the hidden parameters should be automatically handledresult = await forward(visible_x=visible_x)@@ -195,7 +195,7 @@ async def test_hide_required_param_with_user_default_works():async def test_forward_with_argument_mapping(add_tool):"""Test that forward() applies argument mapping correctly."""- async def custom_fn(new_x: int, new_y: int = 5) -> int:+ async def custom_fn(new_x: int, new_y: int = 5) -> ToolResult:return await forward(new_x=new_x, new_y=new_y)new_tool = Tool.from_tool(@@ -213,7 +213,7 @@ async def test_forward_with_argument_mapping(add_tool):async def test_forward_with_incorrect_args_raises_error(add_tool):- async def custom_fn(new_x: int, new_y: int = 5) -> int:+ async def custom_fn(new_x: int, new_y: int = 5) -> ToolResult:# the forward should use the new args, not the old onesreturn await forward(old_x=new_x, old_y=new_y)@@ -234,7 +234,7 @@ async def test_forward_with_incorrect_args_raises_error(add_tool):async def test_forward_raw_without_argument_mapping(add_tool):"""Test that forward_raw() calls parent directly without mapping."""- async def custom_fn(new_x: int, new_y: int = 5) -> int:+ async def custom_fn(new_x: int, new_y: int = 5) -> ToolResult:# Call parent directly with original argument namesresult = await forward_raw(old_x=new_x, old_y=new_y)return result@@ -254,7 +254,7 @@ async def test_forward_raw_without_argument_mapping(add_tool):async def test_custom_fn_with_kwargs_and_no_transform_args(add_tool):- async def custom_fn(extra: int, **kwargs) -> int:+ async def custom_fn(extra: int, **kwargs) -> ToolResult:sum = await forward(**kwargs)return int(sum.content[0].text) + extra # type: ignore[attr-defined]@@ -271,7 +271,7 @@ async def test_custom_fn_with_kwargs_and_no_transform_args(add_tool):async def test_fn_with_kwargs_passes_through_original_args(add_tool):- async def custom_fn(new_y: int = 5, **kwargs) -> int:+ async def custom_fn(new_y: int = 5, **kwargs) -> ToolResult:assert kwargs == {"old_y": 3}result = await forward(old_x=new_y, **kwargs)return result@@ -285,7 +285,7 @@ async def test_fn_with_kwargs_passes_through_original_args(add_tool):async def test_fn_with_kwargs_receives_transformed_arg_names(add_tool):"""Test that **kwargs receives arguments with their transformed names from transform_args."""- async def custom_fn(new_x: int, **kwargs) -> int:+ async def custom_fn(new_x: int, **kwargs) -> ToolResult:# kwargs should contain 'old_y': 3 (transformed name), not 'old_y': 3 (original name)assert kwargs == {"old_y": 3}result = await forward(new_x=new_x, **kwargs)@@ -304,7 +304,9 @@ async def test_fn_with_kwargs_receives_transformed_arg_names(add_tool):async def test_fn_with_kwargs_handles_partial_explicit_args(add_tool):"""Test that function can explicitly handle some transformed args while others pass through kwargs."""- async def custom_fn(new_x: int, some_other_param: str = "default", **kwargs) -> int:+ async def custom_fn(+ new_x: int, some_other_param: str = "default", **kwargs+ ) -> ToolResult:# x is explicitly handled, y should come through kwargs with transformed nameassert kwargs == {"old_y": 7}result = await forward(new_x=new_x, **kwargs)@@ -325,7 +327,7 @@ async def test_fn_with_kwargs_handles_partial_explicit_args(add_tool):async def test_fn_with_kwargs_mixed_mapped_and_unmapped_args(add_tool):"""Test **kwargs behavior with mix of mapped and unmapped arguments."""- async def custom_fn(new_x: int, **kwargs) -> int:+ async def custom_fn(new_x: int, **kwargs) -> ToolResult:# new_x is explicitly handled, old_y should pass through kwargs with original name (unmapped)assert kwargs == {"old_y": 5}result = await forward(new_x=new_x, **kwargs)@@ -344,7 +346,7 @@ async def test_fn_with_kwargs_mixed_mapped_and_unmapped_args(add_tool):async def test_fn_with_kwargs_dropped_args_not_in_kwargs(add_tool):"""Test that dropped arguments don't appear in **kwargs."""- async def custom_fn(new_x: int, **kwargs) -> int:+ async def custom_fn(new_x: int, **kwargs) -> ToolResult:# 'b' was dropped, so kwargs should be emptyassert kwargs == {}# Can't use 'old_y' since it was dropped, so just use 'old_x' mapped to 'new_x'@@ -486,7 +488,7 @@ async def test_tool_transform_chaining(add_tool):# Transform tool1 with custom function that handles all parametersasync def custom(final_x: int, **kwargs) -> str:result = await forward(final_x=final_x, **kwargs)- return f"custom {result.content[0].text}" # Extract text from content+ return f"custom {result.content[0].text}" # Extract text from content # type: ignore[attr-defined]tool3 = Tool.from_tool(tool1, transform_fn=custom, transform_args={"x": ArgTransform(name="final_x")}@@ -620,7 +622,7 @@ async def test_arg_transform_precedence_over_function_with_kwargs():# Function signature has different types/defaults than ArgTransformasync def custom_fn(x: str = "function_default", **kwargs) -> str:result = await forward(x=x, **kwargs)- return f"custom: {result.content[0].text}"+ return f"custom: {result.content[0].text}" # type: ignore[attr-defined]tool = Tool.from_tool(base,@@ -1168,7 +1170,7 @@ class TestTransformToolOutputSchema:"""Test custom function returning object type."""async def custom_fn(x: int) -> dict[str, int]:- result = await forward(x=x)+ await forward(x=x)return {"original": x, "transformed": x * 2}new_tool = Tool.from_tool(base_string_tool, transform_fn=custom_fn)@@ -1176,7 +1178,7 @@ class TestTransformToolOutputSchema:# Object types should not be wrappedexpected_schema = TypeAdapter(dict[str, int]).json_schema()assert new_tool.output_schema == expected_schema- assert "x-fastmcp-wrap-result" not in new_tool.output_schema+ assert "x-fastmcp-wrap-result" not in new_tool.output_schema # type: ignore[attr-defined]result = await new_tool.run({"x": 4})# Direct value, not wrapped@@ -1189,7 +1191,7 @@ class TestTransformToolOutputSchema:result = await new_tool.run({"x": 7})# Should wrap because parent schema has wrap markerassert result.structured_content == {"result": "Result: 7"}- assert "x-fastmcp-wrap-result" in new_tool.output_schema+ assert "x-fastmcp-wrap-result" in new_tool.output_schema # type: ignore[attr-defined]def test_transform_chained_output_schema_inheritance(self, base_string_tool):"""Test output schema inheritance through multiple transformations."""@@ -1214,10 +1216,10 @@ class TestTransformToolOutputSchema:):"""Test transformation handling of mixed content types."""- async def custom_fn(x: int) -> list:+ async def custom_fn(x: int) -> ToolResult:# Return mixed content including ToolResultif x == 1:- return ["text", {"data": x}]+ return ["text", {"data": x}] # type: ignore[return-value]else:# Return ToolResult directlyreturn ToolResult(@@ -1271,7 +1273,7 @@ class TestTransformToolOutputSchema:"result": "Result: 5"} # Inherits wrappingassert result_false.structured_content is None # Disabled- assert result_none.content[0].text == result_false.content[0].text+ assert result_none.content[0].text == result_false.content[0].text # type: ignore[attr-defined]async def test_transform_output_schema_with_tool_result_return(self, base_string_tool