pub struct Connection { /* private fields */ }Expand description
An active connection to an MCP server using the Streamable HTTP transport.
Cheaply clonable (one Arc bump). When the last clone is dropped, the
inner Arc ref count hits zero, [ConnectionInner::Drop] runs, the
listener-cancel DropGuard is dropped, and the SSE listener task is
cancelled — exiting any in-flight lines.next_line(), reconnect
send(), or backoff sleep immediately without retrying against
the now-dead proxy session.
Use the public methods (list_tools, call_tool, list_resources,
read_resource, call_tool_as_message, tool_key) for the upstream
MCP protocol surface. The inner state (ConnectionInner) is also
reachable via Deref for read-only field access (e.g.
connection.url, connection.initialize_result.server_info.name),
but its methods are private — you must go through Connection.
Implementations§
Source§impl Connection
impl Connection
Sourcepub fn tool_key(&self) -> String
pub fn tool_key(&self) -> String
Returns a key identifying this connection for tool namespacing.
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
Returns the session ID for this connection.
Sourcepub async fn list_tools(&self) -> Result<Arc<Vec<Tool>>, Arc<Error>>
pub async fn list_tools(&self) -> Result<Arc<Vec<Tool>>, Arc<Error>>
Returns all tools from the upstream server.
Sourcepub async fn call_tool(
&self,
params: &CallToolRequestParams,
) -> Result<CallToolResult, Error>
pub async fn call_tool( &self, params: &CallToolRequestParams, ) -> Result<CallToolResult, Error>
Calls a tool on the upstream server.
Sourcepub async fn call_tool_as_message(
&self,
params: &CallToolRequestParams,
tool_call_id: String,
) -> Result<ToolMessage, Error>
pub async fn call_tool_as_message( &self, params: &CallToolRequestParams, tool_call_id: String, ) -> Result<ToolMessage, Error>
Calls a tool and converts the result into a [ToolMessage].
Sourcepub async fn list_resources(&self) -> Result<Arc<Vec<Resource>>, Arc<Error>>
pub async fn list_resources(&self) -> Result<Arc<Vec<Resource>>, Arc<Error>>
Returns all resources from the upstream server.
Sourcepub async fn subscribe_tools(
&self,
current: &[Tool],
timeout: Duration,
) -> Result<Arc<Vec<Tool>>, Arc<Error>>
pub async fn subscribe_tools( &self, current: &[Tool], timeout: Duration, ) -> Result<Arc<Vec<Tool>>, Arc<Error>>
Returns the cached tool list as soon as it differs from current,
or waits up to timeout for the next notifications/tools/list_changed
from the upstream server before re-reading.
Wakes the moment a refresh writer takes the cache write lock, so
the post-wake read is guaranteed to observe the new list rather
than racing against the install. Safe to call from any number of
tasks concurrently.
Sourcepub async fn subscribe_resources(
&self,
current: &[Resource],
timeout: Duration,
) -> Result<Arc<Vec<Resource>>, Arc<Error>>
pub async fn subscribe_resources( &self, current: &[Resource], timeout: Duration, ) -> Result<Arc<Vec<Resource>>, Arc<Error>>
Resource counterpart of Connection::subscribe_tools.
Sourcepub async fn read_resource(
&self,
uri: &str,
) -> Result<ReadResourceResult, Error>
pub async fn read_resource( &self, uri: &str, ) -> Result<ReadResourceResult, Error>
Reads a resource from the upstream server.
Sourcepub fn set_on_tools_list_changed<F>(&self, callback: F)
pub fn set_on_tools_list_changed<F>(&self, callback: F)
Register a callback to fire whenever the upstream emits
notifications/tools/list_changed.
Timing: the callback runs after the tool cache’s write lock
is acquired but before the network paginate that replaces it.
That means readers blocked on the read lock won’t return until the
new list is in place, and the callback observes the moment the
staleness window opens. The proxy uses this to emit its own
notifications/tools/list_changed to downstream clients at the
right instant.
Replaces any previously-registered tools-list-changed callback.
All clones of this Connection share the same callback slot.
Sourcepub fn set_on_resources_list_changed<F>(&self, callback: F)
pub fn set_on_resources_list_changed<F>(&self, callback: F)
Register a callback to fire whenever the upstream emits
notifications/resources/list_changed. Same timing contract as
Connection::set_on_tools_list_changed.
Replaces any previously-registered resources-list-changed callback.
All clones of this Connection share the same callback slot.
Trait Implementations§
Source§impl Clone for Connection
impl Clone for Connection
Source§impl Debug for Connection
impl Debug for Connection
Source§impl Deref for Connection
impl Deref for Connection
Source§type Target = ConnectionInner
type Target = ConnectionInner
Source§fn deref(&self) -> &ConnectionInner
fn deref(&self) -> &ConnectionInner
Auto Trait Implementations§
impl Freeze for Connection
impl !RefUnwindSafe for Connection
impl Send for Connection
impl Sync for Connection
impl Unpin for Connection
impl UnsafeUnpin for Connection
impl !UnwindSafe for Connection
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