Skip to main content

ConnectionRefresher

Struct ConnectionRefresher 

Source
pub struct ConnectionRefresher<S, F>
where S: Service<RoleClient> + Send + Sync + 'static, F: ConnectionFactory<S>,
{ /* private fields */ }
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 client
  • F - 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));

// Operations automatically retry on connection failure
let tools = refresher.list_tools().await?;
let result = refresher.call_tool(params).await?;

Implementations§

Source§

impl<S, F> ConnectionRefresher<S, F>
where S: Service<RoleClient> + Send + Sync + 'static, F: ConnectionFactory<S>,

Source

pub fn new(client: RunningService<RoleClient, S>, factory: Arc<F>) -> Self

Create a new connection refresher with an initial client and factory.

§Arguments
  • client - The initial MCP client connection
  • factory - Factory for creating new connections when needed
Source

pub fn lazy(factory: Arc<F>) -> Self

Create a new connection refresher without an initial connection.

The first operation will trigger a connection.

Source

pub fn with_config(self, config: RefreshConfig) -> Self

Set the refresh configuration.

Source

pub fn with_max_attempts(self, attempts: u32) -> Self

Set the maximum number of reconnection attempts.

Source

pub async fn list_tools(&self) -> Result<RetryResult<Vec<McpTool>>, 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).

Source

pub async fn call_tool( &self, params: CallToolRequestParams, ) -> Result<RetryResult<CallToolResult>, String>

Call a tool on the MCP server with automatic reconnection.

Source

pub async fn cancellation_token( &self, ) -> Option<RunningServiceCancellationToken>

Get the cancellation token for the current connection.

Source

pub async fn is_connected(&self) -> bool

Check if currently connected.

Source

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

Force a reconnection.

Source

pub async fn close(&self)

Close the connection.

Auto Trait Implementations§

§

impl<S, F> Freeze for ConnectionRefresher<S, F>

§

impl<S, F> !RefUnwindSafe 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>

§

impl<S, F> !UnwindSafe for ConnectionRefresher<S, F>

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
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