Skip to content

Commit b496f16

Browse files
[Jimy][mcp] Allow metadata to be passed from tool call (#19)
## Description * We need to pass in tool-id and session-id (to identify which Jimy the request is for) * Pass that along to interactive sessions ## Issues * Ref GIT-6378 ## Test Plan * Tested locally ## Revert Plan * Revert
1 parent e3ab919 commit b496f16

14 files changed

Lines changed: 322 additions & 84 deletions

File tree

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ reqwest = { version = "0.12", features = ["json"] }
3333
reqwest-eventsource = "0.6"
3434
mime = "0.3"
3535
tokio-tungstenite = { version = "0.26", features = ["native-tls"] }
36+
typed-builder = "0.21.0"
3637
tracing-core = "0.1"
3738
async-recursion = "1"
3839
http = "1.1"

bin/client.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use clap::{Parser, Subcommand};
22
use mcp_rs::{
33
client::{Client, ClientInfo},
44
error::McpError,
5-
transport::sse::SseTransport,
6-
transport::stdio::StdioTransport,
7-
transport::ws::WebSocketTransport,
5+
tools::CallToolArgs,
6+
transport::{sse::SseTransport, stdio::StdioTransport, ws::WebSocketTransport},
87
};
98
use serde_json::json;
109

@@ -69,6 +68,10 @@ enum Commands {
6968
name: String,
7069
#[arg(short, long)]
7170
args: String,
71+
#[arg(short, long)]
72+
tool_id: Option<String>,
73+
#[arg(short, long)]
74+
session_id: Option<String>,
7275
},
7376
/// Set log level
7477
SetLogLevel {
@@ -236,10 +239,26 @@ async fn main() -> Result<(), McpError> {
236239
println!("{}", json!(res));
237240
}
238241

239-
Commands::CallTool { name, args } => {
242+
Commands::CallTool {
243+
name,
244+
args,
245+
tool_id,
246+
session_id,
247+
} => {
240248
let arguments =
241249
serde_json::from_str(&args).map_err(|e| McpError::InvalidRequest(e.to_string()))?;
242-
let res = client.call_tool(name, arguments).await?;
250+
let res = client
251+
.call_tool(
252+
name,
253+
arguments,
254+
Some(
255+
CallToolArgs::builder()
256+
.session_id(session_id)
257+
.tool_id(tool_id)
258+
.build(),
259+
),
260+
)
261+
.await?;
243262
println!("{}", json!(res));
244263
}
245264

src/client/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use crate::{
99
ListResourcesRequest, ListResourcesResponse, ReadResourceRequest, ReadResourceResponse,
1010
ResourceCapabilities,
1111
},
12-
tools::{CallToolRequest, ListToolsRequest, ListToolsResponse, ToolCapabilities, ToolResult},
12+
tools::{
13+
CallToolArgs, CallToolRequest, ListToolsRequest, ListToolsResponse, ToolCapabilities,
14+
ToolResult,
15+
},
1316
transport::{Transport, TransportCommand},
1417
};
1518
use serde::{Deserialize, Serialize};
@@ -245,14 +248,19 @@ impl Client {
245248
&self,
246249
name: String,
247250
arguments: serde_json::Value,
251+
metadata: Option<CallToolArgs>,
248252
) -> Result<ToolResult, McpError> {
249253
self.assert_initialized().await?;
250254
self.assert_capability("tools").await?;
251255

252256
self.protocol
253257
.request(
254258
"tools/call",
255-
Some(CallToolRequest { name, arguments }),
259+
Some(CallToolRequest {
260+
name,
261+
arguments,
262+
metadata,
263+
}),
256264
None,
257265
)
258266
.await

src/server/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ where
347347
serde_json::from_value(req.params.unwrap_or_default())
348348
.map_err(|_| McpError::InvalidParams)?;
349349
let result = tool_manager
350-
.call_tool(&params.name, params.arguments)
350+
.call_tool(&params.name, params.arguments, params.metadata)
351351
.await?;
352352
Ok(serde_json::to_value(result).unwrap())
353353
})

src/tools/calculator.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
protocol::{RequestHandler, ServerCapabilities},
88
};
99

10-
use super::{Tool, ToolContent, ToolInputSchema, ToolProvider, ToolResult};
10+
use super::{CallToolArgs, Tool, ToolContent, ToolInputSchema, ToolProvider, ToolResult};
1111

1212
// Domain types
1313
#[derive(Debug, serde::Deserialize)]
@@ -205,7 +205,11 @@ impl ToolProvider for CalculatorTool {
205205
}
206206
}
207207

208-
async fn execute(&self, arguments: serde_json::Value) -> Result<ToolResult, McpError> {
208+
async fn execute(
209+
&self,
210+
arguments: Value,
211+
_metadata: Option<CallToolArgs>,
212+
) -> Result<ToolResult, McpError> {
209213
let params: CalculatorParams = serde_json::from_value(arguments).map_err(|e| {
210214
tracing::error!("Error parsing calculator arguments: {:?}", e);
211215
McpError::InvalidParams
@@ -301,6 +305,12 @@ mod tests {
301305
"a": 2.0,
302306
"b": 3.0
303307
}),
308+
Some(
309+
CallToolArgs::builder()
310+
.session_id(Some("session-1234".to_string()))
311+
.tool_id(Some("calculator-1234".to_string()))
312+
.build(),
313+
),
304314
)
305315
.await
306316
.unwrap();
@@ -319,6 +329,12 @@ mod tests {
319329
"operation": "ln",
320330
"a": 2.718281828459045
321331
}),
332+
Some(
333+
CallToolArgs::builder()
334+
.session_id(Some("session-5678".to_string()))
335+
.tool_id(Some("calculator-5678".to_string()))
336+
.build(),
337+
),
322338
)
323339
.await
324340
.unwrap();
@@ -350,6 +366,12 @@ mod tests {
350366
"a": -1.0,
351367
"b": 10.0
352368
}),
369+
Some(
370+
CallToolArgs::builder()
371+
.session_id(Some("session-9999".to_string()))
372+
.tool_id(Some("calculator-9999".to_string()))
373+
.build(),
374+
),
353375
)
354376
.await
355377
.unwrap();

src/tools/file_system/directory.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tokio::fs;
55

66
use crate::{
77
error::McpError,
8-
tools::{Tool, ToolContent, ToolInputSchema, ToolProvider, ToolResult},
8+
tools::{CallToolArgs, Tool, ToolContent, ToolInputSchema, ToolProvider, ToolResult},
99
};
1010

1111
pub struct DirectoryTool;
@@ -57,7 +57,11 @@ impl ToolProvider for DirectoryTool {
5757
}
5858
}
5959

60-
async fn execute(&self, arguments: Value) -> Result<ToolResult, McpError> {
60+
async fn execute(
61+
&self,
62+
arguments: Value,
63+
_metadata: Option<CallToolArgs>,
64+
) -> Result<ToolResult, McpError> {
6165
match arguments["operation"].as_str() {
6266
Some("create_directory") => {
6367
let path = arguments["path"].as_str().ok_or(McpError::InvalidParams)?;

0 commit comments

Comments
 (0)