Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

ZAP client for connecting to ZAP gateways

The client manages a Cap’n Proto RPC connection and provides high-level async methods for MCP operations (tools, resources, prompts).

Implementations§

Source§

impl Client

Source

pub async fn connect(url: &str) -> Result<Self>

Connect to a ZAP server at the given URL.

Supported URL schemes:

  • zap:// or zap+tcp:// - TCP transport (default port 9999)
  • tcp:// - Plain TCP
§Example
let client = Client::connect("zap://localhost:9999").await?;
Source

pub async fn connect_tcp(addr: &str) -> Result<Self>

Connect to a ZAP server via TCP at the given address.

§Example
let client = Client::connect_tcp("127.0.0.1:9999").await?;
Source

pub async fn from_tcp_stream(stream: TcpStream) -> Result<Self>

Create a client from an existing TCP stream.

This is useful for testing or when you have a pre-established connection.

Source

pub async fn init(&self, name: &str, version: &str) -> Result<ServerInfo>

Initialize the connection with client information.

This should be called after connecting to exchange client/server info and verify capabilities.

§Example
let server_info = client.init("my-app", "1.0.0").await?;
if server_info.capabilities.tools {
    println!("Server supports tools!");
}
Source

pub async fn list_tools(&self) -> Result<Vec<Tool>>

List available tools from the server.

§Example
let tools = client.list_tools().await?;
for tool in &tools {
    println!("Tool: {} - {}", tool.name, tool.description);
}
Source

pub async fn call_tool(&self, name: &str, args: Value) -> Result<Value>

Call a tool with the given arguments.

§Arguments
  • name - The name of the tool to call
  • args - JSON arguments for the tool
§Example
use serde_json::json;

let result = client.call_tool("search", json!({
    "query": "rust programming",
    "limit": 10
})).await?;
Source

pub async fn call_tool_with_id( &self, id: &str, name: &str, args: Value, ) -> Result<Value>

Call a tool with a specific request ID.

This is useful for request tracking and correlation.

Source

pub async fn list_resources(&self) -> Result<Vec<Resource>>

List available resources from the server.

§Example
let resources = client.list_resources().await?;
for resource in &resources {
    println!("Resource: {} ({}) - {}",
        resource.name, resource.uri, resource.mime_type);
}
Source

pub async fn read_resource(&self, uri: &str) -> Result<ResourceContent>

Read a resource by URI.

§Example
let content = client.read_resource("file:///etc/hosts").await?;
match content.content {
    Content::Text(text) => println!("{}", text),
    Content::Blob(data) => println!("Binary: {} bytes", data.len()),
}
Source

pub async fn subscribe(&self, uri: &str) -> Result<ResourceStream>

Subscribe to resource updates.

Returns a stream that yields resource content updates.

§Example
let stream = client.subscribe("file:///var/log/app.log").await?;
while let Some(content) = stream.next().await? {
    println!("Update: {:?}", content);
}
Source

pub async fn list_prompts(&self) -> Result<Vec<Prompt>>

List available prompts from the server.

§Example
let prompts = client.list_prompts().await?;
for prompt in &prompts {
    println!("Prompt: {} - {}", prompt.name, prompt.description);
}
Source

pub async fn get_prompt( &self, name: &str, args: &[(&str, &str)], ) -> Result<Vec<PromptMessage>>

Get a prompt with the given arguments.

§Arguments
  • name - The name of the prompt
  • args - Key-value pairs for prompt arguments
§Example
let messages = client.get_prompt("code_review", &[
    ("language", "rust"),
    ("file", "main.rs"),
]).await?;
for msg in &messages {
    println!("{:?}: {:?}", msg.role, msg.content);
}
Source

pub async fn log( &self, level: LogLevel, message: &str, data: Option<Value>, ) -> Result<()>

Send a log message to the server.

§Arguments
  • level - Log level (debug, info, warn, error)
  • message - The log message
  • data - Optional structured data as JSON
§Example
use serde_json::json;

client.log(LogLevel::Info, "Operation completed", Some(json!({
    "duration_ms": 42,
    "items_processed": 100
}))).await?;
Source

pub async fn disconnect(self) -> Result<()>

Disconnect from the server gracefully.

This will complete any pending requests before closing the connection.

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl !Send for Client

§

impl !Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more