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:
- Service Cancellation: rmcp’s
DropGuardcancels the internalCancellationToken - Loop Exit: Service loop detects cancellation and exits gracefully
- 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)
- 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 errorsImplementations§
Source§impl KodegenConnection
impl KodegenConnection
Sourcepub fn from_service(service: RunningService<RoleClient, ClientInfo>) -> Self
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.
Sourcepub fn client(&self) -> KodegenClient
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.
Sourcepub async fn close(self) -> Result<(), ClientError>
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
- Cancels the service task via
CancellationToken - Service loop exits and calls
transport.close() - For stdio transports: stdin closed, 3-second wait, force kill if needed
- 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
Sourcepub async fn wait(self) -> Result<(), ClientError>
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.