Skip to content

Commit 3196c95

Browse files
authored
chore(deps): update schemars requirement from 0.8 to 1.0 (#258)
* chore(deps): update schemars requirement from 0.8 to 0.9 * fix: adapt the new openApi structure * fix : fix the shemar deps in example
1 parent e615300 commit 3196c95

File tree

8 files changed

+968
-854
lines changed

8 files changed

+968
-854
lines changed

crates/rmcp/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ paste = { version = "1", optional = true }
2828
oauth2 = { version = "5.0", optional = true }
2929

3030
# for auto generate schema
31-
schemars = { version = "0.8", optional = true, features = ["chrono"] }
31+
schemars = { version = "1.0", optional = true, features = ["chrono04"] }
3232

3333
# for image encoding
3434
base64 = { version = "0.22", optional = true }
@@ -138,7 +138,7 @@ schemars = ["dep:schemars"]
138138

139139
[dev-dependencies]
140140
tokio = { version = "1", features = ["full"] }
141-
schemars = { version = "0.8" }
141+
schemars = { version = "1.0", features = ["chrono04"] }
142142

143143
anyhow = "1.0"
144144
tracing-subscriber = { version = "0.3", features = [

crates/rmcp/src/handler/server/tool.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@ use std::{
33
};
44

55
use futures::future::{BoxFuture, FutureExt};
6-
use schemars::JsonSchema;
6+
use schemars::{JsonSchema, transform::AddNullable};
77
use serde::{Deserialize, Serialize, de::DeserializeOwned};
88
use tokio_util::sync::CancellationToken;
99

1010
pub use super::router::tool::{ToolRoute, ToolRouter};
1111
use crate::{
1212
RoleServer,
1313
model::{CallToolRequestParam, CallToolResult, IntoContents, JsonObject},
14+
schemars::generate::SchemaSettings,
1415
service::RequestContext,
1516
};
16-
1717
/// A shortcut for generating a JSON schema for a type.
1818
pub fn schema_for_type<T: JsonSchema>() -> JsonObject {
1919
// explicitly to align json schema version to official specifications.
2020
// https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.json
21-
let mut settings = schemars::r#gen::SchemaSettings::draft07();
22-
settings.option_nullable = true;
23-
settings.option_add_null_type = false;
24-
settings.visitors = Vec::default();
21+
// TODO: update to 2020-12 waiting for the mcp spec update
22+
let mut settings = SchemaSettings::draft07();
23+
settings.transforms = vec![Box::new(AddNullable::default())];
2524
let generator = settings.into_generator();
2625
let schema = generator.into_root_schema_for::<T>();
2726
let object = serde_json::to_value(schema).expect("failed to serialize schema");
@@ -185,11 +184,11 @@ pub type DynCallToolHandler<S> = dyn for<'s> Fn(ToolCallContext<'s, S>) -> BoxFu
185184
pub struct Parameters<P>(pub P);
186185

187186
impl<P: JsonSchema> JsonSchema for Parameters<P> {
188-
fn schema_name() -> String {
187+
fn schema_name() -> Cow<'static, str> {
189188
P::schema_name()
190189
}
191190

192-
fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
191+
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
193192
P::json_schema(generator)
194193
}
195194
}

crates/rmcp/src/model.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,19 @@ macro_rules! const_string {
9898

9999
#[cfg(feature = "schemars")]
100100
impl schemars::JsonSchema for $name {
101-
fn schema_name() -> String {
102-
stringify!($name).to_string()
101+
fn schema_name() -> Cow<'static, str> {
102+
Cow::Borrowed(stringify!($name))
103103
}
104104

105-
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
106-
// Create a schema for a constant value of type String
107-
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
108-
instance_type: Some(schemars::schema::InstanceType::String.into()),
109-
format: Some("const".to_string()),
110-
const_value: Some(serde_json::Value::String($value.into())),
111-
..Default::default()
112-
})
105+
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
106+
use serde_json::{Map, json};
107+
108+
let mut schema_map = Map::new();
109+
schema_map.insert("type".to_string(), json!("string"));
110+
schema_map.insert("format".to_string(), json!("const"));
111+
schema_map.insert("const".to_string(), json!($value));
112+
113+
schemars::Schema::from(schema_map)
113114
}
114115
}
115116
};
@@ -233,27 +234,23 @@ impl<'de> Deserialize<'de> for NumberOrString {
233234

234235
#[cfg(feature = "schemars")]
235236
impl schemars::JsonSchema for NumberOrString {
236-
fn schema_name() -> String {
237-
"NumberOrString".to_string()
237+
fn schema_name() -> Cow<'static, str> {
238+
Cow::Borrowed("NumberOrString")
238239
}
239240

240-
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
241-
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
242-
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
243-
one_of: Some(vec![
244-
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
245-
instance_type: Some(schemars::schema::InstanceType::Number.into()),
246-
..Default::default()
247-
}),
248-
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
249-
instance_type: Some(schemars::schema::InstanceType::String.into()),
250-
..Default::default()
251-
}),
252-
]),
253-
..Default::default()
254-
})),
255-
..Default::default()
256-
})
241+
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
242+
use serde_json::{Map, json};
243+
244+
let mut number_schema = Map::new();
245+
number_schema.insert("type".to_string(), json!("number"));
246+
247+
let mut string_schema = Map::new();
248+
string_schema.insert("type".to_string(), json!("string"));
249+
250+
let mut schema_map = Map::new();
251+
schema_map.insert("oneOf".to_string(), json!([number_schema, string_schema]));
252+
253+
schemars::Schema::from(schema_map)
257254
}
258255
}
259256

crates/rmcp/tests/test_message_schema.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod tests {
22
use rmcp::model::{ClientJsonRpcMessage, ServerJsonRpcMessage};
3-
use schemars::schema_for;
3+
use schemars::generate::SchemaSettings;
44

55
fn compare_schemas(name: &str, actual: &str, expected_file: &str) {
66
let expected = match std::fs::read_to_string(expected_file) {
@@ -48,7 +48,10 @@ mod tests {
4848

4949
#[test]
5050
fn test_client_json_rpc_message_schema() {
51-
let schema = schema_for!(ClientJsonRpcMessage);
51+
let settings = SchemaSettings::draft07();
52+
let schema = settings
53+
.into_generator()
54+
.into_root_schema_for::<ClientJsonRpcMessage>();
5255
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
5356

5457
compare_schemas(
@@ -60,7 +63,10 @@ mod tests {
6063

6164
#[test]
6265
fn test_server_json_rpc_message_schema() {
63-
let schema = schema_for!(ServerJsonRpcMessage);
66+
let settings = SchemaSettings::draft07();
67+
let schema = settings
68+
.into_generator()
69+
.into_root_schema_for::<ServerJsonRpcMessage>();
6470
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
6571

6672
compare_schemas(

0 commit comments

Comments
 (0)