@oimdb/mcp 2.0.0 → 3.0.0
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 +17 -221
- package/bin/oimdb-mcp.cjs +2 -14
- package/dist/index.cjs +32 -447
- package/dist/index.d.cts +5 -71
- package/dist/index.d.ts +5 -71
- package/dist/index.js +37 -444
- package/package.json +9 -12
- package/bin/cdp.cjs +0 -98
- package/bin/devtools.cjs +0 -26
- package/ui/app.css +0 -323
- package/ui/app.js +0 -409
- package/ui/index.html +0 -63
package/dist/index.cjs
CHANGED
|
@@ -21,236 +21,36 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
OIMDevMcpServer: () => OIMDevMcpServer,
|
|
24
|
-
|
|
25
|
-
createOIMDevMcpServer: () => createOIMDevMcpServer,
|
|
26
|
-
createOIMDevUIServer: () => createOIMDevUIServer
|
|
24
|
+
createOIMDevMcpServer: () => createOIMDevMcpServer
|
|
27
25
|
});
|
|
28
26
|
module.exports = __toCommonJS(index_exports);
|
|
29
27
|
|
|
30
28
|
// src/core/OIMDevMcpServer.ts
|
|
31
29
|
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
32
30
|
var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
33
|
-
var import_node_url = require("url");
|
|
34
|
-
var import_node_path = require("path");
|
|
35
|
-
var import_zod2 = require("zod");
|
|
36
|
-
|
|
37
|
-
// src/core/OIMDevBridge.ts
|
|
38
|
-
var import_ws = require("ws");
|
|
39
|
-
|
|
40
|
-
// src/core/OIMDevBridgeMessageReader.ts
|
|
41
31
|
var import_zod = require("zod");
|
|
42
|
-
var
|
|
43
|
-
read(raw) {
|
|
44
|
-
let parsed;
|
|
45
|
-
try {
|
|
46
|
-
parsed = JSON.parse(raw);
|
|
47
|
-
} catch {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
const result = _OIMDevBridgeMessageReader.SCHEMA.safeParse(parsed);
|
|
51
|
-
if (!result.success) return null;
|
|
52
|
-
return result.data;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
_OIMDevBridgeMessageReader.SCHEMA = import_zod.z.discriminatedUnion("type", [
|
|
56
|
-
import_zod.z.object({
|
|
57
|
-
type: import_zod.z.literal("hello"),
|
|
58
|
-
url: import_zod.z.string(),
|
|
59
|
-
title: import_zod.z.string(),
|
|
60
|
-
timestamp: import_zod.z.number()
|
|
61
|
-
}),
|
|
62
|
-
import_zod.z.object({
|
|
63
|
-
type: import_zod.z.literal("response"),
|
|
64
|
-
id: import_zod.z.string(),
|
|
65
|
-
data: import_zod.z.unknown(),
|
|
66
|
-
error: import_zod.z.string().optional()
|
|
67
|
-
})
|
|
68
|
-
]);
|
|
69
|
-
var OIMDevBridgeMessageReader = _OIMDevBridgeMessageReader;
|
|
70
|
-
|
|
71
|
-
// src/core/OIMDevLoopbackPolicy.ts
|
|
72
|
-
var _OIMDevLoopbackPolicy = class _OIMDevLoopbackPolicy {
|
|
73
|
-
static isLoopbackHost(host) {
|
|
74
|
-
return _OIMDevLoopbackPolicy.HOSTS.has(host) || host.endsWith(".localhost");
|
|
75
|
-
}
|
|
76
|
-
static isLoopbackAuthority(authority) {
|
|
77
|
-
const host = authority.startsWith("[") ? authority.slice(0, authority.indexOf("]") + 1) : authority.split(":", 1)[0];
|
|
78
|
-
return _OIMDevLoopbackPolicy.isLoopbackHost(host);
|
|
79
|
-
}
|
|
80
|
-
// The literal string `null` is NOT loopback: a sandboxed iframe on any site
|
|
81
|
-
// in the world sends exactly that.
|
|
82
|
-
static isLoopbackOrigin(origin) {
|
|
83
|
-
try {
|
|
84
|
-
return _OIMDevLoopbackPolicy.isLoopbackHost(new URL(origin).hostname);
|
|
85
|
-
} catch {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
_OIMDevLoopbackPolicy.HOSTS = /* @__PURE__ */ new Set([
|
|
91
|
-
"localhost",
|
|
92
|
-
"127.0.0.1",
|
|
93
|
-
"::1",
|
|
94
|
-
"[::1]"
|
|
95
|
-
]);
|
|
96
|
-
var OIMDevLoopbackPolicy = _OIMDevLoopbackPolicy;
|
|
97
|
-
|
|
98
|
-
// src/core/OIMDevBridge.ts
|
|
99
|
-
var OIM_REQUEST_TIMEOUT_MS = 5e3;
|
|
100
|
-
var OIMDevBridge = class {
|
|
101
|
-
constructor(bridgeOptions) {
|
|
102
|
-
this.bridgeOptions = bridgeOptions;
|
|
103
|
-
this.messageReader = new OIMDevBridgeMessageReader();
|
|
104
|
-
this.pending = /* @__PURE__ */ new Map();
|
|
105
|
-
this.webSocketServer = null;
|
|
106
|
-
this.devConnectedBrowser = null;
|
|
107
|
-
}
|
|
108
|
-
get connectedBrowser() {
|
|
109
|
-
return this.devConnectedBrowser;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* May a page use this bridge?
|
|
113
|
-
*
|
|
114
|
-
* From this machine, or named by whoever started the server. An ABSENT
|
|
115
|
-
* origin is a non-browser client — a CLI, a test — and is allowed; the
|
|
116
|
-
* literal `'null'` is not, because a sandboxed iframe on any site can send
|
|
117
|
-
* exactly that.
|
|
118
|
-
*/
|
|
119
|
-
isOriginAllowed(origin) {
|
|
120
|
-
if (origin === void 0 || origin === "") return true;
|
|
121
|
-
if (this.bridgeOptions.allowedOrigins.has(origin)) return true;
|
|
122
|
-
return OIMDevLoopbackPolicy.isLoopbackOrigin(origin);
|
|
123
|
-
}
|
|
124
|
-
request(action) {
|
|
125
|
-
const browser = this.devConnectedBrowser;
|
|
126
|
-
if (browser === null) {
|
|
127
|
-
return Promise.reject(
|
|
128
|
-
new Error(this.bridgeOptions.requestTimeoutMessage)
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
return new Promise((resolve, reject) => {
|
|
132
|
-
const id = Math.random().toString(36).slice(2);
|
|
133
|
-
const timer = setTimeout(() => {
|
|
134
|
-
this.pending.delete(id);
|
|
135
|
-
reject(new Error(this.bridgeOptions.requestTimeoutMessage));
|
|
136
|
-
}, OIM_REQUEST_TIMEOUT_MS);
|
|
137
|
-
this.pending.set(id, { resolve, reject, timer });
|
|
138
|
-
browser.ws.send(JSON.stringify({ type: "request", id, action }));
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
start() {
|
|
142
|
-
this.webSocketServer = new import_ws.WebSocketServer({
|
|
143
|
-
port: this.bridgeOptions.wsPort,
|
|
144
|
-
host: this.bridgeOptions.host,
|
|
145
|
-
verifyClient: (info) => this.isOriginAllowed(info.origin)
|
|
146
|
-
});
|
|
147
|
-
this.webSocketServer.on("error", (err) => {
|
|
148
|
-
if (err.code === "EADDRINUSE") {
|
|
149
|
-
this.write(
|
|
150
|
-
`Port ${this.bridgeOptions.wsPort} is already in use.
|
|
151
|
-
`
|
|
152
|
-
);
|
|
153
|
-
process.stderr.write(
|
|
154
|
-
`Set a different port: OIMDB_WS_PORT=7433 ${this.bridgeOptions.portInUseHint}
|
|
155
|
-
`
|
|
156
|
-
);
|
|
157
|
-
process.exit(1);
|
|
158
|
-
}
|
|
159
|
-
this.write(`Bridge error: ${err.message}
|
|
160
|
-
`);
|
|
161
|
-
process.exit(1);
|
|
162
|
-
});
|
|
163
|
-
this.webSocketServer.on("connection", (ws) => {
|
|
164
|
-
ws.on("error", (err) => {
|
|
165
|
-
this.write(`Bridge socket error: ${err.message}
|
|
166
|
-
`);
|
|
167
|
-
});
|
|
168
|
-
ws.on("message", (raw) => this.readFrame(ws, raw.toString()));
|
|
169
|
-
ws.on("close", () => {
|
|
170
|
-
if (this.devConnectedBrowser?.ws === ws) {
|
|
171
|
-
this.devConnectedBrowser = null;
|
|
172
|
-
this.write("Browser disconnected\n");
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
this.write(
|
|
177
|
-
`Bridge ready on ws://localhost:${this.bridgeOptions.wsPort} (bound to ${this.bridgeOptions.host})
|
|
178
|
-
`
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
readFrame(ws, raw) {
|
|
182
|
-
const devBridgeMessage = this.messageReader.read(raw);
|
|
183
|
-
if (devBridgeMessage === null) {
|
|
184
|
-
this.write("Dropping a client that sent a malformed frame\n");
|
|
185
|
-
ws.close(1007, "Malformed bridge message");
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
if (devBridgeMessage.type === "hello") {
|
|
189
|
-
this.adoptBrowser(ws, devBridgeMessage.url, devBridgeMessage.title);
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
if (this.devConnectedBrowser?.ws !== ws) return;
|
|
193
|
-
const devPendingRequest = this.pending.get(devBridgeMessage.id);
|
|
194
|
-
if (!devPendingRequest) return;
|
|
195
|
-
this.pending.delete(devBridgeMessage.id);
|
|
196
|
-
clearTimeout(devPendingRequest.timer);
|
|
197
|
-
if (devBridgeMessage.error) devPendingRequest.reject(new Error(devBridgeMessage.error));
|
|
198
|
-
else devPendingRequest.resolve(devBridgeMessage.data);
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Newest tab wins — a reload is the ordinary reason for a second `hello` —
|
|
202
|
-
* but the tab it displaces is CLOSED and the swap is printed. Overwriting the
|
|
203
|
-
* field in place leaves the displaced socket open for ever and lets a second
|
|
204
|
-
* connection take the bridge over in silence.
|
|
205
|
-
*/
|
|
206
|
-
adoptBrowser(ws, url, title) {
|
|
207
|
-
const previousDevConnectedBrowser = this.devConnectedBrowser;
|
|
208
|
-
this.devConnectedBrowser = { ws, url, title };
|
|
209
|
-
if (previousDevConnectedBrowser !== null && previousDevConnectedBrowser.ws !== ws) {
|
|
210
|
-
this.write(
|
|
211
|
-
`Bridge taken over by "${title}" (${url}); dropping "${previousDevConnectedBrowser.title}"
|
|
212
|
-
`
|
|
213
|
-
);
|
|
214
|
-
previousDevConnectedBrowser.ws.close(1e3, "Replaced by a newer OIMDB DevTools tab");
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
this.write(`Browser connected: ${title} (${url})
|
|
218
|
-
`);
|
|
219
|
-
}
|
|
220
|
-
write(line) {
|
|
221
|
-
process.stderr.write(`${this.bridgeOptions.logPrefix} ${line}`);
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
// src/core/OIMDevMcpServer.ts
|
|
226
|
-
async function loadRegistryFromModule(path) {
|
|
227
|
-
const url = (0, import_node_url.pathToFileURL)((0, import_node_path.resolve)(path)).href;
|
|
228
|
-
const mod = await import(url);
|
|
229
|
-
let candidate = mod.oimDevRegistry ?? mod.default ?? mod;
|
|
230
|
-
if (typeof candidate === "function") {
|
|
231
|
-
candidate = await candidate();
|
|
232
|
-
}
|
|
233
|
-
if (!candidate || typeof candidate.inspect !== "function" || typeof candidate.dumpString !== "function") {
|
|
234
|
-
throw new Error(
|
|
235
|
-
`[OIMDB MCP] Module "${path}" must export a registry exposing inspect() and dumpString() (as an "oimDevRegistry" or default export, or a zero-arg factory returning one).`
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
return candidate;
|
|
239
|
-
}
|
|
32
|
+
var import_cli = require("@oimdb/cli");
|
|
240
33
|
var OIMDevMcpServer = class {
|
|
241
34
|
constructor(options = {}) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
35
|
+
const wsPort = options.wsPort ?? 7432;
|
|
36
|
+
let devStateSource;
|
|
37
|
+
if (options.registry !== void 0) {
|
|
38
|
+
devStateSource = new import_cli.OIMDevRegistryStateSource(options.registry);
|
|
39
|
+
} else if (options.modelModulePath !== void 0) {
|
|
40
|
+
this.devModuleStateSource = new import_cli.OIMDevModuleStateSource(options.modelModulePath);
|
|
41
|
+
devStateSource = this.devModuleStateSource;
|
|
42
|
+
} else {
|
|
43
|
+
this.devBridge = new import_cli.OIMDevBridge({
|
|
44
|
+
wsPort,
|
|
45
|
+
host: options.host ?? "127.0.0.1",
|
|
46
|
+
allowedOrigins: new Set(options.allowedOrigins ?? []),
|
|
47
|
+
logPrefix: "[OIMDB MCP]",
|
|
48
|
+
portInUseHint: "npx @oimdb/mcp",
|
|
49
|
+
requestTimeoutMessage: "Browser response timeout (5s). Is the page active?"
|
|
50
|
+
});
|
|
51
|
+
devStateSource = new import_cli.OIMDevBridgeStateSource(this.devBridge, wsPort);
|
|
52
|
+
}
|
|
53
|
+
this.devStateReader = new import_cli.OIMDevStateReader(devStateSource);
|
|
254
54
|
this.mcpServer = new import_mcp.McpServer(
|
|
255
55
|
{ name: "oimdb", version: "1.0.0" },
|
|
256
56
|
{
|
|
@@ -259,240 +59,30 @@ var OIMDevMcpServer = class {
|
|
|
259
59
|
);
|
|
260
60
|
this.registerTools();
|
|
261
61
|
}
|
|
262
|
-
/**
|
|
263
|
-
* Resolves the in-process registry, loading the model module on first use.
|
|
264
|
-
* Returns null when no in-process source is configured (browser-bridge mode).
|
|
265
|
-
*/
|
|
266
|
-
async getInProcessRegistry() {
|
|
267
|
-
if (this.inProcessRegistry) return this.inProcessRegistry;
|
|
268
|
-
if (this.modelModulePath && !this.modelModuleLoaded) {
|
|
269
|
-
this.modelModuleLoaded = true;
|
|
270
|
-
this.inProcessRegistry = await loadRegistryFromModule(
|
|
271
|
-
this.modelModulePath
|
|
272
|
-
);
|
|
273
|
-
}
|
|
274
|
-
return this.inProcessRegistry;
|
|
275
|
-
}
|
|
276
|
-
/** Reads the inspect snapshot from the in-process model or the browser. */
|
|
277
|
-
async readInspect() {
|
|
278
|
-
const registry = await this.getInProcessRegistry();
|
|
279
|
-
if (registry) return registry.inspect();
|
|
280
|
-
return this.requestBrowser("inspect");
|
|
281
|
-
}
|
|
282
|
-
/** Reads the human-readable dump from the in-process model or the browser. */
|
|
283
|
-
async readDump() {
|
|
284
|
-
const registry = await this.getInProcessRegistry();
|
|
285
|
-
if (registry) return registry.dumpString();
|
|
286
|
-
return await this.requestBrowser("dump");
|
|
287
|
-
}
|
|
288
62
|
registerTools() {
|
|
289
63
|
const server = this.mcpServer;
|
|
290
|
-
const nameSchema = { name:
|
|
64
|
+
const nameSchema = { name: import_zod.z.string().describe("Registered name") };
|
|
65
|
+
const asText = (text) => ({ content: [{ type: "text", text }] });
|
|
291
66
|
server.registerTool("oimdb_inspect", {
|
|
292
67
|
description: "Get full state of all registered OIMDB collections and computeds. Returns structured JSON with entity counts, field names, index keys, relations, and computed statuses. Source is the connected browser tab, or an in-process model when one is configured (offline/static \u2014 field names appear only if the model holds sample entities)."
|
|
293
|
-
}, async () =>
|
|
294
|
-
const data = await this.readInspect();
|
|
295
|
-
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
296
|
-
});
|
|
68
|
+
}, async () => asText(await this.devStateReader.readInspectText()));
|
|
297
69
|
server.registerTool("oimdb_dump", {
|
|
298
70
|
description: "Get a human-readable text summary of OIMDB state. Same output as oimDevRegistry.dump(). Reads the connected browser tab or an in-process model when configured."
|
|
299
|
-
}, async () =>
|
|
300
|
-
const text = await this.readDump();
|
|
301
|
-
return { content: [{ type: "text", text }] };
|
|
302
|
-
});
|
|
71
|
+
}, async () => asText(await this.devStateReader.readDumpText()));
|
|
303
72
|
server.registerTool("oimdb_collection", {
|
|
304
73
|
description: "Get details of a specific registered collection: entity count, field names, index keys, and relations.",
|
|
305
74
|
inputSchema: nameSchema
|
|
306
|
-
}, async ({ name }) =>
|
|
307
|
-
const data = await this.readInspect();
|
|
308
|
-
const info = data.collections[name];
|
|
309
|
-
if (!info) {
|
|
310
|
-
const available = Object.keys(data.collections).join(", ") || "(none registered)";
|
|
311
|
-
return { content: [{ type: "text", text: `Collection '${name}' not found. Registered: ${available}` }] };
|
|
312
|
-
}
|
|
313
|
-
return { content: [{ type: "text", text: JSON.stringify(info, null, 2) }] };
|
|
314
|
-
});
|
|
75
|
+
}, async ({ name }) => asText(await this.devStateReader.readCollectionText(name)));
|
|
315
76
|
server.registerTool("oimdb_computed", {
|
|
316
77
|
description: "Get status and resolved dependency names of a specific registered computed value. Shows fresh/stale/pending status, current value, and which registered sources it depends on.",
|
|
317
78
|
inputSchema: nameSchema
|
|
318
|
-
}, async ({ name }) =>
|
|
319
|
-
const data = await this.readInspect();
|
|
320
|
-
const info = data.computeds[name];
|
|
321
|
-
if (!info) {
|
|
322
|
-
const available = Object.keys(data.computeds).join(", ") || "(none registered)";
|
|
323
|
-
return { content: [{ type: "text", text: `Computed '${name}' not found. Registered: ${available}` }] };
|
|
324
|
-
}
|
|
325
|
-
return { content: [{ type: "text", text: JSON.stringify(info, null, 2) }] };
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
requestBrowser(action) {
|
|
329
|
-
if (this.bridge.connectedBrowser === null) {
|
|
330
|
-
throw new Error(
|
|
331
|
-
`No browser tab connected to the OIMDB MCP bridge.
|
|
332
|
-
|
|
333
|
-
Live mode \u2014 add to your debug entry point:
|
|
334
|
-
import { oimDevRegistry } from '@oimdb/devtools';
|
|
335
|
-
oimDevRegistry.connect(); // connects to ws://localhost:${this.wsPort}
|
|
336
|
-
Then reload the page.
|
|
337
|
-
|
|
338
|
-
Offline mode \u2014 point the server at a model module instead:
|
|
339
|
-
OIMDB_MODEL_MODULE=./path/to/model.js npx @oimdb/mcp
|
|
340
|
-
(the module exports an "oimDevRegistry" with inspect()/dumpString()).`
|
|
341
|
-
);
|
|
342
|
-
}
|
|
343
|
-
return this.bridge.request(action);
|
|
79
|
+
}, async ({ name }) => asText(await this.devStateReader.readComputedText(name)));
|
|
344
80
|
}
|
|
345
81
|
async start() {
|
|
346
|
-
|
|
347
|
-
if (
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
);
|
|
351
|
-
} else {
|
|
352
|
-
this.bridge.start();
|
|
353
|
-
}
|
|
354
|
-
const transport = new import_stdio.StdioServerTransport();
|
|
355
|
-
await this.mcpServer.connect(transport);
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
// src/core/OIMDevUIServer.ts
|
|
360
|
-
var import_http = require("http");
|
|
361
|
-
var import_child_process = require("child_process");
|
|
362
|
-
|
|
363
|
-
// src/core/OIMDevUIAssetCatalog.ts
|
|
364
|
-
var import_fs = require("fs");
|
|
365
|
-
var import_path = require("path");
|
|
366
|
-
var _OIMDevUIAssetCatalog = class _OIMDevUIAssetCatalog {
|
|
367
|
-
constructor(uiDir) {
|
|
368
|
-
this.uiDir = uiDir;
|
|
369
|
-
}
|
|
370
|
-
resolve(requestUrl) {
|
|
371
|
-
const path = requestUrl.split("?", 1)[0].split("#", 1)[0];
|
|
372
|
-
return _OIMDevUIAssetCatalog.ASSETS.get(path) ?? null;
|
|
373
|
-
}
|
|
374
|
-
// Per request, so the UI stays editable; the constant file name is what
|
|
375
|
-
// makes that safe, not the timing.
|
|
376
|
-
read(asset) {
|
|
377
|
-
return (0, import_fs.readFileSync)((0, import_path.join)(this.uiDir, asset.fileName));
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
_OIMDevUIAssetCatalog.ASSETS = /* @__PURE__ */ new Map([
|
|
381
|
-
["/", { fileName: "index.html", contentType: "text/html; charset=utf-8" }],
|
|
382
|
-
["/index.html", { fileName: "index.html", contentType: "text/html; charset=utf-8" }],
|
|
383
|
-
["/app.js", { fileName: "app.js", contentType: "text/javascript; charset=utf-8" }],
|
|
384
|
-
["/app.css", { fileName: "app.css", contentType: "text/css; charset=utf-8" }]
|
|
385
|
-
]);
|
|
386
|
-
var OIMDevUIAssetCatalog = _OIMDevUIAssetCatalog;
|
|
387
|
-
|
|
388
|
-
// src/core/OIMDevUIServer.ts
|
|
389
|
-
var OIMDevUIServer = class {
|
|
390
|
-
constructor(options) {
|
|
391
|
-
this.uiPort = options.uiPort ?? 7433;
|
|
392
|
-
this.shouldOpen = options.open ?? true;
|
|
393
|
-
this.host = options.host ?? "127.0.0.1";
|
|
394
|
-
this.allowedOrigins = new Set(options.allowedOrigins ?? []);
|
|
395
|
-
this.devUIAssetCatalog = new OIMDevUIAssetCatalog(options.uiDir);
|
|
396
|
-
this.bridge = new OIMDevBridge({
|
|
397
|
-
wsPort: options.wsPort ?? 7432,
|
|
398
|
-
host: this.host,
|
|
399
|
-
allowedOrigins: this.allowedOrigins,
|
|
400
|
-
logPrefix: "[OIMDB DevTools]",
|
|
401
|
-
portInUseHint: "npx @oimdb/mcp devtools",
|
|
402
|
-
requestTimeoutMessage: "timeout"
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* While bound to loopback, answer only what was addressed to a loopback
|
|
407
|
-
* name.
|
|
408
|
-
*
|
|
409
|
-
* Without this, a page on any site can point a hostname it controls at
|
|
410
|
-
* 127.0.0.1 and become same-origin with this server. The `Host` header is
|
|
411
|
-
* the part of that a browser cannot forge. Binding elsewhere is an explicit
|
|
412
|
-
* decision, so the check stands down there.
|
|
413
|
-
*/
|
|
414
|
-
isRequestAddressedHere(req) {
|
|
415
|
-
if (!OIMDevLoopbackPolicy.isLoopbackHost(this.host)) return true;
|
|
416
|
-
const host = req.headers.host;
|
|
417
|
-
return host !== void 0 && OIMDevLoopbackPolicy.isLoopbackAuthority(host);
|
|
418
|
-
}
|
|
419
|
-
requestBrowser(action) {
|
|
420
|
-
if (this.bridge.connectedBrowser === null) return Promise.resolve(null);
|
|
421
|
-
return this.bridge.request(action);
|
|
422
|
-
}
|
|
423
|
-
setupHTTP() {
|
|
424
|
-
const server = (0, import_http.createServer)((req, res) => {
|
|
425
|
-
if (!this.isRequestAddressedHere(req)) {
|
|
426
|
-
res.writeHead(421);
|
|
427
|
-
res.end("Misdirected request");
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
const origin = req.headers.origin;
|
|
431
|
-
if (origin !== void 0 && this.allowedOrigins.has(origin)) {
|
|
432
|
-
res.setHeader("Access-Control-Allow-Origin", origin);
|
|
433
|
-
res.setHeader("Vary", "Origin");
|
|
434
|
-
}
|
|
435
|
-
const url = req.url ?? "/";
|
|
436
|
-
if (url.split("?", 1)[0] === "/api/inspect") {
|
|
437
|
-
this.requestBrowser("inspect").then((data) => {
|
|
438
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
439
|
-
const connectedBrowser = this.bridge.connectedBrowser;
|
|
440
|
-
res.end(JSON.stringify({
|
|
441
|
-
connected: connectedBrowser !== null,
|
|
442
|
-
tab: connectedBrowser === null ? null : { url: connectedBrowser.url, title: connectedBrowser.title },
|
|
443
|
-
data
|
|
444
|
-
}));
|
|
445
|
-
}).catch(() => {
|
|
446
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
447
|
-
res.end(JSON.stringify({ connected: false, tab: null, data: null }));
|
|
448
|
-
});
|
|
449
|
-
return;
|
|
450
|
-
}
|
|
451
|
-
const asset = this.devUIAssetCatalog.resolve(url);
|
|
452
|
-
if (asset === null) {
|
|
453
|
-
res.writeHead(404);
|
|
454
|
-
res.end("Not found");
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
try {
|
|
458
|
-
res.writeHead(200, { "Content-Type": asset.contentType });
|
|
459
|
-
res.end(this.devUIAssetCatalog.read(asset));
|
|
460
|
-
} catch {
|
|
461
|
-
res.writeHead(404);
|
|
462
|
-
res.end("Not found");
|
|
463
|
-
}
|
|
464
|
-
});
|
|
465
|
-
server.on("error", (err) => {
|
|
466
|
-
if (err.code === "EADDRINUSE") {
|
|
467
|
-
process.stderr.write(
|
|
468
|
-
`[OIMDB DevTools] UI port ${this.uiPort} is already in use.
|
|
469
|
-
Set a different port: OIMDB_UI_PORT=7434 npx @oimdb/mcp devtools
|
|
470
|
-
`
|
|
471
|
-
);
|
|
472
|
-
process.exit(1);
|
|
473
|
-
}
|
|
474
|
-
});
|
|
475
|
-
server.listen(this.uiPort, this.host, () => {
|
|
476
|
-
const uiUrl = `http://localhost:${this.uiPort}`;
|
|
477
|
-
process.stderr.write(`[OIMDB DevTools] UI ready \u2192 ${uiUrl} (bound to ${this.host})
|
|
478
|
-
`);
|
|
479
|
-
if (this.shouldOpen) this.openURL(uiUrl);
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
openURL(url) {
|
|
483
|
-
const cmd = process.platform === "win32" ? `start "" "${url}"` : process.platform === "darwin" ? `open "${url}"` : `xdg-open "${url}"`;
|
|
484
|
-
(0, import_child_process.exec)(cmd, (err) => {
|
|
485
|
-
if (err) process.stderr.write(`[OIMDB DevTools] Could not open browser: ${err.message}
|
|
486
|
-
`);
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
start() {
|
|
490
|
-
this.bridge.start();
|
|
491
|
-
this.setupHTTP();
|
|
492
|
-
process.stderr.write(
|
|
493
|
-
`[OIMDB DevTools] Add to debug.ts: oimDevRegistry.connect();
|
|
494
|
-
`
|
|
495
|
-
);
|
|
82
|
+
if (this.devModuleStateSource !== void 0) await this.devModuleStateSource.loadDevRegistry();
|
|
83
|
+
if (this.devBridge !== void 0) this.devBridge.start();
|
|
84
|
+
else process.stderr.write("[OIMDB MCP] In-process model loaded \u2014 serving offline (no browser bridge).\n");
|
|
85
|
+
await this.mcpServer.connect(new import_stdio.StdioServerTransport());
|
|
496
86
|
}
|
|
497
87
|
};
|
|
498
88
|
|
|
@@ -500,13 +90,8 @@ Set a different port: OIMDB_UI_PORT=7434 npx @oimdb/mcp devtools
|
|
|
500
90
|
function createOIMDevMcpServer(options) {
|
|
501
91
|
return new OIMDevMcpServer(options);
|
|
502
92
|
}
|
|
503
|
-
function createOIMDevUIServer(options) {
|
|
504
|
-
return new OIMDevUIServer(options);
|
|
505
|
-
}
|
|
506
93
|
// Annotate the CommonJS export names for ESM import in node:
|
|
507
94
|
0 && (module.exports = {
|
|
508
95
|
OIMDevMcpServer,
|
|
509
|
-
|
|
510
|
-
createOIMDevMcpServer,
|
|
511
|
-
createOIMDevUIServer
|
|
96
|
+
createOIMDevMcpServer
|
|
512
97
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
inspect(): unknown;
|
|
3
|
-
dumpString(): string;
|
|
4
|
-
}
|
|
1
|
+
import { IOIMDevRegistry } from '@oimdb/cli';
|
|
5
2
|
|
|
6
3
|
type TOIMDevMcpOptions = {
|
|
7
4
|
/** Default 7432; unused when `registry` is given — no bridge is started. */
|
|
@@ -17,77 +14,14 @@ type TOIMDevMcpOptions = {
|
|
|
17
14
|
|
|
18
15
|
declare class OIMDevMcpServer {
|
|
19
16
|
private readonly mcpServer;
|
|
20
|
-
private readonly
|
|
21
|
-
private readonly
|
|
22
|
-
private
|
|
23
|
-
private readonly modelModulePath?;
|
|
24
|
-
private modelModuleLoaded;
|
|
17
|
+
private readonly devBridge;
|
|
18
|
+
private readonly devModuleStateSource;
|
|
19
|
+
private readonly devStateReader;
|
|
25
20
|
constructor(options?: TOIMDevMcpOptions);
|
|
26
|
-
/**
|
|
27
|
-
* Resolves the in-process registry, loading the model module on first use.
|
|
28
|
-
* Returns null when no in-process source is configured (browser-bridge mode).
|
|
29
|
-
*/
|
|
30
|
-
private getInProcessRegistry;
|
|
31
|
-
/** Reads the inspect snapshot from the in-process model or the browser. */
|
|
32
|
-
private readInspect;
|
|
33
|
-
/** Reads the human-readable dump from the in-process model or the browser. */
|
|
34
|
-
private readDump;
|
|
35
21
|
private registerTools;
|
|
36
|
-
private requestBrowser;
|
|
37
22
|
start(): Promise<void>;
|
|
38
23
|
}
|
|
39
24
|
|
|
40
|
-
type TOIMDevUIServerOptions = {
|
|
41
|
-
wsPort?: number;
|
|
42
|
-
uiPort?: number;
|
|
43
|
-
uiDir: string;
|
|
44
|
-
open?: boolean;
|
|
45
|
-
/**
|
|
46
|
-
* The interface both ports bind to. LOOPBACK BY DEFAULT: a tool that hands
|
|
47
|
-
* out the application's entire data model has no business being reachable
|
|
48
|
-
* from the café Wi-Fi, and `listen` without a host binds every interface.
|
|
49
|
-
*
|
|
50
|
-
* Set it — `'0.0.0.0'` — when the browser genuinely runs somewhere else: a
|
|
51
|
-
* container, a VM, a phone. That is an opt-in to everyone who can route
|
|
52
|
-
* there reading the model.
|
|
53
|
-
*/
|
|
54
|
-
host?: string;
|
|
55
|
-
/**
|
|
56
|
-
* Page origins allowed onto the bridge and allowed to read `/api/inspect`,
|
|
57
|
-
* on top of the loopback origins allowed by default.
|
|
58
|
-
*
|
|
59
|
-
* Exact origins (`'http://app.test:3000'`). Never `'*'`: with that on every
|
|
60
|
-
* response, any page in any tab the developer has open can read every entity
|
|
61
|
-
* of every registered collection.
|
|
62
|
-
*/
|
|
63
|
-
allowedOrigins?: readonly string[];
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
declare class OIMDevUIServer {
|
|
67
|
-
private readonly uiPort;
|
|
68
|
-
private readonly shouldOpen;
|
|
69
|
-
private readonly host;
|
|
70
|
-
private readonly allowedOrigins;
|
|
71
|
-
private readonly devUIAssetCatalog;
|
|
72
|
-
private readonly bridge;
|
|
73
|
-
constructor(options: TOIMDevUIServerOptions);
|
|
74
|
-
/**
|
|
75
|
-
* While bound to loopback, answer only what was addressed to a loopback
|
|
76
|
-
* name.
|
|
77
|
-
*
|
|
78
|
-
* Without this, a page on any site can point a hostname it controls at
|
|
79
|
-
* 127.0.0.1 and become same-origin with this server. The `Host` header is
|
|
80
|
-
* the part of that a browser cannot forge. Binding elsewhere is an explicit
|
|
81
|
-
* decision, so the check stands down there.
|
|
82
|
-
*/
|
|
83
|
-
private isRequestAddressedHere;
|
|
84
|
-
private requestBrowser;
|
|
85
|
-
private setupHTTP;
|
|
86
|
-
private openURL;
|
|
87
|
-
start(): void;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
25
|
declare function createOIMDevMcpServer(options?: TOIMDevMcpOptions): OIMDevMcpServer;
|
|
91
|
-
declare function createOIMDevUIServer(options: TOIMDevUIServerOptions): OIMDevUIServer;
|
|
92
26
|
|
|
93
|
-
export {
|
|
27
|
+
export { OIMDevMcpServer, type TOIMDevMcpOptions, createOIMDevMcpServer };
|