@pairdraw/mcp 0.0.0-stage → 0.16.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/LICENSE +21 -0
- package/README.md +19 -2
- package/dist/connect.d.mts +1 -0
- package/dist/connect.mjs +192 -0
- package/dist/connect.mjs.map +1 -0
- package/dist/discovery-CB2Bv5WA.mjs +167 -0
- package/dist/discovery-CB2Bv5WA.mjs.map +1 -0
- package/dist/discovery.d.mts +59 -0
- package/dist/discovery.mjs +2 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +95 -0
- package/dist/index.mjs.map +1 -0
- package/dist/metadata-DVz00c8R.d.mts +26 -0
- package/dist/paths-BpcXnrbP.mjs +168 -0
- package/dist/paths-BpcXnrbP.mjs.map +1 -0
- package/dist/policy-B8Ka4WRH.mjs +206 -0
- package/dist/policy-B8Ka4WRH.mjs.map +1 -0
- package/dist/rolldown-runtime-ClRpJifh.mjs +3 -0
- package/dist/root-BzmoiT2g.mjs +9 -0
- package/dist/root-BzmoiT2g.mjs.map +1 -0
- package/dist/scope-D2LTTsDw.mjs +66 -0
- package/dist/scope-D2LTTsDw.mjs.map +1 -0
- package/dist/scope-DJ9hsBYV.d.mts +31 -0
- package/dist/server-BdiJUhSs.mjs +3261 -0
- package/dist/server-BdiJUhSs.mjs.map +1 -0
- package/dist/server.d.mts +265 -0
- package/dist/server.mjs +2 -0
- package/dist/stdio.d.mts +1 -0
- package/dist/stdio.mjs +411 -0
- package/dist/stdio.mjs.map +1 -0
- package/dist/tools.d.mts +17 -0
- package/dist/tools.mjs +33 -0
- package/dist/tools.mjs.map +1 -0
- package/dist/transport.d.mts +59 -0
- package/dist/transport.mjs +2 -0
- package/package.json +78 -4
- package/src/agents/acp.ts +110 -0
- package/src/agents/claude-cloud.ts +99 -0
- package/src/agents/filter.ts +85 -0
- package/src/agents/index.ts +299 -0
- package/src/agents/install.ts +44 -0
- package/src/agents/link.ts +79 -0
- package/src/agents/lookup.ts +136 -0
- package/src/agents/pairing.ts +182 -0
- package/src/agents/path.ts +85 -0
- package/src/agents/process.ts +88 -0
- package/src/agents/registry.ts +36 -0
- package/src/agents/schema.ts +77 -0
- package/src/agents/sign-in.ts +106 -0
- package/src/agents/socket.ts +79 -0
- package/src/auth.ts +21 -0
- package/src/browser-rpc.ts +440 -0
- package/src/connect.ts +170 -0
- package/src/global.d.ts +5 -0
- package/src/http-options.ts +14 -0
- package/src/index.ts +179 -0
- package/src/json.ts +1 -0
- package/src/result.ts +37 -0
- package/src/root.ts +5 -0
- package/src/rpc-types.ts +5 -0
- package/src/server/connections.ts +58 -0
- package/src/server/files.ts +49 -0
- package/src/server/install-command.ts +24 -0
- package/src/server/lifecycle.ts +389 -0
- package/src/server/origin.ts +53 -0
- package/src/server/pending-requests.ts +62 -0
- package/src/server/sessions.ts +335 -0
- package/src/server.ts +597 -0
- package/src/skill/content.ts +40 -0
- package/src/skill/instructions.ts +69 -0
- package/src/skill/register.ts +77 -0
- package/src/stdio/bridge.ts +460 -0
- package/src/stdio.ts +130 -0
- package/src/tool/browser.ts +183 -0
- package/src/tool/index.ts +12 -0
- package/src/tool/manifest.ts +221 -0
- package/src/tool/metadata.ts +44 -0
- package/src/tool/output.ts +336 -0
- package/src/tool/policy.ts +37 -0
- package/src/tool/registration.ts +523 -0
- package/src/tool/scope.ts +71 -0
- package/src/transport/discovery.ts +235 -0
- package/src/transport/origins.ts +85 -0
- package/src/transport/paths.ts +179 -0
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer'
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
McpServer,
|
|
6
|
+
ToolCallback,
|
|
7
|
+
ToolAnnotations,
|
|
8
|
+
ServerContext
|
|
9
|
+
} from '@modelcontextprotocol/server'
|
|
10
|
+
// eslint-disable-next-line pairdraw/no-mixed-case-acronym-identifiers -- Upstream export spelling.
|
|
11
|
+
import { toStandardJsonSchema as toStandardJSONSchema } from '@valibot/to-json-schema'
|
|
12
|
+
import * as v from 'valibot'
|
|
13
|
+
|
|
14
|
+
import { CODEGEN_PROMPT } from '@pairdraw/core/tools'
|
|
15
|
+
|
|
16
|
+
import type { RPCJSONObject } from '#mcp/json'
|
|
17
|
+
import { MAX_RESULT_BYTES, fail, ok, resultTooLargeMessage } from '#mcp/result'
|
|
18
|
+
import { SKILL_FILE_PATHS, SKILL_FILES, type SkillFilePath } from '#mcp/skill/content'
|
|
19
|
+
import { registerBrowserTools } from '#mcp/tool/browser'
|
|
20
|
+
import { createToolDescriptors, getMCPToolDefinitions } from '#mcp/tool/manifest'
|
|
21
|
+
import type { ToolDescriptor, ToolEffect, ToolPolicy } from '#mcp/tool/metadata'
|
|
22
|
+
import { resolveSafePath, writeToolOutput, writesFileOutput } from '#mcp/tool/output'
|
|
23
|
+
import { isToolEnabled } from '#mcp/tool/policy'
|
|
24
|
+
import { SELECTION_SCOPE_FILE_OUTPUT_ERROR } from '#mcp/tool/scope'
|
|
25
|
+
|
|
26
|
+
export type RPCSender = (body: Record<string, unknown>) => Promise<unknown>
|
|
27
|
+
export type MCPToolSettled = (context: ServerContext) => Promise<void>
|
|
28
|
+
|
|
29
|
+
export const automationTargetSchema = {
|
|
30
|
+
document_id: v.optional(
|
|
31
|
+
v.pipe(v.string(), v.description('Optional Pairdraw document/tab ID to target'))
|
|
32
|
+
),
|
|
33
|
+
page_id: v.optional(
|
|
34
|
+
v.pipe(v.string(), v.description('Optional page ID to target within the document'))
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function splitAutomationTarget(args: Record<string, unknown>): {
|
|
39
|
+
target: { document_id?: string; page_id?: string }
|
|
40
|
+
args: Record<string, unknown>
|
|
41
|
+
} {
|
|
42
|
+
const { document_id, page_id, ...rest } = args
|
|
43
|
+
const target: { document_id?: string; page_id?: string } = {}
|
|
44
|
+
if (typeof document_id === 'string') target.document_id = document_id
|
|
45
|
+
if (typeof page_id === 'string') target.page_id = page_id
|
|
46
|
+
return { target, args: rest }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type RPCResponse = { ok?: boolean; result?: unknown; target?: unknown; error?: string }
|
|
50
|
+
|
|
51
|
+
/** The app's recent files; the server keeps only those `open_file` could reach. */
|
|
52
|
+
const recentFilesResultSchema = v.object({
|
|
53
|
+
files: v.array(v.object({ path: v.string(), name: v.string(), updated_at: v.string() }))
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
export async function sendCommand(
|
|
57
|
+
sendRPC: RPCSender,
|
|
58
|
+
command: string,
|
|
59
|
+
args: Record<string, unknown>
|
|
60
|
+
): Promise<RPCResponse> {
|
|
61
|
+
const res = (await sendRPC({ command, args })) as RPCResponse
|
|
62
|
+
if (res.ok === false) throw new Error(res.error)
|
|
63
|
+
return res
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function withTarget<T extends object>(body: T, res: RPCResponse): T & { target?: unknown } {
|
|
67
|
+
return res.target ? { ...body, target: res.target } : body
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Who sends a session's tool calls: any MCP client, or an ACP or Pi harness chat in the app. */
|
|
71
|
+
export const MCP_AGENT_KINDS = ['mcp', 'acp', 'harness'] as const
|
|
72
|
+
export type MCPAgentKind = (typeof MCP_AGENT_KINDS)[number]
|
|
73
|
+
|
|
74
|
+
/** The session tool calls come from, so the app can show the client as an agent at work. */
|
|
75
|
+
export interface MCPAgentSession {
|
|
76
|
+
id: string
|
|
77
|
+
kind: MCPAgentKind
|
|
78
|
+
/** The app chat the session belongs to, from its `x-pairdraw-chat` header. */
|
|
79
|
+
chat?: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface RegisterToolsOptions {
|
|
83
|
+
policy: ToolPolicy
|
|
84
|
+
mcpRoot?: string | null
|
|
85
|
+
sendRPC: RPCSender
|
|
86
|
+
agentSession?: MCPAgentSession
|
|
87
|
+
/** Transport lifecycle cleanup after tool execution, including cancellation. */
|
|
88
|
+
onToolSettled?: MCPToolSettled
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function toolAnnotations(effect: ToolEffect): ToolAnnotations {
|
|
92
|
+
return {
|
|
93
|
+
readOnlyHint: effect === 'read',
|
|
94
|
+
destructiveHint: effect === 'write'
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function descriptorByName(descriptors: readonly ToolDescriptor[]): Map<string, ToolDescriptor> {
|
|
99
|
+
return new Map(descriptors.map((descriptor) => [descriptor.name, descriptor]))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The id the agent gave a call in its own conversation, which an app chat shows it under: Codex
|
|
104
|
+
* sends it as `callId`, Claude Code as `claudecode/toolUseId`.
|
|
105
|
+
*/
|
|
106
|
+
function agentCallId(meta: Record<string, unknown> | undefined): string | undefined {
|
|
107
|
+
const id = meta?.callId ?? meta?.['claudecode/toolUseId']
|
|
108
|
+
return typeof id === 'string' && id.length > 0 && id.length <= 128 ? id : undefined
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function registerTools(mcpServer: McpServer, options: RegisterToolsOptions): void {
|
|
112
|
+
const { policy, sendRPC, agentSession } = options
|
|
113
|
+
// Sent with each tool call: the session, the client's name once it has introduced itself,
|
|
114
|
+
// and for an app chat's agent, the chat and the call's id in it.
|
|
115
|
+
const agent = (meta: Record<string, unknown> | undefined) => {
|
|
116
|
+
if (!agentSession) return undefined
|
|
117
|
+
const session = {
|
|
118
|
+
session: agentSession.id,
|
|
119
|
+
kind: agentSession.kind,
|
|
120
|
+
client: mcpServer.server.getClientVersion()?.name
|
|
121
|
+
}
|
|
122
|
+
if (!agentSession.chat) return session
|
|
123
|
+
return { ...session, chat: agentSession.chat, call: agentCallId(meta) }
|
|
124
|
+
}
|
|
125
|
+
const resolvedRoot = options.mcpRoot ? resolve(options.mcpRoot) : null
|
|
126
|
+
const descriptors = descriptorByName(createToolDescriptors(resolvedRoot !== null))
|
|
127
|
+
const register = <InputArgs extends v.GenericSchema>(
|
|
128
|
+
name: string,
|
|
129
|
+
toolOptions: { description?: string; inputSchema: InputArgs },
|
|
130
|
+
handler: ToolCallback<ReturnType<typeof toStandardJSONSchema<InputArgs>>>
|
|
131
|
+
) => {
|
|
132
|
+
const descriptor = descriptors.get(name)
|
|
133
|
+
if (!descriptor) throw new Error(`Missing MCP tool descriptor for "${name}"`)
|
|
134
|
+
if (!isToolEnabled(descriptor, policy)) return
|
|
135
|
+
mcpServer.registerTool(
|
|
136
|
+
name,
|
|
137
|
+
{
|
|
138
|
+
description: toolOptions.description ?? descriptor.description,
|
|
139
|
+
inputSchema: toStandardJSONSchema(toolOptions.inputSchema),
|
|
140
|
+
annotations: toolAnnotations(descriptor.effect),
|
|
141
|
+
_meta: { 'pairdraw/capabilities': descriptor.capabilities }
|
|
142
|
+
},
|
|
143
|
+
async (args, context) => {
|
|
144
|
+
try {
|
|
145
|
+
return await handler(args, context)
|
|
146
|
+
} finally {
|
|
147
|
+
await options.onToolSettled?.(context)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const def of getMCPToolDefinitions()) {
|
|
154
|
+
register(
|
|
155
|
+
def.name,
|
|
156
|
+
{
|
|
157
|
+
description: def.description,
|
|
158
|
+
inputSchema: v.strictObject({ ...def.input.entries, ...automationTargetSchema })
|
|
159
|
+
},
|
|
160
|
+
async (args: Record<string, unknown>, context) => {
|
|
161
|
+
try {
|
|
162
|
+
const { target, args: toolArgs } = splitAutomationTarget(args)
|
|
163
|
+
const filePath =
|
|
164
|
+
writesFileOutput(def.name) && typeof toolArgs.path === 'string' ? toolArgs.path : null
|
|
165
|
+
if (policy.scope === 'selection' && filePath !== null) {
|
|
166
|
+
return fail(new Error(SELECTION_SCOPE_FILE_OUTPUT_ERROR))
|
|
167
|
+
}
|
|
168
|
+
const result = await sendRPC({
|
|
169
|
+
command: 'tool',
|
|
170
|
+
args: {
|
|
171
|
+
...target,
|
|
172
|
+
name: def.name,
|
|
173
|
+
args: toolArgs,
|
|
174
|
+
agent: agent(context.mcpReq._meta),
|
|
175
|
+
// A client limited to the selection says so, whatever the server's own scope.
|
|
176
|
+
...(policy.scope === 'selection' ? { scope: policy.scope } : {})
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
const res = result as { ok?: boolean; result?: unknown; error?: string }
|
|
180
|
+
if (res.ok === false) return fail(new Error(res.error))
|
|
181
|
+
const r = res.result as RPCJSONObject | undefined
|
|
182
|
+
if (r && filePath && resolvedRoot) {
|
|
183
|
+
const written = await writeToolOutput(def.name, r, filePath, resolvedRoot)
|
|
184
|
+
if (written) return written
|
|
185
|
+
}
|
|
186
|
+
// Only an image goes back as one; a PDF stays base64 in the JSON result, as described.
|
|
187
|
+
if (
|
|
188
|
+
r &&
|
|
189
|
+
'base64' in r &&
|
|
190
|
+
typeof r.mimeType === 'string' &&
|
|
191
|
+
r.mimeType.startsWith('image/')
|
|
192
|
+
) {
|
|
193
|
+
const base64 = String(r.base64)
|
|
194
|
+
const bytes = Buffer.byteLength(base64, 'utf8')
|
|
195
|
+
if (bytes > MAX_RESULT_BYTES) {
|
|
196
|
+
return fail(
|
|
197
|
+
new Error(
|
|
198
|
+
resultTooLargeMessage(
|
|
199
|
+
`Image from "${def.name}"`,
|
|
200
|
+
bytes,
|
|
201
|
+
'Export a smaller region or lower the scale/resolution.'
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
content: [
|
|
208
|
+
{
|
|
209
|
+
type: 'image' as const,
|
|
210
|
+
data: base64,
|
|
211
|
+
mimeType: r.mimeType
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return ok(r, def.name)
|
|
217
|
+
} catch (e) {
|
|
218
|
+
return fail(e)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
register(
|
|
225
|
+
'list_documents',
|
|
226
|
+
{
|
|
227
|
+
description:
|
|
228
|
+
'List open Pairdraw documents/tabs with their IDs, file paths, current pages, and pages.',
|
|
229
|
+
inputSchema: v.strictObject({})
|
|
230
|
+
},
|
|
231
|
+
async () => {
|
|
232
|
+
try {
|
|
233
|
+
const result = await sendRPC({ command: 'list_documents', args: {} })
|
|
234
|
+
const res = result as { ok?: boolean; result?: unknown; error?: string }
|
|
235
|
+
if (res.ok === false) return fail(new Error(res.error))
|
|
236
|
+
return ok(res.result ?? {})
|
|
237
|
+
} catch (e) {
|
|
238
|
+
return fail(e)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
register(
|
|
244
|
+
'save_file',
|
|
245
|
+
{
|
|
246
|
+
description: resolvedRoot
|
|
247
|
+
? 'Save the current document to disk. If path is provided, it must be inside the configured MCP root.'
|
|
248
|
+
: 'Save the current document to disk. Uses the existing file path if available, otherwise prompts for a location.',
|
|
249
|
+
inputSchema: resolvedRoot
|
|
250
|
+
? v.strictObject({
|
|
251
|
+
path: v.optional(
|
|
252
|
+
v.pipe(
|
|
253
|
+
v.string(),
|
|
254
|
+
v.minLength(1),
|
|
255
|
+
v.description('Path for the .fig file, absolute or relative to the MCP root')
|
|
256
|
+
)
|
|
257
|
+
),
|
|
258
|
+
...automationTargetSchema
|
|
259
|
+
})
|
|
260
|
+
: v.strictObject({ ...automationTargetSchema })
|
|
261
|
+
},
|
|
262
|
+
async (args: { path?: string; document_id?: string; page_id?: string }) => {
|
|
263
|
+
try {
|
|
264
|
+
const safePath =
|
|
265
|
+
args.path !== undefined && resolvedRoot
|
|
266
|
+
? await resolveSafePath(args.path, resolvedRoot)
|
|
267
|
+
: undefined
|
|
268
|
+
const { target } = splitAutomationTarget(args)
|
|
269
|
+
const result = await sendRPC({
|
|
270
|
+
command: 'save_file',
|
|
271
|
+
args: { ...target, path: safePath?.realPath }
|
|
272
|
+
})
|
|
273
|
+
const res = result as { ok?: boolean; result?: unknown; target?: unknown; error?: string }
|
|
274
|
+
if (res.ok === false) return fail(new Error(res.error))
|
|
275
|
+
const response: { saved: true; path?: string; target?: unknown } = { saved: true }
|
|
276
|
+
if (safePath) response.path = safePath.resolved
|
|
277
|
+
if (res.target) response.target = res.target
|
|
278
|
+
return ok(response)
|
|
279
|
+
} catch (e) {
|
|
280
|
+
return fail(e)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
if (resolvedRoot) {
|
|
286
|
+
register(
|
|
287
|
+
'open_file',
|
|
288
|
+
{
|
|
289
|
+
description: 'Open a .fig or .pen file from inside the configured MCP root.',
|
|
290
|
+
inputSchema: v.strictObject({
|
|
291
|
+
path: v.pipe(
|
|
292
|
+
v.string(),
|
|
293
|
+
v.minLength(1),
|
|
294
|
+
v.description('Path to the design file, absolute or relative to the MCP root')
|
|
295
|
+
),
|
|
296
|
+
...automationTargetSchema
|
|
297
|
+
})
|
|
298
|
+
},
|
|
299
|
+
async (args: { path: string; document_id?: string; page_id?: string }) => {
|
|
300
|
+
try {
|
|
301
|
+
const safe = await resolveSafePath(args.path, resolvedRoot)
|
|
302
|
+
const { target } = splitAutomationTarget(args)
|
|
303
|
+
const result = await sendRPC({
|
|
304
|
+
command: 'open_file',
|
|
305
|
+
args: { ...target, path: safe.realPath }
|
|
306
|
+
})
|
|
307
|
+
const res = result as { ok?: boolean; result?: unknown; target?: unknown; error?: string }
|
|
308
|
+
if (res.ok === false) return fail(new Error(res.error))
|
|
309
|
+
const response: { opened: true; target?: unknown } = { opened: true }
|
|
310
|
+
if (res.target) response.target = res.target
|
|
311
|
+
return ok(response)
|
|
312
|
+
} catch (e) {
|
|
313
|
+
return fail(e)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
register('list_recent_files', { inputSchema: v.strictObject({}) }, async () => {
|
|
319
|
+
try {
|
|
320
|
+
const res = await sendCommand(sendRPC, 'list_recent_files', {})
|
|
321
|
+
const { files } = v.parse(recentFilesResultSchema, res.result)
|
|
322
|
+
const insideRoot = await Promise.all(
|
|
323
|
+
files.map((file) =>
|
|
324
|
+
resolveSafePath(file.path, resolvedRoot).then(
|
|
325
|
+
() => true,
|
|
326
|
+
() => false
|
|
327
|
+
)
|
|
328
|
+
)
|
|
329
|
+
)
|
|
330
|
+
return ok({ files: files.filter((_, index) => insideRoot[index]) })
|
|
331
|
+
} catch (e) {
|
|
332
|
+
return fail(e)
|
|
333
|
+
}
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
register(
|
|
337
|
+
'new_document',
|
|
338
|
+
{
|
|
339
|
+
description:
|
|
340
|
+
'Create a new empty document with an optional save path inside the configured MCP root.',
|
|
341
|
+
inputSchema: v.strictObject({
|
|
342
|
+
path: v.optional(
|
|
343
|
+
v.pipe(
|
|
344
|
+
v.string(),
|
|
345
|
+
v.minLength(1),
|
|
346
|
+
v.description('Path for the new file, absolute or relative to the MCP root')
|
|
347
|
+
)
|
|
348
|
+
),
|
|
349
|
+
...automationTargetSchema
|
|
350
|
+
})
|
|
351
|
+
},
|
|
352
|
+
async (args: { path?: string; document_id?: string; page_id?: string }) => {
|
|
353
|
+
try {
|
|
354
|
+
const safePath =
|
|
355
|
+
args.path !== undefined ? await resolveSafePath(args.path, resolvedRoot) : undefined
|
|
356
|
+
const { target } = splitAutomationTarget(args)
|
|
357
|
+
const result = await sendRPC({
|
|
358
|
+
command: 'new_document',
|
|
359
|
+
args: { ...target, path: safePath?.realPath }
|
|
360
|
+
})
|
|
361
|
+
const res = result as { ok?: boolean; result?: unknown; target?: unknown; error?: string }
|
|
362
|
+
if (res.ok === false) return fail(new Error(res.error))
|
|
363
|
+
const response: { created: true; target?: unknown } = { created: true }
|
|
364
|
+
if (res.target) response.target = res.target
|
|
365
|
+
return ok(response)
|
|
366
|
+
} catch (e) {
|
|
367
|
+
return fail(e)
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const closeEntries = {
|
|
374
|
+
unsaved: v.optional(
|
|
375
|
+
v.pipe(
|
|
376
|
+
v.picklist(['error', 'save', 'discard']),
|
|
377
|
+
v.description(
|
|
378
|
+
'What to do with unsaved changes: "error" (default) fails, "save" saves first, "discard" drops them'
|
|
379
|
+
)
|
|
380
|
+
)
|
|
381
|
+
),
|
|
382
|
+
...automationTargetSchema
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
register(
|
|
386
|
+
'close_file',
|
|
387
|
+
{
|
|
388
|
+
inputSchema: resolvedRoot
|
|
389
|
+
? v.strictObject({
|
|
390
|
+
...closeEntries,
|
|
391
|
+
path: v.optional(
|
|
392
|
+
v.pipe(
|
|
393
|
+
v.string(),
|
|
394
|
+
v.minLength(1),
|
|
395
|
+
v.description(
|
|
396
|
+
'With unsaved "save": .fig path for a document never saved, inside the MCP root'
|
|
397
|
+
)
|
|
398
|
+
)
|
|
399
|
+
)
|
|
400
|
+
})
|
|
401
|
+
: v.strictObject(closeEntries)
|
|
402
|
+
},
|
|
403
|
+
async (args: {
|
|
404
|
+
unsaved?: 'error' | 'save' | 'discard'
|
|
405
|
+
path?: string
|
|
406
|
+
document_id?: string
|
|
407
|
+
page_id?: string
|
|
408
|
+
}) => {
|
|
409
|
+
try {
|
|
410
|
+
const safePath =
|
|
411
|
+
args.path !== undefined && resolvedRoot
|
|
412
|
+
? await resolveSafePath(args.path, resolvedRoot)
|
|
413
|
+
: undefined
|
|
414
|
+
const { target } = splitAutomationTarget(args)
|
|
415
|
+
const rpcArgs: Record<string, unknown> = { ...target }
|
|
416
|
+
if (args.unsaved) rpcArgs.unsaved = args.unsaved
|
|
417
|
+
if (safePath) rpcArgs.path = safePath.realPath
|
|
418
|
+
const res = await sendCommand(sendRPC, 'close_file', rpcArgs)
|
|
419
|
+
const closed = (res.result as { closed?: boolean } | undefined)?.closed === true
|
|
420
|
+
return ok(withTarget({ closed }, res))
|
|
421
|
+
} catch (e) {
|
|
422
|
+
return fail(e)
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
register(
|
|
428
|
+
'activate_document',
|
|
429
|
+
{
|
|
430
|
+
inputSchema: v.strictObject({
|
|
431
|
+
document_id: v.pipe(v.string(), v.description('Document/tab ID from list_documents')),
|
|
432
|
+
page_id: automationTargetSchema.page_id
|
|
433
|
+
})
|
|
434
|
+
},
|
|
435
|
+
async (args: { document_id: string; page_id?: string }) => {
|
|
436
|
+
try {
|
|
437
|
+
const { target } = splitAutomationTarget(args)
|
|
438
|
+
const res = await sendCommand(sendRPC, 'activate_document', target)
|
|
439
|
+
return ok(withTarget({ activated: true }, res))
|
|
440
|
+
} catch (e) {
|
|
441
|
+
return fail(e)
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
)
|
|
445
|
+
|
|
446
|
+
for (const command of ['undo', 'redo'] as const) {
|
|
447
|
+
register(
|
|
448
|
+
command,
|
|
449
|
+
{ inputSchema: v.strictObject({ ...automationTargetSchema }) },
|
|
450
|
+
async (args) => {
|
|
451
|
+
try {
|
|
452
|
+
const { target } = splitAutomationTarget(args)
|
|
453
|
+
const res = await sendCommand(sendRPC, command, target)
|
|
454
|
+
return ok(withTarget({ ...(res.result as RPCJSONObject | undefined) }, res))
|
|
455
|
+
} catch (e) {
|
|
456
|
+
return fail(e)
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
register('get_settings', { inputSchema: v.strictObject({}) }, async () => {
|
|
463
|
+
try {
|
|
464
|
+
const res = await sendCommand(sendRPC, 'get_settings', {})
|
|
465
|
+
return ok(res.result ?? {})
|
|
466
|
+
} catch (e) {
|
|
467
|
+
return fail(e)
|
|
468
|
+
}
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
register(
|
|
472
|
+
'update_settings',
|
|
473
|
+
{
|
|
474
|
+
inputSchema: v.strictObject({
|
|
475
|
+
settings: v.pipe(
|
|
476
|
+
v.record(v.string(), v.unknown()),
|
|
477
|
+
v.description(
|
|
478
|
+
'Partial settings, e.g. {"appearance":{"theme":"light"},"editing":{"snapping":{"pixelGrid":false}}}. Unknown keys and invalid values are rejected.'
|
|
479
|
+
)
|
|
480
|
+
)
|
|
481
|
+
})
|
|
482
|
+
},
|
|
483
|
+
async (args: { settings: Record<string, unknown> }) => {
|
|
484
|
+
try {
|
|
485
|
+
// Echo only the applied patch: with get_settings disabled, writing must not read.
|
|
486
|
+
await sendCommand(sendRPC, 'update_settings', { settings: args.settings })
|
|
487
|
+
return ok({ updated: args.settings })
|
|
488
|
+
} catch (e) {
|
|
489
|
+
return fail(e)
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
)
|
|
493
|
+
|
|
494
|
+
register(
|
|
495
|
+
'get_codegen_prompt',
|
|
496
|
+
{
|
|
497
|
+
description:
|
|
498
|
+
'Get design-to-code generation guidelines. Call before generating frontend code.',
|
|
499
|
+
inputSchema: v.strictObject({})
|
|
500
|
+
},
|
|
501
|
+
async () => ok({ prompt: CODEGEN_PROMPT })
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
register(
|
|
505
|
+
'read_skill',
|
|
506
|
+
{
|
|
507
|
+
inputSchema: v.strictObject({
|
|
508
|
+
path: v.optional(
|
|
509
|
+
v.pipe(
|
|
510
|
+
v.picklist(SKILL_FILE_PATHS),
|
|
511
|
+
v.description('File inside the skill, as SKILL.md links it. Defaults to SKILL.md.')
|
|
512
|
+
)
|
|
513
|
+
)
|
|
514
|
+
})
|
|
515
|
+
},
|
|
516
|
+
// Markdown goes back as is: wrapped in JSON, its code examples would arrive escaped.
|
|
517
|
+
async (args: { path?: SkillFilePath }) => ({
|
|
518
|
+
content: [{ type: 'text' as const, text: SKILL_FILES[args.path ?? 'SKILL.md'].text }]
|
|
519
|
+
})
|
|
520
|
+
)
|
|
521
|
+
|
|
522
|
+
registerBrowserTools(register, sendRPC)
|
|
523
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { RPCJSONObject } from '#mcp/json'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What MCP clients can reach: the whole document, or only the layers the user selected. In
|
|
5
|
+
* selection scope the server exposes these read tools, stamps every call it sends to the app with
|
|
6
|
+
* the scope, and the app rejects node IDs outside the selected layers and their descendants.
|
|
7
|
+
*/
|
|
8
|
+
export const MCP_TOOL_SCOPES = ['document', 'selection'] as const
|
|
9
|
+
export type MCPToolScope = (typeof MCP_TOOL_SCOPES)[number]
|
|
10
|
+
|
|
11
|
+
/** The tools selection scope keeps, each with the arguments that name the nodes it reads. */
|
|
12
|
+
export const SELECTION_SCOPE_TOOLS: Readonly<Record<string, readonly string[]>> = {
|
|
13
|
+
get_selection: [],
|
|
14
|
+
get_node: ['id'],
|
|
15
|
+
get_page_tree: ['root_id'],
|
|
16
|
+
describe: ['id', 'ids'],
|
|
17
|
+
export_image: ['ids']
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isSelectionScopeTool(name: string): boolean {
|
|
21
|
+
return Object.hasOwn(SELECTION_SCOPE_TOOLS, name)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function parseToolScope(value: string | undefined): MCPToolScope {
|
|
25
|
+
const scope = value?.trim() || 'document'
|
|
26
|
+
if (scope === 'document' || scope === 'selection') return scope
|
|
27
|
+
throw new Error('PAIRDRAW_MCP_SCOPE must be "document" or "selection"')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The narrower of the scopes, so a client never offers more than the server it talks to. */
|
|
31
|
+
export function narrowestScope(...scopes: MCPToolScope[]): MCPToolScope {
|
|
32
|
+
return scopes.includes('selection') ? 'selection' : 'document'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Why a selection-scoped call cannot write files, such as an export with a `path`. */
|
|
36
|
+
export const SELECTION_SCOPE_FILE_OUTPUT_ERROR =
|
|
37
|
+
'Writing files is unavailable while Pairdraw shares only the selection; omit path'
|
|
38
|
+
|
|
39
|
+
/** Commands the app may receive in selection scope besides calls of the tools above. */
|
|
40
|
+
const SELECTION_SCOPE_COMMANDS: ReadonlySet<string> = new Set(['agent_session_closed'])
|
|
41
|
+
|
|
42
|
+
type RPCSend = (msg: RPCJSONObject) => Promise<unknown>
|
|
43
|
+
|
|
44
|
+
function toolName(msg: RPCJSONObject): string | null {
|
|
45
|
+
const args = msg.args
|
|
46
|
+
if (!args || typeof args !== 'object' || Array.isArray(args)) return null
|
|
47
|
+
const name = Reflect.get(args, 'name')
|
|
48
|
+
return typeof name === 'string' ? name : null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sends to the app within the scope: in selection scope, only the selection tools and session
|
|
53
|
+
* notices go through, each tool call carrying the scope however the client called it, so neither
|
|
54
|
+
* `/rpc` nor a stale client can reach the rest of the document or the settings.
|
|
55
|
+
*/
|
|
56
|
+
export function scopeRPC(send: RPCSend, scope: MCPToolScope): RPCSend {
|
|
57
|
+
if (scope === 'document') return send
|
|
58
|
+
return async (msg) => {
|
|
59
|
+
const command = typeof msg.command === 'string' ? msg.command : ''
|
|
60
|
+
if (SELECTION_SCOPE_COMMANDS.has(command)) return send(msg)
|
|
61
|
+
const name = command === 'tool' ? toolName(msg) : null
|
|
62
|
+
if (!name || !isSelectionScopeTool(name)) {
|
|
63
|
+
return {
|
|
64
|
+
ok: false,
|
|
65
|
+
error: `${name ?? command} is unavailable while Pairdraw shares only the selection with MCP clients`
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const args = msg.args as RPCJSONObject
|
|
69
|
+
return send({ ...msg, args: { ...args, scope } })
|
|
70
|
+
}
|
|
71
|
+
}
|