Overview
Concrete function that relays a tool-call request from the MCP bridge to the daemon via WebSocket using JSON-RPC 2.0, with timeout and pending-call tracking.
Description
The sendToolCallToDaemon function sends a `tools/call` JSON-RPC message to the daemon WebSocket and returns a Promise that resolves when the daemon responds. It assigns a unique `id` to each request, stores a `PendingCall` (resolve, reject, timer) in a `pendingCalls` map, and sets a 60-second timeout. When a response message arrives (handled by `handleDaemonMessage`), it matches the `id`, clears the timer, and resolves or rejects the Promise. On connection loss, all pending calls are rejected via `rejectAllPending`.
Usage
- Relay tool call: `const result = await sendToolCallToDaemon("click", { uid: "abc123" })`
- Ensure connection first: call `ensureDaemonConnection()` before sending.
Code Reference
Source Location
- Repository: AIPex
- File: mcp-bridge/src/bridge.ts
- Lines: L101-170 (sendToolCallToDaemon, handleDaemonMessage, rejectAllPending), L265-271 (ensureDaemonConnection)
Signature
function sendToolCallToDaemon(
toolName: string,
args: Record<string, unknown>
): Promise<unknown>
function handleDaemonMessage(raw: string): void
function rejectAllPending(reason: string): void
async function ensureDaemonConnection(): Promise<void>
function isDaemonConnected(): boolean
// Configuration:
const TOOL_CALL_TIMEOUT_MS = 60_000
interface PendingCall {
resolve: (result: unknown) => void;
reject: (error: Error) => void;
timer: ReturnType<typeof setTimeout>;
}
Import
// Internal to mcp-bridge package — called by the CallToolRequest handler.
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| toolName |
string |
Yes |
Name of the browser tool to call
|
| args |
Record<string, unknown> |
Yes |
Tool arguments
|
Outputs
| Name |
Type |
Description
|
| returns |
Promise<unknown> |
Resolves with tool result from daemon/extension
|
Usage Examples
MCP CallTool Handler
// The MCP server's CallToolRequest handler:
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
await ensureDaemonConnection();
const result = await sendToolCallToDaemon(name, args ?? {});
if (result?.content) {
return result; // Already MCP-formatted content blocks
}
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
});
JSON-RPC Message Format
// Sent to daemon:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": { "name": "click", "arguments": { "uid": "abc123" } }
}
// Response from daemon:
{
"jsonrpc": "2.0",
"id": 1,
"result": { "content": [{ "type": "text", "text": "..." }] }
}
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.