pub struct ConnectionRefresher<S, F>{ /* private fields */ }mcp and tools only.Expand description
Connection refresher that wraps an MCP client and handles automatic reconnection.
This is similar to adk-go’s connectionRefresher struct. It transparently
retries operations after reconnecting when the underlying session fails.
§Type Parameters
S- The service type for the MCP clientF- The factory type for creating new connections
§Example
use adk_tool::mcp::{ConnectionRefresher, ConnectionFactory};
struct MyFactory { /* ... */ }
#[async_trait::async_trait]
impl ConnectionFactory<MyService> for MyFactory {
async fn create_connection(&self) -> Result<RunningService<RoleClient, MyService>, String> {
// Create and return a new connection
}
}
let refresher = ConnectionRefresher::new(initial_client, Arc::new(factory));
// Read-only discovery automatically retries on connection failure.
let tools = refresher.list_tools().await?;
// Tool calls are not replayed unless the caller explicitly opts in.
// The server chooses whether to answer inline or with a task, so the
// caller reads the response to find out which happened.
let response = refresher
.with_tool_call_retries()
.call_tool(params)
.await?
.value;
match response {
CallToolResponse::Complete(result) => println!("{result:?}"),
other => println!("not a plain result: {other:?}"),
}Implementations§
Source§impl<S, F> ConnectionRefresher<S, F>
impl<S, F> ConnectionRefresher<S, F>
Sourcepub fn new(
client: RunningService<RoleClient, S>,
factory: Arc<F>,
) -> ConnectionRefresher<S, F>
pub fn new( client: RunningService<RoleClient, S>, factory: Arc<F>, ) -> ConnectionRefresher<S, F>
Create a new connection refresher with an initial client and factory.
§Arguments
client- The initial MCP client connectionfactory- Factory for creating new connections when needed
Sourcepub fn lazy(factory: Arc<F>) -> ConnectionRefresher<S, F>
pub fn lazy(factory: Arc<F>) -> ConnectionRefresher<S, F>
Create a new connection refresher without an initial connection.
The first operation will trigger a connection.
Sourcepub fn with_config(self, config: RefreshConfig) -> ConnectionRefresher<S, F>
pub fn with_config(self, config: RefreshConfig) -> ConnectionRefresher<S, F>
Set the refresh configuration.
Sourcepub fn with_max_attempts(self, attempts: u32) -> ConnectionRefresher<S, F>
pub fn with_max_attempts(self, attempts: u32) -> ConnectionRefresher<S, F>
Set the maximum number of reconnection attempts.
Sourcepub fn with_tool_call_retries(self) -> ConnectionRefresher<S, F>
pub fn with_tool_call_retries(self) -> ConnectionRefresher<S, F>
Allow tools/call to be replayed after reconnecting.
A transport failure after request transmission is an ambiguous outcome: a mutating tool may have completed its external effect before the response was lost. Enable this only for read-only tools or operations protected by a stable provider idempotency guarantee.
§Example
let refresher = ConnectionRefresher::new(client, Arc::new(factory))
.with_tool_call_retries();Sourcepub async fn list_tools(&self) -> Result<RetryResult<Vec<Tool>>, String>
pub async fn list_tools(&self) -> Result<RetryResult<Vec<Tool>>, String>
List all tools from the MCP server with automatic reconnection.
Handles pagination internally and restarts from scratch if reconnection occurs (per MCP spec, cursors don’t persist across sessions).
Sourcepub async fn call_tool(
&self,
params: CallToolRequestParams,
) -> Result<RetryResult<CallToolResponse>, String>
pub async fn call_tool( &self, params: CallToolRequestParams, ) -> Result<RetryResult<CallToolResponse>, String>
Call a tool on the MCP server.
Tool calls are not replayed by default after an ambiguous connection
failure. Use Self::with_tool_call_retries only when the operation is
known to be replay-safe.
Sourcepub async fn cancellation_token(
&self,
) -> Option<RunningServiceCancellationToken>
pub async fn cancellation_token( &self, ) -> Option<RunningServiceCancellationToken>
Get the cancellation token for the current connection.
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Check if currently connected.
Auto Trait Implementations§
impl<S, F> !RefUnwindSafe for ConnectionRefresher<S, F>
impl<S, F> !UnwindSafe for ConnectionRefresher<S, F>
impl<S, F> Freeze for ConnectionRefresher<S, F>
impl<S, F> Send for ConnectionRefresher<S, F>
impl<S, F> Sync for ConnectionRefresher<S, F>
impl<S, F> Unpin for ConnectionRefresher<S, F>
impl<S, F> UnsafeUnpin for ConnectionRefresher<S, F>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.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 moreimpl<T> MaybeSend for Twhere
T: Send,
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.