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
This commit is contained in:
jokemanfire 2025-07-11 11:15:45 +08:00 • committed by GitHub
commit 3196c95f1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 968 additions and 854 deletions

View file

@ -28,7 +28,7 @@ paste = { version = "1", optional = true }
oauth2 = { version = "5.0", optional = true } oauth2 = { version = "5.0", optional = true }
# for auto generate schema # for auto generate schema
schemars = { version = "0.8", optional = true, features = ["chrono"] } schemars = { version = "1.0", optional = true, features = ["chrono04"] }
# for image encoding # for image encoding
base64 = { version = "0.22", optional = true } base64 = { version = "0.22", optional = true }
@ -138,7 +138,7 @@ schemars = ["dep:schemars"]
[dev-dependencies] [dev-dependencies]
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
schemars = { version = "0.8" } schemars = { version = "1.0", features = ["chrono04"] }
anyhow = "1.0" anyhow = "1.0"
tracing-subscriber = { version = "0.3", features = [ tracing-subscriber = { version = "0.3", features = [

View file

@ -3,7 +3,7 @@ use std::{
}; };
use futures::future::{BoxFuture, FutureExt}; use futures::future::{BoxFuture, FutureExt};
use schemars::JsonSchema; use schemars::{JsonSchema, transform::AddNullable};
use serde::{Deserialize, Serialize, de::DeserializeOwned}; use serde::{Deserialize, Serialize, de::DeserializeOwned};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
@ -11,17 +11,16 @@ pub use super::router::tool::{ToolRoute, ToolRouter};
use crate::{ use crate::{
RoleServer, RoleServer,
model::{CallToolRequestParam, CallToolResult, IntoContents, JsonObject}, model::{CallToolRequestParam, CallToolResult, IntoContents, JsonObject},
schemars::generate::SchemaSettings,
service::RequestContext, service::RequestContext,
}; };
/// A shortcut for generating a JSON schema for a type. /// A shortcut for generating a JSON schema for a type.
pub fn schema_for_type<T: JsonSchema>() -> JsonObject { pub fn schema_for_type<T: JsonSchema>() -> JsonObject {
// explicitly to align json schema version to official specifications. // explicitly to align json schema version to official specifications.
// https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.json // https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.json
let mut settings = schemars::r#gen::SchemaSettings::draft07(); // TODO: update to 2020-12 waiting for the mcp spec update
settings.option_nullable = true; let mut settings = SchemaSettings::draft07();
settings.option_add_null_type = false; settings.transforms = vec![Box::new(AddNullable::default())];
settings.visitors = Vec::default();
let generator = settings.into_generator(); let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<T>(); let schema = generator.into_root_schema_for::<T>();
let object = serde_json::to_value(schema).expect("failed to serialize schema"); 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
pub struct Parameters<P>(pub P); pub struct Parameters<P>(pub P);
impl<P: JsonSchema> JsonSchema for Parameters<P> { impl<P: JsonSchema> JsonSchema for Parameters<P> {
fn schema_name() -> String { fn schema_name() -> Cow<'static, str> {
P::schema_name() P::schema_name()
} }
fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema { fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
P::json_schema(generator) P::json_schema(generator)
} }
} }

View file

@ -98,18 +98,19 @@ macro_rules! const_string {
#[cfg(feature = "schemars")] #[cfg(feature = "schemars")]
impl schemars::JsonSchema for $name { impl schemars::JsonSchema for $name {
fn schema_name() -> String { fn schema_name() -> Cow<'static, str> {
stringify!($name).to_string() Cow::Borrowed(stringify!($name))
} }
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema { fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
// Create a schema for a constant value of type String use serde_json::{Map, json};
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()), let mut schema_map = Map::new();
format: Some("const".to_string()), schema_map.insert("type".to_string(), json!("string"));
const_value: Some(serde_json::Value::String($value.into())), schema_map.insert("format".to_string(), json!("const"));
..Default::default() schema_map.insert("const".to_string(), json!($value));
})
schemars::Schema::from(schema_map)
} }
} }
}; };
@ -233,27 +234,23 @@ impl<'de> Deserialize<'de> for NumberOrString {
#[cfg(feature = "schemars")] #[cfg(feature = "schemars")]
impl schemars::JsonSchema for NumberOrString { impl schemars::JsonSchema for NumberOrString {
fn schema_name() -> String { fn schema_name() -> Cow<'static, str> {
"NumberOrString".to_string() Cow::Borrowed("NumberOrString")
} }
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema { fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::schema::Schema::Object(schemars::schema::SchemaObject { use serde_json::{Map, json};
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
one_of: Some(vec![ let mut number_schema = Map::new();
schemars::schema::Schema::Object(schemars::schema::SchemaObject { number_schema.insert("type".to_string(), json!("number"));
instance_type: Some(schemars::schema::InstanceType::Number.into()),
..Default::default() let mut string_schema = Map::new();
}), string_schema.insert("type".to_string(), json!("string"));
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()), let mut schema_map = Map::new();
..Default::default() schema_map.insert("oneOf".to_string(), json!([number_schema, string_schema]));
}),
]), schemars::Schema::from(schema_map)
..Default::default()
})),
..Default::default()
})
} }
} }

