Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AI QL Tuui connect disconnect

From Leeroopedia
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
  }
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment