pub struct Client {
pub http_client: Client,
pub user_agent: String,
pub x_title: String,
pub http_referer: String,
pub connect_timeout: Duration,
pub backoff_current_interval: Duration,
pub backoff_initial_interval: Duration,
pub backoff_randomization_factor: f64,
pub backoff_multiplier: f64,
pub backoff_max_interval: Duration,
pub backoff_max_elapsed_time: Duration,
pub call_timeout: Duration,
}Expand description
Client for creating MCP connections.
Holds shared configuration (HTTP client, headers, backoff parameters)
and creates Connection instances via
connect.
Fields§
§http_client: ClientHTTP client for making requests.
user_agent: StringUser-Agent header value.
x_title: StringX-Title header value.
http_referer: StringReferer header value.
connect_timeout: DurationTimeout for the initial connection (initialize request).
backoff_current_interval: DurationCurrent backoff interval for retry logic.
backoff_initial_interval: DurationInitial backoff interval for retry logic.
backoff_randomization_factor: f64Randomization factor for backoff jitter.
backoff_multiplier: f64Multiplier for exponential backoff growth.
backoff_max_interval: DurationMaximum backoff interval.
backoff_max_elapsed_time: DurationMaximum total time to spend on retries.
call_timeout: DurationTimeout for individual RPC calls after connection is established.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(
http_client: Client,
user_agent: String,
x_title: String,
http_referer: String,
connect_timeout: Duration,
backoff_current_interval: Duration,
backoff_initial_interval: Duration,
backoff_randomization_factor: f64,
backoff_multiplier: f64,
backoff_max_interval: Duration,
backoff_max_elapsed_time: Duration,
call_timeout: Duration,
) -> Self
pub fn new( http_client: Client, user_agent: String, x_title: String, http_referer: String, connect_timeout: Duration, backoff_current_interval: Duration, backoff_initial_interval: Duration, backoff_randomization_factor: f64, backoff_multiplier: f64, backoff_max_interval: Duration, backoff_max_elapsed_time: Duration, call_timeout: Duration, ) -> Self
Creates a new MCP client.
Sourcepub async fn connect(
&self,
url: String,
session_id: Option<String>,
headers: Option<IndexMap<String, String>>,
) -> Result<Connection, Error>
pub async fn connect( &self, url: String, session_id: Option<String>, headers: Option<IndexMap<String, String>>, ) -> Result<Connection, Error>
Connects to an MCP server using the Streamable HTTP transport.
Sends an initialize JSON-RPC request to the server and extracts
the Mcp-Session-Id from the response. Returns a [Connection]
that can be used to list/call tools and list/read resources.
headers are forwarded on every request this connection makes
to the upstream — both the initial initialize POST and every
subsequent RPC. The client merges its own defaults
(User-Agent, X-Title, Referer, HTTP-Referer) into this
map, but caller-supplied values for any of those win on
conflict. Authorization (when needed) is just another entry
in headers. The Mcp-Session-Id header is reserved — pass it
via session_id instead so the explicit argument can never be
clobbered by the headers map.
§SSE handoff
Accept is text/event-stream, application/json — stream first
— so the server is encouraged to keep the underlying connection
open. If the response comes back as SSE we read the initialize
event off the stream and hand the still-open line reader to the
returned [Connection]’s list-changed listener. The listener
starts reading from that pre-opened stream immediately, which
closes the race where a peer (e.g. an in-process rmcp upstream)
would broadcast notifications/tools/list_changed before our
listener had managed to open its own GET / SSE.
If the response is unary JSON and the server advertises either
tools.list_changed or resources.list_changed, we proactively
open a GET / SSE stream before returning and hand it to the
listener for the same reason. If neither capability is set, no
listener is needed and we return without touching SSE.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more