pub struct McpConnection { /* private fields */ }Expand description
A live connection to a single MCP server.
Handles JSON-RPC request/response framing, automatic auth enrichment, and high-level methods for tool calls, resource reads, prompt retrieval, and capability discovery.
Create a connection with McpConnection::connect or indirectly through
McpServerManager::connect_server.
§Example
use agentkit_mcp::{McpConnection, McpServerConfig, McpTransportBinding, StdioTransportConfig};
let config = McpServerConfig::new(
"filesystem",
McpTransportBinding::Stdio(StdioTransportConfig::new("npx")
.with_arg("-y")
.with_arg("@modelcontextprotocol/server-filesystem")),
);
let connection = McpConnection::connect(&config).await?;
let snapshot = connection.discover().await?;
println!("found {} tools", snapshot.tools.len());Implementations§
Source§impl McpConnection
impl McpConnection
Sourcepub async fn connect(config: &McpServerConfig) -> Result<Self, McpError>
pub async fn connect(config: &McpServerConfig) -> Result<Self, McpError>
Connects to an MCP server, performs the JSON-RPC initialize handshake, and
returns a ready-to-use connection.
§Errors
Returns McpError if the transport fails to connect, the handshake is
rejected, or the server requires authentication (McpError::AuthRequired).
Sourcepub fn server_id(&self) -> &McpServerId
pub fn server_id(&self) -> &McpServerId
Returns the McpServerId for this connection.
Sourcepub async fn resolve_auth(
&self,
resolution: AuthResolution,
) -> Result<(), McpError>
pub async fn resolve_auth( &self, resolution: AuthResolution, ) -> Result<(), McpError>
Stores or clears authentication credentials for future requests on this connection.
After calling this method with AuthResolution::Provided, every subsequent
JSON-RPC request will include the credentials in an auth field.
§Errors
Returns McpError if the resolution cannot be applied.
Sourcepub async fn discover(&self) -> Result<McpDiscoverySnapshot, McpError>
pub async fn discover(&self) -> Result<McpDiscoverySnapshot, McpError>
Performs full capability discovery by listing tools, resources, and prompts.
Returns an McpDiscoverySnapshot containing everything the server advertises.
§Errors
Returns McpError if any of the list requests fail.
Sourcepub async fn list_tools(&self) -> Result<Vec<McpToolDescriptor>, McpError>
pub async fn list_tools(&self) -> Result<Vec<McpToolDescriptor>, McpError>
Sourcepub async fn list_resources(
&self,
) -> Result<Vec<McpResourceDescriptor>, McpError>
pub async fn list_resources( &self, ) -> Result<Vec<McpResourceDescriptor>, McpError>
Sourcepub async fn list_prompts(&self) -> Result<Vec<McpPromptDescriptor>, McpError>
pub async fn list_prompts(&self) -> Result<Vec<McpPromptDescriptor>, McpError>
Sourcepub async fn call_tool(
&self,
name: &str,
arguments: Value,
) -> Result<Value, McpError>
pub async fn call_tool( &self, name: &str, arguments: Value, ) -> Result<Value, McpError>
Invokes a tool on the MCP server and returns the raw JSON result.
§Arguments
name- The tool name as it appears in the server’s tool list.arguments- A JSON value matching the tool’s input schema.
§Errors
Returns McpError::AuthRequired if the server demands authentication,
or another McpError variant on transport or protocol failures.
Sourcepub async fn read_resource(
&self,
uri: &str,
) -> Result<ResourceContents, McpError>
pub async fn read_resource( &self, uri: &str, ) -> Result<ResourceContents, McpError>
Sourcepub async fn get_prompt(
&self,
name: &str,
arguments: Value,
) -> Result<PromptContents, McpError>
pub async fn get_prompt( &self, name: &str, arguments: Value, ) -> Result<PromptContents, McpError>
Retrieves a prompt from the MCP server, rendering it with the given arguments.
§Arguments
name- The prompt name as it appears in the server’s prompt list.arguments- A JSON value containing the prompt’s input arguments.
§Errors
Returns McpError if the prompt cannot be retrieved or the response is malformed.
Sourcepub async fn replay_auth_operation(
&self,
operation: &AuthOperation,
) -> Result<McpOperationResult, McpError>
pub async fn replay_auth_operation( &self, operation: &AuthOperation, ) -> Result<McpOperationResult, McpError>
Replays an MCP operation that previously failed with an auth challenge.
This is called after credentials have been resolved via resolve_auth.
The operation is re-issued with the stored credentials attached.
§Errors
Returns McpError::AuthResolution if the operation targets a different server,
or other McpError variants if the replayed operation itself fails.