Skip to main content

McpConnection

Struct McpConnection 

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

A live connection to a single MCP server, wrapping an rmcp::service::RunningService.

Implementations§

Source§

impl McpConnection

Source

pub async fn connect(config: &McpServerConfig) -> Result<Self, McpError>

Connects to an MCP server, performs the rmcp initialize handshake, and returns a ready-to-use connection. No sampling / elicitation / roots responders are wired; use Self::connect_with_handler when the server may issue those requests.

Source

pub async fn connect_with_handler( config: &McpServerConfig, handler_config: McpHandlerConfig, ) -> Result<Self, McpError>

Connects to an MCP server with a fully configured McpHandlerConfig.

Source

pub fn from_running_service( server_id: impl Into<McpServerId>, service: RunningService<RoleClient, McpClientHandler>, notifications: UnboundedReceiver<McpServerNotification>, ) -> Self

Adopts an externally constructed rmcp::service::RunningService as an McpConnection.

Use this when you need a transport rmcp supports but McpTransportBinding does not (in-memory pipes for tests, websockets, custom IO). Pair the service with the notification receiver returned by McpHandlerConfig::build so list-change notifications stay observable.

The connection has no McpServerConfig attached, so reconnect-on-auth is unavailable; resolve_auth only updates stored credentials in this mode. Server-pushed events from the underlying handler are not forwarded to subscribers — use Self::from_running_service_with_events paired with the broadcast sender from McpClientChannels when you need event delivery.

Source

pub fn from_running_service_with_events( server_id: impl Into<McpServerId>, service: RunningService<RoleClient, McpClientHandler>, notifications: UnboundedReceiver<McpServerNotification>, events: Sender<McpServerEvent>, ) -> Self

Variant of Self::from_running_service that wires the broadcast sender returned by McpHandlerConfig::build (or build_with) so Self::subscribe_events receivers observe the same stream the handler is publishing into.

Source

pub fn from_running_service_with_events_and_handler_config( server_id: impl Into<McpServerId>, service: RunningService<RoleClient, McpClientHandler>, notifications: UnboundedReceiver<McpServerNotification>, events: Sender<McpServerEvent>, handler_config: McpHandlerConfig, ) -> Self

Variant of Self::from_running_service_with_events that also preserves the handler config used to build the adopted service.

Use this when the connection needs to reach client-side hooks that are not carried by the rmcp handler itself, such as McpAuthResponder and McpErrorResponder during McpToolAdapter invocation.

Source

pub fn server_id(&self) -> &McpServerId

Returns the McpServerId for this connection.

Source

pub fn capabilities(&self) -> &McpServerCapabilities

Returns the capabilities advertised by the server during initialize.

Source

pub fn handler_config(&self) -> &McpHandlerConfig

Returns the McpHandlerConfig this connection was built with. Used by McpToolAdapter to reach the registered McpAuthResponder when an auth challenge surfaces.

Source

pub fn subscribe_events(&self) -> Receiver<McpServerEvent>

Subscribes to the per-connection McpServerEvent broadcast.

Receivers buffer up to events_capacity (configured via McpHandlerConfig::with_events_capacity, defaults to DEFAULT_EVENTS_CAPACITY) before slow consumers are signalled with broadcast::error::RecvError::Lagged. Catalog *ListChanged events are also delivered through the legacy McpServerNotification receiver consumed by McpServerManager::refresh_changed_catalogs.

Source

pub async fn subscribe_resource( &self, uri: impl Into<String>, ) -> Result<(), McpError>

Subscribes to notifications/resources/updated for the given URI.

Updates surface as McpServerEvent::ResourceUpdated on every receiver returned by Self::subscribe_events.

Source

pub async fn unsubscribe_resource( &self, uri: impl Into<String>, ) -> Result<(), McpError>

Cancels a previous Self::subscribe_resource subscription.

Source

pub async fn set_logging_level( &self, level: McpLoggingLevel, ) -> Result<(), McpError>

Negotiates the minimum severity the server should emit through notifications/message. Surfaced as McpServerEvent::Logging.

Source

pub async fn notify_cancelled( &self, params: McpCancelledNotificationParam, ) -> Result<(), McpError>

Sends a notifications/cancelled to the server, asking it to stop processing a previously issued request.

Source

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

Notifies the server that the client’s roots list has changed; servers may respond by re-issuing roots/list.

Source

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

Gracefully closes the underlying rmcp service.

For Streamable HTTP this drives the rmcp transport to issue a DELETE against the negotiated session, releasing server-side state.

Source

pub async fn resolve_auth( &self, resolution: AuthResolution, ) -> Result<(), McpError>

Stores or clears authentication credentials and, when configured to do so via McpServerConfig, reconnects to apply them.

Source

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

Discovers tools, resources, and prompts that the server advertised.

Source

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

Lists all tools advertised by the connected MCP server.

Source

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

Lists all resources advertised by the connected MCP server.

Source

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

Lists all prompts advertised by the connected MCP server.

Source

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

Invokes a tool on the MCP server.

Returns the typed CallToolResult — the Vec<Content> block list, the optional structured_content field, and the is_error flag are all preserved. Adapters convert this into agentkit ToolOutput/[InvocableOutput] at the boundary.

Source

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

Reads a resource from the MCP server by URI.

Returns the typed ReadResourceResult — the full Vec<McpResourceContents> is preserved (text vs blob, mime types, metadata). Use McpResourceHandle for the agentkit ResourceProvider view that collapses to a single inline DataRef.

Source

pub async fn get_prompt( &self, name: &str, arguments: Value, ) -> Result<GetPromptResult, McpError>

Retrieves a prompt from the MCP server, rendering it with the given arguments.

Returns the typed GetPromptResult — message role and content blocks (text/image/audio/embedded resource) are preserved. Use McpPromptHandle for the collapsed agentkit PromptProvider view.

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: Sized + 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: Sized + 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> 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