It can list tools!

This commit is contained in:
Joshua Coles 2025-03-14 21:31:12 +00:00
parent 10879e431c
commit 212de0524e

View File

@ -1,19 +1,20 @@
use std::collections::HashMap;
use std::process::Stdio;
use std::sync::Arc;
use jsonrpsee::core::async_trait;
use jsonrpsee::core::client::{
Client, ClientBuilder, ClientT, ReceivedMessage, TransportReceiverT, TransportSenderT,
};
use jsonrpsee::core::params::ObjectParams;
use jsonrpsee::core::traits::ToRpcParams;
use jsonrpsee::rpc_params;
use tokio::process::{Child, ChildStdin, ChildStdout};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, Stdin, Stdout};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use serde::{Deserialize, Serialize};
use serde_json::Error;
use serde_json::value::RawValue;
use tokio::time::sleep;
use tracing::debug;
use types::{Implementation, InitializeRequestParams, InitializeResult};
use crate::types::ClientCapabilities;
use crate::types::{ClientCapabilities, ListToolsRequestParams, ListToolsResult};
mod types;
@ -37,6 +38,7 @@ impl TransportSenderT for StdioTransport {
#[tracing::instrument(skip(self), level = "trace")]
async fn send(&mut self, msg: String) -> Result<(), Self::Error> {
debug!("Sending: {}", msg);
let mut stdin = self.stdin.lock().await;
stdin.write_all(msg.as_bytes()).await?;
stdin.write_all(b"\n").await?;
@ -53,6 +55,7 @@ impl TransportReceiverT for StdioTransport {
let mut stdout = self.stdout.lock().await;
let mut str = String::new();
stdout.read_line(&mut str).await?;
debug!("Received: {}", str);
Ok(ReceivedMessage::Text(str))
}
}
@ -66,6 +69,14 @@ impl<T: Serialize> ToRpcParams for RpcArg<T> {
}
}
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()
@ -98,5 +109,10 @@ async fn main() -> anyhow::Result<()> {
println!("Response: {:?}", response);
client.notification("notifications/initialized", NoParams).await?;
let response: ListToolsResult = client.request("tools/list", RpcArg(ListToolsRequestParams::default())).await?;
println!("Response: {:#?}", response);
Ok(())
}