Tool calls!
Ruby / Ruby ${{ matrix.ruby }} (3.4.1) (push) Failing after 9m36s

This commit is contained in:
2025-03-15 11:58:34 +00:00
parent 554082f88c
commit b4bbbd28ef
5 changed files with 61 additions and 56 deletions
+30 -11
View File
@@ -3,6 +3,7 @@ use tokio::process::Command;
use crate::mcp_client::McpClient;
use jsonrpsee::core::client::ClientT;
use once_cell::sync::Lazy;
use magnus::prelude::*;
mod mcp_client;
mod types;
@@ -10,15 +11,12 @@ mod rpc_helpers;
mod stdio_transport;
use std::{
cell::RefCell,
fmt,
hash::{Hash, Hasher},
};
use magnus::{function, method, prelude::*, scan_args::{get_kwargs, scan_args}, typed_data, Error, Ruby, Value};
use magnus::{function, method, prelude::*, scan_args::{get_kwargs, scan_args}, typed_data, Error, RHash, Ruby, Symbol, TryConvert, Value};
use serde_magnus::serialize;
use crate::types::{Implementation, InitializeRequestParams, InitializeResult};
use crate::types::builder::Tool;
use crate::types::{CallToolRequestParams, Implementation, InitializeRequestParams, InitializeResult};
// Create global runtime
static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
@@ -83,12 +81,32 @@ impl McpClientRb {
})
}
// fn call_rpc(&self, method: &str, params: &[&str]) -> Result<Value, magnus::Error> {
// RUNTIME.block_on(async {
// self.client.client.request(method, params).await
// .map_err(|e| magnus::Error::new(magnus::exception::runtime_error(), e.to_string()))
// })
// }
fn call_tool(&self, values: &[Value]) -> Result<Value, magnus::Error> {
let args = scan_args::<(Value,), (), (), (), RHash, ()>(values)?;
let ((name,)) = args.required;
let kwargs: RHash = args.keywords;
let kwargs: serde_json::Map<String, serde_json::Value> = serde_magnus::deserialize(kwargs)?;
let name = match Symbol::from_value(name) {
Some(symbol) => symbol.name()?.to_string(),
None => String::try_convert(name)?,
};
RUNTIME.block_on(async {
let a = self.client.call_tool::<serde_json::Value>(CallToolRequestParams {
name,
arguments: kwargs,
}).await;
match a {
Ok(a) => Ok(serde_magnus::serialize(&a)?),
Err(e) => Err(Error::new(
magnus::exception::runtime_error(),
e.to_string(),
)),
}
})
}
}
#[magnus::init]
@@ -99,6 +117,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
client_class.define_singleton_method("new", function!(McpClientRb::new, 2))?;
client_class.define_method("connect", method!(McpClientRb::connect, 0))?;
client_class.define_method("list_tools", method!(McpClientRb::list_tools, 0))?;
client_class.define_method("call_tool", method!(McpClientRb::call_tool, -1))?;
Ok(())
}
+1 -1
View File
@@ -31,7 +31,7 @@ impl McpClient {
Ok(tools)
}
async fn call_tool<T: serde::de::DeserializeOwned>(&self, params: CallToolRequestParams) -> Result<T, anyhow::Error> {
pub async fn call_tool<T: serde::de::DeserializeOwned>(&self, params: CallToolRequestParams) -> Result<T, anyhow::Error> {
Ok(self.client.request("tools/call", params.to_rpc()).await?)
}
}