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
impl McpClient
Sourcepub async fn connect(
command: &str,
args: &[&str],
env: &BTreeMap<String, String>,
) -> Result<McpClient, Error>
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.
Sourcepub async fn connect_http(
url: &str,
headers: &BTreeMap<String, String>,
network_policy: Option<&NetworkPolicy>,
) -> Result<McpClient, Error>
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).
Sourcepub async fn connect_sse(
url: &str,
headers: &BTreeMap<String, String>,
network_policy: Option<&NetworkPolicy>,
) -> Result<McpClient, Error>
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.
Sourcepub async fn reconnect(&self) -> Result<McpClient, Error>
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).
Sourcepub fn set_elicitation_handler(
&mut self,
handler: Arc<dyn McpElicitationHandler>,
)
pub fn set_elicitation_handler( &mut self, handler: Arc<dyn McpElicitationHandler>, )
Install a non-default elicitation handler (e.g. a tui integration).
Sourcepub fn set_timeout(&mut self, timeout: Duration)
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.
Sourcepub fn take_pending_notifications(&self) -> Vec<Value>
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.
Sourcepub async fn list_tools(&mut self) -> Result<Vec<McpToolDef>, Error>
pub async fn list_tools(&mut self) -> Result<Vec<McpToolDef>, Error>
List the tools the server offers.
Sourcepub async fn call_tool(
&mut self,
name: &str,
arguments: Value,
) -> Result<String, Error>
pub async fn call_tool( &mut self, name: &str, arguments: Value, ) -> Result<String, Error>
Call a tool and return its text content.
Sourcepub async fn list_resources(&mut self) -> Result<Vec<McpResourceDef>, Error>
pub async fn list_resources(&mut self) -> Result<Vec<McpResourceDef>, Error>
List the resources the server offers.
Sourcepub async fn list_resource_templates(
&mut self,
) -> Result<Vec<McpResourceTemplateDef>, Error>
pub async fn list_resource_templates( &mut self, ) -> Result<Vec<McpResourceTemplateDef>, Error>
List the resource templates the server offers.
Sourcepub async fn read_resource(&mut self, uri: &str) -> Result<String, Error>
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.
Sourcepub async fn subscribe_resource(&mut self, uri: &str) -> Result<(), Error>
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.
Sourcepub async fn list_prompts(&mut self) -> Result<Vec<McpPromptDef>, Error>
pub async fn list_prompts(&mut self) -> Result<Vec<McpPromptDef>, Error>
List the prompts the server offers.
Sourcepub async fn get_prompt(
&mut self,
name: &str,
args: BTreeMap<String, String>,
) -> Result<String, Error>
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.