feat!: implement SEP-2549 cache hints (#889)
implements https://github.com/modelcontextprotocol/rust-sdk/issues/875 Co-authored-by: Jack Amadeo <jackamadeo@squareup.com>
This commit is contained in:
parent
2d4c29fb02
commit
d8331d9442
3 changed files with 403 additions and 7 deletions
79
crates/rmcp/tests/test_cache_hints.rs
Normal file
79
crates/rmcp/tests/test_cache_hints.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
use rmcp::model::{CacheScope, ListToolsResult, ReadResourceResult, ResourceContents};
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn paginated_results_serialize_cache_hints_as_top_level_fields() {
|
||||
let result = ListToolsResult::with_all_items(Vec::new())
|
||||
.with_ttl_ms(5_000)
|
||||
.with_cache_scope(CacheScope::Private);
|
||||
|
||||
let actual = serde_json::to_value(result).expect("serialize list tools result");
|
||||
|
||||
assert_eq!(
|
||||
actual,
|
||||
json!({
|
||||
"ttlMs": 5000,
|
||||
"cacheScope": "private",
|
||||
"tools": [],
|
||||
"resultType": "complete"
|
||||
})
|
||||
);
|
||||
assert!(actual.get("_meta").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_resource_results_serialize_cache_hints_as_top_level_fields() {
|
||||
let result =
|
||||
ReadResourceResult::new(vec![ResourceContents::text("hello", "file:///example.txt")])
|
||||
.with_ttl_ms(10_000)
|
||||
.with_cache_scope(CacheScope::Public);
|
||||
|
||||
let actual = serde_json::to_value(result).expect("serialize read resource result");
|
||||
|
||||
assert_eq!(actual["ttlMs"], 10000);
|
||||
assert_eq!(actual["cacheScope"], "public");
|
||||
assert!(actual["contents"][0].get("_meta").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_hints_are_omitted_when_absent() {
|
||||
let result = ListToolsResult::with_all_items(Vec::new());
|
||||
let actual = serde_json::to_value(result).expect("serialize list tools result");
|
||||
|
||||
assert_eq!(actual, json!({ "tools": [], "resultType": "complete" }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_hints_default_to_none_and_negative_ttl_is_normalized_to_zero() {
|
||||
let absent: ListToolsResult = serde_json::from_value(json!({
|
||||
"tools": []
|
||||
}))
|
||||
.expect("deserialize result without ttlMs");
|
||||
assert_eq!(absent.ttl_ms, None);
|
||||
assert_eq!(absent.cache_scope, None);
|
||||
|
||||
let negative: ReadResourceResult = serde_json::from_value(json!({
|
||||
"ttlMs": -42,
|
||||
"cacheScope": "private",
|
||||
"contents": []
|
||||
}))
|
||||
.expect("deserialize result with negative ttlMs");
|
||||
assert_eq!(negative.ttl_ms, Some(0));
|
||||
assert_eq!(negative.cache_scope, Some(CacheScope::Private));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_scope_round_trips() {
|
||||
assert_eq!(
|
||||
serde_json::to_value(CacheScope::Public).unwrap(),
|
||||
json!("public")
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::to_value(CacheScope::Private).unwrap(),
|
||||
json!("private")
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_value::<CacheScope>(json!("private")).unwrap(),
|
||||
CacheScope::Private
|
||||
);
|
||||
}
|
||||
|
|
@ -150,6 +150,21 @@
|
|||
"format": "const",
|
||||
"const": "boolean"
|
||||
},
|
||||
"CacheScope": {
|
||||
"description": "Scope describing who may cache cacheable list/read results (SEP-2549).\n\nDefaults to [`CacheScope::Public`] when absent from the wire.",
|
||||
"oneOf": [
|
||||
{
|
||||
"description": "Any client or intermediary may cache and serve the response to any user.",
|
||||
"type": "string",
|
||||
"const": "public"
|
||||
},
|
||||
{
|
||||
"description": "Only the requesting user's client may cache the response.",
|
||||
"type": "string",
|
||||
"const": "private"
|
||||
}
|
||||
]
|
||||
},
|
||||
"CallToolResult": {
|
||||
"description": "The result of a tool call operation.\n\nContains the content returned by the tool execution and an optional\nflag indicating whether the operation resulted in an error.",
|
||||
"type": "object",
|
||||
|
|
@ -1515,6 +1530,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1535,6 +1561,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -1551,6 +1586,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1571,6 +1617,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -1587,6 +1642,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1607,6 +1673,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -1655,6 +1730,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1675,6 +1761,15 @@
|
|||
"items": {
|
||||
"$ref": "#/definitions/Tool"
|
||||
}
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -2214,6 +2309,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"contents": {
|
||||
"description": "The actual content of the resource",
|
||||
"type": "array",
|
||||
|
|
@ -2221,7 +2327,7 @@
|
|||
"$ref": "#/definitions/ResourceContents"
|
||||
}
|
||||
},
|
||||
"result_type": {
|
||||
"resultType": {
|
||||
"description": "Result type discriminator. Absent values deserialize as `\"complete\"`.",
|
||||
"allOf": [
|
||||
{
|
||||
|
|
@ -2229,6 +2335,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
|
|||
|
|
@ -150,6 +150,21 @@
|
|||
"format": "const",
|
||||
"const": "boolean"
|
||||
},
|
||||
"CacheScope": {
|
||||
"description": "Scope describing who may cache cacheable list/read results (SEP-2549).\n\nDefaults to [`CacheScope::Public`] when absent from the wire.",
|
||||
"oneOf": [
|
||||
{
|
||||
"description": "Any client or intermediary may cache and serve the response to any user.",
|
||||
"type": "string",
|
||||
"const": "public"
|
||||
},
|
||||
{
|
||||
"description": "Only the requesting user's client may cache the response.",
|
||||
"type": "string",
|
||||
"const": "private"
|
||||
}
|
||||
]
|
||||
},
|
||||
"CallToolResult": {
|
||||
"description": "The result of a tool call operation.\n\nContains the content returned by the tool execution and an optional\nflag indicating whether the operation resulted in an error.",
|
||||
"type": "object",
|
||||
|
|
@ -1515,6 +1530,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1535,6 +1561,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -1551,6 +1586,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1571,6 +1617,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -1587,6 +1642,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1607,6 +1673,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -1655,6 +1730,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextCursor": {
|
||||
"type": [
|
||||
"string",
|
||||
|
|
@ -1675,6 +1761,15 @@
|
|||
"items": {
|
||||
"$ref": "#/definitions/Tool"
|
||||
}
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
@ -2214,6 +2309,17 @@
|
|||
],
|
||||
"additionalProperties": true
|
||||
},
|
||||
"cacheScope": {
|
||||
"description": "Scope describing who may cache this result (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/CacheScope"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"contents": {
|
||||
"description": "The actual content of the resource",
|
||||
"type": "array",
|
||||
|
|
@ -2221,7 +2327,7 @@
|
|||
"$ref": "#/definitions/ResourceContents"
|
||||
}
|
||||
},
|
||||
"result_type": {
|
||||
"resultType": {
|
||||
"description": "Result type discriminator. Absent values deserialize as `\"complete\"`.",
|
||||
"allOf": [
|
||||
{
|
||||
|
|
@ -2229,6 +2335,15 @@
|
|||
}
|
||||
],
|
||||
"default": "complete"
|
||||
},
|
||||
"ttlMs": {
|
||||
"description": "Time, in milliseconds, that this result may be treated as fresh (SEP-2549).\nRequired by spec version 2026-07-28, but optional here to maintain compatibility\nwith older spec versions.",
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint64",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
|
|
|||
Loading…
Reference in a new issue