@flowdrop/flowdrop 2.5.0 → 2.7.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/CHANGELOG.md +59 -0
- package/dist/chat/commandClassifier.d.ts +19 -1
- package/dist/chat/commandClassifier.js +33 -1
- package/dist/chat/index.d.ts +1 -1
- package/dist/chat/index.js +1 -1
- package/dist/commands/executor.js +270 -37
- package/dist/commands/index.d.ts +1 -1
- package/dist/commands/parser.js +20 -0
- package/dist/commands/types.d.ts +77 -12
- package/dist/components/App.svelte +8 -0
- package/dist/components/BindablePortListbox.svelte +426 -0
- package/dist/components/BindablePortListbox.svelte.d.ts +23 -0
- package/dist/components/WorkflowInterfaceEditor.svelte +142 -36
- package/dist/components/WorkflowInterfaceEditor.svelte.d.ts +8 -0
- package/dist/components/WorkflowInterfaceEntryCard.svelte +537 -294
- package/dist/components/WorkflowInterfaceEntryCard.svelte.d.ts +6 -3
- package/dist/components/WorkflowInterfaceEntryComposer.svelte +331 -0
- package/dist/components/WorkflowInterfaceEntryComposer.svelte.d.ts +19 -0
- package/dist/components/console/CommandConsole.svelte +8 -0
- package/dist/components/console/formatters.d.ts +9 -1
- package/dist/components/console/formatters.js +54 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/messages/context.d.ts +6 -0
- package/dist/messages/context.js +8 -0
- package/dist/messages/defaults.d.ts +78 -2
- package/dist/messages/defaults.js +50 -3
- package/dist/messages/index.d.ts +1 -1
- package/dist/messages/index.js +1 -1
- package/dist/services/globalSave.d.ts +7 -1
- package/dist/services/globalSave.js +17 -3
- package/dist/stores/instanceContainer.svelte.d.ts +26 -0
- package/dist/stores/instanceContainer.svelte.js +24 -0
- package/dist/stores/workflowStore.svelte.d.ts +10 -0
- package/dist/stores/workflowStore.svelte.js +15 -0
- package/dist/stories/WebMCPFakeAgent.svelte +229 -0
- package/dist/stories/WebMCPFakeAgent.svelte.d.ts +10 -0
- package/dist/styles/base.css +4 -2
- package/dist/styles/tokens.css +6 -0
- package/dist/svelte-app.d.ts +24 -2
- package/dist/svelte-app.js +65 -3
- package/dist/types/index.d.ts +49 -0
- package/dist/utils/focus.d.ts +18 -0
- package/dist/utils/focus.js +24 -0
- package/dist/utils/whenWorkflowLoaded.svelte.d.ts +17 -0
- package/dist/utils/whenWorkflowLoaded.svelte.js +42 -0
- package/dist/utils/workflowInterface.d.ts +47 -3
- package/dist/utils/workflowInterface.js +84 -9
- package/dist/webmcp/WebMCP.stories.svelte +101 -0
- package/dist/webmcp/WebMCP.stories.svelte.d.ts +18 -0
- package/dist/webmcp/WebMCPConfirm.svelte +216 -0
- package/dist/webmcp/WebMCPConfirm.svelte.d.ts +21 -0
- package/dist/webmcp/bridge.d.ts +63 -0
- package/dist/webmcp/bridge.js +106 -0
- package/dist/webmcp/descriptors.d.ts +59 -0
- package/dist/webmcp/descriptors.js +504 -0
- package/dist/webmcp/fake.d.ts +23 -0
- package/dist/webmcp/fake.js +42 -0
- package/dist/webmcp/gate.d.ts +76 -0
- package/dist/webmcp/gate.js +188 -0
- package/dist/webmcp/index.d.ts +36 -0
- package/dist/webmcp/index.js +32 -0
- package/dist/webmcp/register.d.ts +33 -0
- package/dist/webmcp/register.js +504 -0
- package/dist/webmcp/types.d.ts +268 -0
- package/dist/webmcp/types.js +23 -0
- package/dist/webmcp/validate.d.ts +32 -0
- package/dist/webmcp/validate.js +118 -0
- package/package.json +6 -1
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — types.
|
|
3
|
+
*
|
|
4
|
+
* The editor's typed command union (`commands/types.ts`) exposed as WebMCP
|
|
5
|
+
* "editor tools" that a browser-resident agent can discover and call. These
|
|
6
|
+
* are tools *for editing a workflow in the browser*; they are unrelated to the
|
|
7
|
+
* tools a running workflow hands to an LLM node.
|
|
8
|
+
*
|
|
9
|
+
* Spec pinned 2026-09-02 (webmachinelearning/webmcp): `document.modelContext`
|
|
10
|
+
* with `registerTool(tool, { signal, exposedTo })`, `execute(input, { signal })`
|
|
11
|
+
* returning a promise, `annotations.readOnlyHint`, and a `toolchange` event.
|
|
12
|
+
* The adapter feature-detects against {@link ModelContextLike} — the minimal
|
|
13
|
+
* structural shape it needs — and pins nothing else.
|
|
14
|
+
*
|
|
15
|
+
* @module webmcp/types
|
|
16
|
+
*/
|
|
17
|
+
import type { Command, UIAction } from '../commands/types.js';
|
|
18
|
+
import type { NodeMetadata } from '../types/index.js';
|
|
19
|
+
import type { MessagesOverride } from '../messages/types.js';
|
|
20
|
+
/** One property of a tool's input schema. */
|
|
21
|
+
export interface ToolSchemaProperty {
|
|
22
|
+
type?: 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array';
|
|
23
|
+
description?: string;
|
|
24
|
+
enum?: readonly string[];
|
|
25
|
+
properties?: Record<string, ToolSchemaProperty>;
|
|
26
|
+
required?: readonly string[];
|
|
27
|
+
additionalProperties?: boolean;
|
|
28
|
+
items?: ToolSchemaProperty;
|
|
29
|
+
/** `value` in `set_config` accepts several primitive shapes. */
|
|
30
|
+
anyOf?: readonly ToolSchemaProperty[];
|
|
31
|
+
minimum?: number;
|
|
32
|
+
maximum?: number;
|
|
33
|
+
}
|
|
34
|
+
/** A tool's input schema: always an object with `additionalProperties: false`. */
|
|
35
|
+
export interface ToolInputSchema {
|
|
36
|
+
type: 'object';
|
|
37
|
+
properties: Record<string, ToolSchemaProperty>;
|
|
38
|
+
required?: readonly string[];
|
|
39
|
+
additionalProperties: false;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* One editor tool, before registration. Pure data plus a pure mapping from
|
|
43
|
+
* validated arguments to the commands it runs — no DOM, no Svelte, so the same
|
|
44
|
+
* records can back a server-side MCP transport later.
|
|
45
|
+
*/
|
|
46
|
+
export interface ToolDescriptor {
|
|
47
|
+
/** Verb part of the tool name; registered as `${prefix}_${verb}`. */
|
|
48
|
+
verb: string;
|
|
49
|
+
description: string;
|
|
50
|
+
inputSchema: ToolInputSchema;
|
|
51
|
+
/** True when every command the tool can produce is read-only. */
|
|
52
|
+
readOnly: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Map already-validated arguments to the commands to execute. Throws a
|
|
55
|
+
* {@link ToolArgumentError} for anything the schema validator cannot express.
|
|
56
|
+
*/
|
|
57
|
+
toCommands(args: Record<string, unknown>): Command[];
|
|
58
|
+
}
|
|
59
|
+
/** Thrown by {@link ToolDescriptor.toCommands} and the argument validator. */
|
|
60
|
+
export declare class ToolArgumentError extends Error {
|
|
61
|
+
constructor(message: string);
|
|
62
|
+
}
|
|
63
|
+
/** What `execute` receives from the runtime. */
|
|
64
|
+
export interface ToolExecuteOptions {
|
|
65
|
+
signal?: AbortSignal;
|
|
66
|
+
}
|
|
67
|
+
/** The result shape we return: MCP-style content blocks with JSON in `text`. */
|
|
68
|
+
export interface ToolResult {
|
|
69
|
+
content: Array<{
|
|
70
|
+
type: 'text';
|
|
71
|
+
text: string;
|
|
72
|
+
}>;
|
|
73
|
+
/** Mirrors MCP: set when the call failed. */
|
|
74
|
+
isError?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/** A tool as handed to `registerTool`. */
|
|
77
|
+
export interface RegisteredToolDefinition {
|
|
78
|
+
name: string;
|
|
79
|
+
description: string;
|
|
80
|
+
inputSchema: ToolInputSchema;
|
|
81
|
+
annotations?: {
|
|
82
|
+
readOnlyHint?: boolean;
|
|
83
|
+
untrustedContentHint?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Spec added 2026-09-03: "executing the tool will result in consequential
|
|
86
|
+
* actions that are significant, real-world, or non-reversible."
|
|
87
|
+
*/
|
|
88
|
+
consequentialHint?: boolean;
|
|
89
|
+
};
|
|
90
|
+
execute(input: unknown, options?: ToolExecuteOptions): Promise<ToolResult>;
|
|
91
|
+
}
|
|
92
|
+
/** Options accepted by `registerTool`. Only `signal` is ever passed. */
|
|
93
|
+
export interface RegisterToolOptions {
|
|
94
|
+
signal?: AbortSignal;
|
|
95
|
+
exposedTo?: string[];
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The minimal structural type the adapter feature-detects against.
|
|
99
|
+
* `document.modelContext` (spec) and the earlier `navigator.modelContext`
|
|
100
|
+
* both satisfy it; so does the fake used in tests.
|
|
101
|
+
*/
|
|
102
|
+
export interface ModelContextLike {
|
|
103
|
+
registerTool(tool: RegisteredToolDefinition, options?: RegisterToolOptions): unknown;
|
|
104
|
+
/** Pre-spec runtimes only; called when present, never relied upon. */
|
|
105
|
+
unregisterTool?(name: string): unknown;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Stable codes a host hook (`onSave`, `onRun`, `onRunStatus`) may answer with.
|
|
109
|
+
* They align with fddo's API-8 `error_code` vocabulary where the two meet
|
|
110
|
+
* (`CONFLICT`, `NOT_FOUND`); the rest name what a browser agent must do next.
|
|
111
|
+
*
|
|
112
|
+
* - `FORBIDDEN` — the server refused; the agent should tell the user.
|
|
113
|
+
* - `UNAVAILABLE` — nothing to act on yet (unsaved workflow, no runtime).
|
|
114
|
+
* - `NEEDS_APPROVAL` — a person must approve in the UI first.
|
|
115
|
+
* - `INVALID` — the workflow failed validation; `message` says where.
|
|
116
|
+
* - `CONFLICT` — the server copy changed since it was loaded; reload.
|
|
117
|
+
* - `PENDING` — a run is paused for a person (never for the agent, D8).
|
|
118
|
+
* - `NOT_FOUND` — the run id is unknown.
|
|
119
|
+
*/
|
|
120
|
+
export type HostCode = 'FORBIDDEN' | 'UNAVAILABLE' | 'NEEDS_APPROVAL' | 'INVALID' | 'CONFLICT' | 'PENDING' | 'NOT_FOUND' | (string & Record<never, never>);
|
|
121
|
+
/**
|
|
122
|
+
* What every host hook returns. `ok: false` needs a `code`; `ok: true` may
|
|
123
|
+
* still carry one (`PENDING`). `can` lets a host refresh what the user may do
|
|
124
|
+
* after the call — passed through to the agent untouched.
|
|
125
|
+
*/
|
|
126
|
+
export interface HostEnvelope<T = unknown> {
|
|
127
|
+
ok: boolean;
|
|
128
|
+
code?: HostCode;
|
|
129
|
+
message?: string;
|
|
130
|
+
data?: T;
|
|
131
|
+
can?: Record<string, boolean>;
|
|
132
|
+
}
|
|
133
|
+
/** What `onRun` reports back once a run is accepted. */
|
|
134
|
+
export interface RunStarted {
|
|
135
|
+
/** The id `run_status` polls with. */
|
|
136
|
+
runId: string;
|
|
137
|
+
status?: string;
|
|
138
|
+
queued?: boolean;
|
|
139
|
+
}
|
|
140
|
+
/** The person-facing pause a paused run is waiting on (D8: no tool answers it). */
|
|
141
|
+
export interface RunPending {
|
|
142
|
+
interruptId?: string;
|
|
143
|
+
type?: string;
|
|
144
|
+
nodeId?: string;
|
|
145
|
+
message?: string;
|
|
146
|
+
}
|
|
147
|
+
/** What `onRunStatus` reports. `status` vocabulary is the host's; `paused` is special-cased. */
|
|
148
|
+
export interface RunStatus {
|
|
149
|
+
runId: string;
|
|
150
|
+
status: 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'cancelled' | (string & Record<never, never>);
|
|
151
|
+
startedAt?: string | null;
|
|
152
|
+
finishedAt?: string | null;
|
|
153
|
+
/** Set when `status` is `paused` and a person must act. */
|
|
154
|
+
pending?: RunPending | null;
|
|
155
|
+
/** Why an execution budget paused the run, when it did. */
|
|
156
|
+
pausedReason?: string | null;
|
|
157
|
+
/** The run's output once it has one. Host-shaped. */
|
|
158
|
+
outputs?: unknown;
|
|
159
|
+
/** Per-node status once the host has it. Host-shaped. */
|
|
160
|
+
nodeStatuses?: unknown;
|
|
161
|
+
error?: string;
|
|
162
|
+
}
|
|
163
|
+
/** Context passed to a custom {@link WebMCPApproval} callback alongside the commands. */
|
|
164
|
+
export interface WebMCPApprovalRequest {
|
|
165
|
+
/** The tool being called, without prefix: `add_node`, `batch`, `save`, … */
|
|
166
|
+
tool: string;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Approval policy for mutating tools.
|
|
170
|
+
*
|
|
171
|
+
* - `'confirm'` (default): a confirm dialog rendered inside the page.
|
|
172
|
+
* - `'auto'`: run without asking. Only for hosts that already trust every
|
|
173
|
+
* agent on the page (kiosks, tests).
|
|
174
|
+
* - A callback receiving the commands about to run and the request they came
|
|
175
|
+
* from; resolve `true` to run. For `save`, `commands` is empty and
|
|
176
|
+
* `request.tool === 'save'` — there is no command to describe, only the
|
|
177
|
+
* act of persisting.
|
|
178
|
+
*/
|
|
179
|
+
export type WebMCPApproval = 'confirm' | 'auto' | ((commands: Command[], request: WebMCPApprovalRequest) => Promise<boolean>);
|
|
180
|
+
export interface WebMCPOptions {
|
|
181
|
+
/**
|
|
182
|
+
* Node type definitions the tools resolve `add_node` and `list_types`
|
|
183
|
+
* against, as an array or a getter that returns the current list. Defaults
|
|
184
|
+
* to the instance's own list (`instance.nodeTypes`), which the editor fills
|
|
185
|
+
* as its fetch lands — pass this only when the host knows better.
|
|
186
|
+
*/
|
|
187
|
+
nodeTypes?: NodeMetadata[] | (() => NodeMetadata[]);
|
|
188
|
+
/** Tool name prefix. Default `flowdrop`. Must be unique per document. */
|
|
189
|
+
prefix?: string;
|
|
190
|
+
/** Approval policy for mutating tools. Default `'confirm'`. */
|
|
191
|
+
approval?: WebMCPApproval;
|
|
192
|
+
/**
|
|
193
|
+
* Handler for `view` actions (select node, open config, canvas viewport).
|
|
194
|
+
* When omitted the `view` tool is not registered at all — there is nothing
|
|
195
|
+
* it could do.
|
|
196
|
+
*/
|
|
197
|
+
onUIAction?: (action: UIAction) => void;
|
|
198
|
+
/**
|
|
199
|
+
* Persist the workflow (the editor's Save). When supplied, a `save` tool
|
|
200
|
+
* is registered; it is gated like a change because a save cannot be
|
|
201
|
+
* undone from the editor. When omitted the tool is not registered —
|
|
202
|
+
* there is nothing it could do.
|
|
203
|
+
*
|
|
204
|
+
* May resolve to nothing (saved) or to a {@link HostEnvelope} — e.g.
|
|
205
|
+
* `{ok: false, code: 'CONFLICT'}` when the server copy moved on. A thrown
|
|
206
|
+
* error is reported as `SAVE_FAILED`, except an error whose `errorData.error_code`
|
|
207
|
+
* or `status` says conflict, which is relayed as `CONFLICT`.
|
|
208
|
+
*/
|
|
209
|
+
onSave?: () => Promise<void | HostEnvelope>;
|
|
210
|
+
/**
|
|
211
|
+
* Start a run of the workflow being edited, with optional inputs for its
|
|
212
|
+
* interface ports. When supplied, a `run` tool is registered — gated like
|
|
213
|
+
* `save`, and flagged consequential — and `run_status` too when
|
|
214
|
+
* {@link onRunStatus} is also given. The hook answers with an envelope; the
|
|
215
|
+
* tool relays it. `data.runId` is what `run_status` polls with.
|
|
216
|
+
*/
|
|
217
|
+
onRun?: (inputs: Record<string, unknown>) => Promise<HostEnvelope<RunStarted>>;
|
|
218
|
+
/**
|
|
219
|
+
* Report a run's status. A read: never gated. `status: 'paused'` is relayed
|
|
220
|
+
* as `code: 'PENDING'` with `pending.nodeId` / `pending.message` — the agent
|
|
221
|
+
* tells the person, and the person acts in the UI (D8).
|
|
222
|
+
*/
|
|
223
|
+
onRunStatus?: (runId: string) => Promise<HostEnvelope<RunStatus>>;
|
|
224
|
+
/**
|
|
225
|
+
* Offer "don't ask again for edits in this session" in the confirm dialog.
|
|
226
|
+
* Ticked, later *edits* run without the dialog until the adapter detaches;
|
|
227
|
+
* `save` and `run` always ask. Default true. Irrelevant unless `approval`
|
|
228
|
+
* is `'confirm'`.
|
|
229
|
+
*/
|
|
230
|
+
rememberEdits?: boolean;
|
|
231
|
+
/** Where the built-in confirm dialog mounts. Default `document.body`. */
|
|
232
|
+
container?: HTMLElement;
|
|
233
|
+
/**
|
|
234
|
+
* Strings for the built-in confirm dialog, in the shape of the library's
|
|
235
|
+
* `messages` prop: a partial override of the defaults, or a getter for one
|
|
236
|
+
* when an i18n library drives the locale. English when omitted.
|
|
237
|
+
*/
|
|
238
|
+
messages?: MessagesOverride | (() => MessagesOverride);
|
|
239
|
+
/**
|
|
240
|
+
* Runtime override. Default: `document.modelContext`, then
|
|
241
|
+
* `navigator.modelContext`. Tests pass a fake here.
|
|
242
|
+
*/
|
|
243
|
+
modelContext?: ModelContextLike;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* The `webmcp` mount option of `mountFlowDropApp`. Node types default to the
|
|
247
|
+
* mount's `nodes` option or the ones the editor fetches; there is no UI-action
|
|
248
|
+
* handler on this path, so the `view` tool is not registered.
|
|
249
|
+
*/
|
|
250
|
+
export type WebMCPMountOptions = Omit<WebMCPOptions, 'nodeTypes' | 'onUIAction' | 'modelContext'> & {
|
|
251
|
+
nodeTypes?: WebMCPOptions['nodeTypes'];
|
|
252
|
+
};
|
|
253
|
+
/** Returned by `attachWebMCP`. */
|
|
254
|
+
export interface WebMCPHandle {
|
|
255
|
+
/**
|
|
256
|
+
* Fully qualified names of the tools the runtime accepted. Registration is
|
|
257
|
+
* asynchronous in the spec, so this fills in as registrations settle; a
|
|
258
|
+
* tool the runtime refused is warned about once and never listed. Await
|
|
259
|
+
* {@link ready} for the final list.
|
|
260
|
+
*/
|
|
261
|
+
readonly tools: readonly string[];
|
|
262
|
+
/** Resolves once every registration has settled — accepted or refused. */
|
|
263
|
+
readonly ready: Promise<void>;
|
|
264
|
+
/** False after `detach()` (or after the instance was destroyed). */
|
|
265
|
+
readonly attached: boolean;
|
|
266
|
+
/** Abort the registration, remove the tools, free the prefix. Idempotent. */
|
|
267
|
+
detach(): void;
|
|
268
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — types.
|
|
3
|
+
*
|
|
4
|
+
* The editor's typed command union (`commands/types.ts`) exposed as WebMCP
|
|
5
|
+
* "editor tools" that a browser-resident agent can discover and call. These
|
|
6
|
+
* are tools *for editing a workflow in the browser*; they are unrelated to the
|
|
7
|
+
* tools a running workflow hands to an LLM node.
|
|
8
|
+
*
|
|
9
|
+
* Spec pinned 2026-09-02 (webmachinelearning/webmcp): `document.modelContext`
|
|
10
|
+
* with `registerTool(tool, { signal, exposedTo })`, `execute(input, { signal })`
|
|
11
|
+
* returning a promise, `annotations.readOnlyHint`, and a `toolchange` event.
|
|
12
|
+
* The adapter feature-detects against {@link ModelContextLike} — the minimal
|
|
13
|
+
* structural shape it needs — and pins nothing else.
|
|
14
|
+
*
|
|
15
|
+
* @module webmcp/types
|
|
16
|
+
*/
|
|
17
|
+
/** Thrown by {@link ToolDescriptor.toCommands} and the argument validator. */
|
|
18
|
+
export class ToolArgumentError extends Error {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = 'ToolArgumentError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — argument validation.
|
|
3
|
+
*
|
|
4
|
+
* A hand-written validator for exactly the JSON Schema subset the tool
|
|
5
|
+
* descriptors use. That subset is `ToolSchemaProperty` in `./types.ts`: the
|
|
6
|
+
* type is the contract, and this file must understand every keyword it
|
|
7
|
+
* declares — `VALIDATOR_KEYWORDS` lists them, and a test in
|
|
8
|
+
* `tests/unit/webmcp` walks every schema and fails when a schema uses a key
|
|
9
|
+
* this set lacks. Adding a keyword therefore means adding it in three places:
|
|
10
|
+
* the type, the check below, and the set. No dependency is taken for this;
|
|
11
|
+
* the schemas are small, hand-written, and never come from outside the
|
|
12
|
+
* library.
|
|
13
|
+
*
|
|
14
|
+
* The runtime is supposed to validate too. Chrome's behaviour there is not
|
|
15
|
+
* something to lean on yet, and validating here keeps behaviour identical
|
|
16
|
+
* across runtimes and against the fake used in tests.
|
|
17
|
+
*
|
|
18
|
+
* @module webmcp/validate
|
|
19
|
+
*/
|
|
20
|
+
import { type ToolInputSchema, type ToolSchemaProperty } from './types.js';
|
|
21
|
+
/**
|
|
22
|
+
* Every schema keyword the validator understands. `description` is listed
|
|
23
|
+
* because it is a keyword schemas use, even though it needs no check.
|
|
24
|
+
*/
|
|
25
|
+
export declare const VALIDATOR_KEYWORDS: ReadonlySet<keyof ToolSchemaProperty>;
|
|
26
|
+
/**
|
|
27
|
+
* Validate raw tool input against a tool's schema and return it typed as an
|
|
28
|
+
* argument object.
|
|
29
|
+
*
|
|
30
|
+
* @throws ToolArgumentError
|
|
31
|
+
*/
|
|
32
|
+
export declare function validateToolArgs(schema: ToolInputSchema, input: unknown): Record<string, unknown>;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — argument validation.
|
|
3
|
+
*
|
|
4
|
+
* A hand-written validator for exactly the JSON Schema subset the tool
|
|
5
|
+
* descriptors use. That subset is `ToolSchemaProperty` in `./types.ts`: the
|
|
6
|
+
* type is the contract, and this file must understand every keyword it
|
|
7
|
+
* declares — `VALIDATOR_KEYWORDS` lists them, and a test in
|
|
8
|
+
* `tests/unit/webmcp` walks every schema and fails when a schema uses a key
|
|
9
|
+
* this set lacks. Adding a keyword therefore means adding it in three places:
|
|
10
|
+
* the type, the check below, and the set. No dependency is taken for this;
|
|
11
|
+
* the schemas are small, hand-written, and never come from outside the
|
|
12
|
+
* library.
|
|
13
|
+
*
|
|
14
|
+
* The runtime is supposed to validate too. Chrome's behaviour there is not
|
|
15
|
+
* something to lean on yet, and validating here keeps behaviour identical
|
|
16
|
+
* across runtimes and against the fake used in tests.
|
|
17
|
+
*
|
|
18
|
+
* @module webmcp/validate
|
|
19
|
+
*/
|
|
20
|
+
import { ToolArgumentError } from './types.js';
|
|
21
|
+
/**
|
|
22
|
+
* Every schema keyword the validator understands. `description` is listed
|
|
23
|
+
* because it is a keyword schemas use, even though it needs no check.
|
|
24
|
+
*/
|
|
25
|
+
export const VALIDATOR_KEYWORDS = new Set([
|
|
26
|
+
'type',
|
|
27
|
+
'description',
|
|
28
|
+
'enum',
|
|
29
|
+
'properties',
|
|
30
|
+
'required',
|
|
31
|
+
'additionalProperties',
|
|
32
|
+
'items',
|
|
33
|
+
'anyOf',
|
|
34
|
+
'minimum',
|
|
35
|
+
'maximum'
|
|
36
|
+
]);
|
|
37
|
+
function typeOf(value) {
|
|
38
|
+
if (value === null)
|
|
39
|
+
return 'null';
|
|
40
|
+
if (Array.isArray(value))
|
|
41
|
+
return 'array';
|
|
42
|
+
return typeof value;
|
|
43
|
+
}
|
|
44
|
+
function matchesType(value, type) {
|
|
45
|
+
switch (type) {
|
|
46
|
+
case 'string':
|
|
47
|
+
case 'boolean':
|
|
48
|
+
return typeof value === type;
|
|
49
|
+
case 'number':
|
|
50
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
51
|
+
case 'integer':
|
|
52
|
+
return typeof value === 'number' && Number.isInteger(value);
|
|
53
|
+
case 'object':
|
|
54
|
+
return typeOf(value) === 'object';
|
|
55
|
+
case 'array':
|
|
56
|
+
return Array.isArray(value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function validateProperty(path, value, schema) {
|
|
60
|
+
if (schema.anyOf) {
|
|
61
|
+
if (!schema.anyOf.some((alt) => !alt.type || matchesType(value, alt.type))) {
|
|
62
|
+
throw new ToolArgumentError(`${path}: expected one of ${schema.anyOf.map((a) => a.type).join(', ')}, got ${typeOf(value)}`);
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (schema.type && !matchesType(value, schema.type)) {
|
|
67
|
+
throw new ToolArgumentError(`${path}: expected ${schema.type}, got ${typeOf(value)}`);
|
|
68
|
+
}
|
|
69
|
+
if (schema.enum && !schema.enum.includes(value)) {
|
|
70
|
+
throw new ToolArgumentError(`${path}: expected one of ${schema.enum.map((v) => JSON.stringify(v)).join(', ')}, got ${JSON.stringify(value)}`);
|
|
71
|
+
}
|
|
72
|
+
if (schema.minimum !== undefined && value < schema.minimum) {
|
|
73
|
+
throw new ToolArgumentError(`${path}: must be >= ${schema.minimum}`);
|
|
74
|
+
}
|
|
75
|
+
if (schema.maximum !== undefined && value > schema.maximum) {
|
|
76
|
+
throw new ToolArgumentError(`${path}: must be <= ${schema.maximum}`);
|
|
77
|
+
}
|
|
78
|
+
if (schema.type === 'object' && schema.properties) {
|
|
79
|
+
validateObject(path, value, schema);
|
|
80
|
+
}
|
|
81
|
+
if (schema.type === 'array' && schema.items) {
|
|
82
|
+
value.forEach((item, i) => validateProperty(`${path}[${i}]`, item, schema.items));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function validateObject(path, value, schema) {
|
|
86
|
+
const props = schema.properties ?? {};
|
|
87
|
+
for (const key of schema.required ?? []) {
|
|
88
|
+
if (value[key] === undefined) {
|
|
89
|
+
throw new ToolArgumentError(`${path ? path + '.' : ''}${key} is required`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const [key, v] of Object.entries(value)) {
|
|
93
|
+
if (v === undefined)
|
|
94
|
+
continue;
|
|
95
|
+
const prop = props[key];
|
|
96
|
+
if (!prop) {
|
|
97
|
+
if (schema.additionalProperties === false) {
|
|
98
|
+
throw new ToolArgumentError(`${path ? path + '.' : ''}${key}: unknown property`);
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
validateProperty(`${path ? path + '.' : ''}${key}`, v, prop);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Validate raw tool input against a tool's schema and return it typed as an
|
|
107
|
+
* argument object.
|
|
108
|
+
*
|
|
109
|
+
* @throws ToolArgumentError
|
|
110
|
+
*/
|
|
111
|
+
export function validateToolArgs(schema, input) {
|
|
112
|
+
const args = input === undefined || input === null ? {} : input;
|
|
113
|
+
if (typeOf(args) !== 'object') {
|
|
114
|
+
throw new ToolArgumentError(`arguments must be an object, got ${typeOf(args)}`);
|
|
115
|
+
}
|
|
116
|
+
validateObject('', args, schema);
|
|
117
|
+
return args;
|
|
118
|
+
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "A drop-in visual workflow editor for any web application. You own the backend. You own the data. You own the orchestration.",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"private": false,
|
|
6
|
-
"version": "2.
|
|
6
|
+
"version": "2.7.0",
|
|
7
7
|
"author": "Shibin Das (D34dMan)",
|
|
8
8
|
"bugs": {
|
|
9
9
|
"url": "https://github.com/flowdrop-io/flowdrop/issues"
|
|
@@ -123,6 +123,11 @@
|
|
|
123
123
|
"svelte": "./dist/chat/index.js",
|
|
124
124
|
"default": "./dist/chat/index.js"
|
|
125
125
|
},
|
|
126
|
+
"./webmcp": {
|
|
127
|
+
"types": "./dist/webmcp/index.d.ts",
|
|
128
|
+
"svelte": "./dist/webmcp/index.js",
|
|
129
|
+
"default": "./dist/webmcp/index.js"
|
|
130
|
+
},
|
|
126
131
|
"./schema/v1": {
|
|
127
132
|
"default": "./dist/schemas/v1/workflow.schema.json"
|
|
128
133
|
},
|