Skip to content

chore(deps): update schemars requirement from 0.8 to 0.9 #258

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/rmcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ paste = { version = "1", optional = true }
oauth2 = { version = "5.0", optional = true }

# for auto generate schema
schemars = { version = "0.8", optional = true, features = ["chrono"] }
schemars = { version = "0.9", optional = true }

# for image encoding
base64 = { version = "0.22", optional = true }
Expand Down Expand Up @@ -134,7 +134,7 @@ schemars = ["dep:schemars"]

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
schemars = { version = "0.8" }
schemars = { version = "0.9", features = ["chrono04"] }

anyhow = "1.0"
tracing-subscriber = { version = "0.3", features = [
Expand Down
15 changes: 7 additions & 8 deletions crates/rmcp/src/handler/server/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
};

use futures::future::BoxFuture;
use schemars::JsonSchema;
use schemars::{JsonSchema, transform::AddNullable};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use tokio_util::sync::CancellationToken;

Expand All @@ -14,12 +14,11 @@ use crate::{
};
/// A shortcut for generating a JSON schema for a type.
pub fn schema_for_type<T: JsonSchema>() -> JsonObject {
let mut settings = schemars::r#gen::SchemaSettings::default();
settings.option_nullable = true;
settings.option_add_null_type = false;
settings.definitions_path = "#/components/schemas/".to_owned();
let mut settings = schemars::generate::SchemaSettings::default();

settings.definitions_path = Cow::Borrowed("#/components/schemas/");
settings.meta_schema = None;
settings.visitors = Vec::default();
settings.transforms = vec![Box::new(AddNullable::default())];
settings.inline_subschemas = false;
let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<T>();
Expand Down Expand Up @@ -180,11 +179,11 @@ pub struct Parameter<K: ConstString, V>(pub K, pub V);
pub struct Parameters<P>(pub P);

impl<P: JsonSchema> JsonSchema for Parameters<P> {
fn schema_name() -> String {
fn schema_name() -> Cow<'static, str> {
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)
}
}
Expand Down
55 changes: 26 additions & 29 deletions crates/rmcp/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,19 @@ macro_rules! const_string {

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

fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
// Create a schema for a constant value of type String
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
format: Some("const".to_string()),
const_value: Some(serde_json::Value::String($value.into())),
..Default::default()
})
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
use serde_json::{Map, json};

let mut schema_map = Map::new();
schema_map.insert("type".to_string(), json!("string"));
schema_map.insert("format".to_string(), json!("const"));
schema_map.insert("const".to_string(), json!($value));

schemars::Schema::from(schema_map)
}
}
};
Expand Down Expand Up @@ -212,27 +213,23 @@ impl<'de> Deserialize<'de> for NumberOrString {

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for NumberOrString {
fn schema_name() -> String {
"NumberOrString".to_string()
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("NumberOrString")
}

fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
one_of: Some(vec![
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::Number.into()),
..Default::default()
}),
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
..Default::default()
}),
]),
..Default::default()
})),
..Default::default()
})
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
use serde_json::{Map, json};

let mut number_schema = Map::new();
number_schema.insert("type".to_string(), json!("number"));

let mut string_schema = Map::new();
string_schema.insert("type".to_string(), json!("string"));

let mut schema_map = Map::new();
schema_map.insert("oneOf".to_string(), json!([number_schema, string_schema]));

schemars::Schema::from(schema_map)
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/rmcp/tests/test_complex_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ fn test_complex_schema() {
let attr = Demo::chat_tool_attr();
let input_schema = attr.input_schema;
let enum_number = input_schema
.get("definitions")
.get("components")
.unwrap()
.as_object()
.unwrap()
.get("schemas")
.unwrap()
.as_object()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tracing-subscriber = { version = "0.3", features = [
futures = "0.3"
rand = { version = "0.9", features = ["std"] }
axum = { version = "0.8", features = ["macros"] }
schemars = { version = "0.8", optional = true }
schemars = { version = "0.9", optional = true }
reqwest = { version = "0.12", features = ["json"] }
chrono = "0.4"
uuid = { version = "1.6", features = ["v4", "serde"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/src/common/counter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{borrow::Cow, sync::Arc};

use rmcp::{
Error as McpError, RoleServer, ServerHandler, const_string, model::*, schemars,
Expand Down
2 changes: 1 addition & 1 deletion examples/transport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tracing-subscriber = { version = "0.3", features = [
] }
futures = "0.3"
rand = { version = "0.9" }
schemars = { version = "0.8", optional = true }
schemars = { version = "0.9", optional = true }
hyper = { version = "1", features = ["client", "server", "http1"] }
hyper-util = { version = "0.1", features = ["tokio"] }
tokio-tungstenite = "0.26.2"
Expand Down
Loading