Skip to content

Commit 4b078f2

Browse files
committed
PR feedback - mpangrazzi
1 parent fa89a10 commit 4b078f2

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@
2929
logger = logging.getLogger(__name__)
3030

3131

32-
def _serialize_state_config(config: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]] | None:
32+
def _serialize_state_config(config: dict[str, dict[str, Any]] | None) -> dict[str, dict[str, Any]] | None:
3333
"""
3434
Serialize a state configuration dictionary, converting any callable handlers to their string representation.
3535
3636
Works for both outputs_to_state (tool_name -> {state_key -> {source, handler}})
3737
and outputs_to_string (tool_name -> {source, handler}).
3838
39+
Note: The keys "source" and "handler" are reserved and used internally to distinguish between
40+
outputs_to_string format and outputs_to_state format. Do not use these as state keys in
41+
outputs_to_state configurations.
42+
3943
:param config: The state configuration dictionary to serialize
4044
:returns: The serialized configuration dictionary, or None if empty
4145
"""

integrations/mcp/tests/test_mcp_toolset.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ async def test_toolset_with_state_config(self, calculator_toolset_with_state_con
292292
assert add_tool.outputs_to_string is not None
293293
assert subtract_tool.outputs_to_string is None
294294

295-
async def test_toolset_state_config_serde(self, calculator_toolset_with_state_config):
295+
async def test_toolset_state_config_serde(self, calculator_toolset_with_state_config, mcp_tool_cleanup):
296296
"""Test serialization and deserialization of MCPToolset with state configuration."""
297297
toolset = calculator_toolset_with_state_config
298298

@@ -308,20 +308,19 @@ async def test_toolset_state_config_serde(self, calculator_toolset_with_state_co
308308
# Handler should be serialized as a string
309309
assert isinstance(toolset_dict["data"]["outputs_to_string"]["add"]["handler"], str)
310310

311-
# Test deserialization
312-
with patch("haystack_integrations.tools.mcp.mcp_toolset.MCPToolset.__init__", return_value=None) as mock_init:
313-
MCPToolset.from_dict(toolset_dict)
311+
# Test deserialization with full roundtrip
312+
new_toolset = MCPToolset.from_dict(toolset_dict)
313+
mcp_tool_cleanup(new_toolset)
314314

315-
mock_init.assert_called_once()
316-
_, kwargs = mock_init.call_args
317-
assert kwargs["inputs_from_state"] == {
318-
"add": {"first_number": "a"},
319-
"subtract": {"first_number": "a", "second_number": "b"},
320-
}
321-
assert "add" in kwargs["outputs_to_state"]
322-
assert "add" in kwargs["outputs_to_string"]
323-
# Handler should be deserialized back to a callable
324-
assert callable(kwargs["outputs_to_string"]["add"]["handler"])
315+
# Verify state configs are correctly deserialized
316+
assert new_toolset.inputs_from_state == {
317+
"add": {"first_number": "a"},
318+
"subtract": {"first_number": "a", "second_number": "b"},
319+
}
320+
assert "add" in new_toolset.outputs_to_state
321+
assert "add" in new_toolset.outputs_to_string
322+
# Handler should be deserialized back to a callable
323+
assert callable(new_toolset.outputs_to_string["add"]["handler"])
325324

326325
async def test_toolset_state_config_unknown_tool_warning(self, caplog):
327326
"""Test that a warning is logged when state config references unknown tools.

0 commit comments

Comments
 (0)