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