File tree 4 files changed +34
-3
lines changed
examples/servers/src/common
4 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ use crate::{
6
6
7
7
mod resource;
8
8
pub mod tool;
9
-
9
+ pub mod wrapper ;
10
10
impl < H : ServerHandler > Service < RoleServer > for H {
11
11
async fn handle_request (
12
12
& self ,
Original file line number Diff line number Diff line change
1
+ mod json;
2
+ pub use json:: * ;
Original file line number Diff line number Diff line change
1
+ use serde:: Serialize ;
2
+
3
+ use crate :: model:: IntoContents ;
4
+
5
+ /// Json wrapper
6
+ ///
7
+ /// This is used to tell the SDK to serialize the inner value into json
8
+ pub struct Json < T > ( pub T ) ;
9
+
10
+ impl < T > IntoContents for Json < T >
11
+ where
12
+ T : Serialize ,
13
+ {
14
+ fn into_contents ( self ) -> Vec < crate :: model:: Content > {
15
+ let result = crate :: model:: Content :: json ( self . 0 ) ;
16
+ debug_assert ! (
17
+ result. is_ok( ) ,
18
+ "Json wrapped content should be able to serialized into json"
19
+ ) ;
20
+ match result {
21
+ Ok ( content) => vec ! [ content] ,
22
+ Err ( e) => {
23
+ tracing:: error!( "failed to convert json content: {e}" ) ;
24
+ vec ! [ ]
25
+ }
26
+ }
27
+ }
28
+ }
Original file line number Diff line number Diff line change 1
1
use rmcp:: {
2
2
ServerHandler ,
3
+ handler:: server:: wrapper:: Json ,
3
4
model:: { ServerCapabilities , ServerInfo } ,
4
5
schemars, tool,
5
6
} ;
@@ -28,8 +29,8 @@ impl Calculator {
28
29
#[ tool( param) ]
29
30
#[ schemars( description = "the left hand side number" ) ]
30
31
b : i32 ,
31
- ) -> String {
32
- ( a - b) . to_string ( )
32
+ ) -> Json < i32 > {
33
+ Json ( a - b)
33
34
}
34
35
}
35
36
You can’t perform that action at this time.
0 commit comments