Principle:AI Shell Team Aish MCP Connection Handshake
| Knowledge Sources | |
|---|---|
| Domains | MCP, Protocol |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Pattern for connecting to an MCP server by starting its transport, running the JSON-RPC initialize handshake, and returning a live connection with protocol version and server capabilities.
Description
MCP Connection Handshake is the mechanism that establishes a connection to an MCP server. The handshake selects a transport (stdio child process or HTTP endpoint) based on the server settings, starts an event loop that processes transport events and republishes them as server events, and runs the `initialize` JSON-RPC request to negotiate the protocol version and exchange server/client capabilities.
The handshake is bounded by a connect deadline (30 seconds) even when the configured request timeout is zero (the documented way to disable client-side deadlines). A failed handshake closes the transport before returning, so a rejected server never leaves a subprocess behind. The event loop answers server-to-client requests (`ping`, `roots/list`) during the handshake to prevent deadlocks.
Usage
Use this principle when connecting to MCP servers that may use different transports (stdio or HTTP). The handshake should be the single connection entry point, and it must handle the case where the server asks client-side questions during initialization.
Theoretical Basis
The handshake is a transport-agnostic initialization with event loop:
# Abstract algorithm (NOT real implementation)
async def connect(settings, options):
transport = create_transport(settings) # stdio or HTTP
events_task = spawn(event_loop(transport, options.roots, options.events))
try:
initialize = await timeout(CONNECT_DEADLINE, handshake(transport, options.cancel))
except error:
transport.close()
events_task.abort()
raise
return Connection(
settings=settings,
transport=transport,
server_info=initialize.server_info,
protocol_version=initialize.protocol_version,
events_task=events_task,
)
Key design ideas:
- Transport abstraction: stdio and HTTP are interchangeable behind a common interface
- Event loop during handshake: the loop must run during initialization to answer `roots/list`
- Bounded deadline: even with no configured timeout, a connect deadline prevents parking forever
- Cleanup on failure: failed handshake closes transport and aborts event loop