surfcad-mcp 0.2.4 → 0.3.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 +14 -3
- package/data/commands.json +11252 -4296
- package/data/helpers.json +1765 -0
- package/package.json +1 -1
- package/src/helpers.mjs +209 -0
- package/src/prompt/buildGuidance.mjs +21 -0
- package/src/prompt/errorHints.mjs +288 -0
- package/src/prompt/guide.mjs +285 -0
- package/src/prompt/promptSession.mjs +94 -11
- package/src/prompt/promptTools.mjs +31 -3
- package/src/relay.mjs +9 -3
- package/src/server.mjs +107 -19
package/src/server.mjs
CHANGED
|
@@ -2,7 +2,11 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
|
2
2
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
3
|
import {
|
|
4
4
|
CallToolRequestSchema,
|
|
5
|
+
ErrorCode,
|
|
6
|
+
ListResourcesRequestSchema,
|
|
5
7
|
ListToolsRequestSchema,
|
|
8
|
+
McpError,
|
|
9
|
+
ReadResourceRequestSchema,
|
|
6
10
|
} from '@modelcontextprotocol/sdk/types.js';
|
|
7
11
|
import { bridgeToolDefinitions, loadCommandsCatalog } from './catalog.mjs';
|
|
8
12
|
import {
|
|
@@ -27,9 +31,22 @@ import {
|
|
|
27
31
|
PROMPT_WEBHOOK_ACTIVE,
|
|
28
32
|
promptToolDefinitions,
|
|
29
33
|
} from './prompt/promptTools.mjs';
|
|
34
|
+
import {
|
|
35
|
+
callGuideTool,
|
|
36
|
+
GUIDE_TOOL_NAME,
|
|
37
|
+
guideResourceContents,
|
|
38
|
+
guideResourceList,
|
|
39
|
+
guideToolDefinition,
|
|
40
|
+
SERVER_INSTRUCTIONS,
|
|
41
|
+
} from './prompt/guide.mjs';
|
|
42
|
+
import { withErrorHint, withRowHints } from './prompt/errorHints.mjs';
|
|
43
|
+
import { callListHelpersTool, LIST_HELPERS_TOOL_NAME, listHelpersToolDefinition } from './helpers.mjs';
|
|
30
44
|
|
|
31
45
|
const PAIR_TOOL_NAME = 'pair';
|
|
32
46
|
|
|
47
|
+
/** One line in the pair result; the guidance itself is guide { topic: "start" }. */
|
|
48
|
+
const PAIR_GUIDE_POINTER = 'Before the first model call read guide { topic: "start" } (how to build in SurfCAD: block out first, features first, hardware as patterned parts); guide {} lists every topic.';
|
|
49
|
+
|
|
33
50
|
const STATUS_PROMPT_NOTE = ' This process also adds webhookRegistered (true when a prompt webhook is registered; the URL and any bearer are not included), queued (prompt messages waiting for read_messages; cancelled prompts are not counted), counts ({ queued, delivered, replied, cancelled }), and last ({ id, state } of the latest prompt, or null).';
|
|
34
51
|
|
|
35
52
|
/**
|
|
@@ -68,19 +85,59 @@ async function waitForPairing(attempt, waitMs) {
|
|
|
68
85
|
}
|
|
69
86
|
}
|
|
70
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Compact JSON. Pretty printing put every number of a bbox or a face key on
|
|
90
|
+
* its own line and made each payload about 2.4 times larger (list_commands
|
|
91
|
+
* was 240 KB indented, 99 KB compact).
|
|
92
|
+
*/
|
|
71
93
|
function textResult(value, isError = false) {
|
|
72
94
|
return {
|
|
73
|
-
content: [{ type: 'text', text: JSON.stringify(value
|
|
95
|
+
content: [{ type: 'text', text: JSON.stringify(value) }],
|
|
74
96
|
isError,
|
|
75
97
|
};
|
|
76
98
|
}
|
|
77
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Every error payload leaves with a hint (and a see pointer when the code
|
|
102
|
+
* has an errors.<code> topic); the tool name feeds the fallback hint.
|
|
103
|
+
* @param {unknown} payload
|
|
104
|
+
* @param {string} [tool]
|
|
105
|
+
*/
|
|
106
|
+
function errorResult(payload, tool) {
|
|
107
|
+
return textResult(withErrorHint(payload, { tool }), true);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The `return` default over MCP. The page keeps `full`; an agent reads the script with get_script. */
|
|
111
|
+
const MCP_RETURN_DEFAULT = 'status';
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Tools whose catalog row takes `return` (mutating tools, undo, redo,
|
|
115
|
+
* status) get `return: "status"` when the caller left it out. A catalog
|
|
116
|
+
* row without the property is an older tab contract and is left alone.
|
|
117
|
+
* @param {{ args?: { properties?: Record<string, unknown> } } | undefined} row
|
|
118
|
+
* @param {Record<string, unknown>} args
|
|
119
|
+
*/
|
|
120
|
+
export function withDefaultReturn(row, args) {
|
|
121
|
+
if (!row?.args?.properties || !Object.prototype.hasOwnProperty.call(row.args.properties, 'return')) return args;
|
|
122
|
+
if (Object.prototype.hasOwnProperty.call(args, 'return')) return args;
|
|
123
|
+
return { ...args, return: MCP_RETURN_DEFAULT };
|
|
124
|
+
}
|
|
125
|
+
|
|
78
126
|
/**
|
|
79
127
|
* Pair success is ordinary text content so every MCP client, including Claude Code,
|
|
80
128
|
* shows the note. No structuredContent and no _meta.
|
|
81
129
|
* @param {Record<string, unknown>} result
|
|
82
130
|
* @param {{ webhookRegistered: () => boolean }} prompts
|
|
83
131
|
*/
|
|
132
|
+
/**
|
|
133
|
+
* Does this bridge tool change the model? The catalog row says so
|
|
134
|
+
* (`mutates`); the name is the fallback for a catalog copy that predates it.
|
|
135
|
+
* @param {string} name
|
|
136
|
+
*/
|
|
137
|
+
function mutatingTool(name) {
|
|
138
|
+
return /^(set_script|run|insert_|invoke_helper|confirm_feature|delete_feature|rollback_to|undo|redo|create_|delete_|rename_|part_set_transform|insert_part|open_|paint_confirm|paint$|joint_commit|sheet_commit|sheet_set_params|sheet_edit|sketch_commit|slot_commit|hole_commit|git_|export_|edit_feature|pick_done|select_)/.test(name);
|
|
139
|
+
}
|
|
140
|
+
|
|
84
141
|
function pairToolResult(result, prompts) {
|
|
85
142
|
const webhookRegistered = prompts.webhookRegistered() === true;
|
|
86
143
|
const note = webhookRegistered ? PROMPT_WEBHOOK_ACTIVE : PROMPT_ONBOARDING_NOTE;
|
|
@@ -93,6 +150,8 @@ function pairToolResult(result, prompts) {
|
|
|
93
150
|
content: [
|
|
94
151
|
{ type: 'text', text: JSON.stringify(payload, null, 2) },
|
|
95
152
|
{ type: 'text', text: note },
|
|
153
|
+
// The build guidance lives in the guide tool; one pointer here, for every client.
|
|
154
|
+
{ type: 'text', text: PAIR_GUIDE_POINTER },
|
|
96
155
|
],
|
|
97
156
|
isError: false,
|
|
98
157
|
};
|
|
@@ -119,19 +178,34 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
119
178
|
const session = options.session || new RelaySession();
|
|
120
179
|
const bridgeTools = bridgeToolDefinitions(catalog).map((tool) => augmentStatusTool(augmentScreenshotTool(tool)));
|
|
121
180
|
|
|
181
|
+
const version = loadPackageVersion();
|
|
122
182
|
const server = new Server(
|
|
123
|
-
{ name: 'surfcad-mcp', version
|
|
183
|
+
{ name: 'surfcad-mcp', version },
|
|
124
184
|
{
|
|
125
|
-
capabilities: { tools: {} },
|
|
126
|
-
instructions:
|
|
185
|
+
capabilities: { tools: {}, resources: {} },
|
|
186
|
+
instructions: SERVER_INSTRUCTIONS,
|
|
127
187
|
},
|
|
128
188
|
);
|
|
129
189
|
|
|
190
|
+
// The guide topics as resources (surfcad://guide/<topic>), the same text
|
|
191
|
+
// as guide { topic }, versioned with the package for hosts that index them.
|
|
192
|
+
server.setRequestHandler(ListResourcesRequestSchema, async () => ({
|
|
193
|
+
resources: guideResourceList(version),
|
|
194
|
+
}));
|
|
195
|
+
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
196
|
+
const uri = request.params.uri;
|
|
197
|
+
const contents = guideResourceContents(uri, version);
|
|
198
|
+
if (!contents) {
|
|
199
|
+
throw new McpError(ErrorCode.InvalidParams, `Unknown resource: ${uri}. resources/list names the surfcad://guide/<topic> resources.`);
|
|
200
|
+
}
|
|
201
|
+
return contents;
|
|
202
|
+
});
|
|
203
|
+
|
|
130
204
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
131
205
|
tools: [
|
|
132
206
|
{
|
|
133
207
|
name: PAIR_TOOL_NAME,
|
|
134
|
-
description: `Pair this MCP server to an open SurfCAD tab via the local WebSocket relay. ${pairingEnvHint()} ${PROMPT_SERVER_INSTRUCTIONS}
|
|
208
|
+
description: `Pair this MCP server to an open SurfCAD tab via the local WebSocket relay. ${pairingEnvHint()} ${PROMPT_SERVER_INSTRUCTIONS} After pairing call guide { topic: "start" } (How to build in SurfCAD: block out first, features first, hardware as patterned parts) before the first model call.`,
|
|
135
209
|
inputSchema: {
|
|
136
210
|
type: 'object',
|
|
137
211
|
additionalProperties: false,
|
|
@@ -143,6 +217,8 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
143
217
|
},
|
|
144
218
|
},
|
|
145
219
|
},
|
|
220
|
+
guideToolDefinition(),
|
|
221
|
+
listHelpersToolDefinition(),
|
|
146
222
|
...promptToolDefinitions(),
|
|
147
223
|
...bridgeTools,
|
|
148
224
|
],
|
|
@@ -154,14 +230,22 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
154
230
|
? request.params.arguments
|
|
155
231
|
: {};
|
|
156
232
|
|
|
233
|
+
if (name === GUIDE_TOOL_NAME) {
|
|
234
|
+
return callGuideTool(args);
|
|
235
|
+
}
|
|
236
|
+
if (name === LIST_HELPERS_TOOL_NAME) {
|
|
237
|
+
const out = callListHelpersTool(args);
|
|
238
|
+
return out.isError ? errorResult(JSON.parse(out.content[0].text), name) : out;
|
|
239
|
+
}
|
|
240
|
+
|
|
157
241
|
if (isPromptTool(name)) {
|
|
158
242
|
if (name !== 'set_status') session.noteAgentToolCall();
|
|
159
243
|
try {
|
|
160
244
|
const out = await callPromptTool(session.prompts, name, args);
|
|
161
|
-
if (out.isError) return
|
|
245
|
+
if (out.isError) return errorResult(out.error, name);
|
|
162
246
|
return { content: out.content, isError: false };
|
|
163
247
|
} catch (err) {
|
|
164
|
-
return
|
|
248
|
+
return errorResult(toolErrorPayload(err), name);
|
|
165
249
|
}
|
|
166
250
|
}
|
|
167
251
|
|
|
@@ -170,22 +254,22 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
170
254
|
? args.code.trim()
|
|
171
255
|
: (process.env.SURFCAD_PAIRING_CODE || '').trim();
|
|
172
256
|
if (!code) {
|
|
173
|
-
return
|
|
257
|
+
return errorResult({
|
|
174
258
|
code: 'bad_pairing_code',
|
|
175
259
|
message: `Missing pairing code. Pass { "code": "......" } or set SURFCAD_PAIRING_CODE. ${pairingEnvHint()}`,
|
|
176
|
-
},
|
|
260
|
+
}, name);
|
|
177
261
|
}
|
|
178
262
|
try {
|
|
179
263
|
const result = await waitForPairing(session.pair(code), PAIR_TOOL_WAIT_MS);
|
|
180
264
|
return pairToolResult(result, session.prompts);
|
|
181
265
|
} catch (err) {
|
|
182
|
-
return
|
|
266
|
+
return errorResult(toolErrorPayload(err), name);
|
|
183
267
|
}
|
|
184
268
|
}
|
|
185
269
|
|
|
186
270
|
const known = bridgeTools.some((t) => t.name === name);
|
|
187
271
|
if (!known) {
|
|
188
|
-
return
|
|
272
|
+
return errorResult({ code: 'unknown_tool', message: `Unknown tool: ${name}` });
|
|
189
273
|
}
|
|
190
274
|
|
|
191
275
|
// A lost session is already re-pairing in the background; session.call
|
|
@@ -196,20 +280,24 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
196
280
|
try {
|
|
197
281
|
await waitForPairing(session.pair(envCode), AUTO_PAIR_WAIT_MS);
|
|
198
282
|
} catch (err) {
|
|
199
|
-
return
|
|
283
|
+
return errorResult(toolErrorPayload(err), name);
|
|
200
284
|
}
|
|
201
285
|
} else if (session.state !== 'pairing') {
|
|
202
|
-
return
|
|
286
|
+
return errorResult(notPairedError(), name);
|
|
203
287
|
}
|
|
204
288
|
}
|
|
205
289
|
|
|
206
290
|
try {
|
|
207
|
-
|
|
208
|
-
const
|
|
291
|
+
// A model-changing tool switches the tab to working on its own.
|
|
292
|
+
const row = catalog.commands.find((c) => c.name === name);
|
|
293
|
+
session.noteAgentToolCall({ mutates: row?.mutates === true || (row?.mutates == null && mutatingTool(name)) });
|
|
294
|
+
const out = await callPairedBridgeTool(session, name, withDefaultReturn(row, args));
|
|
209
295
|
if (name === 'status' && out.isError !== true && out.value && typeof out.value === 'object' && !Array.isArray(out.value)) {
|
|
210
296
|
return textResult({ ...out.value, ...session.prompts.deliveryStatus() });
|
|
211
297
|
}
|
|
212
|
-
|
|
298
|
+
if (out.isError === true) return errorResult(out.value, name);
|
|
299
|
+
// A run that failed answers ok: false with errors rows; those carry hints too.
|
|
300
|
+
return textResult(withRowHints(out.value));
|
|
213
301
|
} catch (err) {
|
|
214
302
|
const code = err?.code;
|
|
215
303
|
if (
|
|
@@ -218,13 +306,13 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
218
306
|
|| code === 'confirm_required'
|
|
219
307
|
|| code === 'pair_rejected'
|
|
220
308
|
) {
|
|
221
|
-
return
|
|
309
|
+
return errorResult(formatBridgeError({
|
|
222
310
|
code,
|
|
223
311
|
message: err.message,
|
|
224
312
|
details: err.details,
|
|
225
|
-
}),
|
|
313
|
+
}), name);
|
|
226
314
|
}
|
|
227
|
-
return
|
|
315
|
+
return errorResult(toolErrorPayload(err), name);
|
|
228
316
|
}
|
|
229
317
|
});
|
|
230
318
|
|