@@ -30,18 +30,33 @@ mcp_sdk_rs = "0.1.0"
30
30
use mcp_sdk_rs :: {Client , transport :: WebSocketTransport };
31
31
32
32
#[tokio:: main]
33
- async fn main () -> Result <(), Box <dyn std :: error :: Error >> {
34
- // Create a WebSocket transport
35
- let transport = WebSocketTransport :: new (" ws://localhost:8080" ). await ? ;
36
-
37
- // Create and connect the client
38
- let client = Client :: new (transport );
39
- client . connect (). await ? ;
40
-
41
- // Make requests
42
- let response = client . request (" method_name" , Some (params )). await ? ;
43
-
44
- Ok (())
33
+ async fn main () -> Result <(), Error > {
34
+ // Create a transport
35
+ let transport = WebSocketTransport :: new (self . url. as_str ())
36
+ . await
37
+ . map_err (| _ | Error :: Internal )? ;
38
+
39
+ // Create mpsc channels for communication between the client and session
40
+ let (request_tx , request_rx ): (UnboundedSender <Message >, UnboundedReceiver <Message >) =
41
+ tokio :: sync :: mpsc :: unbounded_channel ();
42
+ let (response_tx , response_rx ): (UnboundedSender <Message >, UnboundedReceiver <Message >) =
43
+ tokio :: sync :: mpsc :: unbounded_channel ();
44
+
45
+ // Create and start the session
46
+ // Optionally pass an implementation of ClientHandler for custom handling of requests and notifications
47
+ let session = Session :: new (Arc :: new (transport ), response_tx , request_rx , None );
48
+ session . start (). await . map_err (| _ | Error :: Internal )? ;
49
+
50
+ // Create the client and make requests, receive notifications etc
51
+ let client = Client :: new (request_tx , response_rx );
52
+ let response = client . request (
53
+ " tools/call" ,
54
+ Some (json! ({
55
+ " name" : " methondName" ,
56
+ " arguments" : json! ({})
57
+ })),
58
+ )
59
+ . await ?
45
60
}
46
61
```
47
62
0 commit comments