Split
This commit is contained in:
parent
4d2ff19d41
commit
2b2371a6c5
@ -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>(T);
|
||||
|
||||
impl<T: Serialize> ToRpcParams for RpcArg<T> {
|
||||
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, 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<Self>;
|
||||
}
|
||||
|
||||
impl<T: Serialize> ToRpcArg for &T {
|
||||
fn to_rpc(self) -> RpcArg<Self> {
|
||||
RpcArg(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct NoParams;
|
||||
|
||||
impl ToRpcParams for NoParams {
|
||||
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, Error> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
|
||||
@ -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()
|
||||
|
||||
16
ext/mcp/src/mcp_client.rs
Normal file
16
ext/mcp/src/mcp_client.rs
Normal file
@ -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<InitializeResult, anyhow::Error> {
|
||||
let result: InitializeResult = self.client.request("initialize", params.to_rpc()).await?;
|
||||
self.client.notification("notifications/initialized", NoParams).await?;
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
31
ext/mcp/src/rpc_helpers.rs
Normal file
31
ext/mcp/src/rpc_helpers.rs
Normal file
@ -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>(T);
|
||||
|
||||
impl<T: Serialize> ToRpcParams for RpcArg<T> {
|
||||
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, 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<Self>;
|
||||
}
|
||||
|
||||
impl<T: Serialize> ToRpcArg for &T {
|
||||
fn to_rpc(self) -> RpcArg<Self> {
|
||||
RpcArg(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NoParams;
|
||||
|
||||
impl ToRpcParams for NoParams {
|
||||
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, Error> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user