fix(test): fix tool deserialization error (#68)

This commit is contained in:
4t145 2025-03-31 13:05:35 +08:00 • committed by GitHub
commit 0eea9aad4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 63 additions and 34 deletions

View file

@ -1,4 +1,4 @@
cargo-features = ["edition2024"]
[package]
name = "rmcp"
@ -95,5 +95,5 @@ path = "tests/test_with_python.rs"
[[test]]
name = "test_with_js"
required-features = ["server", "transport-sse-server"]
required-features = ["server", "client", "transport-sse-server", "transport-child-process"]
path = "tests/test_with_js.rs"

View file

@ -14,7 +14,8 @@ pub struct Tool {
/// The name of the tool
pub name: Cow<'static, str>,
/// A description of what the tool does
pub description: Cow<'static, str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<Cow<'static, str>>,
/// A JSON Schema object defining the expected parameters for the tool
pub input_schema: Arc<JsonObject>,
}
@ -29,7 +30,7 @@ impl Tool {
{
Tool {
name: name.into(),
description: description.into(),
description: Some(description.into()),
input_schema: input_schema.into(),
}
}

View file

@ -0,0 +1,15 @@
use rmcp::model::{JsonRpcResponse, ServerJsonRpcMessage, ServerResult};
#[test]
fn test_tool_list_result() {
let json = std::fs::read("tests/test_deserialization/tool_list_result.json").unwrap();
let result: ServerJsonRpcMessage = serde_json::from_slice(&json).unwrap();
println!("{result:#?}");
assert!(matches!(
result,
ServerJsonRpcMessage::Response(JsonRpcResponse {
result: ServerResult::ListToolsResult(_),
..
})
));
}

View file

@ -0,0 +1,28 @@
{
"result": {
"tools": [
{
"name": "add",
"inputSchema": {
"type": "object",
"properties": {
"a": {
"type": "number"
},
"b": {
"type": "number"
}
},
"required": [
"a",
"b"
],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
}
]
},
"jsonrpc": "2.0",
"id": 2
}

View file

@ -10,13 +10,13 @@ const BIND_ADDRESS: &str = "127.0.0.1:8000";
#[tokio::test]
async fn test_with_js_client() -> anyhow::Result<()> {
tracing_subscriber::registry()
let _ = tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "debug".to_string().into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
.try_init();
tokio::process::Command::new("npm")
.arg("install")
.current_dir("tests/test_with_js")