-
Notifications
You must be signed in to change notification settings - Fork 548
feat: add SEP-2575 meta helpers #942
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
howardjohn
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
howardjohn:john/sep-2575-meta-helpers
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.
+145
−2
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,8 +4,9 @@ use serde::{Deserialize, Serialize}; | |||||
| use serde_json::Value; | ||||||
|
|
||||||
| use super::{ | ||||||
| ClientNotification, ClientRequest, CustomNotification, CustomRequest, Extensions, JsonObject, | ||||||
| JsonRpcMessage, NumberOrString, ProgressToken, ServerNotification, ServerRequest, TaskMetadata, | ||||||
| ClientCapabilities, ClientNotification, ClientRequest, CustomNotification, CustomRequest, | ||||||
| Extensions, Implementation, JsonObject, JsonRpcMessage, LoggingLevel, NumberOrString, | ||||||
| ProgressToken, ProtocolVersion, ServerNotification, ServerRequest, TaskMetadata, | ||||||
| }; | ||||||
|
|
||||||
| pub trait GetMeta { | ||||||
|
|
@@ -200,6 +201,16 @@ variant_extension! { | |||||
| #[expect(clippy::exhaustive_structs, reason = "intentionally exhaustive")] | ||||||
| pub struct Meta(pub JsonObject); | ||||||
| const PROGRESS_TOKEN_FIELD: &str = "progressToken"; | ||||||
|
|
||||||
| /// `_meta` key carrying the MCP protocol version for this request. | ||||||
| pub const META_KEY_PROTOCOL_VERSION: &str = "io.modelcontextprotocol/protocolVersion"; | ||||||
| /// `_meta` key carrying the client implementation identity for this request. | ||||||
| pub const META_KEY_CLIENT_INFO: &str = "io.modelcontextprotocol/clientInfo"; | ||||||
| /// `_meta` key carrying the client capabilities for this request. | ||||||
| pub const META_KEY_CLIENT_CAPABILITIES: &str = "io.modelcontextprotocol/clientCapabilities"; | ||||||
| /// `_meta` key carrying the requested per-request log level. | ||||||
| pub const META_KEY_LOG_LEVEL: &str = "io.modelcontextprotocol/logLevel"; | ||||||
|
|
||||||
| impl Meta { | ||||||
| pub fn new() -> Self { | ||||||
| Self(JsonObject::new()) | ||||||
|
|
@@ -249,11 +260,72 @@ impl Meta { | |||||
| }; | ||||||
| } | ||||||
|
|
||||||
| /// Get the MCP protocol version carried in `_meta`, if present and valid. | ||||||
| pub fn protocol_version(&self) -> Option<ProtocolVersion> { | ||||||
| self.decode_value(META_KEY_PROTOCOL_VERSION) | ||||||
| } | ||||||
|
|
||||||
| /// Set the MCP protocol version carried in `_meta`. | ||||||
| pub fn set_protocol_version(&mut self, protocol_version: ProtocolVersion) { | ||||||
| self.0.insert( | ||||||
| META_KEY_PROTOCOL_VERSION.to_string(), | ||||||
| Value::String(protocol_version.to_string()), | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /// Get the client implementation identity carried in `_meta`, if present and valid. | ||||||
| pub fn client_info(&self) -> Option<Implementation> { | ||||||
| self.decode_value(META_KEY_CLIENT_INFO) | ||||||
| } | ||||||
|
|
||||||
| /// Set the client implementation identity carried in `_meta`. | ||||||
| pub fn set_client_info(&mut self, client_info: Implementation) { | ||||||
| self.insert_serialized(META_KEY_CLIENT_INFO, client_info); | ||||||
| } | ||||||
|
|
||||||
| /// Get the client capabilities carried in `_meta`, if present and valid. | ||||||
| pub fn client_capabilities(&self) -> Option<ClientCapabilities> { | ||||||
| self.decode_value(META_KEY_CLIENT_CAPABILITIES) | ||||||
| } | ||||||
|
|
||||||
| /// Set the client capabilities carried in `_meta`. | ||||||
| pub fn set_client_capabilities(&mut self, client_capabilities: ClientCapabilities) { | ||||||
| self.insert_serialized(META_KEY_CLIENT_CAPABILITIES, client_capabilities); | ||||||
| } | ||||||
|
|
||||||
| /// Get the requested per-request log level carried in `_meta`, if present and valid. | ||||||
| pub fn log_level(&self) -> Option<LoggingLevel> { | ||||||
| self.decode_value(META_KEY_LOG_LEVEL) | ||||||
| } | ||||||
|
|
||||||
| /// Set the requested per-request log level carried in `_meta`. | ||||||
| pub fn set_log_level(&mut self, log_level: LoggingLevel) { | ||||||
| self.insert_serialized(META_KEY_LOG_LEVEL, log_level); | ||||||
| } | ||||||
|
|
||||||
| pub fn extend(&mut self, other: Meta) { | ||||||
| for (k, v) in other.0.into_iter() { | ||||||
| self.0.insert(k, v); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn decode_value<T>(&self, key: &str) -> Option<T> | ||||||
| where | ||||||
| T: for<'de> Deserialize<'de>, | ||||||
| { | ||||||
| self.0 | ||||||
| .get(key) | ||||||
| .and_then(|value| serde_json::from_value(value.clone()).ok()) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Serde can deserialize directly from
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| fn insert_serialized<T>(&mut self, key: &str, value: T) | ||||||
| where | ||||||
| T: Serialize, | ||||||
| { | ||||||
| let value = serde_json::to_value(value) | ||||||
| .expect("MCP meta helper value should serialize to valid JSON"); | ||||||
| self.0.insert(key.to_string(), value); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl Deref for Meta { | ||||||
|
|
||||||
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,71 @@ | ||
| #![allow(deprecated)] | ||
|
|
||
| use rmcp::model::{ | ||
| ClientCapabilities, Implementation, LoggingLevel, META_KEY_CLIENT_CAPABILITIES, | ||
| META_KEY_CLIENT_INFO, META_KEY_LOG_LEVEL, META_KEY_PROTOCOL_VERSION, Meta, ProtocolVersion, | ||
| }; | ||
| use serde_json::json; | ||
|
|
||
| #[test] | ||
| fn meta_setters_store_sep_2575_values() { | ||
| let mut meta = Meta::new(); | ||
| meta.set_protocol_version(ProtocolVersion::V_2026_07_28); | ||
| meta.set_client_info(Implementation::new("test-client", "1.0.0")); | ||
| meta.set_client_capabilities(ClientCapabilities::default()); | ||
| meta.set_log_level(LoggingLevel::Warning); | ||
|
|
||
| assert_eq!( | ||
| meta.get(META_KEY_PROTOCOL_VERSION), | ||
| Some(&json!("2026-07-28")) | ||
| ); | ||
| assert_eq!( | ||
| meta.get(META_KEY_CLIENT_INFO), | ||
| Some(&json!({ "name": "test-client", "version": "1.0.0" })) | ||
| ); | ||
| assert_eq!(meta.get(META_KEY_CLIENT_CAPABILITIES), Some(&json!({}))); | ||
| assert_eq!(meta.get(META_KEY_LOG_LEVEL), Some(&json!("warning"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn meta_accessors_decode_wire_values() { | ||
| let meta: Meta = serde_json::from_value(json!({ | ||
| "progressToken": "progress-1", | ||
| "io.modelcontextprotocol/protocolVersion": "2026-07-28", | ||
| "io.modelcontextprotocol/clientInfo": { | ||
| "name": "wire-client", | ||
| "version": "9.0.0" | ||
| }, | ||
| "io.modelcontextprotocol/clientCapabilities": { | ||
| "sampling": {} | ||
| }, | ||
| "io.modelcontextprotocol/logLevel": "error" | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(meta.protocol_version(), Some(ProtocolVersion::V_2026_07_28)); | ||
| assert_eq!( | ||
| meta.client_info(), | ||
| Some(Implementation::new("wire-client", "9.0.0")) | ||
| ); | ||
| assert!( | ||
| meta.client_capabilities() | ||
| .is_some_and(|capabilities| capabilities.sampling.is_some()) | ||
| ); | ||
| assert_eq!(meta.log_level(), Some(LoggingLevel::Error)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn meta_accessors_ignore_missing_or_malformed_values() { | ||
| let meta: Meta = serde_json::from_value(json!({ | ||
| "io.modelcontextprotocol/protocolVersion": 20260728, | ||
| "io.modelcontextprotocol/clientInfo": "not an implementation", | ||
| "io.modelcontextprotocol/clientCapabilities": "not capabilities", | ||
| "io.modelcontextprotocol/logLevel": "loud" | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(meta.protocol_version(), None); | ||
| assert_eq!(meta.client_info(), None); | ||
| assert_eq!(meta.client_capabilities(), None); | ||
| assert_eq!(meta.log_level(), None); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these constants need to be part of the public API? It seems like the getters and setters below should be sufficient for consumers. How about we keep them internal until there's a clear need for them downstream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also consider making these private associated constants on
Metainstead of module-level constants. The raw keys are only used by theMetahelpers, so keeping them underimpl Metamakes the ownership clearer.