Implementation:AI QL Tuui connect disconnect
| Knowledge Sources | |
|---|---|
| Domains | MCP, Transport |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Concrete tool for connecting and disconnecting MCP client transports with error-safe cleanup, provided by the tuui Electron application.
Description
The connect function wraps client.connect(transport) with automatic disconnection on failure. The disconnect function wraps transport.close() with error wrapping. Together they provide safe lifecycle management for MCP stdio transports.
Usage
Import these functions when establishing or tearing down MCP client-to-server connections. connect is used during server initialization; disconnect is used during server shutdown.
Code Reference
Source Location
- Repository: tuui
- File: src/main/mcp/connection.ts
Signature
export async function connect(
client: Client,
transport: Transport
): Promise<void>
export async function disconnect(
transport: Transport
): Promise<void>
export async function setLoggingLevel(
client: Client,
level: LogLevel
): Promise<Record<string, unknown>>
Import
import { connect, disconnect, setLoggingLevel } from './mcp/connection'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| client | Client | Yes (connect) | MCP SDK Client instance |
| transport | Transport | Yes | MCP transport (StdioClientTransport) |
| level | LogLevel | Yes (setLoggingLevel) | One of 'trace', 'debug', 'info', 'warn', 'error' |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Promise<void> | Resolves on success; throws Error with descriptive message on failure |
Usage Examples
Connect a Client to a Transport
import { connect } from './mcp/connection'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
const client = new Client({ name: 'my-client', version: '1.0.0' }, { capabilities: {} })
const transport = new StdioClientTransport({ command: 'node', args: ['server.js'] })
await connect(client, transport)
// On failure, transport is automatically closed before re-throwing
Disconnect All Servers on Shutdown
import { disconnect } from './mcp/connection'
for (const client of connectedClients) {
if (client.connection?.transport) {
await disconnect(client.connection.transport)
delete client.connection.transport
}
}