surfcad-mcp 0.0.1 → 0.1.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 +71 -1
- package/bin/surfcad-mcp.js +7 -0
- package/data/commands.json +14368 -0
- package/package.json +40 -9
- package/src/catalog.mjs +26 -0
- package/src/errors.mjs +21 -0
- package/src/nodeClient.mjs +42 -0
- package/src/packageVersion.mjs +16 -0
- package/src/relay.mjs +119 -0
- package/src/relayMessages.mjs +16 -0
- package/src/server.mjs +142 -0
package/package.json
CHANGED
|
@@ -1,14 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "surfcad-mcp",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "SurfCAD MCP server (
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SurfCAD agent bridge MCP server (stdio) — pairs to a browser tab via the local WebSocket relay",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "artur0x0",
|
|
7
|
+
"homepage": "https://github.com/artur0x0/surfcad/tree/main/packages/surfcad-mcp",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/artur0x0/surfcad.git",
|
|
11
|
+
"directory": "packages/surfcad-mcp"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"surfcad",
|
|
15
|
+
"mcp",
|
|
16
|
+
"model-context-protocol",
|
|
17
|
+
"cad",
|
|
18
|
+
"agent",
|
|
19
|
+
"stdio",
|
|
20
|
+
"websocket"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18"
|
|
25
|
+
},
|
|
26
|
+
"bin": {
|
|
27
|
+
"surfcad-mcp": "./bin/surfcad-mcp.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"bin",
|
|
31
|
+
"data",
|
|
32
|
+
"src",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
6
35
|
"scripts": {
|
|
7
|
-
"
|
|
36
|
+
"prepack": "node ./scripts/prepack-check.mjs",
|
|
37
|
+
"start": "node ./bin/surfcad-mcp.js",
|
|
38
|
+
"test": "node --test test/*.test.mjs",
|
|
39
|
+
"smoke:pack": "node --test test/packInstall.test.mjs"
|
|
8
40
|
},
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"mcpName": "io.github.artur0x0/surfcad"
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
43
|
+
"ws": "^8.22.0"
|
|
44
|
+
}
|
|
14
45
|
}
|
package/src/catalog.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
const PKG_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
6
|
+
export const COMMANDS_JSON_REL = 'data/commands.json';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @returns {{ version: number, count: number, commands: Array<{ name: string, description: string, args: object }> }}
|
|
10
|
+
*/
|
|
11
|
+
export function loadCommandsCatalog(catalogPath = join(PKG_ROOT, COMMANDS_JSON_REL)) {
|
|
12
|
+
const raw = readFileSync(catalogPath, 'utf8');
|
|
13
|
+
const doc = JSON.parse(raw);
|
|
14
|
+
if (!doc || typeof doc !== 'object' || !Array.isArray(doc.commands)) {
|
|
15
|
+
throw new Error('Invalid commands catalog');
|
|
16
|
+
}
|
|
17
|
+
return doc;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function bridgeToolDefinitions(doc) {
|
|
21
|
+
return doc.commands.map((row) => ({
|
|
22
|
+
name: row.name,
|
|
23
|
+
description: row.description,
|
|
24
|
+
inputSchema: row.args,
|
|
25
|
+
}));
|
|
26
|
+
}
|
package/src/errors.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {{ code?: string, message?: string, details?: unknown }} err
|
|
3
|
+
*/
|
|
4
|
+
export function formatBridgeError(err) {
|
|
5
|
+
const code = err?.code || 'bridge_error';
|
|
6
|
+
const message = err?.message || 'Bridge call failed';
|
|
7
|
+
const payload = { code, message };
|
|
8
|
+
if (err?.details !== undefined) payload.details = err.details;
|
|
9
|
+
return payload;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function notPairedError() {
|
|
13
|
+
return {
|
|
14
|
+
code: 'not_paired',
|
|
15
|
+
message: 'Not paired to a SurfCAD tab. Set SURFCAD_PAIRING_CODE or call the pair tool, then click Allow in the app.',
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function pairingEnvHint() {
|
|
20
|
+
return 'Copy the 6-character code from SurfCAD → Local agent → Show pairing code.';
|
|
21
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node WebSocket client for the M1 agent-bridge relay.
|
|
3
|
+
*/
|
|
4
|
+
import { WebSocket } from 'ws';
|
|
5
|
+
import { PAIR_MSG } from './relayMessages.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} url
|
|
9
|
+
* @param {string} token
|
|
10
|
+
* @returns {Promise<WebSocket>}
|
|
11
|
+
*/
|
|
12
|
+
export function connectPeer(url, token) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const ws = new WebSocket(url);
|
|
15
|
+
const fail = (err) => {
|
|
16
|
+
ws.removeAllListeners('message');
|
|
17
|
+
try { ws.close(); } catch { /* noop */ }
|
|
18
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
19
|
+
};
|
|
20
|
+
ws.once('open', () => {
|
|
21
|
+
ws.send(JSON.stringify({ type: PAIR_MSG.PAIR, token: token.trim().toUpperCase() }));
|
|
22
|
+
});
|
|
23
|
+
ws.on('message', (raw) => {
|
|
24
|
+
let msg;
|
|
25
|
+
try {
|
|
26
|
+
msg = JSON.parse(String(raw));
|
|
27
|
+
} catch {
|
|
28
|
+
fail(new Error('Invalid JSON from relay'));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (msg?.type === PAIR_MSG.PAIRED) {
|
|
32
|
+
ws.removeAllListeners('message');
|
|
33
|
+
resolve(ws);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (msg?.type === PAIR_MSG.ERROR || (msg?.code && !msg?.id)) {
|
|
37
|
+
fail(new Error(msg.message || msg.code || 'Pairing failed'));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
ws.once('error', fail);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
const PKG_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
6
|
+
|
|
7
|
+
/** @returns {string} Semver from packages/surfcad-mcp/package.json */
|
|
8
|
+
export function loadPackageVersion() {
|
|
9
|
+
const raw = readFileSync(join(PKG_ROOT, 'package.json'), 'utf8');
|
|
10
|
+
const doc = JSON.parse(raw);
|
|
11
|
+
const version = doc?.version;
|
|
12
|
+
if (typeof version !== 'string' || !version.trim()) {
|
|
13
|
+
throw new Error('package.json is missing a version field');
|
|
14
|
+
}
|
|
15
|
+
return version.trim();
|
|
16
|
+
}
|
package/src/relay.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { WebSocket } from 'ws';
|
|
2
|
+
import { connectPeer } from './nodeClient.mjs';
|
|
3
|
+
import { PAIR_MSG } from './relayMessages.mjs';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_WS_URL = 'ws://127.0.0.1:17341';
|
|
6
|
+
|
|
7
|
+
export function defaultRelayUrl() {
|
|
8
|
+
const fromEnv = process.env.SURFCAD_AGENT_BRIDGE_WS_URL;
|
|
9
|
+
if (typeof fromEnv === 'string' && fromEnv.trim()) return fromEnv.trim();
|
|
10
|
+
return DEFAULT_WS_URL;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {WebSocket} ws
|
|
15
|
+
* @param {string} command
|
|
16
|
+
* @param {Record<string, unknown>} args
|
|
17
|
+
* @param {string | number} id
|
|
18
|
+
*/
|
|
19
|
+
export function invokeBridge(ws, command, args = {}, id = '1') {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const onMessage = (raw) => {
|
|
22
|
+
let msg;
|
|
23
|
+
try {
|
|
24
|
+
msg = JSON.parse(String(raw));
|
|
25
|
+
} catch {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (msg?.id !== id) return;
|
|
29
|
+
ws.off('message', onMessage);
|
|
30
|
+
if (msg.ok === true) {
|
|
31
|
+
resolve(msg.result);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const error = msg.error && typeof msg.error === 'object'
|
|
35
|
+
? msg.error
|
|
36
|
+
: { code: 'bridge_error', message: 'Command refused' };
|
|
37
|
+
reject(Object.assign(new Error(error.message || error.code || 'Bridge call failed'), {
|
|
38
|
+
code: error.code,
|
|
39
|
+
details: error.details,
|
|
40
|
+
}));
|
|
41
|
+
};
|
|
42
|
+
ws.on('message', onMessage);
|
|
43
|
+
ws.send(JSON.stringify({ id, command, args }));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class RelaySession {
|
|
48
|
+
/** @type {WebSocket | null} */
|
|
49
|
+
#ws = null;
|
|
50
|
+
#url;
|
|
51
|
+
#code = '';
|
|
52
|
+
|
|
53
|
+
constructor(options = {}) {
|
|
54
|
+
this.#url = options.url || defaultRelayUrl();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get url() {
|
|
58
|
+
return this.#url;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get pairingCode() {
|
|
62
|
+
return this.#code;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
isPaired() {
|
|
66
|
+
return this.#ws != null && this.#ws.readyState === WebSocket.OPEN;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {string} token
|
|
71
|
+
*/
|
|
72
|
+
async pair(token) {
|
|
73
|
+
const code = String(token || '').trim().toUpperCase();
|
|
74
|
+
if (!code) {
|
|
75
|
+
throw Object.assign(new Error('Pairing code is required'), { code: 'bad_pairing_code' });
|
|
76
|
+
}
|
|
77
|
+
if (this.#ws) {
|
|
78
|
+
try { this.#ws.close(); } catch { /* noop */ }
|
|
79
|
+
this.#ws = null;
|
|
80
|
+
}
|
|
81
|
+
this.#code = code;
|
|
82
|
+
try {
|
|
83
|
+
this.#ws = await connectPeer(this.#url, code);
|
|
84
|
+
} catch (err) {
|
|
85
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
86
|
+
if (/rejected/i.test(message) || message.includes('pair_rejected')) {
|
|
87
|
+
throw Object.assign(new Error('Pairing rejected in SurfCAD (user clicked Deny)'), {
|
|
88
|
+
code: 'pair_rejected',
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (/unknown_token/i.test(message)) {
|
|
92
|
+
throw Object.assign(new Error('No SurfCAD tab is showing this pairing code'), {
|
|
93
|
+
code: 'unknown_token',
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
throw Object.assign(new Error(message), { code: err?.code || 'pair_failed' });
|
|
97
|
+
}
|
|
98
|
+
this.#ws.on('close', () => {
|
|
99
|
+
this.#ws = null;
|
|
100
|
+
});
|
|
101
|
+
return { paired: true, token: code, relay: this.#url };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async call(command, args = {}) {
|
|
105
|
+
if (!this.isPaired()) {
|
|
106
|
+
throw Object.assign(new Error('Not paired'), { code: 'not_paired' });
|
|
107
|
+
}
|
|
108
|
+
return invokeBridge(this.#ws, command, args, `mcp-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
disconnect() {
|
|
112
|
+
if (this.#ws) {
|
|
113
|
+
try { this.#ws.close(); } catch { /* noop */ }
|
|
114
|
+
this.#ws = null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export { PAIR_MSG };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire message types for the SurfCAD M1 agent-bridge relay (browser tab ↔ local peer).
|
|
3
|
+
* Kept in sync with `src/agent/ws/relayHub.js` in the SurfCAD monorepo.
|
|
4
|
+
*/
|
|
5
|
+
export const PAIR_MSG = Object.freeze({
|
|
6
|
+
ERROR: 'error',
|
|
7
|
+
REGISTER_TAB: 'register_tab',
|
|
8
|
+
PAIR: 'pair',
|
|
9
|
+
PAIR_REQUEST: 'pair_request',
|
|
10
|
+
PAIR_APPROVE: 'pair_approve',
|
|
11
|
+
PAIR_REJECT: 'pair_reject',
|
|
12
|
+
PAIRED: 'paired',
|
|
13
|
+
UNPAIRED: 'unpaired',
|
|
14
|
+
CALL: 'call',
|
|
15
|
+
RESPONSE: 'response',
|
|
16
|
+
});
|
package/src/server.mjs
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import {
|
|
4
|
+
CallToolRequestSchema,
|
|
5
|
+
ListToolsRequestSchema,
|
|
6
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
import { bridgeToolDefinitions, loadCommandsCatalog } from './catalog.mjs';
|
|
8
|
+
import { formatBridgeError, notPairedError, pairingEnvHint } from './errors.mjs';
|
|
9
|
+
import { loadPackageVersion } from './packageVersion.mjs';
|
|
10
|
+
import { RelaySession } from './relay.mjs';
|
|
11
|
+
|
|
12
|
+
const PAIR_TOOL_NAME = 'pair';
|
|
13
|
+
|
|
14
|
+
function textResult(value, isError = false) {
|
|
15
|
+
return {
|
|
16
|
+
content: [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
17
|
+
isError,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function toolErrorPayload(err) {
|
|
22
|
+
if (err && typeof err === 'object' && err.code) {
|
|
23
|
+
return formatBridgeError({
|
|
24
|
+
code: err.code,
|
|
25
|
+
message: err.message,
|
|
26
|
+
details: err.details,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return formatBridgeError({ message: String(err?.message || err) });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {{ catalog?: ReturnType<typeof loadCommandsCatalog>, session?: RelaySession, autoPair?: boolean }} [options]
|
|
34
|
+
*/
|
|
35
|
+
export async function createSurfcadMcpServer(options = {}) {
|
|
36
|
+
const catalog = options.catalog || loadCommandsCatalog();
|
|
37
|
+
const session = options.session || new RelaySession();
|
|
38
|
+
const bridgeTools = bridgeToolDefinitions(catalog);
|
|
39
|
+
|
|
40
|
+
const server = new Server(
|
|
41
|
+
{ name: 'surfcad-mcp', version: loadPackageVersion() },
|
|
42
|
+
{ capabilities: { tools: {} } },
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
46
|
+
tools: [
|
|
47
|
+
{
|
|
48
|
+
name: PAIR_TOOL_NAME,
|
|
49
|
+
description: `Pair this MCP server to an open SurfCAD tab via the local WebSocket relay. ${pairingEnvHint()}`,
|
|
50
|
+
inputSchema: {
|
|
51
|
+
type: 'object',
|
|
52
|
+
additionalProperties: false,
|
|
53
|
+
properties: {
|
|
54
|
+
code: {
|
|
55
|
+
type: 'string',
|
|
56
|
+
description: '6-character pairing code from the SurfCAD Local agent panel',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
...bridgeTools,
|
|
62
|
+
],
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
66
|
+
const name = request.params.name;
|
|
67
|
+
const args = request.params.arguments && typeof request.params.arguments === 'object'
|
|
68
|
+
? request.params.arguments
|
|
69
|
+
: {};
|
|
70
|
+
|
|
71
|
+
if (name === PAIR_TOOL_NAME) {
|
|
72
|
+
const code = typeof args.code === 'string' && args.code.trim()
|
|
73
|
+
? args.code.trim()
|
|
74
|
+
: (process.env.SURFCAD_PAIRING_CODE || '').trim();
|
|
75
|
+
if (!code) {
|
|
76
|
+
return textResult({
|
|
77
|
+
code: 'bad_pairing_code',
|
|
78
|
+
message: `Missing pairing code. Pass { "code": "......" } or set SURFCAD_PAIRING_CODE. ${pairingEnvHint()}`,
|
|
79
|
+
}, true);
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const result = await session.pair(code);
|
|
83
|
+
return textResult(result);
|
|
84
|
+
} catch (err) {
|
|
85
|
+
return textResult(toolErrorPayload(err), true);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const known = bridgeTools.some((t) => t.name === name);
|
|
90
|
+
if (!known) {
|
|
91
|
+
return textResult({ code: 'unknown_tool', message: `Unknown tool: ${name}` }, true);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!session.isPaired()) {
|
|
95
|
+
const envCode = (process.env.SURFCAD_PAIRING_CODE || '').trim();
|
|
96
|
+
if (envCode) {
|
|
97
|
+
try {
|
|
98
|
+
await session.pair(envCode);
|
|
99
|
+
} catch (err) {
|
|
100
|
+
return textResult(toolErrorPayload(err), true);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
return textResult(notPairedError(), true);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const result = await session.call(name, args);
|
|
109
|
+
return textResult(result);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
const code = err?.code;
|
|
112
|
+
if (code === 'real_repo_blocked' || code === 'confirm_required' || code === 'pair_rejected') {
|
|
113
|
+
return textResult(formatBridgeError({
|
|
114
|
+
code,
|
|
115
|
+
message: err.message,
|
|
116
|
+
details: err.details,
|
|
117
|
+
}), true);
|
|
118
|
+
}
|
|
119
|
+
return textResult(toolErrorPayload(err), true);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return { server, session, catalog, bridgeTools };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {{ autoPairFromEnv?: boolean }} [options]
|
|
128
|
+
*/
|
|
129
|
+
export async function runSurfcadMcpServer(options = {}) {
|
|
130
|
+
const { server, session } = await createSurfcadMcpServer();
|
|
131
|
+
const transport = new StdioServerTransport();
|
|
132
|
+
await server.connect(transport);
|
|
133
|
+
|
|
134
|
+
const envCode = (process.env.SURFCAD_PAIRING_CODE || '').trim();
|
|
135
|
+
if (options.autoPairFromEnv !== false && envCode) {
|
|
136
|
+
session.pair(envCode).catch(() => {
|
|
137
|
+
/* user may approve later; tools will retry */
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return { server, session, transport };
|
|
142
|
+
}
|