|
1 | 1 | use std::sync::Arc;
|
2 | 2 |
|
3 | 3 | use rmcp::{ServerHandler, handler::server::tool::ToolCallContext, tool};
|
4 |
| -use schemars::JsonSchema; |
| 4 | +use schemars::{JsonSchema, schema_for}; |
5 | 5 | use serde::{Deserialize, Serialize};
|
6 | 6 |
|
7 | 7 | #[derive(Serialize, Deserialize, JsonSchema)]
|
@@ -100,3 +100,87 @@ async fn test_tool_macros_with_generics() {
|
100 | 100 | }
|
101 | 101 |
|
102 | 102 | impl GetWeatherRequest {}
|
| 103 | + |
| 104 | +#[test] |
| 105 | +fn test_optional_field_schema_generation() { |
| 106 | + // tests https://github.com/modelcontextprotocol/rust-sdk/issues/135 |
| 107 | + #[derive(Debug, Deserialize, Serialize, JsonSchema)] |
| 108 | + pub struct DefaultOptionalSchema { |
| 109 | + #[schemars(description = "An optional description field")] |
| 110 | + pub description: Option<String>, |
| 111 | + } |
| 112 | + |
| 113 | + let schema = schema_for!(DefaultOptionalSchema); |
| 114 | + let schema_json = serde_json::to_value(&schema).unwrap(); |
| 115 | + |
| 116 | + // Print the actual generated schema for debugging |
| 117 | + println!( |
| 118 | + "Actual schema for DefaultOptionalSchema: {:#?}", |
| 119 | + schema_json |
| 120 | + ); |
| 121 | + |
| 122 | + // Verify the default schema generation for Option<String> |
| 123 | + // This format (`type: [T, 'null']`) is reported as incompatible with some clients (Cursor, Windsurf). |
| 124 | + let description_schema = &schema_json["properties"]["description"]; |
| 125 | + assert_eq!( |
| 126 | + description_schema["type"], |
| 127 | + serde_json::json!(["string", "null"]), |
| 128 | + "Default schema for Option<String> should be type: [\"string\", \"null\"] as per schemars default" |
| 129 | + ); |
| 130 | + // Ensure 'nullable' field is NOT present at this level in the default schema |
| 131 | + assert!( |
| 132 | + description_schema.get("nullable").is_none(), |
| 133 | + "Default schema should not have top-level 'nullable' field" |
| 134 | + ); |
| 135 | + |
| 136 | + // We still check the description is correct |
| 137 | + assert_eq!( |
| 138 | + description_schema["description"], |
| 139 | + "An optional description field" |
| 140 | + ); |
| 141 | + |
| 142 | + // --- Demonstrate the workaround --- |
| 143 | + |
| 144 | + // Custom schema function for the workaround for https://github.com/modelcontextprotocol/rust-sdk/issues/135 |
| 145 | + pub fn nullable_string_schema( |
| 146 | + _: &mut schemars::r#gen::SchemaGenerator, |
| 147 | + ) -> schemars::schema::Schema { |
| 148 | + serde_json::from_value(serde_json::json!({ |
| 149 | + "type": "string", |
| 150 | + "nullable": true, |
| 151 | + // Note: Description needs to be reapplied here if using schema_with |
| 152 | + "description": "An optional description field (workaround)" |
| 153 | + })) |
| 154 | + .unwrap() |
| 155 | + } |
| 156 | + |
| 157 | + #[derive(Debug, Deserialize, Serialize, JsonSchema)] |
| 158 | + pub struct WorkaroundOptionalSchema { |
| 159 | + #[schemars( |
| 160 | + schema_with = "nullable_string_schema", |
| 161 | + description = "An optional description field (workaround)" // Description here is mainly for docs, schema_with overrides generated schema description |
| 162 | + )] |
| 163 | + #[serde(default)] // Needed for deserialization when field is absent |
| 164 | + pub description: Option<String>, |
| 165 | + } |
| 166 | + |
| 167 | + let schema_workaround = schema_for!(WorkaroundOptionalSchema); |
| 168 | + let schema_workaround_json = serde_json::to_value(&schema_workaround).unwrap(); |
| 169 | + |
| 170 | + // Verify the workaround schema generation |
| 171 | + let description_schema_workaround = &schema_workaround_json["properties"]["description"]; |
| 172 | + assert_eq!( |
| 173 | + description_schema_workaround["type"], |
| 174 | + serde_json::json!("string"), |
| 175 | + "Workaround schema for Option<String> should be type: \"string\"" |
| 176 | + ); |
| 177 | + assert_eq!( |
| 178 | + description_schema_workaround["nullable"], |
| 179 | + serde_json::json!(true), |
| 180 | + "Workaround schema for Option<String> should have nullable: true" |
| 181 | + ); |
| 182 | + assert_eq!( |
| 183 | + description_schema_workaround["description"], |
| 184 | + "An optional description field (workaround)" |
| 185 | + ); |
| 186 | +} |
0 commit comments