View file

@ -1,6 +1,6 @@
mod tests { mod tests {
use rmcp::model::{ClientJsonRpcMessage, ServerJsonRpcMessage}; use rmcp::model::{ClientJsonRpcMessage, ServerJsonRpcMessage};
use schemars::schema_for; use schemars::generate::SchemaSettings;
fn compare_schemas(name: &str, actual: &str, expected_file: &str) { fn compare_schemas(name: &str, actual: &str, expected_file: &str) {
let expected = match std::fs::read_to_string(expected_file) { let expected = match std::fs::read_to_string(expected_file) {
@ -48,7 +48,10 @@ mod tests {
#[test] #[test]
fn test_client_json_rpc_message_schema() { fn test_client_json_rpc_message_schema() {
let schema = schema_for!(ClientJsonRpcMessage); let settings = SchemaSettings::draft07();
let schema = settings
.into_generator()
.into_root_schema_for::<ClientJsonRpcMessage>();
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema"); let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
compare_schemas( compare_schemas(
@ -60,7 +63,10 @@ mod tests {
#[test] #[test]
fn test_server_json_rpc_message_schema() { fn test_server_json_rpc_message_schema() {
let schema = schema_for!(ServerJsonRpcMessage); let settings = SchemaSettings::draft07();
let schema = settings
.into_generator()
.into_root_schema_for::<ServerJsonRpcMessage>();
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema"); let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
compare_schemas( compare_schemas(

View file

@ -33,7 +33,7 @@ tracing-subscriber = { version = "0.3", features = [
futures = "0.3" futures = "0.3"
rand = { version = "0.9", features = ["std"] } rand = { version = "0.9", features = ["std"] }
axum = { version = "0.8", features = ["macros"] } axum = { version = "0.8", features = ["macros"] }
schemars = { version = "0.8", optional = true } schemars = { version = "1.0", optional = true }
reqwest = { version = "0.12", features = ["json"] } reqwest = { version = "0.12", features = ["json"] }
chrono = "0.4" chrono = "0.4"
uuid = { version = "1.6", features = ["v4", "serde"] } uuid = { version = "1.6", features = ["v4", "serde"] }

View file

@ -37,7 +37,7 @@ tracing-subscriber = { version = "0.3", features = [
] } ] }
futures = "0.3" futures = "0.3"
rand = { version = "0.9" } rand = { version = "0.9" }
schemars = { version = "0.8", optional = true } schemars = { version = "1.0", optional = true }
hyper = { version = "1", features = ["client", "server", "http1"] } hyper = { version = "1", features = ["client", "server", "http1"] }
hyper-util = { version = "0.1", features = ["tokio"] } hyper-util = { version = "0.1", features = ["tokio"] }
tokio-tungstenite = "0.27.0" tokio-tungstenite = "0.27.0"