-
Notifications
You must be signed in to change notification settings - Fork 712
Add SEP-2549 caching hints (ttlMs and cacheScope) to cacheable results #1623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tarekgh
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
tarekgh:sep-2549-ttl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,133
−124
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ModelContextProtocol.Protocol; | ||
|
|
||
| /// <summary> | ||
| /// Indicates the intended scope of a cached response, analogous to the HTTP | ||
| /// <c>Cache-Control: public</c> and <c>Cache-Control: private</c> directives. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This is used by <see cref="ICacheableResult.CacheScope"/> to control who may cache a | ||
| /// response returned by <c>tools/list</c>, <c>prompts/list</c>, <c>resources/list</c>, | ||
| /// <c>resources/templates/list</c>, and <c>resources/read</c>. | ||
| /// </para> | ||
| /// <para> | ||
| /// When the field is absent from a response, clients should treat it as <see cref="Public"/>. | ||
| /// </para> | ||
| /// </remarks> | ||
| [JsonConverter(typeof(JsonStringEnumConverter<CacheScope>))] | ||
| public enum CacheScope | ||
| { | ||
| /// <summary> | ||
| /// The response does not contain user-specific data. Any client, shared gateway, or caching | ||
| /// proxy may store and serve the cached response to any user. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is appropriate for lists of tools, prompts, and resource templates that are identical | ||
| /// for all users. | ||
| /// </remarks> | ||
| [JsonStringEnumMemberName("public")] | ||
| Public, | ||
|
|
||
| /// <summary> | ||
| /// The response contains user-specific data. Only the requesting user's client may cache it. | ||
| /// Shared caches (for example, multi-tenant gateways) must not serve the cached response to a | ||
| /// different user. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is appropriate for <c>resources/read</c> results that depend on the authenticated user, | ||
| /// or for filtered list results that vary per user. | ||
| /// </remarks> | ||
| [JsonStringEnumMemberName("private")] | ||
| Private | ||
| } |
70 changes: 70 additions & 0 deletions
70
src/ModelContextProtocol.Core/Protocol/CacheScopeConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ModelContextProtocol.Protocol; | ||
|
|
||
| /// <summary> | ||
| /// Serializes <see cref="CacheScope"/> caching-scope hints, tolerating unknown or future values on read. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// SEP-2549 introduces <c>cacheScope</c> as a forward-looking caching hint. If a server sends an | ||
| /// unrecognized scope string (for example, a value added in a later revision of the specification) or a | ||
| /// non-string token, this converter maps it to <see langword="null"/> rather than throwing. This prevents | ||
| /// a single unexpected hint from breaking deserialization of the entire result (for example, the whole | ||
| /// tool list). A <see langword="null"/> result is the same as an absent field, which clients treat as | ||
| /// <see cref="CacheScope.Public"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// This converter is applied per-property on the cacheable result types. The <see cref="CacheScope"/> | ||
| /// enum itself retains a standard string converter for any standalone serialization. | ||
| /// </para> | ||
| /// </remarks> | ||
| internal sealed class CacheScopeConverter : JsonConverter<CacheScope?> | ||
| { | ||
| public override CacheScope? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (reader.TokenType is JsonTokenType.String) | ||
| { | ||
| string? value = reader.GetString(); | ||
|
|
||
| // Match case-insensitively so a non-conforming casing of "private" (a security-relevant hint) | ||
| // is honored rather than falling through to null, which clients would treat as "public" and | ||
| // could cache user-specific data in a shared cache. Genuinely unknown values still map to null. | ||
| if (string.Equals(value, "public", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return CacheScope.Public; | ||
| } | ||
|
|
||
| if (string.Equals(value, "private", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return CacheScope.Private; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| // Any non-string token (number, bool, object, array) is an unrecognized hint. Consume the whole | ||
| // value, including the contents of an object or array, so the reader is left correctly positioned | ||
| // before mapping to null. Skipping is required for container tokens: returning without consuming | ||
| // them would leave the reader mispositioned and break deserialization of the enclosing result. | ||
| reader.Skip(); | ||
| return null; | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, CacheScope? value, JsonSerializerOptions options) | ||
| { | ||
| if (value is null) | ||
| { | ||
| writer.WriteNullValue(); | ||
| return; | ||
| } | ||
|
|
||
| writer.WriteStringValue(value switch | ||
| { | ||
| CacheScope.Public => "public", | ||
| CacheScope.Private => "private", | ||
| _ => throw new JsonException($"Unsupported {nameof(CacheScope)} value: {value}."), | ||
| }); | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
src/ModelContextProtocol.Core/Protocol/ICacheableResult.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| namespace ModelContextProtocol.Protocol; | ||
|
|
||
| /// <summary> | ||
| /// Represents a result that carries time-to-live (TTL) caching hints, allowing clients to cache | ||
| /// the response for a period of time before re-fetching. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This interface corresponds to the <c>CacheableResult</c> type in the Model Context Protocol | ||
| /// schema and is implemented by the results of <c>tools/list</c>, <c>prompts/list</c>, | ||
| /// <c>resources/list</c>, <c>resources/templates/list</c>, and <c>resources/read</c>. | ||
| /// </para> | ||
| /// <para> | ||
| /// The TTL is a freshness hint, not a guarantee. It supplements rather than replaces the existing | ||
| /// <c>list_changed</c> and <c>resources/updated</c> notification mechanisms; both can coexist. A | ||
| /// relevant notification invalidates a cached response regardless of any remaining TTL. | ||
| /// </para> | ||
| /// </remarks> | ||
| public interface ICacheableResult | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets a hint indicating how long the client may cache this response before re-fetching. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The semantics are analogous to the HTTP <c>Cache-Control: max-age</c> directive. The value is | ||
| /// serialized as an integer number of milliseconds under the <c>ttlMs</c> JSON property. | ||
| /// </para> | ||
| /// <para> | ||
| /// A value of <see cref="TimeSpan.Zero"/> indicates the response should be considered immediately | ||
| /// stale; a positive value indicates the client should consider the response fresh for that | ||
| /// duration from the time it was received. | ||
| /// </para> | ||
| /// <para> | ||
| /// When this property is <see langword="null"/> (the field was absent from the response), clients | ||
| /// should assume a default of <see cref="TimeSpan.Zero"/> (immediately stale) and rely on their | ||
| /// own caching heuristics or notifications. A negative value should likewise be treated as | ||
| /// <see cref="TimeSpan.Zero"/>. | ||
| /// </para> | ||
| /// </remarks> | ||
| TimeSpan? TimeToLive { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the intended scope of the cached response. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// When this property is <see langword="null"/> (the field was absent from the response), clients | ||
| /// should treat the response as <see cref="Protocol.CacheScope.Public"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// An unrecognized or future scope value sent by a server (or a non-string value) is tolerated and | ||
| /// surfaced as <see langword="null"/> rather than causing deserialization of the whole result to | ||
| /// fail, so a single unexpected hint never prevents a client from reading the result. | ||
| /// </para> | ||
| /// </remarks> | ||
| CacheScope? CacheScope { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.