pub struct McpClient<T: McpTransport> { /* private fields */ }mcp only.Expand description
MCP client for communicating with MCP servers.
The client handles the MCP protocol, including initialization, tool discovery, and tool execution.
§Example
use agent_sdk::mcp::{McpClient, StdioTransport};
// Spawn server and create client
let transport = StdioTransport::spawn("npx", &["-y", "mcp-server"]).await?;
let client = McpClient::new(transport, "my-server".to_string()).await?;
// List available tools
let tools = client.list_tools().await?;
// Call a tool
let result = client.call_tool("tool_name", json!({"arg": "value"})).await?;Implementations§
Source§impl<T: McpTransport> McpClient<T>
impl<T: McpTransport> McpClient<T>
Sourcepub const fn new_uninitialized(transport: Arc<T>, server_name: String) -> Self
pub const fn new_uninitialized(transport: Arc<T>, server_name: String) -> Self
Create a client without initialization.
Use this if you need to control when initialization happens.
Sourcepub async fn initialize(&mut self) -> Result<&InitializeResult>
pub async fn initialize(&mut self) -> Result<&InitializeResult>
Initialize the MCP connection.
This must be called before using other methods.
§Errors
Returns an error if the server rejects initialization.
Sourcepub fn server_name(&self) -> &str
pub fn server_name(&self) -> &str
Get the server name.
Sourcepub const fn server_info(&self) -> Option<&InitializeResult>
pub const fn server_info(&self) -> Option<&InitializeResult>
Get server info if initialized.
Sourcepub fn protocol_version(&self) -> Option<&str>
pub fn protocol_version(&self) -> Option<&str>
The MCP protocol revision negotiated with the server.
Returns None until McpClient::initialize has completed. This is
the revision the server selected, which may be older than
PREFERRED_PROTOCOL_VERSION if the server is on a legacy build.
Sourcepub async fn list_tools(&self) -> Result<Vec<McpToolDefinition>>
pub async fn list_tools(&self) -> Result<Vec<McpToolDefinition>>
Sourcepub async fn call_tool_raw(
&self,
name: &str,
arguments: Option<Value>,
) -> Result<McpToolCallResult>
pub async fn call_tool_raw( &self, name: &str, arguments: Option<Value>, ) -> Result<McpToolCallResult>
Sourcepub async fn list_resources(&self) -> Result<Vec<McpResource>>
pub async fn list_resources(&self) -> Result<Vec<McpResource>>
List resources exposed by the server (resources/list).
Resources are addressable data (files, database rows, API payloads) the
server makes available for reading. Returns an empty list if the server
did not advertise the resources capability.
§Errors
Returns an error if the request fails or the response cannot be parsed.
Sourcepub async fn read_resource(&self, uri: &str) -> Result<ResourceReadResult>
pub async fn read_resource(&self, uri: &str) -> Result<ResourceReadResult>
Read a resource by URI (resources/read).
§Errors
Returns an error if the request fails or the response cannot be parsed.
Sourcepub async fn list_prompts(&self) -> Result<Vec<McpPrompt>>
pub async fn list_prompts(&self) -> Result<Vec<McpPrompt>>
List prompts exposed by the server (prompts/list).
Returns an empty list if the server did not advertise the prompts
capability.
§Errors
Returns an error if the request fails or the response cannot be parsed.
Sourcepub async fn get_prompt(
&self,
name: &str,
arguments: Option<Value>,
) -> Result<PromptGetResult>
pub async fn get_prompt( &self, name: &str, arguments: Option<Value>, ) -> Result<PromptGetResult>
Sourcepub fn supports_resources(&self) -> bool
pub fn supports_resources(&self) -> bool
Whether the server advertised the resources capability.
Sourcepub fn supports_prompts(&self) -> bool
pub fn supports_prompts(&self) -> bool
Whether the server advertised the prompts capability.