Skip to content

Commit b2e1e2b

Browse files
authored
Merge pull request #1 from jgmartin/feature/resource-templates
add ResourceTemplate
2 parents fd97761 + 473e1c8 commit b2e1e2b

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@ mcp_sdk_rs = "0.1.0"
3030
use mcp_sdk_rs::{Client, transport::WebSocketTransport};
3131

3232
#[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?
4560
}
4661
```
4762

src/client/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub trait ClientHandler: Send + Sync {
118118
pub struct DefaultClientHandler;
119119
#[async_trait]
120120
impl ClientHandler for DefaultClientHandler {
121+
/// Handle an incoming request
121122
async fn handle_request(&self, method: String, _params: Option<Value>) -> Result<Value, Error> {
122123
match method.as_str() {
123124
"sampling/createMessage" => {
@@ -128,6 +129,7 @@ impl ClientHandler for DefaultClientHandler {
128129
}
129130
}
130131

132+
/// Handle an incoming notification
131133
async fn handle_notification(
132134
&self,
133135
method: String,

src/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ pub struct Resource {
6666
pub size: Option<String>,
6767
}
6868

69+
/// A resource template
70+
#[derive(Debug, Clone, Serialize, Deserialize)]
71+
pub struct ResourceTemplate {
72+
/// Unique identifier for the resource
73+
#[serde(rename = "uriTemplate")]
74+
pub uri_template: String,
75+
/// Human-readable name
76+
pub name: String,
77+
/// Optional description
78+
#[serde(skip_serializing_if = "Option::is_none")]
79+
pub description: Option<String>,
80+
/// The MIME type of this resource, if known
81+
#[serde(skip_serializing_if = "Option::is_none")]
82+
#[serde(rename = "mimeType")]
83+
pub mime_type: Option<String>,
84+
}
85+
6986
/// Model preferences for completion requests
7087
#[derive(Debug, Clone, Serialize, Deserialize)]
7188
pub struct ModelPreferences {

0 commit comments

Comments
 (0)