@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,216 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
WebMCPConfirm
|
|
3
|
+
|
|
4
|
+
The built-in approval dialog for mutating editor tools called by a browser
|
|
5
|
+
agent. Lists what is about to run and asks the person at the keyboard.
|
|
6
|
+
Mounted imperatively by `gate.ts` into the host's container (default
|
|
7
|
+
`document.body`), one at a time; a second request while one is open is
|
|
8
|
+
refused upstream with a `busy` error rather than stacking dialogs.
|
|
9
|
+
-->
|
|
10
|
+
|
|
11
|
+
<script lang="ts">
|
|
12
|
+
import { m } from '../messages/index.js';
|
|
13
|
+
import { focusOnMount } from '../utils/focus.js';
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
/** Editor / workflow name, so two editors on one page are distinguishable. */
|
|
17
|
+
editorName: string;
|
|
18
|
+
/** One human-readable line per command (see `describeCommand`). */
|
|
19
|
+
lines: string[];
|
|
20
|
+
/**
|
|
21
|
+
* Overrides the default "N changes — applied together, undone together"
|
|
22
|
+
* hint, e.g. for `save`, which has no commands to count.
|
|
23
|
+
*/
|
|
24
|
+
hint?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Show the "apply further edits without asking" checkbox. Off for
|
|
27
|
+
* consequential calls (`save`, `run`), which always ask.
|
|
28
|
+
*/
|
|
29
|
+
offerRemember?: boolean;
|
|
30
|
+
/** Called exactly once with the decision and whether to remember it for edits. */
|
|
31
|
+
onResolve: (approved: boolean, remember?: boolean) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let { editorName, lines, hint, offerRemember = false, onResolve }: Props = $props();
|
|
35
|
+
|
|
36
|
+
let rejectButton = $state<HTMLButtonElement | null>(null);
|
|
37
|
+
let approveButton = $state<HTMLButtonElement | null>(null);
|
|
38
|
+
let rememberBox = $state<HTMLInputElement | null>(null);
|
|
39
|
+
let remember = $state(false);
|
|
40
|
+
|
|
41
|
+
// Focus starts on Apply and stays inside the dialog: Tab cycles Apply →
|
|
42
|
+
// Reject → (checkbox) → Apply, Escape rejects. The handler sits on the
|
|
43
|
+
// dialog itself, which holds focus through its controls, not on the window.
|
|
44
|
+
function handleKeydown(event: KeyboardEvent): void {
|
|
45
|
+
if (event.key === 'Escape') {
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
onResolve(false);
|
|
48
|
+
} else if (event.key === 'Tab') {
|
|
49
|
+
event.preventDefault();
|
|
50
|
+
const order = [approveButton, rejectButton, rememberBox].filter(
|
|
51
|
+
(el): el is HTMLButtonElement | HTMLInputElement => el !== null
|
|
52
|
+
);
|
|
53
|
+
const i = order.indexOf(document.activeElement as HTMLButtonElement | HTMLInputElement);
|
|
54
|
+
const step = event.shiftKey ? -1 : 1;
|
|
55
|
+
order[(i + step + order.length) % order.length]?.focus();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<div class="fd-webmcp-confirm" data-testid="flowdrop-webmcp-confirm">
|
|
61
|
+
<div
|
|
62
|
+
class="fd-webmcp-confirm__dialog"
|
|
63
|
+
role="alertdialog"
|
|
64
|
+
aria-modal="true"
|
|
65
|
+
aria-labelledby="fd-webmcp-confirm-title"
|
|
66
|
+
aria-describedby="fd-webmcp-confirm-list"
|
|
67
|
+
tabindex="-1"
|
|
68
|
+
onkeydown={handleKeydown}
|
|
69
|
+
>
|
|
70
|
+
<h2 id="fd-webmcp-confirm-title" class="fd-webmcp-confirm__title">
|
|
71
|
+
{m().webmcp.confirmTitle({ name: editorName })}
|
|
72
|
+
</h2>
|
|
73
|
+
<p class="fd-webmcp-confirm__hint">
|
|
74
|
+
{hint ?? m().webmcp.confirmCount({ count: lines.length })}
|
|
75
|
+
</p>
|
|
76
|
+
<ol id="fd-webmcp-confirm-list" class="fd-webmcp-confirm__list">
|
|
77
|
+
{#each lines as line, i (i)}
|
|
78
|
+
<li class="fd-webmcp-confirm__line">{line}</li>
|
|
79
|
+
{/each}
|
|
80
|
+
</ol>
|
|
81
|
+
{#if offerRemember}
|
|
82
|
+
<label class="fd-webmcp-confirm__remember">
|
|
83
|
+
<input
|
|
84
|
+
type="checkbox"
|
|
85
|
+
data-testid="flowdrop-webmcp-remember"
|
|
86
|
+
bind:this={rememberBox}
|
|
87
|
+
bind:checked={remember}
|
|
88
|
+
/>
|
|
89
|
+
{m().webmcp.rememberEdits}
|
|
90
|
+
</label>
|
|
91
|
+
{/if}
|
|
92
|
+
<div class="fd-webmcp-confirm__actions">
|
|
93
|
+
<button
|
|
94
|
+
type="button"
|
|
95
|
+
class="fd-webmcp-confirm__button fd-webmcp-confirm__button--reject"
|
|
96
|
+
data-testid="flowdrop-webmcp-reject"
|
|
97
|
+
bind:this={rejectButton}
|
|
98
|
+
onclick={() => onResolve(false)}
|
|
99
|
+
>
|
|
100
|
+
{m().webmcp.reject}
|
|
101
|
+
</button>
|
|
102
|
+
<button
|
|
103
|
+
type="button"
|
|
104
|
+
class="fd-webmcp-confirm__button fd-webmcp-confirm__button--approve"
|
|
105
|
+
data-testid="flowdrop-webmcp-approve"
|
|
106
|
+
bind:this={approveButton}
|
|
107
|
+
{@attach focusOnMount()}
|
|
108
|
+
onclick={() => onResolve(true, remember)}
|
|
109
|
+
>
|
|
110
|
+
{m().webmcp.apply}
|
|
111
|
+
</button>
|
|
112
|
+
</div>
|
|
113
|
+
</div>
|
|
114
|
+
</div>
|
|
115
|
+
|
|
116
|
+
<style>
|
|
117
|
+
.fd-webmcp-confirm {
|
|
118
|
+
position: fixed;
|
|
119
|
+
inset: 0;
|
|
120
|
+
z-index: 10000;
|
|
121
|
+
display: flex;
|
|
122
|
+
align-items: center;
|
|
123
|
+
justify-content: center;
|
|
124
|
+
background: rgb(0 0 0 / 0.4);
|
|
125
|
+
/* Mounted on document.body, outside the editor: no host font to inherit. */
|
|
126
|
+
font-family: var(--fd-font-sans, system-ui, sans-serif);
|
|
127
|
+
font-size: var(--fd-text-md, 1rem);
|
|
128
|
+
line-height: var(--fd-leading-normal, 1.5);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
.fd-webmcp-confirm__dialog {
|
|
132
|
+
min-width: 20rem;
|
|
133
|
+
max-width: min(36rem, calc(100vw - 2rem));
|
|
134
|
+
max-height: calc(100vh - 2rem);
|
|
135
|
+
overflow: auto;
|
|
136
|
+
padding: var(--fd-space-lg, 1rem);
|
|
137
|
+
border: 1px solid var(--fd-border, #d4d4d8);
|
|
138
|
+
border-radius: var(--fd-radius-lg, 0.75rem);
|
|
139
|
+
background: var(--fd-background, #fff);
|
|
140
|
+
color: var(--fd-foreground, #18181b);
|
|
141
|
+
box-shadow: var(--fd-shadow-xl, 0 20px 40px rgb(0 0 0 / 0.25));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.fd-webmcp-confirm__title {
|
|
145
|
+
margin: 0 0 var(--fd-space-xs, 0.25rem);
|
|
146
|
+
font-size: var(--fd-text-md, 1rem);
|
|
147
|
+
font-weight: 600;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.fd-webmcp-confirm__hint {
|
|
151
|
+
margin: 0 0 var(--fd-space-md, 0.75rem);
|
|
152
|
+
font-size: var(--fd-text-sm, 0.875rem);
|
|
153
|
+
color: var(--fd-muted-foreground, #71717a);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.fd-webmcp-confirm__list {
|
|
157
|
+
margin: 0 0 var(--fd-space-lg, 1rem);
|
|
158
|
+
padding: var(--fd-space-sm, 0.5rem) var(--fd-space-sm, 0.5rem) var(--fd-space-sm, 0.5rem)
|
|
159
|
+
var(--fd-space-xl, 1.5rem);
|
|
160
|
+
border-radius: var(--fd-radius-md, 0.5rem);
|
|
161
|
+
background: var(--fd-muted, #f4f4f5);
|
|
162
|
+
font-family: var(--fd-font-mono, ui-monospace, monospace);
|
|
163
|
+
font-size: var(--fd-text-sm, 0.875rem);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.fd-webmcp-confirm__line {
|
|
167
|
+
padding: 0.125rem 0;
|
|
168
|
+
overflow-wrap: anywhere;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
.fd-webmcp-confirm__remember {
|
|
172
|
+
display: flex;
|
|
173
|
+
align-items: flex-start;
|
|
174
|
+
gap: var(--fd-space-sm, 0.5rem);
|
|
175
|
+
margin: 0 0 var(--fd-space-lg, 1rem);
|
|
176
|
+
font-size: var(--fd-text-sm, 0.875rem);
|
|
177
|
+
color: var(--fd-muted-foreground, #71717a);
|
|
178
|
+
cursor: pointer;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
.fd-webmcp-confirm__remember input {
|
|
182
|
+
margin: 0.2em 0 0;
|
|
183
|
+
accent-color: var(--fd-primary, #2563eb);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.fd-webmcp-confirm__actions {
|
|
187
|
+
display: flex;
|
|
188
|
+
justify-content: flex-end;
|
|
189
|
+
gap: var(--fd-space-sm, 0.5rem);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
.fd-webmcp-confirm__button {
|
|
193
|
+
padding: var(--fd-space-xs, 0.375rem) var(--fd-space-md, 0.75rem);
|
|
194
|
+
border: 1px solid var(--fd-border, #d4d4d8);
|
|
195
|
+
border-radius: var(--fd-control-radius, 0.375rem);
|
|
196
|
+
background: var(--fd-background, #fff);
|
|
197
|
+
color: inherit;
|
|
198
|
+
font: inherit;
|
|
199
|
+
cursor: pointer;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
.fd-webmcp-confirm__button--approve {
|
|
203
|
+
border-color: var(--fd-primary, #2563eb);
|
|
204
|
+
background: var(--fd-primary, #2563eb);
|
|
205
|
+
color: var(--fd-primary-foreground, #fff);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.fd-webmcp-confirm__button--approve:hover {
|
|
209
|
+
background: var(--fd-primary-hover, #1d4ed8);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
.fd-webmcp-confirm__button:focus-visible {
|
|
213
|
+
outline: 2px solid var(--fd-primary, #2563eb);
|
|
214
|
+
outline-offset: 2px;
|
|
215
|
+
}
|
|
216
|
+
</style>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
/** Editor / workflow name, so two editors on one page are distinguishable. */
|
|
3
|
+
editorName: string;
|
|
4
|
+
/** One human-readable line per command (see `describeCommand`). */
|
|
5
|
+
lines: string[];
|
|
6
|
+
/**
|
|
7
|
+
* Overrides the default "N changes — applied together, undone together"
|
|
8
|
+
* hint, e.g. for `save`, which has no commands to count.
|
|
9
|
+
*/
|
|
10
|
+
hint?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Show the "apply further edits without asking" checkbox. Off for
|
|
13
|
+
* consequential calls (`save`, `run`), which always ask.
|
|
14
|
+
*/
|
|
15
|
+
offerRemember?: boolean;
|
|
16
|
+
/** Called exactly once with the decision and whether to remember it for edits. */
|
|
17
|
+
onResolve: (approved: boolean, remember?: boolean) => void;
|
|
18
|
+
}
|
|
19
|
+
declare const WebMCPConfirm: import("svelte").Component<Props, {}, "">;
|
|
20
|
+
type WebMCPConfirm = ReturnType<typeof WebMCPConfirm>;
|
|
21
|
+
export default WebMCPConfirm;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — a bridge to a desktop MCP client for browsers without the
|
|
3
|
+
* origin trial.
|
|
4
|
+
*
|
|
5
|
+
* Chrome's `document.modelContext` is the runtime the adapter was written for,
|
|
6
|
+
* but it sits behind a flag and only Chrome's own agent consumes it. This
|
|
7
|
+
* module presents the same {@link ModelContextLike} surface over a *widget*
|
|
8
|
+
* that relays tools to a local MCP server — the shape of
|
|
9
|
+
* `@jason.today/webmcp`'s page-side `WebMCP` class:
|
|
10
|
+
* `registerTool(name, description, inputSchema, execute)`, with tool calls
|
|
11
|
+
* arriving over its websocket and the returned value passed to the MCP client
|
|
12
|
+
* as the tool result. The adapter's tools already return MCP-shaped
|
|
13
|
+
* `{content: [{type: 'text', text}]}` results, so nothing is translated.
|
|
14
|
+
*
|
|
15
|
+
* Install it before the editor mounts:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { installBridgedModelContext } from '@flowdrop/flowdrop/webmcp';
|
|
19
|
+
* const widget = new WebMCP(); // the bridge's page-side script
|
|
20
|
+
* installBridgedModelContext(widget); // no-op when a real runtime exists
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* The widget cannot unregister a tool. When the adapter aborts a
|
|
24
|
+
* registration (editor destroyed), the relayed function keeps answering — with
|
|
25
|
+
* a structured `UNAVAILABLE` error — until the page unloads. A re-registration
|
|
26
|
+
* under the same name takes over again.
|
|
27
|
+
*
|
|
28
|
+
* The relay's MCP server answers a tool call with a timeout after 30 seconds.
|
|
29
|
+
* A confirm dialog left open longer than that returns a timeout to the agent
|
|
30
|
+
* while the dialog stays up; approving it then still applies the change. Hosts
|
|
31
|
+
* that expect slow approvals should say so in the dialog copy or use a custom
|
|
32
|
+
* `approval` callback.
|
|
33
|
+
*
|
|
34
|
+
* Nothing here decides *who* may reach the page: the bridge is localhost-only
|
|
35
|
+
* and token-gated by design, and the adapter's confirm dialog and the
|
|
36
|
+
* server's permissions still gate every mutation. Hosts should still make the
|
|
37
|
+
* bridge an explicit opt-in rather than a default.
|
|
38
|
+
*
|
|
39
|
+
* @module webmcp/bridge
|
|
40
|
+
*/
|
|
41
|
+
import type { ModelContextLike, RegisteredToolDefinition, ToolInputSchema } from './types.js';
|
|
42
|
+
/** The page-side widget surface the bridge relays to (`@jason.today/webmcp`). */
|
|
43
|
+
export interface WebMCPWidgetLike {
|
|
44
|
+
registerTool(name: string, description: string, inputSchema: ToolInputSchema, execute: (args: unknown) => unknown): unknown;
|
|
45
|
+
}
|
|
46
|
+
export interface BridgedModelContext extends ModelContextLike {
|
|
47
|
+
/** Tools currently registered through the bridge, by name. */
|
|
48
|
+
readonly tools: ReadonlyMap<string, RegisteredToolDefinition>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Wrap a bridge widget as a `modelContext`-shaped runtime.
|
|
52
|
+
*
|
|
53
|
+
* Each `registerTool` is relayed once per name; the relayed function looks the
|
|
54
|
+
* tool up at call time so aborts and re-registrations are honoured without the
|
|
55
|
+
* widget having to know about them.
|
|
56
|
+
*/
|
|
57
|
+
export declare function createBridgedModelContext(widget: WebMCPWidgetLike): BridgedModelContext;
|
|
58
|
+
/**
|
|
59
|
+
* Install a bridged runtime as `document.modelContext` unless a runtime is
|
|
60
|
+
* already present (Chrome's, or one installed earlier). Returns the runtime
|
|
61
|
+
* in effect, or null outside a browser (or when `target` is null).
|
|
62
|
+
*/
|
|
63
|
+
export declare function installBridgedModelContext(widget: WebMCPWidgetLike, target?: object | null): ModelContextLike | null;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — a bridge to a desktop MCP client for browsers without the
|
|
3
|
+
* origin trial.
|
|
4
|
+
*
|
|
5
|
+
* Chrome's `document.modelContext` is the runtime the adapter was written for,
|
|
6
|
+
* but it sits behind a flag and only Chrome's own agent consumes it. This
|
|
7
|
+
* module presents the same {@link ModelContextLike} surface over a *widget*
|
|
8
|
+
* that relays tools to a local MCP server — the shape of
|
|
9
|
+
* `@jason.today/webmcp`'s page-side `WebMCP` class:
|
|
10
|
+
* `registerTool(name, description, inputSchema, execute)`, with tool calls
|
|
11
|
+
* arriving over its websocket and the returned value passed to the MCP client
|
|
12
|
+
* as the tool result. The adapter's tools already return MCP-shaped
|
|
13
|
+
* `{content: [{type: 'text', text}]}` results, so nothing is translated.
|
|
14
|
+
*
|
|
15
|
+
* Install it before the editor mounts:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { installBridgedModelContext } from '@flowdrop/flowdrop/webmcp';
|
|
19
|
+
* const widget = new WebMCP(); // the bridge's page-side script
|
|
20
|
+
* installBridgedModelContext(widget); // no-op when a real runtime exists
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* The widget cannot unregister a tool. When the adapter aborts a
|
|
24
|
+
* registration (editor destroyed), the relayed function keeps answering — with
|
|
25
|
+
* a structured `UNAVAILABLE` error — until the page unloads. A re-registration
|
|
26
|
+
* under the same name takes over again.
|
|
27
|
+
*
|
|
28
|
+
* The relay's MCP server answers a tool call with a timeout after 30 seconds.
|
|
29
|
+
* A confirm dialog left open longer than that returns a timeout to the agent
|
|
30
|
+
* while the dialog stays up; approving it then still applies the change. Hosts
|
|
31
|
+
* that expect slow approvals should say so in the dialog copy or use a custom
|
|
32
|
+
* `approval` callback.
|
|
33
|
+
*
|
|
34
|
+
* Nothing here decides *who* may reach the page: the bridge is localhost-only
|
|
35
|
+
* and token-gated by design, and the adapter's confirm dialog and the
|
|
36
|
+
* server's permissions still gate every mutation. Hosts should still make the
|
|
37
|
+
* bridge an explicit opt-in rather than a default.
|
|
38
|
+
*
|
|
39
|
+
* @module webmcp/bridge
|
|
40
|
+
*/
|
|
41
|
+
function unavailable(name) {
|
|
42
|
+
return {
|
|
43
|
+
content: [
|
|
44
|
+
{
|
|
45
|
+
type: 'text',
|
|
46
|
+
text: JSON.stringify({
|
|
47
|
+
ok: false,
|
|
48
|
+
code: 'UNAVAILABLE',
|
|
49
|
+
error: `Tool "${name}" is no longer registered; the editor that offered it was closed. Reload the page and try again.`
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
isError: true
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Wrap a bridge widget as a `modelContext`-shaped runtime.
|
|
58
|
+
*
|
|
59
|
+
* Each `registerTool` is relayed once per name; the relayed function looks the
|
|
60
|
+
* tool up at call time so aborts and re-registrations are honoured without the
|
|
61
|
+
* widget having to know about them.
|
|
62
|
+
*/
|
|
63
|
+
export function createBridgedModelContext(widget) {
|
|
64
|
+
const tools = new Map();
|
|
65
|
+
const relayed = new Set();
|
|
66
|
+
return {
|
|
67
|
+
tools,
|
|
68
|
+
registerTool(tool, options) {
|
|
69
|
+
tools.set(tool.name, tool);
|
|
70
|
+
options?.signal?.addEventListener('abort', () => {
|
|
71
|
+
if (tools.get(tool.name) === tool)
|
|
72
|
+
tools.delete(tool.name);
|
|
73
|
+
}, { once: true });
|
|
74
|
+
if (!relayed.has(tool.name)) {
|
|
75
|
+
relayed.add(tool.name);
|
|
76
|
+
widget.registerTool(tool.name, tool.description, tool.inputSchema, (args) => {
|
|
77
|
+
const live = tools.get(tool.name);
|
|
78
|
+
if (!live)
|
|
79
|
+
return unavailable(tool.name);
|
|
80
|
+
return live.execute((args ?? {}));
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return Promise.resolve();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Install a bridged runtime as `document.modelContext` unless a runtime is
|
|
89
|
+
* already present (Chrome's, or one installed earlier). Returns the runtime
|
|
90
|
+
* in effect, or null outside a browser (or when `target` is null).
|
|
91
|
+
*/
|
|
92
|
+
export function installBridgedModelContext(widget, target = typeof document === 'undefined' ? null : document) {
|
|
93
|
+
if (!target)
|
|
94
|
+
return null;
|
|
95
|
+
const host = target;
|
|
96
|
+
if (host.modelContext)
|
|
97
|
+
return host.modelContext;
|
|
98
|
+
const nav = typeof navigator === 'undefined'
|
|
99
|
+
? undefined
|
|
100
|
+
: navigator;
|
|
101
|
+
if (nav?.modelContext)
|
|
102
|
+
return nav.modelContext;
|
|
103
|
+
const bridged = createBridgedModelContext(widget);
|
|
104
|
+
Object.defineProperty(target, 'modelContext', { value: bridged, configurable: true });
|
|
105
|
+
return bridged;
|
|
106
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP adapter — tool descriptors.
|
|
3
|
+
*
|
|
4
|
+
* Maps the command union to `{verb, description, inputSchema, readOnly,
|
|
5
|
+
* toCommands}` records. Transport-free: no DOM, no Svelte, nothing from the
|
|
6
|
+
* runtime — so the same records can back a server-side MCP endpoint later.
|
|
7
|
+
*
|
|
8
|
+
* The schemas are written by hand next to the interfaces they mirror
|
|
9
|
+
* (`commands/types.ts`). Each exposed command is one record in `COMMANDS`:
|
|
10
|
+
* its description, its input schema, how validated arguments become the
|
|
11
|
+
* command, and how the command reads back to a human in the confirm dialog.
|
|
12
|
+
* The coverage test in `tests/unit/webmcp` asserts every literal of the
|
|
13
|
+
* `Command` union is either exposed here or on the explicit exclusion list,
|
|
14
|
+
* and that every exposed record carries all four parts, so a new DSL verb
|
|
15
|
+
* cannot slip through unclassified or half-described.
|
|
16
|
+
*
|
|
17
|
+
* @module webmcp/descriptors
|
|
18
|
+
*/
|
|
19
|
+
import type { Command } from '../commands/types.js';
|
|
20
|
+
import { VIEW_COMMAND_TYPES } from '../chat/commandClassifier.js';
|
|
21
|
+
import { type ToolDescriptor, type ToolInputSchema } from './types.js';
|
|
22
|
+
import { validateToolArgs } from './validate.js';
|
|
23
|
+
export { validateToolArgs };
|
|
24
|
+
/** Command types exposed as one tool each (verb === command type). */
|
|
25
|
+
export declare const EXPOSED_COMMAND_TYPES: readonly ["add_node", "delete_node", "rename_node", "move_node", "swap_node", "set_config", "get_config", "connect", "disconnect_ports", "disconnect_node", "list_nodes", "list_edges", "list_types", "describe_type", "search_types", "info", "undo", "redo", "auto_layout", "beautify_layout"];
|
|
26
|
+
/**
|
|
27
|
+
* Command types folded into the single `view` tool (its `action` enum). The
|
|
28
|
+
* list itself lives in the command classifier, next to `isViewCommand`, so
|
|
29
|
+
* the chat panel and this adapter cannot disagree about what a view command is.
|
|
30
|
+
*/
|
|
31
|
+
export { VIEW_COMMAND_TYPES };
|
|
32
|
+
/**
|
|
33
|
+
* Command types deliberately not exposed.
|
|
34
|
+
* - `clear` wipes the whole workflow; an agent can delete nodes one by one and
|
|
35
|
+
* each deletion is gated.
|
|
36
|
+
* - `help` describes the text DSL; the tool schemas are the help.
|
|
37
|
+
*/
|
|
38
|
+
export declare const EXCLUDED_COMMAND_TYPES: readonly ["clear", "help"];
|
|
39
|
+
/** Extra tool verbs that are not command types. */
|
|
40
|
+
export declare const COMPOSITE_VERBS: readonly ["view", "batch"];
|
|
41
|
+
type Args = Record<string, unknown>;
|
|
42
|
+
type ExposedType = (typeof EXPOSED_COMMAND_TYPES)[number];
|
|
43
|
+
/** The record for one exposed command — for tests that check its shape. */
|
|
44
|
+
export declare function commandRecord(type: ExposedType): {
|
|
45
|
+
description: string;
|
|
46
|
+
inputSchema: ToolInputSchema;
|
|
47
|
+
build: (args: Args) => Command;
|
|
48
|
+
summarize: (command: Command) => string;
|
|
49
|
+
};
|
|
50
|
+
export interface BuildDescriptorsOptions {
|
|
51
|
+
/** Register the `view` tool. Default true. */
|
|
52
|
+
view?: boolean;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build the descriptor list. Pure; call it as often as you like.
|
|
56
|
+
*/
|
|
57
|
+
export declare function buildToolDescriptors(options?: BuildDescriptorsOptions): ToolDescriptor[];
|
|
58
|
+
/** One line describing a command for a human about to approve it. */
|
|
59
|
+
export declare function describeCommand(command: Command): string;
|