Skip to content

Commit e9a5ae9

Browse files
authored
feat(model): add json schema generation support for all model types (#176)
* feat(model): add json schema generation support for all model types This commit adds JSON Schema support for all model types by implementing `schemars::JsonSchema` trait. The feature is gated behind the new `schemars` feature flag. This enables automatic schema generation for API documentation and validation purposes. Added tests to verify schema generation for client and server JSON-RPC messages. * fix(model): add manual json schema implementation for `NumberOrString` This commit adds a manual implementation of `JsonSchema` trait for the `NumberOrString` enum to properly represent its union type nature in JSON Schema. The schema now correctly specifies that the type can be either a number or a string using the `oneOf` validation keyword. * fix(model): skip extensions field in json schema generation The `Extensions` type was incorrectly included in JSON schema generation, which could lead to confusing API documentation. This commit adds `#[schemars(skip)]` attribute to all `extensions` fields in request and notification structs, and removes the manual `JsonSchema` implementation for the `Extensions` type since it's an internal implementation detail that shouldn't be exposed in the schema.
1 parent 5d92061 commit e9a5ae9

File tree

12 files changed

+2790
-1
lines changed

12 files changed

+2790
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ See [examples](examples/README.md)
192192
- `client`: use client side sdk
193193
- `server`: use server side sdk
194194
- `macros`: macros default
195+
- `schemars`: implement `JsonSchema` for all model structs
195196

196197
### Transports
197198

crates/rmcp/Cargo.toml

Lines changed: 7 additions & 1 deletion
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 }
31+
schemars = { version = "0.8", optional = true, features = ["chrono"] }
3232

3333
# for image encoding
3434
base64 = { version = "0.21", optional = true }
@@ -89,6 +89,7 @@ tower = ["dep:tower-service"]
8989
__auth = ["dep:oauth2", "dep:reqwest", "dep:url"]
9090
auth = ["__auth", "reqwest?/rustls-tls"]
9191
auth-tls-no-provider = ["auth", "reqwest?/rustls-tls-no-provider"]
92+
schemars = ["dep:schemars"]
9293

9394
[dev-dependencies]
9495
tokio = { version = "1", features = ["full"] }
@@ -131,3 +132,8 @@ name = "test_message_protocol"
131132
required-features = ["client"]
132133
path = "tests/test_message_protocol.rs"
133134

135+
[[test]]
136+
name = "test_message_schema"
137+
required-features = ["server", "client", "schemars"]
138+
path = "tests/test_message_schema.rs"
139+

0 commit comments

Comments
 (0)