KodegenConnection

Struct KodegenConnection 

Source
pub struct KodegenConnection { /* private fields */ }
Expand description

Connection lifecycle manager for MCP client

Manages the underlying connection and provides graceful shutdown. NOT Clone - only one owner should manage the connection lifecycle.

The connection should be held as long as you want the MCP connection to remain active.

§Cleanup Behavior

When dropped or when close() is called, the following cleanup occurs:

  1. Service Cancellation: rmcp’s DropGuard cancels the internal CancellationToken
  2. Loop Exit: Service loop detects cancellation and exits gracefully
  3. Transport Close: For stdio connections, TokioChildProcess.graceful_shutdown() executes:
    • Closes stdin to signal server exit
    • Waits up to 3 seconds for graceful termination
    • Force kills process if timeout exceeded (SIGKILL/TerminateProcess)
  4. Zombie Prevention: tokio::process::Child.wait() reaps the process

Both drop(connection) and connection.close().await trigger the same cleanup flow. Use close() if you need to await and handle cleanup errors; use drop for fire-and-forget.

§Relationship to Clients

All client handles created from this connection become invalid when the connection is dropped:

let (client, conn) = create_http_client(url).await?;
let client2 = client.clone();

drop(conn);  // Closes connection

// Both clients now fail:
client.list_tools().await?;  // Error
client2.call_tool(...).await?;  // Error

§Example

// Implicit cleanup via drop
{
    let (client, conn) = create_stdio_client("node", &["server.js"]).await?;
    // ... use client ...
} // Process terminated here via graceful shutdown

// Explicit cleanup with error handling
let (client, conn) = create_stdio_client("node", &["server.js"]).await?;
// ... use client ...
conn.close().await?;  // Await cleanup, handle errors

Implementations§

Source§

impl KodegenConnection

Source

pub fn from_service(service: RunningService<RoleClient, ClientInfo>) -> Self

Create connection from running service

This is a low-level constructor for creating a connection from an already-initialized MCP service. Most users should use the transport functions like create_http_client() which handle both service creation and connection setup.

Source

pub fn client(&self) -> KodegenClient

Get a clone-able client handle for MCP operations

This creates a lightweight client handle that can be cloned and shared. Multiple client handles can coexist and all operate on the same underlying connection.

Source

pub async fn close(self) -> Result<(), ClientError>

Graceful shutdown with proper MCP protocol cancellation

Consumes the connection and performs a clean shutdown of the MCP protocol. This triggers the same cleanup as dropping the connection, but allows you to await completion and handle any errors.

§Cleanup Sequence
  1. Cancels the service task via CancellationToken
  2. Service loop exits and calls transport.close()
  3. For stdio transports: stdin closed, 3-second wait, force kill if needed
  4. Process reaped to prevent zombies
§Drop vs Close
  • drop(connection): Fire-and-forget cleanup (cleanup errors logged but not returned)
  • connection.close().await: Awaitable cleanup (returns errors to caller)
§Errors

Returns ClientError if:

  • Service cancellation fails
  • Transport close fails (e.g., process kill error)
  • Service task panicked
Source

pub async fn wait(self) -> Result<(), ClientError>

Wait for the connection to close naturally

This will block until the remote side closes the connection or an error occurs. Useful for long-lived connections where you want to keep the connection alive until the remote end terminates it.

§Errors

Returns ClientError if the connection fails or closes with an error.

Auto Trait Implementations§

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<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