From 2b2371a6c5994bf937bf175020d7ca768d847290 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sat, 15 Mar 2025 08:39:47 +0000 Subject: [PATCH] Split --- ext/mcp/src/internal-test.rs | 29 ++--------------------------- ext/mcp/src/lib.rs | 3 +++ ext/mcp/src/mcp_client.rs | 16 ++++++++++++++++ ext/mcp/src/rpc_helpers.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 ext/mcp/src/mcp_client.rs create mode 100644 ext/mcp/src/rpc_helpers.rs diff --git a/ext/mcp/src/internal-test.rs b/ext/mcp/src/internal-test.rs index a883ade..0b5f29e 100644 --- a/ext/mcp/src/internal-test.rs +++ b/ext/mcp/src/internal-test.rs @@ -15,6 +15,8 @@ use types::{Implementation, InitializeRequestParams, InitializeResult}; use crate::types::{CallToolRequestParams, ClientCapabilities, ListToolsRequestParams, ListToolsResult}; mod types; +mod rpc_helpers; +use rpc_helpers::*; #[derive(Debug, Clone)] struct StdioTransport { @@ -58,33 +60,6 @@ impl TransportReceiverT for StdioTransport { } } -struct RpcArg(T); - -impl ToRpcParams for RpcArg { - fn to_rpc_params(self) -> Result>, Error> { - let s = String::from_utf8(serde_json::to_vec(&self.0)?).expect("Valid UTF8 format"); - RawValue::from_string(s).map(Some) - } -} - -trait ToRpcArg: Sized { - fn to_rpc(self) -> RpcArg; -} - -impl ToRpcArg for &T { - fn to_rpc(self) -> RpcArg { - RpcArg(self) - } -} - -struct NoParams; - -impl ToRpcParams for NoParams { - fn to_rpc_params(self) -> Result>, Error> { - Ok(None) - } -} - #[tokio::main] async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt() diff --git a/ext/mcp/src/lib.rs b/ext/mcp/src/lib.rs index 3be93c3..400f6cc 100644 --- a/ext/mcp/src/lib.rs +++ b/ext/mcp/src/lib.rs @@ -1,4 +1,7 @@ use magnus::{function, Error, Ruby}; +mod mcp_client; +mod types; +mod rpc_helpers; fn distance(a: (f64, f64), b: (f64, f64)) -> f64 { ((b.0 - a.0).powi(2) + (b.1 - a.1).powi(2)).sqrt() diff --git a/ext/mcp/src/mcp_client.rs b/ext/mcp/src/mcp_client.rs new file mode 100644 index 0000000..5d9db67 --- /dev/null +++ b/ext/mcp/src/mcp_client.rs @@ -0,0 +1,16 @@ +use jsonrpsee::async_client::Client; +use jsonrpsee::core::client::ClientT; +use crate::rpc_helpers::{NoParams, ToRpcArg}; +use crate::types::{InitializeRequestParams, InitializeResult}; + +struct McpClient { + client: Client, +} + +impl McpClient { + async fn initialize(&self, params: InitializeRequestParams) -> Result { + let result: InitializeResult = self.client.request("initialize", params.to_rpc()).await?; + self.client.notification("notifications/initialized", NoParams).await?; + Ok(result) + } +} diff --git a/ext/mcp/src/rpc_helpers.rs b/ext/mcp/src/rpc_helpers.rs new file mode 100644 index 0000000..6e303e8 --- /dev/null +++ b/ext/mcp/src/rpc_helpers.rs @@ -0,0 +1,31 @@ +use jsonrpsee::core::traits::ToRpcParams; +use serde::Serialize; +use serde_json::Error; +use serde_json::value::RawValue; + +pub struct RpcArg(T); + +impl ToRpcParams for RpcArg { + fn to_rpc_params(self) -> Result>, Error> { + let s = String::from_utf8(serde_json::to_vec(&self.0)?).expect("Valid UTF8 format"); + RawValue::from_string(s).map(Some) + } +} + +pub trait ToRpcArg: Sized { + fn to_rpc(self) -> RpcArg; +} + +impl ToRpcArg for &T { + fn to_rpc(self) -> RpcArg { + RpcArg(self) + } +} + +pub struct NoParams; + +impl ToRpcParams for NoParams { + fn to_rpc_params(self) -> Result>, Error> { + Ok(None) + } +}