Skip to content

Commit 62f286f

Browse files
timof1308timo-fischer
authored andcommitted
feat: Introduce state_header_strict config for strict state header type validation and add a test.
1 parent 1dcf383 commit 62f286f

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/google/adk/tools/mcp_tool/mcp_toolset.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ def from_config(
394394
header_name=header_name,
395395
header_format=state_format.get(header_name, "{value}"),
396396
default_value=None,
397+
strict=mcp_toolset_config.state_header_strict,
397398
)
398399
for state_key, header_name in state_mapping.items()
399400
]
@@ -477,6 +478,21 @@ class McpToolsetConfig(BaseToolConfig):
477478
used as-is.
478479
"""
479480

481+
state_header_strict: bool = False
482+
"""Enable strict type validation for state header values.
483+
484+
When True, raises ValueError if state values are non-primitive types
485+
(not str, int, float, or bool). This helps catch configuration errors
486+
early by preventing accidental serialization of complex objects into headers.
487+
488+
When False (default), non-primitive types trigger a warning but are still
489+
formatted into headers.
490+
491+
Example::
492+
493+
state_header_strict: true # Raises error on non-primitive types
494+
"""
495+
480496
@model_validator(mode="after")
481497
def _check_only_one_params_field(self):
482498
param_fields = [

tests/unittests/tools/mcp_tool/test_jwt_token_propagation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,27 @@ def test_from_config_no_state_mapping_no_provider(self):
318318

319319
# No header provider should be created
320320
assert toolset._header_provider is None
321+
322+
def test_from_config_with_strict_mode(self):
323+
"""Test that from_config respects state_header_strict setting."""
324+
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
325+
from google.adk.tools.tool_configs import ToolArgsConfig
326+
327+
config = ToolArgsConfig(
328+
stdio_server_params={"command": "test_command", "args": []},
329+
state_header_mapping={"data": "X-Data"},
330+
state_header_strict=True, # Enable strict mode
331+
)
332+
333+
toolset = McpToolset.from_config(config, "/fake/path")
334+
335+
# Test with non-primitive type - should raise ValueError
336+
mock_context = Mock(spec=ReadonlyContext)
337+
mock_context.state = {"data": {"nested": "object"}}
338+
339+
with pytest.raises(ValueError) as exc_info:
340+
toolset._header_provider(mock_context)
341+
342+
assert "data" in str(exc_info.value)
343+
assert "dict" in str(exc_info.value)
344+

0 commit comments

Comments
 (0)