Implementation:AI Shell Team Aish client connect
| Knowledge Sources | |
|---|---|
| Domains | MCP, Protocol |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete tool for connecting to an MCP server with transport startup, handshake, and event loop provided by the aish-mcp crate.
Description
The client::connect function establishes a connection to an MCP server. It selects a transport (stdio or HTTP) based on the server settings, starts an event loop that processes transport events and answers server-to-client requests during the handshake, and runs the `initialize` JSON-RPC request to negotiate the protocol version.
The handshake is bounded by a 30-second connect deadline. A failed handshake closes the transport and aborts the event loop, so no subprocess or session is leaked. The Connection object carries the transport, server info, protocol version, and the event loop task handle.
Usage
Call this function from the driver task when connecting to an MCP server. The returned connection is used for tool listing and tool calls.
Code Reference
Source Location
- Repository: aish
- File: crates/aish-mcp/src/client.rs
Signature
pub async fn connect(
settings: &ServerSettings,
options: ConnectOptions,
) -> Result<Connection, McpError>
pub struct ConnectOptions {
pub roots: Vec<String>,
pub cancel: Option<Arc<CancelListener>>,
pub events: mpsc::UnboundedSender<ServerEvent>,
}
pub struct Connection {
// transport, server_info, protocol_version, events_task
}
pub enum ServerEvent {
Notification { method: String, params: serde_json::Value },
Closed { error: Option<McpError> },
}
Import
use aish_mcp::client::{connect, ConnectOptions, Connection, ServerEvent};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| settings | &ServerSettings | Yes | Transport type (Stdio/Http), command/URL, env |
| options | ConnectOptions | Yes | Roots, cancel token, event channel |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Result<Connection, McpError> | Live connection with protocol version and server info; Err on handshake failure |
Usage Examples
Connecting to a Server
use aish_mcp::client::{connect, ConnectOptions};
use aish_mcp::cancel::CancelListener;
let (events_tx, mut events_rx) = mpsc::unbounded_channel();
let cancel = Arc::new(CancelListener::new());
let options = ConnectOptions {
roots: vec!["/home/user/project".into()],
cancel: Some(cancel),
events: events_tx,
};
let connection = connect(&server_settings, options).await?;
println!("Connected: {} (protocol {})", connection.server_info.name, connection.protocol_version);