Implementation:AI QL Tuui initializeClient
| 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
- Principle:AI_QL_Tuui_MCP_Server_Lifecycle
- Principle:AI_QL_Tuui_MCP_Sampling
- Principle:AI_QL_Tuui_MCP_Elicitation