@divlet/mcp 0.0.2 → 0.0.3
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 +7 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +96 -0
- package/dist/lib/connect.d.ts +14 -0
- package/dist/lib/connect.js +155 -0
- package/dist/package.json +3 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -5,7 +5,13 @@ The MCP stdio adapter for divlet. This package owns its implementation and depen
|
|
|
5
5
|
Install:
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npx -y @divlet/mcp@0.0.
|
|
8
|
+
npx -y @divlet/mcp@0.0.3 --cwd ./workspace
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
From 0.0.3: connect to a website that is its own MCP provider (the tools run in a managed browser page; `playwright-core` drives the installed Chrome or Playwright's Chromium):
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx -y @divlet/mcp --connect https://llblab.github.io/divlet/ [--allow-tools a,b] [--headless] [--profile <dir>]
|
|
9
15
|
```
|
|
10
16
|
|
|
11
17
|
The installed binary is `divlet-mcp`. Importing the package exposes `runMcp` without starting a service; the executable starts it explicitly. Protocol output uses stdout; diagnostics use stderr. Checkout commands use `node scripts/source.mjs mcp/index.js` from the repository root.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
export function runMcp(argv?: string[]): {
|
|
3
|
+
connect: {
|
|
4
|
+
receive(line: any): Promise<void>;
|
|
5
|
+
readonly connection: any;
|
|
6
|
+
close(): Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
} | {
|
|
3
9
|
session: import("@divlet/core/contract").Session;
|
|
4
10
|
server: {
|
|
5
11
|
receive(line: any): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* divlet over MCP (stdio), for any MCP client: Claude Desktop, an editor, a script.
|
|
4
4
|
*
|
|
5
5
|
* npx -y @divlet/mcp [--cwd <dir>] [--project <file.divlet>] [--port <0..65535>] [--no-demo]
|
|
6
|
+
* npx -y @divlet/mcp --connect <https://site/> [--allow-tools a,b] [--headless] [--profile <dir>]
|
|
6
7
|
*
|
|
7
8
|
* Nothing starts at import time: the board opens when a tool asks for it, and the process ends when stdin closes or a
|
|
8
9
|
* termination signal arrives, closing only the boards it owns (a board another client owns keeps running for that client).
|
|
@@ -10,6 +11,7 @@
|
|
|
10
11
|
import path from 'node:path';
|
|
11
12
|
import { createSession } from '@divlet/core/contract';
|
|
12
13
|
import { createMcpServer, localBoardOpener, wireBoardAgent } from './lib/server.js';
|
|
14
|
+
import { createConnectServer } from './lib/connect.js';
|
|
13
15
|
import metadata from '#package' with { type: 'json' };
|
|
14
16
|
const VERSION = metadata.version;
|
|
15
17
|
function parseArgs(argv) {
|
|
@@ -44,11 +46,55 @@ function parseArgs(argv) {
|
|
|
44
46
|
options.createDemo = false;
|
|
45
47
|
continue;
|
|
46
48
|
}
|
|
49
|
+
if (arg === '--connect' && value) {
|
|
50
|
+
options.connect = value;
|
|
51
|
+
i++;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (arg === '--allow-tools' && value) {
|
|
55
|
+
options.allowTools = value.split(',').map(name => name.trim()).filter(Boolean);
|
|
56
|
+
i++;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (arg === '--allow-origin' && value) {
|
|
60
|
+
(options.allowOrigins ??= []).push(value);
|
|
61
|
+
i++;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (arg === '--browser' && value) {
|
|
65
|
+
options.browser = path.resolve(value);
|
|
66
|
+
i++;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (arg === '--profile' && value) {
|
|
70
|
+
options.profile = path.resolve(value);
|
|
71
|
+
i++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (arg === '--headless') {
|
|
75
|
+
options.headless = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (arg === '--headed') {
|
|
79
|
+
options.headless = false;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
47
82
|
if (arg.startsWith('--'))
|
|
48
83
|
throw new Error(`Unknown option: ${arg}`);
|
|
49
84
|
}
|
|
50
85
|
if (!Number.isInteger(options.port) || options.port < 0 || options.port > 65535)
|
|
51
86
|
throw new Error('--port takes 0..65535.');
|
|
87
|
+
if (options.connect !== undefined) {
|
|
88
|
+
let url;
|
|
89
|
+
try {
|
|
90
|
+
url = new URL(options.connect);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
throw new Error('--connect takes an http(s) URL.');
|
|
94
|
+
}
|
|
95
|
+
if (!['https:', 'http:'].includes(url.protocol))
|
|
96
|
+
throw new Error('--connect takes an http(s) URL.');
|
|
97
|
+
}
|
|
52
98
|
return options;
|
|
53
99
|
}
|
|
54
100
|
const HELP = `divlet-mcp — divlet over MCP (stdio)
|
|
@@ -60,6 +106,14 @@ Usage: divlet-mcp [options]
|
|
|
60
106
|
--no-demo do not seed a starter frame when a new project is created
|
|
61
107
|
--help, --version
|
|
62
108
|
|
|
109
|
+
Connect to a website that is its own MCP provider (no local board; tools run in the page):
|
|
110
|
+
--connect <url> open this site in a managed browser and offer its tools (for example https://llblab.github.io/divlet/)
|
|
111
|
+
--allow-tools <a,b> offer only these website tools (default: all the site lists)
|
|
112
|
+
--allow-origin <o> an additional origin the site may redirect to (default: none)
|
|
113
|
+
--headless/--headed hide or show the browser window (default: shown when a display is available)
|
|
114
|
+
--browser <path> Chromium-based browser to launch (default: installed Chrome, then Playwright's Chromium)
|
|
115
|
+
--profile <dir> keep the site's browser storage in this directory (default: a fresh profile per run)
|
|
116
|
+
|
|
63
117
|
Speaks MCP on stdin/stdout; diagnostics go to stderr. The board is the single writer of its project: if another client
|
|
64
118
|
already opened it, tool calls are forwarded to that board instead of opening a second one.`;
|
|
65
119
|
let session, server;
|
|
@@ -82,6 +136,8 @@ export function runMcp(argv = process.argv.slice(2)) {
|
|
|
82
136
|
process.exit(0);
|
|
83
137
|
}
|
|
84
138
|
const log = message => process.stderr.write(`${message}\n`);
|
|
139
|
+
if (options.connect)
|
|
140
|
+
return runConnect(options, log);
|
|
85
141
|
session = createSession({
|
|
86
142
|
cwd: options.cwd,
|
|
87
143
|
openBoard: localBoardOpener(),
|
|
@@ -127,6 +183,46 @@ export function runMcp(argv = process.argv.slice(2)) {
|
|
|
127
183
|
log(`divlet-mcp ${VERSION} ready (stdio${options.project ? `, project ${path.resolve(options.cwd, options.project)}` : ''})`);
|
|
128
184
|
return { session, server };
|
|
129
185
|
}
|
|
186
|
+
/** Stdio framing for the connect mode: requests are handled concurrently and the browser closes with stdin. */
|
|
187
|
+
function runConnect(options, log) {
|
|
188
|
+
const display = process.platform === 'darwin' || process.platform === 'win32' || !!process.env.DISPLAY || !!process.env.WAYLAND_DISPLAY;
|
|
189
|
+
const connect = createConnectServer({
|
|
190
|
+
url: options.connect,
|
|
191
|
+
version: VERSION,
|
|
192
|
+
log,
|
|
193
|
+
send: chunk => process.stdout.write(chunk),
|
|
194
|
+
browserOptions: {
|
|
195
|
+
headless: options.headless ?? !display,
|
|
196
|
+
...(options.allowTools ? { allowTools: options.allowTools } : {}),
|
|
197
|
+
...(options.allowOrigins ? { allowedOrigins: options.allowOrigins } : {}),
|
|
198
|
+
...(options.browser ? { executablePath: options.browser } : {}),
|
|
199
|
+
...(options.profile ? { userDataDir: options.profile } : {}),
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
let stopping = false;
|
|
203
|
+
const stop = async (code = 0) => { if (stopping)
|
|
204
|
+
return; stopping = true; await connect.close().catch(() => { }); process.exit(code); };
|
|
205
|
+
process.stdout.on('error', () => stop(0));
|
|
206
|
+
process.on('SIGTERM', () => stop(0));
|
|
207
|
+
process.on('SIGINT', () => stop(0));
|
|
208
|
+
process.on('unhandledRejection', error => { log(`divlet-mcp: ${error?.stack || error}`); });
|
|
209
|
+
let pending = '';
|
|
210
|
+
process.stdin.setEncoding('utf8');
|
|
211
|
+
process.stdin.on('data', chunk => {
|
|
212
|
+
pending += chunk;
|
|
213
|
+
let at;
|
|
214
|
+
while ((at = pending.indexOf('\n')) >= 0) {
|
|
215
|
+
const line = pending.slice(0, at);
|
|
216
|
+
pending = pending.slice(at + 1);
|
|
217
|
+
if (line.trim())
|
|
218
|
+
connect.receive(line);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
process.stdin.on('end', () => { if (pending.trim())
|
|
222
|
+
connect.receive(pending); stop(0); });
|
|
223
|
+
log(`divlet-mcp ${VERSION} ready (stdio, connecting to ${new URL(options.connect).origin})`);
|
|
224
|
+
return { connect };
|
|
225
|
+
}
|
|
130
226
|
export { session, server };
|
|
131
227
|
if (import.meta.main)
|
|
132
228
|
runMcp();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function createConnectServer({ url, browserOptions, send, log, version, connect }: {
|
|
2
|
+
url: any;
|
|
3
|
+
browserOptions?: {} | undefined;
|
|
4
|
+
send: any;
|
|
5
|
+
log?: (() => void) | undefined;
|
|
6
|
+
version?: string | undefined;
|
|
7
|
+
connect?: typeof connectBrowserMcp | undefined;
|
|
8
|
+
}): {
|
|
9
|
+
/** Handle one line; requests run concurrently, each answered once. */
|
|
10
|
+
receive(line: any): Promise<void>;
|
|
11
|
+
readonly connection: any;
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
import { connectBrowserMcp } from '@divlet/core/browser-mcp';
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* `divlet-mcp --connect <url>`: the agent-side adapter for a website that is its own MCP provider. The agent host speaks MCP
|
|
3
|
+
* over stdio with this process; tool calls run inside the website, in a managed browser this process opens with
|
|
4
|
+
* `@divlet/core/browser-mcp`. Nothing listens on a port and nothing is deployed by the website's author.
|
|
5
|
+
*
|
|
6
|
+
* Both protocol eras are answered on stdio: a legacy client opens with `initialize`, a 2026-07-28 client sends per-request
|
|
7
|
+
* `_meta` (and may probe with `server/discover`). The browser side always uses 2026-07-28.
|
|
8
|
+
*
|
|
9
|
+
* Trust: the URL the user configured is the trusted origin; redirects elsewhere fail and leaving it closes the connection.
|
|
10
|
+
* Website-supplied descriptions are passed to the agent marked as website text, behaviour annotations are dropped (they are
|
|
11
|
+
* claims, not guarantees), results are reduced to text and structured content, and `--allow-tools` narrows what is offered.
|
|
12
|
+
*/
|
|
13
|
+
import { connectBrowserMcp, BrowserMcpError, BrowserMcpRemoteError } from '@divlet/core/browser-mcp';
|
|
14
|
+
const LEGACY = new Set(['2024-11-05', '2025-03-26', '2025-06-18', '2025-11-25']);
|
|
15
|
+
const MODERN = '2026-07-28';
|
|
16
|
+
const META_VERSION = 'io.modelcontextprotocol/protocolVersion';
|
|
17
|
+
const MAX_TEXT = 1024 * 1024;
|
|
18
|
+
const isObject = value => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
19
|
+
/** Only text and JSON travel back to the agent: no website-chosen images, links or embedded resources. */
|
|
20
|
+
function sanitizeResult(result) {
|
|
21
|
+
const content = (Array.isArray(result?.content) ? result.content : []).filter(block => block?.type === 'text' && typeof block.text === 'string').map(block => ({ type: 'text', text: block.text.slice(0, MAX_TEXT) }));
|
|
22
|
+
const out = { content: content.length ? content : [{ type: 'text', text: '(no text result)' }], isError: result?.isError === true };
|
|
23
|
+
if (isObject(result?.structuredContent))
|
|
24
|
+
out.structuredContent = result.structuredContent;
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
export function createConnectServer({ url, browserOptions = {}, send, log = () => { }, version = '0.0.0', connect = connectBrowserMcp }) {
|
|
28
|
+
const origin = new URL(url).origin;
|
|
29
|
+
let connection = null;
|
|
30
|
+
let connecting = null;
|
|
31
|
+
let initialized = false;
|
|
32
|
+
let lastTools = '';
|
|
33
|
+
const inflight = new Map();
|
|
34
|
+
const write = message => send(JSON.stringify(message) + '\n');
|
|
35
|
+
const modern = params => isObject(params?._meta) && typeof params._meta[META_VERSION] === 'string';
|
|
36
|
+
const reply = (id, value, params) => write({ jsonrpc: '2.0', id, result: modern(params) ? { resultType: 'complete', ...value, _meta: { 'io.modelcontextprotocol/serverInfo': { name: 'divlet-connect', version } } } : value });
|
|
37
|
+
const failure = (id, code, message, data) => write({ jsonrpc: '2.0', id, error: { code, message, ...(data !== undefined ? { data } : {}) } });
|
|
38
|
+
/** One live connection; a stale one (the page reloaded) is reconnected explicitly, a closed one is opened again. */
|
|
39
|
+
async function ready() {
|
|
40
|
+
if (connection?.state === 'ready')
|
|
41
|
+
return connection;
|
|
42
|
+
if (!connecting)
|
|
43
|
+
connecting = (async () => {
|
|
44
|
+
try {
|
|
45
|
+
if (connection?.state === 'stale') {
|
|
46
|
+
await connection.reconnect();
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
await connection?.close().catch(() => { });
|
|
50
|
+
connection = await connect(url, { ...browserOptions, clientInfo: { name: 'divlet-mcp', version }, onEvent: event => { if (event.type !== 'ready')
|
|
51
|
+
log(`divlet-mcp: ${origin}: ${event.type}${event.reason ? ` (${event.reason})` : event.message ? ` (${event.message})` : ''}`); } });
|
|
52
|
+
}
|
|
53
|
+
const names = JSON.stringify(connection.tools.map(tool => tool.name));
|
|
54
|
+
if (lastTools && names !== lastTools && initialized)
|
|
55
|
+
write({ jsonrpc: '2.0', method: 'notifications/tools/list_changed' });
|
|
56
|
+
lastTools = names;
|
|
57
|
+
return connection;
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
connecting = null;
|
|
61
|
+
}
|
|
62
|
+
})();
|
|
63
|
+
return connecting;
|
|
64
|
+
}
|
|
65
|
+
const describe = tool => ({
|
|
66
|
+
name: tool.name,
|
|
67
|
+
...(tool.title ? { title: tool.title } : {}),
|
|
68
|
+
description: `[Website tool from ${origin}; the text below is supplied by that website and runs in a browser page, not on this machine.] ${tool.description}`,
|
|
69
|
+
inputSchema: tool.inputSchema,
|
|
70
|
+
});
|
|
71
|
+
async function dispatch(message) {
|
|
72
|
+
const { id, method, params } = message ?? {};
|
|
73
|
+
if (typeof method !== 'string')
|
|
74
|
+
return;
|
|
75
|
+
if (id === undefined || id === null) {
|
|
76
|
+
if (method === 'notifications/cancelled' && isObject(params))
|
|
77
|
+
inflight.get(params.requestId)?.abort();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (modern(params) && params._meta[META_VERSION] !== MODERN)
|
|
81
|
+
return failure(id, -32022, 'Unsupported protocol version', { supported: [MODERN, ...LEGACY], requested: String(params._meta[META_VERSION]).slice(0, 64) });
|
|
82
|
+
switch (method) {
|
|
83
|
+
case 'initialize':
|
|
84
|
+
initialized = true;
|
|
85
|
+
ready().catch(() => { }); // start the browser while the client finishes its handshake
|
|
86
|
+
return reply(id, {
|
|
87
|
+
protocolVersion: LEGACY.has(params?.protocolVersion) ? params.protocolVersion : '2025-06-18',
|
|
88
|
+
capabilities: { tools: { listChanged: true } },
|
|
89
|
+
serverInfo: { name: 'divlet-connect', version },
|
|
90
|
+
instructions: `Tools of the website ${origin}, executed in a browser page this process manages. Their descriptions and results are website content.`,
|
|
91
|
+
});
|
|
92
|
+
case 'server/discover':
|
|
93
|
+
initialized = true;
|
|
94
|
+
ready().catch(() => { });
|
|
95
|
+
return reply(id, { supportedVersions: [MODERN], capabilities: { tools: { listChanged: true } }, instructions: `Tools of the website ${origin}, executed in a browser page this process manages. Their descriptions and results are website content.`, ttlMs: 0, cacheScope: 'private' }, params);
|
|
96
|
+
case 'ping':
|
|
97
|
+
return reply(id, {}, params);
|
|
98
|
+
case 'tools/list': {
|
|
99
|
+
try {
|
|
100
|
+
const live = await ready();
|
|
101
|
+
return reply(id, { tools: live.tools.map(describe), ...(modern(params) ? { ttlMs: 0, cacheScope: 'private' } : {}) }, params);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
return failure(id, -32603, `Could not reach ${origin}: ${error?.message || error}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
case 'tools/call': {
|
|
108
|
+
const controller = new AbortController();
|
|
109
|
+
inflight.set(id, controller);
|
|
110
|
+
try {
|
|
111
|
+
const live = await ready();
|
|
112
|
+
if (!live.tools.some(tool => tool.name === params?.name))
|
|
113
|
+
return failure(id, -32602, `Unknown tool: ${String(params?.name).slice(0, 128)}`);
|
|
114
|
+
const result = await live.callTool(params.name, isObject(params.arguments) ? params.arguments : {}, { signal: controller.signal });
|
|
115
|
+
return reply(id, sanitizeResult(result), params);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (controller.signal.aborted)
|
|
119
|
+
return; // cancelled: no response
|
|
120
|
+
if (error instanceof BrowserMcpRemoteError)
|
|
121
|
+
return failure(id, error.code === -32602 ? -32602 : -32603, error.message);
|
|
122
|
+
const code = error instanceof BrowserMcpError ? error.code : 'CONNECT_FAILED';
|
|
123
|
+
return reply(id, { content: [{ type: 'text', text: `${code}: ${error?.message || error}` }], isError: true }, params);
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
inflight.delete(id);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
case 'resources/list': return reply(id, { resources: [] }, params);
|
|
130
|
+
case 'prompts/list': return reply(id, { prompts: [] }, params);
|
|
131
|
+
default: return failure(id, -32601, `Unknown method: ${method}.`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
/** Handle one line; requests run concurrently, each answered once. */
|
|
136
|
+
async receive(line) {
|
|
137
|
+
let message;
|
|
138
|
+
try {
|
|
139
|
+
message = JSON.parse(String(line));
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
write({ jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error.' } });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (!isObject(message)) {
|
|
146
|
+
write({ jsonrpc: '2.0', id: null, error: { code: -32600, message: 'Invalid request.' } });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
await dispatch(message).catch(error => log(`divlet-mcp: ${error?.message || error}`));
|
|
150
|
+
},
|
|
151
|
+
get connection() { return connection; },
|
|
152
|
+
async close() { for (const controller of inflight.values())
|
|
153
|
+
controller.abort(); await connection?.close().catch(() => { }); },
|
|
154
|
+
};
|
|
155
|
+
}
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@divlet/mcp",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The divlet MCP stdio adapter",
|
|
6
6
|
"bin": {
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"#package": "./package.json"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@divlet/core": "0.0.
|
|
33
|
+
"@divlet/core": "0.0.3",
|
|
34
|
+
"playwright-core": "^1.62.1"
|
|
34
35
|
},
|
|
35
36
|
"files": [
|
|
36
37
|
"dist",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@divlet/mcp",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The divlet MCP stdio adapter",
|
|
6
6
|
"bin": {
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"#package": "./package.json"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@divlet/core": "0.0.
|
|
33
|
+
"@divlet/core": "0.0.3",
|
|
34
|
+
"playwright-core": "^1.62.1"
|
|
34
35
|
},
|
|
35
36
|
"files": [
|
|
36
37
|
"dist",
|