This commit is contained in:
parent
923e87af4d
commit
2c0cafdde3
4 changed files with 34 additions and 3 deletions
|
|
@ -6,7 +6,7 @@ use crate::{
|
||||||
|
|
||||||
mod resource;
|
mod resource;
|
||||||
pub mod tool;
|
pub mod tool;
|
||||||
|
pub mod wrapper;
|
||||||
impl<H: ServerHandler> Service<RoleServer> for H {
|
impl<H: ServerHandler> Service<RoleServer> for H {
|
||||||
async fn handle_request(
|
async fn handle_request(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
||||||
2
crates/rmcp/src/handler/server/wrapper.rs
Normal file
2
crates/rmcp/src/handler/server/wrapper.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
mod json;
|
||||||
|
pub use json::*;
|
||||||
28
crates/rmcp/src/handler/server/wrapper/json.rs
Normal file
28
crates/rmcp/src/handler/server/wrapper/json.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::model::IntoContents;
|
||||||
|
|
||||||
|
/// Json wrapper
|
||||||
|
///
|
||||||
|
/// This is used to tell the SDK to serialize the inner value into json
|
||||||
|
pub struct Json<T>(pub T);
|
||||||
|
|
||||||
|
impl<T> IntoContents for Json<T>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
fn into_contents(self) -> Vec<crate::model::Content> {
|
||||||
|
let result = crate::model::Content::json(self.0);
|
||||||
|
debug_assert!(
|
||||||
|
result.is_ok(),
|
||||||
|
"Json wrapped content should be able to serialized into json"
|
||||||
|
);
|
||||||
|
match result {
|
||||||
|
Ok(content) => vec![content],
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("failed to convert json content: {e}");
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use rmcp::{
|
use rmcp::{
|
||||||
ServerHandler,
|
ServerHandler,
|
||||||
|
handler::server::wrapper::Json,
|
||||||
model::{ServerCapabilities, ServerInfo},
|
model::{ServerCapabilities, ServerInfo},
|
||||||
schemars, tool,
|
schemars, tool,
|
||||||
};
|
};
|
||||||
|
|
@ -28,8 +29,8 @@ impl Calculator {
|
||||||
#[tool(param)]
|
#[tool(param)]
|
||||||
#[schemars(description = "the left hand side number")]
|
#[schemars(description = "the left hand side number")]
|
||||||
b: i32,
|
b: i32,
|
||||||
) -> String {
|
) -> Json<i32> {
|
||||||
(a - b).to_string()
|
Json(a - b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue