react-native-mcp-kit 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -13
- package/dist/bin/ios-hid +0 -0
- package/dist/client/contexts/McpContext/McpProvider.d.ts.map +1 -1
- package/dist/client/contexts/McpContext/McpProvider.js +6 -3
- package/dist/client/contexts/McpContext/McpProvider.js.map +1 -1
- package/dist/modules/alert/alert.d.ts.map +1 -1
- package/dist/modules/alert/alert.js +6 -5
- package/dist/modules/alert/alert.js.map +1 -1
- package/dist/modules/console/console.d.ts.map +1 -1
- package/dist/modules/console/console.js +18 -13
- package/dist/modules/console/console.js.map +1 -1
- package/dist/modules/device/device.d.ts.map +1 -1
- package/dist/modules/device/device.js +23 -27
- package/dist/modules/device/device.js.map +1 -1
- package/dist/modules/errors/errors.d.ts.map +1 -1
- package/dist/modules/errors/errors.js +92 -11
- package/dist/modules/errors/errors.js.map +1 -1
- package/dist/modules/errors/types.d.ts +8 -0
- package/dist/modules/errors/types.d.ts.map +1 -1
- package/dist/modules/fiberTree/fiberTree.d.ts +4 -0
- package/dist/modules/fiberTree/fiberTree.d.ts.map +1 -1
- package/dist/modules/fiberTree/fiberTree.js +353 -114
- package/dist/modules/fiberTree/fiberTree.js.map +1 -1
- package/dist/modules/fiberTree/index.d.ts +1 -0
- package/dist/modules/fiberTree/index.d.ts.map +1 -1
- package/dist/modules/fiberTree/index.js +14 -1
- package/dist/modules/fiberTree/index.js.map +1 -1
- package/dist/modules/fiberTree/types.d.ts +40 -0
- package/dist/modules/fiberTree/types.d.ts.map +1 -1
- package/dist/modules/fiberTree/utils.d.ts +13 -0
- package/dist/modules/fiberTree/utils.d.ts.map +1 -1
- package/dist/modules/fiberTree/utils.js +195 -23
- package/dist/modules/fiberTree/utils.js.map +1 -1
- package/dist/modules/i18next/i18next.d.ts.map +1 -1
- package/dist/modules/i18next/i18next.js +30 -16
- package/dist/modules/i18next/i18next.js.map +1 -1
- package/dist/modules/index.d.ts +1 -0
- package/dist/modules/index.d.ts.map +1 -1
- package/dist/modules/index.js +3 -1
- package/dist/modules/index.js.map +1 -1
- package/dist/modules/logBox/index.d.ts +2 -0
- package/dist/modules/logBox/index.d.ts.map +1 -0
- package/dist/modules/logBox/index.js +6 -0
- package/dist/modules/logBox/index.js.map +1 -0
- package/dist/modules/logBox/logBox.d.ts +3 -0
- package/dist/modules/logBox/logBox.d.ts.map +1 -0
- package/dist/modules/logBox/logBox.js +234 -0
- package/dist/modules/logBox/logBox.js.map +1 -0
- package/dist/modules/navigation/navigation.d.ts.map +1 -1
- package/dist/modules/navigation/navigation.js +130 -42
- package/dist/modules/navigation/navigation.js.map +1 -1
- package/dist/modules/network/network.d.ts.map +1 -1
- package/dist/modules/network/network.js +262 -67
- package/dist/modules/network/network.js.map +1 -1
- package/dist/modules/network/types.d.ts +35 -3
- package/dist/modules/network/types.d.ts.map +1 -1
- package/dist/modules/reactQuery/reactQuery.d.ts.map +1 -1
- package/dist/modules/reactQuery/reactQuery.js +25 -15
- package/dist/modules/reactQuery/reactQuery.js.map +1 -1
- package/dist/modules/storage/storage.d.ts.map +1 -1
- package/dist/modules/storage/storage.js +23 -16
- package/dist/modules/storage/storage.js.map +1 -1
- package/dist/server/host/hostModule.d.ts.map +1 -1
- package/dist/server/host/hostModule.js +19 -1
- package/dist/server/host/hostModule.js.map +1 -1
- package/dist/server/host/tools/capture.d.ts.map +1 -1
- package/dist/server/host/tools/capture.js +111 -28
- package/dist/server/host/tools/capture.js.map +1 -1
- package/dist/server/host/tools/devices.js +1 -1
- package/dist/server/host/tools/devices.js.map +1 -1
- package/dist/server/host/tools/input.d.ts +3 -0
- package/dist/server/host/tools/input.d.ts.map +1 -1
- package/dist/server/host/tools/input.js +197 -5
- package/dist/server/host/tools/input.js.map +1 -1
- package/dist/server/host/tools/lifecycle.d.ts.map +1 -1
- package/dist/server/host/tools/lifecycle.js +5 -4
- package/dist/server/host/tools/lifecycle.js.map +1 -1
- package/dist/server/host/tools/symbolicate.d.ts +3 -0
- package/dist/server/host/tools/symbolicate.d.ts.map +1 -0
- package/dist/server/host/tools/symbolicate.js +199 -0
- package/dist/server/host/tools/symbolicate.js.map +1 -0
- package/dist/server/host/tools/tapFiber.d.ts +3 -0
- package/dist/server/host/tools/tapFiber.d.ts.map +1 -0
- package/dist/server/host/tools/tapFiber.js +89 -0
- package/dist/server/host/tools/tapFiber.js.map +1 -0
- package/dist/server/host/types.d.ts +14 -0
- package/dist/server/host/types.d.ts.map +1 -1
- package/dist/server/mcpServer.d.ts +6 -0
- package/dist/server/mcpServer.d.ts.map +1 -1
- package/dist/server/mcpServer.js +456 -94
- package/dist/server/mcpServer.js.map +1 -1
- package/package.json +20 -4
package/dist/server/mcpServer.js
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.McpServerWrapper = void 0;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
4
6
|
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
5
7
|
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
6
8
|
const zod_1 = require("zod");
|
|
7
9
|
const protocol_1 = require("../shared/protocol");
|
|
10
|
+
// Read the shipped package.json so the MCP handshake reports an accurate
|
|
11
|
+
// server version — keeps clients' connection logs in sync with the installed
|
|
12
|
+
// package without a parallel constant to maintain. __dirname at runtime is
|
|
13
|
+
// dist/server, so the relative walk lands on the package root.
|
|
14
|
+
const PACKAGE_VERSION = (() => {
|
|
15
|
+
try {
|
|
16
|
+
const pkgPath = (0, node_path_1.join)(__dirname, '..', '..', 'package.json');
|
|
17
|
+
return JSON.parse((0, node_fs_1.readFileSync)(pkgPath, 'utf8')).version;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return '0.0.0';
|
|
21
|
+
}
|
|
22
|
+
})();
|
|
8
23
|
const BASE_INSTRUCTIONS = `You are connected to a running React Native app via the react-native-mcp-kit bridge.
|
|
9
24
|
|
|
10
25
|
Multiple React Native apps can connect simultaneously — each is identified by a short ID like "ios-1", "android-1", or "client-1". Use \`connection_status\` or \`list_tools\` to see which clients are connected and their IDs, platforms, and labels.
|
|
@@ -12,25 +27,152 @@ Multiple React Native apps can connect simultaneously — each is identified by
|
|
|
12
27
|
## How to interact
|
|
13
28
|
|
|
14
29
|
1. Use \`connection_status\` to check which clients are connected.
|
|
15
|
-
2. Use \`list_tools\` to browse all available tool names and short descriptions. The response is compact — modules that are structurally identical across multiple clients are deduplicated into a single entry with a \`clientIds\` array, and input schemas are omitted.
|
|
30
|
+
2. Use \`list_tools\` to browse all available tool names and short descriptions. The response is compact — modules that are structurally identical across multiple clients are deduplicated into a single entry with a \`clientIds\` array, and input schemas are omitted. Narrow the listing with \`{ module }\` or \`{ clientId }\`, or pass \`{ compact: true }\` to drop module-level descriptions.
|
|
16
31
|
3. Use \`describe_tool\` with \`{ tool, clientId? }\` to fetch the full input schema of a specific tool before calling it. Required when you need to know the argument shape. Host tools are resolved directly (no clientId needed). For in-app tools, omit \`clientId\` to auto-pick; specify it only when multiple clients have the same tool with different schemas.
|
|
17
|
-
4. Use \`call\` to invoke any tool with format: module${protocol_1.MODULE_SEPARATOR}method (e.g. navigation${protocol_1.MODULE_SEPARATOR}navigate). When more than one client is connected, specify \`clientId\`. When exactly one client is connected, \`clientId\` is optional — it's auto-picked.
|
|
18
|
-
5. Use \`
|
|
32
|
+
4. Use \`call\` to invoke any tool with format: module${protocol_1.MODULE_SEPARATOR}method (e.g. navigation${protocol_1.MODULE_SEPARATOR}navigate). When more than one client is connected, specify \`clientId\`. When exactly one client is connected, \`clientId\` is optional — it's auto-picked. \`args\` accepts either a plain object or a JSON string — prefer objects to avoid quote escaping.
|
|
33
|
+
5. Use \`wait_until\` to poll any tool until a predicate over its result holds (or timeout). Replaces "screenshot in a loop + sleep" for things like "wait for screen X", "wait for the spinner to disappear", "wait for network to idle". Predicate supports compound forms: { all: [...] } (AND), { any: [...] } (OR), { not: predicate }.
|
|
34
|
+
6. Use \`assert\` for a single-shot checkpoint after actions — same predicate vocabulary as wait_until, returns { pass, actual, expected?, result? }. Natural pair: do action → wait_until → assert.
|
|
35
|
+
7. Use \`host${protocol_1.MODULE_SEPARATOR}tap_fiber\` to collapse "fiber_tree__query → host__tap at bounds" into one call. Pass fiber_tree steps; if exactly one fiber matches, its center is tapped. Ambiguous match returns the candidate list so you can add \`index\` or narrow the chain.
|
|
36
|
+
8. Use \`state_list\` / \`state_get\` to read app state exposed via useMcpState. State is scoped per client; specify \`clientId\` when multiple clients are connected.
|
|
19
37
|
|
|
20
|
-
Some tools run inline on the MCP server host (e.g. \`host${protocol_1.MODULE_SEPARATOR}screenshot\`, \`host${protocol_1.MODULE_SEPARATOR}list_devices\`, \`host${protocol_1.MODULE_SEPARATOR}launch_app\`, \`host${protocol_1.MODULE_SEPARATOR}terminate_app\`, \`host${protocol_1.MODULE_SEPARATOR}restart_app\`) and work even when no React Native client is connected. They use xcrun simctl / adb on the dev machine. When \`clientId\` is provided, host tools use that client's platform/label/deviceId as hints to resolve the target device; otherwise they prefer the device of the single connected client, falling back to the single booted sim / online device. \`launch_app\`, \`terminate_app\`, and \`restart_app\` accept an \`appId\` arg (iOS bundle ID / Android package name); omit it to reuse the target client's registered \`bundleId\` from its connection metadata.
|
|
38
|
+
Some tools run inline on the MCP server host (e.g. \`host${protocol_1.MODULE_SEPARATOR}screenshot\`, \`host${protocol_1.MODULE_SEPARATOR}list_devices\`, \`host${protocol_1.MODULE_SEPARATOR}launch_app\`, \`host${protocol_1.MODULE_SEPARATOR}terminate_app\`, \`host${protocol_1.MODULE_SEPARATOR}restart_app\`, \`host${protocol_1.MODULE_SEPARATOR}symbolicate\`) and work even when no React Native client is connected. They use xcrun simctl / adb on the dev machine. When \`clientId\` is provided, host tools use that client's platform/label/deviceId as hints to resolve the target device; otherwise they prefer the device of the single connected client, falling back to the single booted sim / online device. \`launch_app\`, \`terminate_app\`, and \`restart_app\` accept an \`appId\` arg (iOS bundle ID / Android package name); omit it to reuse the target client's registered \`bundleId\` from its connection metadata.
|
|
21
39
|
|
|
22
40
|
## Driving the UI — pick the right tool
|
|
23
|
-
1. **\`
|
|
24
|
-
2. **\`fiber_tree${protocol_1.MODULE_SEPARATOR}
|
|
25
|
-
3. **\`
|
|
41
|
+
1. **\`host${protocol_1.MODULE_SEPARATOR}tap_fiber\` with \`steps: [...]\`** — the canonical way to simulate a user tap. One call locates the fiber via fiber_tree__query and taps its center through the real OS gesture pipeline, so Pressable feedback, gesture responders, and hit-test logic all run. Ambiguous matches return a candidate list so you can add \`index\` or narrow \`steps\`. This is what you want whenever the user asks to simulate a tap / press / button click.
|
|
42
|
+
2. **\`fiber_tree${protocol_1.MODULE_SEPARATOR}query\` with \`select: ["mcpId","name","bounds"]\` + \`host${protocol_1.MODULE_SEPARATOR}tap\`** when you want to inspect a match set before committing — e.g. verify bounds, or skim candidates before picking one. \`props\` is opt-in on \`select\` to keep responses small.
|
|
43
|
+
3. **\`fiber_tree${protocol_1.MODULE_SEPARATOR}invoke\`** for non-gesture callbacks — anything state-driving the user didn't trigger with a finger. Good when the component is off-screen / virtualised, when a scroll-handler parent swallows taps, or when you're specifically testing a callback in isolation without the gesture pipeline. For simulating a user tap, prefer tap_fiber (above).
|
|
44
|
+
4. **\`host${protocol_1.MODULE_SEPARATOR}screenshot\` + manual coordinate estimation + \`host${protocol_1.MODULE_SEPARATOR}tap\`** ONLY for non-React surfaces: system permission dialogs, native alerts, the on-screen keyboard, WebView content, native splash. These have no fiber and no bounds. Pair with \`region: { x, y, width, height }\` to screenshot just the area you're inspecting — vision-token cheap.
|
|
45
|
+
|
|
46
|
+
Gesture tools: \`host${protocol_1.MODULE_SEPARATOR}tap\` / \`host${protocol_1.MODULE_SEPARATOR}long_press\` / \`host${protocol_1.MODULE_SEPARATOR}swipe\` / \`host${protocol_1.MODULE_SEPARATOR}drag\` / \`host${protocol_1.MODULE_SEPARATOR}type_text\` / \`host${protocol_1.MODULE_SEPARATOR}type_text_batch\` / \`host${protocol_1.MODULE_SEPARATOR}press_key\` work on both platforms with no external daemons: Android via \`adb shell input\`, iOS via a bundled \`ios-hid\` binary that injects HID events directly into iOS Simulator through SimulatorKit.
|
|
26
47
|
|
|
27
|
-
\`
|
|
48
|
+
Stack traces: \`errors${protocol_1.MODULE_SEPARATOR}get_errors\` and \`log_box${protocol_1.MODULE_SEPARATOR}get_logs\` return parsed \`stackFrames\` you can pass straight into \`host${protocol_1.MODULE_SEPARATOR}symbolicate\` to resolve bundled frames back to source paths via Metro.
|
|
28
49
|
`;
|
|
29
50
|
const jsonError = (msg) => {
|
|
30
51
|
return {
|
|
31
52
|
content: [{ text: JSON.stringify({ error: msg }), type: 'text' }],
|
|
32
53
|
};
|
|
33
54
|
};
|
|
55
|
+
/**
|
|
56
|
+
* Drill into a value by dot-path. Arrays accept numeric indices and also
|
|
57
|
+
* respond to `.length` (handy for "wait until list is empty"). Returns
|
|
58
|
+
* undefined when any intermediate segment is missing.
|
|
59
|
+
*/
|
|
60
|
+
const resolvePath = (value, path) => {
|
|
61
|
+
if (!path)
|
|
62
|
+
return value;
|
|
63
|
+
let current = value;
|
|
64
|
+
for (const key of path.split('.')) {
|
|
65
|
+
if (current == null)
|
|
66
|
+
return undefined;
|
|
67
|
+
if (Array.isArray(current)) {
|
|
68
|
+
if (key === 'length') {
|
|
69
|
+
current = current.length;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const idx = Number.parseInt(key, 10);
|
|
73
|
+
current = Number.isNaN(idx) ? undefined : current[idx];
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (typeof current === 'object') {
|
|
77
|
+
current = current[key];
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
return current;
|
|
83
|
+
};
|
|
84
|
+
const evalLeaf = (actual, op, expected) => {
|
|
85
|
+
switch (op) {
|
|
86
|
+
case 'exists':
|
|
87
|
+
return actual !== undefined && actual !== null;
|
|
88
|
+
case 'notExists':
|
|
89
|
+
return actual === undefined || actual === null;
|
|
90
|
+
case 'equals':
|
|
91
|
+
return Object.is(actual, expected);
|
|
92
|
+
case 'notEquals':
|
|
93
|
+
return !Object.is(actual, expected);
|
|
94
|
+
case 'contains': {
|
|
95
|
+
if (typeof actual === 'string' && typeof expected === 'string') {
|
|
96
|
+
return actual.includes(expected);
|
|
97
|
+
}
|
|
98
|
+
if (Array.isArray(actual))
|
|
99
|
+
return actual.includes(expected);
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
case 'notContains': {
|
|
103
|
+
if (typeof actual === 'string' && typeof expected === 'string') {
|
|
104
|
+
return !actual.includes(expected);
|
|
105
|
+
}
|
|
106
|
+
if (Array.isArray(actual))
|
|
107
|
+
return !actual.includes(expected);
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
case 'gt':
|
|
111
|
+
return typeof actual === 'number' && typeof expected === 'number' && actual > expected;
|
|
112
|
+
case 'gte':
|
|
113
|
+
return typeof actual === 'number' && typeof expected === 'number' && actual >= expected;
|
|
114
|
+
case 'lt':
|
|
115
|
+
return typeof actual === 'number' && typeof expected === 'number' && actual < expected;
|
|
116
|
+
case 'lte':
|
|
117
|
+
return typeof actual === 'number' && typeof expected === 'number' && actual <= expected;
|
|
118
|
+
default:
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Evaluate a predicate (leaf or compound) against a result object. Compound
|
|
124
|
+
* forms short-circuit: all stops on first false, any stops on first true.
|
|
125
|
+
*/
|
|
126
|
+
const evalPredicate = (result, predicate) => {
|
|
127
|
+
if ('all' in predicate && Array.isArray(predicate.all)) {
|
|
128
|
+
for (const sub of predicate.all) {
|
|
129
|
+
if (!evalPredicate(result, sub))
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
if ('any' in predicate && Array.isArray(predicate.any)) {
|
|
135
|
+
for (const sub of predicate.any) {
|
|
136
|
+
if (evalPredicate(result, sub))
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
if ('not' in predicate && predicate.not && typeof predicate.not === 'object') {
|
|
142
|
+
return !evalPredicate(result, predicate.not);
|
|
143
|
+
}
|
|
144
|
+
const leaf = predicate;
|
|
145
|
+
if (typeof leaf.op !== 'string')
|
|
146
|
+
return false;
|
|
147
|
+
return evalLeaf(resolvePath(result, leaf.path), leaf.op, leaf.value);
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Parse a `call`-style args argument that may arrive as a JSON string (older
|
|
151
|
+
* clients) or a plain object (new form). Returns { ok, args } or { ok: false,
|
|
152
|
+
* error } on malformed JSON.
|
|
153
|
+
*/
|
|
154
|
+
const parseCallArgs = (raw) => {
|
|
155
|
+
if (raw === undefined || raw === null)
|
|
156
|
+
return { args: {}, ok: true };
|
|
157
|
+
if (typeof raw === 'string') {
|
|
158
|
+
if (raw.length === 0)
|
|
159
|
+
return { args: {}, ok: true };
|
|
160
|
+
try {
|
|
161
|
+
const parsed = JSON.parse(raw);
|
|
162
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
163
|
+
return { args: parsed, ok: true };
|
|
164
|
+
}
|
|
165
|
+
return { error: 'Parsed args must be an object.', ok: false };
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
return { error: 'Invalid JSON in args.', ok: false };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (typeof raw === 'object' && !Array.isArray(raw)) {
|
|
172
|
+
return { args: raw, ok: true };
|
|
173
|
+
}
|
|
174
|
+
return { error: 'args must be an object or a JSON string.', ok: false };
|
|
175
|
+
};
|
|
34
176
|
/**
|
|
35
177
|
* Recursively serializes a value to JSON with sorted object keys, producing a
|
|
36
178
|
* stable canonical form that's safe to use as a dedup Map key. Arrays keep
|
|
@@ -115,7 +257,7 @@ class McpServerWrapper {
|
|
|
115
257
|
});
|
|
116
258
|
}
|
|
117
259
|
}
|
|
118
|
-
this.mcp = new mcp_js_1.McpServer({ name: 'react-native-mcp-kit', version:
|
|
260
|
+
this.mcp = new mcp_js_1.McpServer({ name: 'react-native-mcp-kit', version: PACKAGE_VERSION }, { instructions: BASE_INSTRUCTIONS });
|
|
119
261
|
this.registerTools();
|
|
120
262
|
}
|
|
121
263
|
async start() {
|
|
@@ -128,12 +270,12 @@ class McpServerWrapper {
|
|
|
128
270
|
openWorldHint: true,
|
|
129
271
|
title: 'Call Tool',
|
|
130
272
|
},
|
|
131
|
-
description: 'Call a tool registered by a React Native app client. Use list_tools first to see available tools. When multiple clients are connected, specify clientId; otherwise it is auto-picked.',
|
|
273
|
+
description: 'Call a tool registered by a React Native app client. Use list_tools first to see available tools. When multiple clients are connected, specify clientId; otherwise it is auto-picked. `args` accepts either a plain object or a JSON string — objects are preferred to avoid escaping quotes.',
|
|
132
274
|
inputSchema: {
|
|
133
275
|
args: zod_1.z
|
|
134
|
-
.string()
|
|
276
|
+
.union([zod_1.z.string(), zod_1.z.record(zod_1.z.string(), zod_1.z.unknown())])
|
|
135
277
|
.optional()
|
|
136
|
-
.describe('
|
|
278
|
+
.describe('Tool arguments as a plain object (e.g. { screen: "AUTH_LOGIN_SCREEN" }) or a JSON string.'),
|
|
137
279
|
clientId: zod_1.z
|
|
138
280
|
.string()
|
|
139
281
|
.optional()
|
|
@@ -143,102 +285,226 @@ class McpServerWrapper {
|
|
|
143
285
|
.describe(`Tool name in format "module${protocol_1.MODULE_SEPARATOR}method" (e.g. "navigation${protocol_1.MODULE_SEPARATOR}navigate")`),
|
|
144
286
|
},
|
|
145
287
|
}, async ({ args, clientId, tool }) => {
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
288
|
+
const parsed = parseCallArgs(args);
|
|
289
|
+
if (!parsed.ok)
|
|
290
|
+
return jsonError(parsed.error);
|
|
291
|
+
const dispatch = await this.dispatchTool(tool, parsed.args, clientId);
|
|
292
|
+
if (!dispatch.ok)
|
|
293
|
+
return jsonError(dispatch.error);
|
|
294
|
+
return { content: this.formatResult(dispatch.result) };
|
|
295
|
+
});
|
|
296
|
+
this.mcp.registerTool('wait_until', {
|
|
297
|
+
annotations: {
|
|
298
|
+
openWorldHint: true,
|
|
299
|
+
title: 'Wait Until',
|
|
300
|
+
},
|
|
301
|
+
description: `Poll a tool until its result satisfies a predicate, or timeout.
|
|
302
|
+
|
|
303
|
+
Replaces "screenshot in a loop + sleep" with a declarative check. Typical use:
|
|
304
|
+
• wait for navigation to land on a screen
|
|
305
|
+
• wait for a spinner / toast to disappear
|
|
306
|
+
• wait for a fiber_tree.query to return matches (or stop returning them)
|
|
307
|
+
• wait for network.get_pending.length to hit 0
|
|
308
|
+
|
|
309
|
+
PREDICATE
|
|
310
|
+
Leaf form: { op, path?, value? }
|
|
311
|
+
op: equals | notEquals | contains | notContains | exists | notExists | gt | gte | lt | lte
|
|
312
|
+
path drills through objects + array indices; arrays also expose .length.
|
|
313
|
+
Compound forms compose and nest:
|
|
314
|
+
{ all: [predicate, ...] } — AND
|
|
315
|
+
{ any: [predicate, ...] } — OR
|
|
316
|
+
{ not: predicate } — negation
|
|
317
|
+
Example: { all: [{op:"equals", path:"name", value:"CART"}, {op:"gt", path:"items.length", value:0}] }
|
|
318
|
+
|
|
319
|
+
RETURNS
|
|
320
|
+
{ ok: true, attempts, elapsedMs, matched? } on success — matched is the path-
|
|
321
|
+
resolved value for leaf predicates, omitted for compound.
|
|
322
|
+
{ ok: false, reason, attempts, elapsedMs, lastResult, lastError? } on timeout.`,
|
|
323
|
+
inputSchema: {
|
|
324
|
+
args: zod_1.z
|
|
325
|
+
.union([zod_1.z.string(), zod_1.z.record(zod_1.z.string(), zod_1.z.unknown())])
|
|
326
|
+
.optional()
|
|
327
|
+
.describe('Arguments for the polled tool — object or JSON string.'),
|
|
328
|
+
clientId: zod_1.z.string().optional().describe('Target client ID, same semantics as `call`.'),
|
|
329
|
+
intervalMs: zod_1.z
|
|
330
|
+
.number()
|
|
331
|
+
.optional()
|
|
332
|
+
.describe('Delay between poll attempts. Default 300, min 50, max 5000.'),
|
|
333
|
+
predicate: zod_1.z
|
|
334
|
+
.object({})
|
|
335
|
+
.passthrough()
|
|
336
|
+
.describe('Leaf { op, path?, value? } or compound { all|any: [...] } / { not: predicate }. See tool description for ops and composition.'),
|
|
337
|
+
timeoutMs: zod_1.z
|
|
338
|
+
.number()
|
|
339
|
+
.optional()
|
|
340
|
+
.describe('Total wait budget. Default 10000, min 500, max 60000.'),
|
|
341
|
+
tool: zod_1.z
|
|
342
|
+
.string()
|
|
343
|
+
.describe(`Tool name to poll (e.g. "navigation${protocol_1.MODULE_SEPARATOR}get_current_route").`),
|
|
344
|
+
},
|
|
345
|
+
}, async ({ args, clientId, intervalMs, predicate, timeoutMs, tool }) => {
|
|
346
|
+
const parsedArgs = parseCallArgs(args);
|
|
347
|
+
if (!parsedArgs.ok)
|
|
348
|
+
return jsonError(parsedArgs.error);
|
|
349
|
+
const pred = predicate;
|
|
350
|
+
const isLeaf = typeof pred.op === 'string';
|
|
351
|
+
const leafPath = isLeaf ? pred.path : undefined;
|
|
352
|
+
const timeout = Math.max(500, Math.min(60_000, timeoutMs ?? 10_000));
|
|
353
|
+
const interval = Math.max(50, Math.min(5_000, intervalMs ?? 300));
|
|
354
|
+
const started = Date.now();
|
|
355
|
+
let attempts = 0;
|
|
356
|
+
let lastResult;
|
|
357
|
+
let lastError;
|
|
358
|
+
while (Date.now() - started < timeout) {
|
|
359
|
+
attempts += 1;
|
|
360
|
+
const dispatch = await this.dispatchTool(tool, parsedArgs.args, clientId);
|
|
361
|
+
if (dispatch.ok) {
|
|
362
|
+
lastResult = dispatch.result;
|
|
363
|
+
if (evalPredicate(lastResult, pred)) {
|
|
364
|
+
const payload = {
|
|
365
|
+
attempts,
|
|
366
|
+
elapsedMs: Date.now() - started,
|
|
367
|
+
ok: true,
|
|
368
|
+
};
|
|
369
|
+
if (isLeaf) {
|
|
370
|
+
payload.matched = resolvePath(lastResult, leafPath);
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
content: [{ text: JSON.stringify(payload, null, 2), type: 'text' }],
|
|
374
|
+
};
|
|
375
|
+
}
|
|
164
376
|
}
|
|
165
|
-
|
|
166
|
-
|
|
377
|
+
else {
|
|
378
|
+
lastError = dispatch.error;
|
|
167
379
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (!resolution.ok) {
|
|
171
|
-
return jsonError(resolution.error);
|
|
172
|
-
}
|
|
173
|
-
const client = resolution.client;
|
|
174
|
-
// Find the module by matching prefix in this client's modules
|
|
175
|
-
let mod;
|
|
176
|
-
let moduleName = '';
|
|
177
|
-
let methodName = '';
|
|
178
|
-
for (const m of client.modules) {
|
|
179
|
-
const prefix = `${m.name}${protocol_1.MODULE_SEPARATOR}`;
|
|
180
|
-
if (tool.startsWith(prefix)) {
|
|
181
|
-
mod = m;
|
|
182
|
-
moduleName = m.name;
|
|
183
|
-
methodName = tool.slice(prefix.length);
|
|
380
|
+
const remaining = timeout - (Date.now() - started);
|
|
381
|
+
if (remaining <= 0)
|
|
184
382
|
break;
|
|
185
|
-
|
|
383
|
+
await new Promise((r) => {
|
|
384
|
+
return setTimeout(r, Math.min(interval, remaining));
|
|
385
|
+
});
|
|
186
386
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
387
|
+
return {
|
|
388
|
+
content: [
|
|
389
|
+
{
|
|
390
|
+
text: JSON.stringify({
|
|
391
|
+
attempts,
|
|
392
|
+
elapsedMs: Date.now() - started,
|
|
393
|
+
lastError,
|
|
394
|
+
lastResult,
|
|
395
|
+
ok: false,
|
|
396
|
+
reason: lastError
|
|
397
|
+
? `Last dispatch failed: ${lastError}`
|
|
398
|
+
: `Predicate did not hold within ${timeout}ms`,
|
|
399
|
+
}, null, 2),
|
|
400
|
+
type: 'text',
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
};
|
|
404
|
+
});
|
|
405
|
+
this.mcp.registerTool('assert', {
|
|
406
|
+
annotations: {
|
|
407
|
+
openWorldHint: true,
|
|
408
|
+
title: 'Assert',
|
|
409
|
+
},
|
|
410
|
+
description: `Single-shot assertion over a tool's result. Same predicate vocabulary (including { all / any / not }) as wait_until, but one attempt and a standardized diff on failure.
|
|
411
|
+
|
|
412
|
+
Returns { pass: true, actual? } on success — actual is the path-resolved value for leaf predicates, omitted for compound.
|
|
413
|
+
Returns { pass: false, actual, expected?, op?, path?, message?, result } on predicate failure.
|
|
414
|
+
Returns { pass: false, error, message? } when the tool dispatch itself threw.
|
|
415
|
+
|
|
416
|
+
Useful after wait_until as a checkpoint — the pair reads "do action → wait → assert" which produces a clean audit trail in session logs.`,
|
|
417
|
+
inputSchema: {
|
|
418
|
+
args: zod_1.z
|
|
419
|
+
.union([zod_1.z.string(), zod_1.z.record(zod_1.z.string(), zod_1.z.unknown())])
|
|
420
|
+
.optional()
|
|
421
|
+
.describe('Arguments for the asserted tool — object or JSON string.'),
|
|
422
|
+
clientId: zod_1.z.string().optional().describe('Target client ID, same semantics as `call`.'),
|
|
423
|
+
message: zod_1.z
|
|
424
|
+
.string()
|
|
425
|
+
.optional()
|
|
426
|
+
.describe('Optional human-readable description of the check; echoed in the failure payload.'),
|
|
427
|
+
predicate: zod_1.z
|
|
428
|
+
.object({})
|
|
429
|
+
.passthrough()
|
|
430
|
+
.describe('Leaf { op, path?, value? } or compound { all|any: [...] } / { not: predicate }. See wait_until for full semantics.'),
|
|
431
|
+
tool: zod_1.z
|
|
432
|
+
.string()
|
|
433
|
+
.describe(`Tool name to call once (e.g. "fiber_tree${protocol_1.MODULE_SEPARATOR}query").`),
|
|
434
|
+
},
|
|
435
|
+
}, async ({ args, clientId, message, predicate, tool }) => {
|
|
436
|
+
const parsedArgs = parseCallArgs(args);
|
|
437
|
+
if (!parsedArgs.ok)
|
|
438
|
+
return jsonError(parsedArgs.error);
|
|
439
|
+
const dispatch = await this.dispatchTool(tool, parsedArgs.args, clientId);
|
|
440
|
+
if (!dispatch.ok) {
|
|
441
|
+
return {
|
|
442
|
+
content: [
|
|
443
|
+
{
|
|
444
|
+
text: JSON.stringify({ error: dispatch.error, message, pass: false }, null, 2),
|
|
445
|
+
type: 'text',
|
|
446
|
+
},
|
|
447
|
+
],
|
|
448
|
+
};
|
|
215
449
|
}
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
450
|
+
const pred = predicate;
|
|
451
|
+
const isLeaf = typeof pred.op === 'string';
|
|
452
|
+
const leafPath = isLeaf ? pred.path : undefined;
|
|
453
|
+
const leafValue = isLeaf ? pred.value : undefined;
|
|
454
|
+
const leafOp = isLeaf ? pred.op : undefined;
|
|
455
|
+
const pass = evalPredicate(dispatch.result, pred);
|
|
456
|
+
const payload = { pass };
|
|
457
|
+
if (isLeaf)
|
|
458
|
+
payload.actual = resolvePath(dispatch.result, leafPath);
|
|
459
|
+
if (!pass) {
|
|
460
|
+
if (isLeaf) {
|
|
461
|
+
payload.expected = leafValue;
|
|
462
|
+
payload.op = leafOp;
|
|
463
|
+
if (leafPath)
|
|
464
|
+
payload.path = leafPath;
|
|
465
|
+
}
|
|
466
|
+
if (message)
|
|
467
|
+
payload.message = message;
|
|
468
|
+
payload.result = dispatch.result;
|
|
225
469
|
}
|
|
226
|
-
|
|
227
|
-
|
|
470
|
+
return {
|
|
471
|
+
content: [{ text: JSON.stringify(payload, null, 2), type: 'text' }],
|
|
472
|
+
};
|
|
228
473
|
});
|
|
229
474
|
this.mcp.registerTool('list_tools', {
|
|
230
475
|
annotations: {
|
|
231
476
|
readOnlyHint: true,
|
|
232
477
|
title: 'List Tools',
|
|
233
478
|
},
|
|
234
|
-
description: 'Browse available tools with compact (schema-free) descriptions. Modules with identical shape across multiple clients are deduplicated into a single entry with a clientIds array. Use describe_tool to fetch the full input schema for a specific tool before calling it.',
|
|
235
|
-
|
|
236
|
-
|
|
479
|
+
description: 'Browse available tools with compact (schema-free) descriptions. Modules with identical shape across multiple clients are deduplicated into a single entry with a clientIds array. Use describe_tool to fetch the full input schema for a specific tool before calling it. Pass `module` to narrow to one module, `clientId` to narrow to one client, `compact: true` to drop long module-level descriptions.',
|
|
480
|
+
inputSchema: {
|
|
481
|
+
clientId: zod_1.z
|
|
482
|
+
.string()
|
|
483
|
+
.optional()
|
|
484
|
+
.describe('Narrow listing to a single client. Omit for all connected clients.'),
|
|
485
|
+
compact: zod_1.z
|
|
486
|
+
.boolean()
|
|
487
|
+
.optional()
|
|
488
|
+
.describe('Drop module-level descriptions (still keeps per-tool one-liners). Default false.'),
|
|
489
|
+
module: zod_1.z
|
|
490
|
+
.string()
|
|
491
|
+
.optional()
|
|
492
|
+
.describe('Narrow listing to a single module name (e.g. "fiber_tree", "host"). Omit for all.'),
|
|
493
|
+
},
|
|
494
|
+
}, async ({ clientId, compact, module }) => {
|
|
495
|
+
const allClients = this.bridge.listClients();
|
|
496
|
+
const clients = clientId
|
|
497
|
+
? allClients.filter((c) => {
|
|
498
|
+
return c.id === clientId;
|
|
499
|
+
})
|
|
500
|
+
: allClients;
|
|
237
501
|
// Dedup tool groups across clients by canonical shape
|
|
238
502
|
const dedupMap = new Map();
|
|
239
503
|
for (const client of clients) {
|
|
240
504
|
const groups = this.buildToolGroups(client);
|
|
241
505
|
for (const group of groups) {
|
|
506
|
+
if (module && group.module !== module)
|
|
507
|
+
continue;
|
|
242
508
|
const key = canonicalizeGroup(group);
|
|
243
509
|
const existing = dedupMap.get(key);
|
|
244
510
|
if (existing) {
|
|
@@ -252,7 +518,7 @@ class McpServerWrapper {
|
|
|
252
518
|
const modulesPayload = [...dedupMap.values()].map(({ clientIds, group }) => {
|
|
253
519
|
return {
|
|
254
520
|
clientIds,
|
|
255
|
-
description: group.description,
|
|
521
|
+
description: compact ? undefined : group.description,
|
|
256
522
|
name: group.module,
|
|
257
523
|
tools: group.tools.map((t) => {
|
|
258
524
|
return {
|
|
@@ -262,9 +528,13 @@ class McpServerWrapper {
|
|
|
262
528
|
}),
|
|
263
529
|
};
|
|
264
530
|
});
|
|
265
|
-
const hostToolsPayload = this.hostModules
|
|
531
|
+
const hostToolsPayload = this.hostModules
|
|
532
|
+
.filter((mod) => {
|
|
533
|
+
return !module || mod.name === module;
|
|
534
|
+
})
|
|
535
|
+
.map((mod) => {
|
|
266
536
|
return {
|
|
267
|
-
description: mod.description,
|
|
537
|
+
description: compact ? undefined : mod.description,
|
|
268
538
|
name: mod.name,
|
|
269
539
|
tools: Object.entries(mod.tools).map(([toolName, tool]) => {
|
|
270
540
|
return {
|
|
@@ -536,6 +806,98 @@ class McpServerWrapper {
|
|
|
536
806
|
return jsonError(`Tool '${tool}' exists on multiple clients with different schemas: ${candidates}. Specify clientId.`);
|
|
537
807
|
});
|
|
538
808
|
}
|
|
809
|
+
/**
|
|
810
|
+
* Execute a single tool by full name, returning the raw handler result.
|
|
811
|
+
* Used by both the `call` tool and meta-tools like `wait_until` that need to
|
|
812
|
+
* invoke other tools without going through the full MCP content wrapping.
|
|
813
|
+
*/
|
|
814
|
+
async dispatchTool(tool, args, clientId) {
|
|
815
|
+
const hostEntry = this.hostToolMap.get(tool);
|
|
816
|
+
if (hostEntry) {
|
|
817
|
+
try {
|
|
818
|
+
const result = await hostEntry.handler(args, {
|
|
819
|
+
bridge: this.bridge,
|
|
820
|
+
dispatch: (nextTool, nextArgs, nextClientId) => {
|
|
821
|
+
return this.dispatchTool(nextTool, nextArgs, nextClientId ?? clientId);
|
|
822
|
+
},
|
|
823
|
+
requestedClientId: clientId,
|
|
824
|
+
});
|
|
825
|
+
return { ok: true, result };
|
|
826
|
+
}
|
|
827
|
+
catch (err) {
|
|
828
|
+
return { error: `Host tool "${tool}" threw: ${err.message}`, ok: false };
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
const resolution = this.bridge.resolveClient(clientId);
|
|
832
|
+
if (!resolution.ok)
|
|
833
|
+
return { error: resolution.error, ok: false };
|
|
834
|
+
const client = resolution.client;
|
|
835
|
+
let mod;
|
|
836
|
+
let moduleName = '';
|
|
837
|
+
let methodName = '';
|
|
838
|
+
for (const m of client.modules) {
|
|
839
|
+
const prefix = `${m.name}${protocol_1.MODULE_SEPARATOR}`;
|
|
840
|
+
if (tool.startsWith(prefix)) {
|
|
841
|
+
mod = m;
|
|
842
|
+
moduleName = m.name;
|
|
843
|
+
methodName = tool.slice(prefix.length);
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
if (!mod) {
|
|
848
|
+
if (tool.startsWith(protocol_1.DYNAMIC_PREFIX)) {
|
|
849
|
+
moduleName = `${protocol_1.MODULE_SEPARATOR}dynamic`;
|
|
850
|
+
methodName = tool.slice(protocol_1.DYNAMIC_PREFIX.length);
|
|
851
|
+
}
|
|
852
|
+
else {
|
|
853
|
+
const idx = tool.indexOf(protocol_1.MODULE_SEPARATOR);
|
|
854
|
+
if (idx <= 0) {
|
|
855
|
+
return {
|
|
856
|
+
error: `Invalid tool name "${tool}". Use "module${protocol_1.MODULE_SEPARATOR}method" format.`,
|
|
857
|
+
ok: false,
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
moduleName = tool.slice(0, idx);
|
|
861
|
+
methodName = tool.slice(idx + protocol_1.MODULE_SEPARATOR.length);
|
|
862
|
+
}
|
|
863
|
+
try {
|
|
864
|
+
const result = await this.bridge.call(client.id, moduleName, methodName, args);
|
|
865
|
+
return { ok: true, result };
|
|
866
|
+
}
|
|
867
|
+
catch {
|
|
868
|
+
const allModules = client.modules
|
|
869
|
+
.map((m) => {
|
|
870
|
+
return m.name;
|
|
871
|
+
})
|
|
872
|
+
.join(', ');
|
|
873
|
+
const dynNames = [...client.dynamicTools.keys()].join(', ');
|
|
874
|
+
return {
|
|
875
|
+
error: `Tool "${tool}" not found on client '${client.id}'. Modules: ${allModules || '(none)'}. Dynamic: ${dynNames || '(none)'}`,
|
|
876
|
+
ok: false,
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
const toolDef = mod.tools.find((t) => {
|
|
881
|
+
return t.name === methodName;
|
|
882
|
+
});
|
|
883
|
+
if (!toolDef) {
|
|
884
|
+
return {
|
|
885
|
+
error: `Tool "${methodName}" not found in module "${moduleName}" on client '${client.id}'. Available: ${mod.tools
|
|
886
|
+
.map((t) => {
|
|
887
|
+
return t.name;
|
|
888
|
+
})
|
|
889
|
+
.join(', ')}`,
|
|
890
|
+
ok: false,
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
try {
|
|
894
|
+
const result = await this.bridge.call(client.id, moduleName, methodName, args, toolDef.timeout);
|
|
895
|
+
return { ok: true, result };
|
|
896
|
+
}
|
|
897
|
+
catch (err) {
|
|
898
|
+
return { error: err.message, ok: false };
|
|
899
|
+
}
|
|
900
|
+
}
|
|
539
901
|
buildToolGroups(client) {
|
|
540
902
|
const groups = client.modules.map((mod) => {
|
|
541
903
|
return {
|