Skip to content

Commit ce0643c

Browse files
committed
Add regression test
1 parent 3529c36 commit ce0643c

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![cfg(all(feature = "server", feature = "macros", not(feature = "local")))]
2+
3+
use rmcp::{
4+
handler::server::wrapper::Parameters,
5+
model::{ListToolsResult, NumberOrString, ServerJsonRpcMessage, ServerResult},
6+
};
7+
8+
/// Parameters for adding two numbers.
9+
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
10+
struct AddRequest {
11+
/// The left-hand number.
12+
a: f64,
13+
/// The right-hand number.
14+
b: f64,
15+
}
16+
17+
/// Add two numbers.
18+
#[rmcp::tool]
19+
fn add(Parameters(AddRequest { a, b }): Parameters<AddRequest>) -> String {
20+
(a + b).to_string()
21+
}
22+
23+
#[test]
24+
fn list_tools_result_matches_expected_json() {
25+
let expected_json = std::fs::read("tests/test_list_tools_result/list_tools_result.json")
26+
.expect("missing expected list tools result JSON fixture");
27+
let expected: serde_json::Value =
28+
serde_json::from_slice(&expected_json).expect("invalid expected JSON fixture");
29+
30+
assert_eq!(add(Parameters(AddRequest { a: 1.0, b: 2.0 })), "3");
31+
32+
let result = ListToolsResult::with_all_items(vec![add_tool_attr()]);
33+
let response = ServerJsonRpcMessage::response(
34+
ServerResult::ListToolsResult(result),
35+
NumberOrString::Number(2),
36+
);
37+
38+
let actual = serde_json::to_value(response).expect("failed to serialize list tools response");
39+
assert_eq!(actual, expected);
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"result": {
3+
"tools": [
4+
{
5+
"name": "add",
6+
"description": "Add two numbers.",
7+
"inputSchema": {
8+
"$schema": "https://json-schema.org/draft/2020-12/schema",
9+
"description": "Parameters for adding two numbers.",
10+
"title": "AddRequest",
11+
"type": "object",
12+
"properties": {
13+
"a": {
14+
"description": "The left-hand number.",
15+
"format": "double",
16+
"type": "number"
17+
},
18+
"b": {
19+
"description": "The right-hand number.",
20+
"format": "double",
21+
"type": "number"
22+
}
23+
},
24+
"required": [
25+
"a",
26+
"b"
27+
]
28+
}
29+
}
30+
]
31+
},
32+
"jsonrpc": "2.0",
33+
"id": 2
34+
}

0 commit comments

Comments
 (0)