Skip to content

The agent that grows with you

fix(mcp): read readOnlyHint from mcp 2.x snake_case annotations

Sergii Osadchyi35332931bf02
diff --git a/tests/tools/test_mcp_trust_gating.py b/tests/tools/test_mcp_trust_gating.py
index 68c1260bce5..26161ed9814 100644
--- a/tests/tools/test_mcp_trust_gating.py
+++ b/tests/tools/test_mcp_trust_gating.py
@@ -358,3 +358,23 @@ class TestAnnotationCaptureAtDiscovery:
assert _mcp_registration._annotation_read_only_hint(
SimpleNamespace()
) is False
+
+ def test_sdk2_snake_case_annotations_supported(self):
+ """mcp 2.x models expose ``read_only_hint``; camelCase is only a serialization alias."""
+ from mcp.types import ToolAnnotations
+ sdk2 = ToolAnnotations.model_validate({"readOnlyHint": True, "destructiveHint": False})
+ assert _mcp_registration._annotation_read_only_hint(SimpleNamespace(annotations=sdk2)) is True
+ assert _mcp_registration._annotation_read_only_hint(
+ SimpleNamespace(annotations=SimpleNamespace(read_only_hint=True))
+ ) is True
+ assert _mcp_registration._annotation_read_only_hint(
+ SimpleNamespace(annotations=SimpleNamespace(read_only_hint=False))
+ ) is False
+ # 1.x-shaped object (camelCase attribute) still works.
+ assert _mcp_registration._annotation_read_only_hint(
+ SimpleNamespace(annotations=SimpleNamespace(readOnlyHint=True))
+ ) is True
+ # Cache dict written by an older client in snake_case is honoured too.
+ assert _mcp_registration._annotation_read_only_hint(
+ SimpleNamespace(annotations={"read_only_hint": True})
+ ) is True
diff --git a/tools/mcp_tool_registration.py b/tools/mcp_tool_registration.py
index 1875e38f91e..6e2bfd99747 100644
--- a/tools/mcp_tool_registration.py
+++ b/tools/mcp_tool_registration.py
@@ -47,9 +47,18 @@ def _normalize_server_trust(value: Any) -> str:
def _annotation_read_only_hint(mcp_tool: Any) -> bool:
- """True only when annotations (SDK object or cache dict) carry ``readOnlyHint is True``; unknown = write-capable."""
+ """True only when annotations (SDK object or cache dict) carry ``readOnlyHint is True``; unknown = write-capable.
+
+ mcp 2.0 renamed ``ToolAnnotations.readOnlyHint`` to ``read_only_hint`` (camelCase survives only as a
+ serialization alias, which pydantic does not apply to attribute access), so a bare camelCase getattr
+ returns None on 2.x and every honestly-annotated read tool fails closed as write-capable — the
+ untrusted gate then asks before reads too, training the user to click through it. Read both names.
+ """
annotations = getattr(mcp_tool, "annotations", None)
- hint = annotations.get("readOnlyHint") if isinstance(annotations, dict) else getattr(annotations, "readOnlyHint", None)
+ if isinstance(annotations, dict):
+ hint = annotations.get("readOnlyHint", annotations.get("read_only_hint"))
+ else:
+ hint = mcp_field(annotations, "read_only_hint", "readOnlyHint") if annotations is not None else None
return hint is True