Skip to main content

McpConnection

Struct McpConnection 

Source
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

Source

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).

Source

pub fn server_id(&self) -> &McpServerId

Returns the McpServerId for this connection.

Source

pub async fn close(&self) -> Result<(), McpError>

Closes the underlying transport, shutting down the connection to the server.

§Errors

Returns McpError if the transport cannot be closed cleanly.

Source

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.

Source

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.

Source

pub async fn list_tools(&self) -> Result<Vec<McpToolDescriptor>, McpError>

Lists all tools advertised by the connected MCP server.

§Errors

Returns McpError if the tools/list request fails.

Source

pub async fn list_resources( &self, ) -> Result<Vec<McpResourceDescriptor>, McpError>

Lists all resources advertised by the connected MCP server.

§Errors

Returns McpError if the resources/list request fails.

Source

pub async fn list_prompts(&self) -> Result<Vec<McpPromptDescriptor>, McpError>

Lists all prompts advertised by the connected MCP server.

§Errors

Returns McpError if the prompts/list request fails.

Source

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.

Source

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

Reads a resource from the MCP server by URI.

§Arguments
  • uri - The resource URI (e.g. "file:///tmp/example.txt").
§Errors

Returns McpError if the resource cannot be read or the response is malformed.

Source

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.

Source

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.

Auto Trait Implementations§

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<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