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 initializeClient

From Leeroopedia
Knowledge Sources
Domains MCP, Transport, Sampling, Elicitation
Last Updated 2026-10-24 00:00 GMT

Overview

Concrete tool for spawning MCP stdio client transports with idle-timeout protection and sampling/elicitation request handlers, provided by the tuui Electron application.

Description

The initializeClient function creates an MCP StdioClientTransport from a server config, instantiates an MCP Client with sampling and elicitation capabilities, connects via the connect wrapper, and attaches request handlers for SamplingRequestSchema and ElicitRequestSchema. It races the stdio connection promise against an idle-timeout promise (default 90s) to prevent hanging on unresponsive servers. The manageRequests helper sends typed MCP requests through the connected client.

Usage

Import initializeClient when spawning a new MCP server as a child process. Import manageRequests when sending typed MCP protocol requests (tools/list, tools/call, prompts/list, etc.) through a connected client.

Code Reference

Source Location

  • Repository: tuui
  • File: src/main/mcp/client.ts

Signature

export async function initializeClient(
  name: string,
  serverConfig: McpServerConfig,
  callback?: McpProgressCallback,
  idleTimeout: number = 90
): Promise<McpClientTransport>

export async function manageRequests(
  client: Client,
  method: string,
  schema: any,
  params?: any
): Promise<any>

Import

import { initializeClient, manageRequests } from './mcp/client'

I/O Contract

Inputs

Name Type Required Description
name string Yes Server name (used for client name and progress callbacks)
serverConfig McpServerConfig (StdioServerParameters) Yes stdio command, args, and env for the child process
callback McpProgressCallback No (name, message, status) progress reporter
idleTimeout number No (default 90) Inactivity timeout in seconds
client Client Yes (manageRequests) Connected MCP client instance
method string Yes (manageRequests) MCP method name (e.g. 'tools/list')
schema any Yes (manageRequests) Zod schema for response validation
params any No (manageRequests) Optional request parameters

Outputs

Name Type Description
return McpClientTransport { client: Client, transport: StdioClientTransport }
side effect setRequestHandler Sampling and elicitation handlers attached to client
side effect stderr pipe Server stderr logged via callback as 'pending'

Usage Examples

Spawn an MCP Server

import { initializeClient } from './mcp/client'

const transport = await initializeClient(
  'my-server',
  {
    command: 'npx',
    args: ['-y', '@modelcontextprotocol/server-filesystem', '/tmp'],
    env: {}
  },
  (name, message, status) => {
    console.log(`[${status}] ${name}: ${message}`)
  }
)

// transport.client is the connected MCP Client
// transport.transport is the StdioClientTransport

Send a Typed MCP Request

import { manageRequests } from './mcp/client'
import { ListToolsResultSchema } from '@modelcontextprotocol/sdk/types.js'

const result = await manageRequests(
  transport.client,
  'tools/list',
  ListToolsResultSchema
)
// result contains { tools: [...] }

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

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