Skip to main content

McpClient

Struct McpClient 

Source
pub struct McpClient {
    pub instructions: Option<String>,
    /* private fields */
}
Expand description

A client connected to an MCP server over stdio, HTTP, or SSE — see the module doc comment for the transport model.

Fields§

§instructions: Option<String>

The instructions field from the server’s initialize response, if any (§2 module 15 D7 row 5 “instructions”). None when the server didn’t send one.

Implementations§

Source§

impl McpClient

Source

pub async fn connect( command: &str, args: &[&str], env: &BTreeMap<String, String>, ) -> Result<McpClient, Error>

Spawn command args... as an MCP server and perform the initialize handshake (stdio transport). env holds extra environment variables for the spawned process (from the server’s config env block, e.g. an API token an MCP server needs) — they’re set ON TOP OF supercode’s own inherited environment, never replacing it: tokio::process::Command inherits the parent’s environment by default (no .env_clear() here), and .envs(env) only adds/overrides the specific named vars. This matches Claude Code / Codex’s own env semantics for MCP servers.

Source

pub async fn connect_http( url: &str, headers: &BTreeMap<String, String>, network_policy: Option<&NetworkPolicy>, ) -> Result<McpClient, Error>

P5-2 (§2 module 15 D7 row 2 “remote HTTP”): connect over a single POST-per-request “Streamable HTTP” transport (the non-streaming case — see the module doc comment for what that scopes out). network_policy, if Some and enabled, is enforced against url BEFORE any connection is attempted (SSRF/domain-allowlist floor, same enforcement point ToolContext::check_network uses).

Source

pub async fn connect_sse( url: &str, headers: &BTreeMap<String, String>, network_policy: Option<&NetworkPolicy>, ) -> Result<McpClient, Error>

P5-2 (§2 module 15 D7 row 2 “remote SSE”): connect over the legacy (2024-11-05) HTTP+SSE transport — a persistent GET url stream whose first event names the client→server POST endpoint. Same NetworkPolicy enforcement as Self::connect_http.

Source

pub async fn reconnect(&self) -> Result<McpClient, Error>

Re-establish this client’s connection from its own remembered McpConnectParams AND its own remembered NetworkPolicy (see Self::network_policy’s field doc comment) — the “reconnect” half of “connection lifecycle, reconnect, timeouts” (§2 module 15 D7 row 1/2). Does NOT mutate self; the caller swaps in the returned client (and its tools/resources need re-wrapping, since a crate::tools::Tool closes over a specific Arc<Mutex<McpClient>>).

Security note (Fable-5 review, latent-SSRF-landmine finding): this method has no callers today (unwired public API) — but a future caller wiring it up gets the SAME NetworkPolicy enforcement the original connect_http/connect_sse applied for free, because the http/sse arms below pass self.network_policy (not None) through to connect_http/connect_sse, which run the exact same pre-connect host check + per-hop redirect re-check as the original connect. Passing None here would silently reconnect with no policy at all — the exact redirect-SSRF class those two constructors otherwise close (mcp_remote.rs’s reconnect_reuses_the_original_network_policy test fails on that revert).

Source

pub fn set_elicitation_handler( &mut self, handler: Arc<dyn McpElicitationHandler>, )

Install a non-default elicitation handler (e.g. a tui integration).

Source

pub fn set_timeout(&mut self, timeout: Duration)

Per-request timeout for the network transports (stdio is unaffected — see DEFAULT_MCP_TIMEOUT’s doc comment). Default 30s.

Source

pub fn take_pending_notifications(&self) -> Vec<Value>

Notifications received but not yet consumed by a caller (see Self::pending_notifications’s field doc comment). Draining (std::mem::take) rather than cloning — a caller that wants to peek without consuming should not call this.

Source

pub async fn list_tools(&mut self) -> Result<Vec<McpToolDef>, Error>

List the tools the server offers.

Source

pub async fn call_tool( &mut self, name: &str, arguments: Value, ) -> Result<String, Error>

Call a tool and return its text content.

Source

pub async fn list_resources(&mut self) -> Result<Vec<McpResourceDef>, Error>

List the resources the server offers.

Source

pub async fn list_resource_templates( &mut self, ) -> Result<Vec<McpResourceTemplateDef>, Error>

List the resource templates the server offers.

Source

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

Read one resource’s content by URI. Errors (fail-closed, named — hardening, see MCP_MAX_RESOURCE_BYTES’s doc comment) if the joined text exceeds the cap, rather than returning/buffering an unbounded string.

Source

pub async fn subscribe_resource(&mut self, uri: &str) -> Result<(), Error>

Subscribe to update notifications for one resource by URI — updates arrive as notifications/resources/updated frames, logged in McpClient::take_pending_notifications.

Source

pub async fn list_prompts(&mut self) -> Result<Vec<McpPromptDef>, Error>

List the prompts the server offers.

Source

pub async fn get_prompt( &mut self, name: &str, args: BTreeMap<String, String>, ) -> Result<String, Error>

Render a server prompt with args (a flat string->string map — the MCP spec’s prompts/get arguments shape) into the concatenated text of every returned message — this crate’s Config.prompts entries are likewise a single flat rendered string (crate::agent::Agent::expand_prompt’s local-template shape), so the two surfaces stay uniform to a caller.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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