Skip to content

fix(core): read a negative ttlMs as zero on every cacheable result - #1193

Open
e5l wants to merge 3 commits into
devcrocod/sep-2575-stateless-mcpfrom
fix/sep2575-negative-ttl-decode
Open

e5l wants to merge 3 commits into
devcrocod/sep-2575-stateless-mcpfrom
fix/sep2575-negative-ttl-decode

Conversation

@e5l

@e5l e5l commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Why

Goal: turn the confirmed review findings on #985 into atomic fixes. Follow-up to #985.

A server that sends "ttlMs": -1 on any of the six cacheable results makes the client fail to decode the whole result. Each result's non-negative init check also runs on decode. The caching spec says: "If ttlMs is negative, clients SHOULD ignore it and treat it as 0."

What changed

  • TtlMsSerializer in serializers.kt reads a negative ttlMs as 0 and writes the value unchanged.
  • ttlMs on DiscoverResult, ListToolsResult, ListPromptsResult, ListResourcesResult, ListResourceTemplatesResult and ReadResourceResult uses it through @Serializable(with = TtlMsSerializer::class).
  • The init checks stay, so constructing any of these results with a negative ttlMs still throws IllegalArgumentException.

Scope

#1188 fixes DiscoverResult on main with the same serializer. The serializers.kt hunk is byte-identical in both PRs, and the discovery.kt change is the same one annotation line, so rebasing this branch onto main resolves to the same code.

Blast Radius

Inbound decoding of the six cacheable results only. Encoding is unchanged. No public API change, so checkKotlinAbi passes with no dump update.

Verification

  • New test CacheableResultTest.a negative freshness window from the wire should read as zero failed before the fix with JsonDecodingException: Deserialization failed because of 'ttlMs must be non-negative, but was -1', and passes after it.
  • The existing a negative freshness window should be refused at construction still passes.
  • Local: ./gradlew :kotlin-sdk-core:jvmTest ktlintCheck detekt checkKotlinAbi passes.
  • CI (dispatched): https://github.com/modelcontextprotocol/kotlin-sdk/actions/runs/44269067457

🤖 Generated with Claude Code

e5l and others added 3 commits November 18, 2026 09:33
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The spec asks clients to ignore a negative ttlMs and treat it as 0, but
each cacheable result's init check also ran on decode and dropped the whole
result. The six results now read ttlMs through TtlMsSerializer, the same
mechanism as the main-branch fix for DiscoverResult; constructing a result
with a negative ttlMs still fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@e5l
e5l requested a review from devcrocod November 20, 2026 09:57

@devcrocod devcrocod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check other ttlMs PRs for using init blocks instead of serializers

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants