From ae87bb179c072bcf056ac411857c71dd80aaf300 Mon Sep 17 00:00:00 2001 From: EnesKartal Date: Wed, 25 Nov 2026 00:32:28 +0300 Subject: [PATCH] fix(client): clamp inbound ttlMs only for cacheable methods --- src/mcp/client/session.py | 3 +- tests/client/test_session_claims.py | 44 +++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/mcp/client/session.py b/src/mcp/client/session.py index 35cae2d314..028eac1b64 100644 --- a/src/mcp/client/session.py +++ b/src/mcp/client/session.py @@ -584,7 +584,8 @@ async def send_request( if metadata.on_resumption_token_update is not None: opts["on_resumption_token"] = metadata.on_resumption_token_update raw = await self._dispatcher.send_raw_request(method, data.get("params"), opts) - _clamp_inbound_ttl(raw) + if method in _methods.CACHEABLE_METHODS: + _clamp_inbound_ttl(raw) # Literal fallback covers pre-handshake and stateless; matches runner.py. version = self._negotiated_version or "2025-11-25" try: diff --git a/tests/client/test_session_claims.py b/tests/client/test_session_claims.py index e6495aa31e..e27db6684b 100644 --- a/tests/client/test_session_claims.py +++ b/tests/client/test_session_claims.py @@ -3,7 +3,7 @@ `allow_claimed` escape hatch.""" from collections.abc import Mapping -from typing import Any, Literal, assert_type +from typing import Annotated, Any, Literal, assert_type import anyio import anyio.abc @@ -23,7 +23,7 @@ ) from mcp_types.methods import validate_server_result from mcp_types.version import LATEST_HANDSHAKE_VERSION, LATEST_MODERN_VERSION -from pydantic import ValidationError +from pydantic import Field, ValidationError from mcp.client.extension import ClaimContext, ResultClaim, UnexpectedClaimedResult from mcp.client.session import ClientSession, _CallToolResultAdapter @@ -465,3 +465,43 @@ def test_claimed_raw_passes_v2026_tools_call_surface_validation() -> None: """Pins the claim path's dependency: an unknown resultType passes `validate_server_result` at 2026-07-28; this failing is the signal that mcp-types tightened the surface.""" validate_server_result("tools/call", LATEST_MODERN_VERSION, {"resultType": "task", "taskId": "t-1"}) + + +class _RetentionResult(types.Result): + result_type: Literal["task"] = "task" + ttl_ms: Annotated[int, Field(strict=True, ge=-9007199254740991, le=9007199254740991)] | None + + +@pytest.mark.anyio +async def test_non_cacheable_claimed_result_preserves_negative_and_fractional_ttl_ms() -> None: + """Only cacheable methods normalize ttlMs; extension results keep raw ttlMs for their own models.""" + + async def unresolved(result: _RetentionResult, ctx: ClaimContext) -> CallToolResult: + raise NotImplementedError + + claim = ResultClaim(result_type="task", model=_RetentionResult, resolve=unresolved) + payloads: list[dict[str, Any]] = [ + {"resultType": "task", "ttlMs": -1}, + {"resultType": "task", "ttlMs": -1.5}, + ] + + class _ReplayDispatcher(_RecordingDispatcher): + async def send_raw_request( + self, + method: str, + params: Mapping[str, Any] | None = None, + opts: CallOptions | None = None, + ) -> dict[str, Any]: + await super().send_raw_request(method, params, opts) + return payloads.pop(0) + + session = _claims_session(_ReplayDispatcher(), claim) + with anyio.fail_after(5): + async with session: + _adopt_modern(session) + preserved = await session.call_tool("issue", {}, allow_claimed=True) + assert isinstance(preserved, _RetentionResult) + assert preserved.ttl_ms == -1 + with pytest.raises(ValidationError): + await session.call_tool("issue", {}, allow_claimed=True) +