Implementation:AI QL Tuui preload mcpServers
| Knowledge Sources | |
|---|---|
| Domains | IPC, MCP, Preload |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Concrete tool for exposing MCP server APIs, DXT manifests, and IPC channel whitelisting to the renderer process via Electron contextBridge, provided by the tuui Electron application.
Description
The preload bridge exposes four API objects to the renderer via contextBridge.exposeInMainWorld: mainApi (whitelisted IPC send/on/invoke with channel validation), mcpServers (MCP server registry with refresh/update), dxtManifest (installed MCPB manifests), and config objects (llmApis, popupApis, startupApis). The refreshAPI function calls list-clients to get connected MCP clients, builds per-server API methods via buildClientAPI, and merges DXT manifest metadata.
Usage
Import the preload bridge logic (via window.mcpServers, window.dxtManifest, window.mainApi) from renderer code when you need to: list/call MCP tools, refresh the server registry after init, send IPC messages to main, or listen for main-process events.
Code Reference
Source Location
- Repository: tuui
- File: src/preload/index.ts
Signature
// Exposed on window.mainApi:
interface MainApi {
send(channel: string, ...data: any[]): void
on(channel: string, listener: (...args) => void): void
once(channel: string, listener: (...args) => void): void
removeListener(channel: string, listener: (...args) => void): void
off(channel: string, listener: (...args) => void): void
invoke(channel: string, ...data: any[]): Promise<any>
}
// Exposed on window.mcpServers:
interface McpServersApi {
get(): MCPAPI
refresh(): Promise<MCPAPI>
update(name: string): Promise<MCPAPI>
}
// Exposed on window.dxtManifest:
interface DxtManifestApi {
get(): DXTAPI
refresh(): Promise<DXTAPI>
update(name: string): Promise<DXTAPI>
}
// Internal:
async function refreshAPI(): Promise<void>
async function buildClientAPI(client: ClientProfile): MCPAPI[string]
async function listClients(): Promise<ClientProfile[]>
async function traverseManifest(): Promise<DXTAPI>
Import
// Preload script is loaded automatically by Electron; renderer accesses via:
// window.mcpServers, window.dxtManifest, window.mainApi, window.llmApis, window.popupApis, window.startupApis
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| channel | string | Yes (mainApi) | Must be in whitelist (mainAvailChannels or rendererAvailChannels) |
| listener | function | Yes (on/once) | IPC event listener |
| name | string | Yes (update) | Server name to update |
Outputs
| Name | Type | Description |
|---|---|---|
| window.mcpServers.get() | MCPAPI | Object mapping server names to { tools, prompts, resources, metadata } |
| window.mcpServers.refresh() | Promise<MCPAPI> | Refreshed server registry |
| window.dxtManifest.get() | DXTAPI | Object mapping manifest names to validated manifests |
Usage Examples
Refresh MCP Server Registry
// From renderer code:
const servers = await window.mcpServers.refresh()
// servers = {
// "my-server": {
// tools: { list: asyncFn, call: asyncFn },
// prompts: { list: asyncFn, get: asyncFn },
// resources: { list: asyncFn, read: asyncFn, "templates/list": asyncFn },
// metadata: { name: "my-server", type: "metadata__stdio_config", config: {...} }
// }
// }
Invoke a Whitelisted IPC Channel
// From renderer:
const result = await window.mainApi.invoke('msgRequestAppInfo')
// result = { version: "1.0.0", homepage: "...", platform: "darwin" }
// Send (fire-and-forget):
window.mainApi.send('msgOpenExternalLink', 'https://example.com')
Get Installed DXT Manifests
const manifests = window.dxtManifest.get()
// manifests = { "my-bundle": { version: "0.3", server: {...}, user_config: {...} } }
Related Pages
Implements Principle
- Principle:AI_QL_Tuui_MCP_Server_Lifecycle
- Principle:AI_QL_Tuui_Capability_Based_IPC_Routing
- Principle:AI_QL_Tuui_MCP_Elicitation
- Principle:AI_QL_Tuui_Global_Shortcut_Command_Picker