Cache TTL normalization changes non-cacheable extension retention values before validation

未關閉
#3,866 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

  • #3913 來自 @EnesKartal —— 已關閉,未合併
  • #3928 來自 @EnesKartal —— 已關閉,未合併

評估

難度
3/5
預估耗時
1-2 天
新手友好度
58/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python

研究方向

Start at ClientSession.send_request() and inspect where _clamp_inbound_ttl(raw) runs before result parsing. Check how CACHEABLE_METHODS distinguishes core cacheable results and how the native Skills extension handles cache hints. Add regressions for non-cacheable extension results preserving values for validation, while confirming existing cache normalization still works.

由索引模型根據 Issue 內容生成。

描述

bug spec-2026-07-28 v2

ClientSession.send_request() applies _clamp_inbound_ttl(raw) to every result before parsing it. A ClientExtension claiming a non-cacheable tools/call result therefore cannot preserve its own ttlMs semantics.

Reproduced through public Client(MCPServer(...)), ClientExtension, and ResultClaim, on published 2.3.0 (Python 3.14) and current main 77001c96f9507be0f4d009b83210a384a5e08bda (Python 3.13). Each row makes exactly one tools/call:

Server value Expected extension validation Actual parsed value
-1 Preserve -1 0
-1.5 Reject fractional retention 0, accepted
-9007199254740992 Reject outside signed safe-integer range 0, accepted

This blocks Kestrel's implementation of the published Tasks extension: its retention schema is different from core cache freshness. The SDK's own CACHEABLE_METHODS excludes tools/call and extension task verbs. Ordinary extension requests using session.send_request also pass through the unconditional normalization. Declaring the extension's validation more strictly cannot recover values already changed by the SDK.

Expected: normalize cache hints only for cacheable results; leave other extension values intact for their own models to validate. Existing core cache normalization (including resources/read) and the native Skills cacheable extension must keep working. We can contribute a narrow correction and native regressions if useful; no new Tasks API is requested here.

Complete in-memory reproducer
from collections.abc import Sequence
from typing import Annotated, Any, Literal
import importlib.metadata
import json
import anyio
import mcp_types as types
from pydantic import Field, ValidationError
from mcp import Client
from mcp.client import ClaimContext, ClientExtension, ResultClaim
from mcp.server.context import ServerRequestContext
from mcp.server.context import CallNext, HandlerResult
from mcp.server.extension import Extension
from mcp.server.mcpserver import MCPServer

class RetentionResult(types.Result):
    result_type: Literal["task"] = "task"
    ttl_ms: Annotated[int, Field(strict=True, ge=-9007199254740991, le=9007199254740991)] | None

async def unresolved(result: RetentionResult, ctx: ClaimContext) -> types.CallToolResult:
    raise NotImplementedError

class RetentionClient(ClientExtension):
    identifier = "com.example/retention"
    def claims(self) -> Sequence[ResultClaim[Any]]:
        return [ResultClaim(result_type="task", model=RetentionResult, resolve=unresolved)]

class Issuer(Extension):
    identifier = "com.example/retention"
    def __init__(self, value: int | float) -> None:
        self.value = value
        self.calls = 0
    async def intercept_tool_call(self, params: types.CallToolRequestParams, ctx: ServerRequestContext[Any, Any], call_next: CallNext) -> HandlerResult:
        assert params.name == "issue"
        self.calls += 1
        return {"resultType": "task", "ttlMs": self.value}

async def main() -> None:
    rows = []
    for value in [-1, -1.5, -9007199254740992]:
        issuer = Issuer(value)
        server = MCPServer("retention", extensions=[issuer])
        @server.tool()
        def issue() -> types.CallToolResult:
            raise NotImplementedError
        with anyio.fail_after(5):
            async with Client(server, extensions=[RetentionClient()]) as client:
                try:
                    result = await client.session.call_tool("issue", allow_claimed=True)
                except ValidationError:
                    observed: int | None | str = "ValidationError"
                else:
                    assert isinstance(result, RetentionResult)
                    observed = result.ttl_ms
        assert issuer.calls == 1
        rows.append({"wire_ttl": value, "native_observed": observed, "tools_call_count": issuer.calls})
    print(json.dumps({"sdk": importlib.metadata.version("mcp"), "rows": rows}))

if __name__ == "__main__":
    anyio.run(main)

AI disclosure: Codex prepared this report and reproducer and ran the reproductions, under explicit authorization from Kestrel's human maintainer. No response rewriting or installed SDK modification was used.

主要語言
Python
星號
25k
分支
4.2k
平均合併
10 分鐘
30 天內合併 PR
1

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

modelcontextprotocol/python-sdk 的其他 Issue

查看 modelcontextprotocol/python-sdk 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。