feat(webui): publish WebMCP tools for the dashboard
Register the dashboard's own tools with document.modelContext so an in-page agent can read tasks, events, and daemon health, and recover a stuck task: list_tasks, get_task, recent_events and daemon_health (read-only), plus retry_task, stop_task and cancel_task (consequential). The human review gate (approve/reject) and the destructive verbs (abandon/archive) are deliberately not exposed. Every tool calls the same ui/src/lib/api.ts client the interface calls, so the agent and the UI exercise one set of endpoints, one CSRF header and one error shape. Registration is framework-agnostic (lib/webmcp.ts machinery, lib/webmcp-tools.ts list, lib/archie-webmcp.ts the api binding) and no-ops when document.modelContext is absent, which it is outside the WebMCP origin trial. Tools returning agent, operator or inbound content carry untrustedContentHint; only the recovery actions carry consequentialHint. Verified in system Chrome 152 with --enable-features=WebMCP: seven tools listed with schemas and annotations, all four read-only tools executed against the live dashboard, and the flagless run mounts the dashboard with no exception.
This commit is contained in:
1 parent
1257e08e78
commit
d55dedab86
6 files changed
+414
-9
No files matched your search
@@ -0,0 +1,178 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
// The model context is read at registration time, so each test installs its own
|
||||
// fake `document` before calling. The tool list takes its API client as a
|
||||
// parameter, so these tests never load the real fetch client.
|
||||
const { registerWebMcpTools, createWebMcpRegistrar } = await import("../src/lib/webmcp.ts");
|
||||
const { archieWebMcpTools } = await import("../src/lib/webmcp-tools.ts");
|
||||
|
||||
interface FakeRegistered {
|
||||
name: string;
|
||||
description: string;
|
||||
inputSchema: unknown;
|
||||
annotations: Record<string, unknown> | undefined;
|
||||
signal: AbortSignal | undefined;
|
||||
execute: (input: Record<string, unknown>) => unknown;
|
||||
}
|
||||
|
||||
function installModelContext(): { registered: FakeRegistered[] } {
|
||||
const registered: FakeRegistered[] = [];
|
||||
Object.defineProperty(globalThis, "document", {
|
||||
configurable: true,
|
||||
value: {
|
||||
modelContext: {
|
||||
registerTool: async (
|
||||
tool: Omit<FakeRegistered, "signal">,
|
||||
options?: { signal?: AbortSignal },
|
||||
) => {
|
||||
registered.push({ ...tool, signal: options?.signal });
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
return { registered };
|
||||
}
|
||||
|
||||
function installNoModelContext(): void {
|
||||
Object.defineProperty(globalThis, "document", { configurable: true, value: {} });
|
||||
}
|
||||
|
||||
interface Call {
|
||||
method: string;
|
||||
args: unknown[];
|
||||
}
|
||||
|
||||
function fakeClient(): { calls: Call[]; client: Parameters<typeof archieWebMcpTools>[0] } {
|
||||
const calls: Call[] = [];
|
||||
const reply = (method: string, args: unknown[]) => {
|
||||
calls.push({ method, args });
|
||||
return { ok: true, method, args };
|
||||
};
|
||||
return {
|
||||
calls,
|
||||
client: {
|
||||
health: async () => reply("health", []),
|
||||
tasks: async () => reply("tasks", []),
|
||||
task: async (id: string) => reply("task", [id]),
|
||||
captures: async (limit?: number) => reply("captures", [limit]),
|
||||
taskAction: async (id: string, action: string) => reply("taskAction", [id, action]),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function toolNamed(client: Parameters<typeof archieWebMcpTools>[0], name: string) {
|
||||
const tool = archieWebMcpTools(client).find((candidate) => candidate.name === name);
|
||||
assert.ok(tool, `no tool named ${name}`);
|
||||
return tool;
|
||||
}
|
||||
|
||||
test("registration no-ops without document.modelContext instead of throwing", async () => {
|
||||
installNoModelContext();
|
||||
const registration = await registerWebMcpTools(archieWebMcpTools(fakeClient().client));
|
||||
assert.equal(registration.registered, false);
|
||||
assert.doesNotThrow(() => registration.dispose());
|
||||
});
|
||||
|
||||
test("registration passes each tool's schema and annotations, and disposes via the signal", async () => {
|
||||
const { registered } = installModelContext();
|
||||
const tools = archieWebMcpTools(fakeClient().client);
|
||||
const registration = await registerWebMcpTools(tools);
|
||||
|
||||
assert.equal(registration.registered, true);
|
||||
assert.deepEqual(
|
||||
registered.map((tool) => tool.name),
|
||||
tools.map((tool) => tool.name),
|
||||
);
|
||||
for (const tool of registered) {
|
||||
assert.ok(tool.description.length > 0, `${tool.name} has no description`);
|
||||
assert.equal(typeof tool.inputSchema, "object");
|
||||
assert.ok(tool.signal instanceof AbortSignal, `${tool.name} has no abort signal`);
|
||||
assert.equal(tool.signal.aborted, false);
|
||||
}
|
||||
|
||||
registration.dispose();
|
||||
for (const tool of registered) {
|
||||
assert.equal(tool.signal?.aborted, true, `${tool.name} was not unregistered`);
|
||||
}
|
||||
});
|
||||
|
||||
test("read-only tools are read-only; recovery tools are consequential", () => {
|
||||
const annotations = new Map(
|
||||
archieWebMcpTools(fakeClient().client).map((tool) => [tool.name, tool.annotations]),
|
||||
);
|
||||
|
||||
for (const name of ["list_tasks", "get_task", "recent_events", "daemon_health"]) {
|
||||
assert.equal(annotations.get(name)?.readOnlyHint, true, `${name} should be read-only`);
|
||||
assert.notEqual(annotations.get(name)?.consequentialHint, true, `${name} must not be consequential`);
|
||||
}
|
||||
for (const name of ["retry_task", "stop_task", "cancel_task"]) {
|
||||
assert.equal(annotations.get(name)?.consequentialHint, true, `${name} needs a confirmation`);
|
||||
assert.notEqual(annotations.get(name)?.readOnlyHint, true, `${name} must not be read-only`);
|
||||
}
|
||||
});
|
||||
|
||||
test("tools returning agent, operator or inbound content carry untrustedContentHint", () => {
|
||||
const annotations = new Map(
|
||||
archieWebMcpTools(fakeClient().client).map((tool) => [tool.name, tool.annotations]),
|
||||
);
|
||||
for (const name of ["list_tasks", "get_task", "recent_events"]) {
|
||||
assert.equal(annotations.get(name)?.untrustedContentHint, true, `${name} returns untrusted content`);
|
||||
}
|
||||
assert.equal(annotations.get("daemon_health")?.untrustedContentHint, false);
|
||||
assert.equal(annotations.get("list_tasks")?.consequentialHint, undefined);
|
||||
});
|
||||
|
||||
test("execute returns the envelope and calls the dashboard's own client methods", async () => {
|
||||
const { calls, client } = fakeClient();
|
||||
|
||||
const listed = (await toolNamed(client, "list_tasks").execute({})) as {
|
||||
content: { type: string; text: string }[];
|
||||
};
|
||||
assert.equal(calls[0].method, "tasks");
|
||||
assert.equal(listed.content[0].type, "text");
|
||||
assert.deepEqual(JSON.parse(listed.content[0].text), { ok: true, method: "tasks", args: [] });
|
||||
|
||||
await toolNamed(client, "get_task").execute({ task_id: "42" });
|
||||
assert.deepEqual(calls[1], { method: "task", args: ["42"] });
|
||||
|
||||
await toolNamed(client, "recent_events").execute({ limit: 10 });
|
||||
assert.deepEqual(calls[2], { method: "captures", args: [10] });
|
||||
|
||||
await toolNamed(client, "daemon_health").execute({});
|
||||
assert.deepEqual(calls[3], { method: "health", args: [] });
|
||||
});
|
||||
|
||||
test("recovery tools post the dashboard's own action verbs", async () => {
|
||||
const { calls, client } = fakeClient();
|
||||
await toolNamed(client, "retry_task").execute({ task_id: "9" });
|
||||
await toolNamed(client, "stop_task").execute({ task_id: "9" });
|
||||
await toolNamed(client, "cancel_task").execute({ task_id: "9" });
|
||||
|
||||
assert.deepEqual(calls, [
|
||||
{ method: "taskAction", args: ["9", "retry"] },
|
||||
{ method: "taskAction", args: ["9", "stop"] },
|
||||
{ method: "taskAction", args: ["9", "cancel"] },
|
||||
]);
|
||||
});
|
||||
|
||||
test("a recovery tool without a task id fails before touching the API", async () => {
|
||||
const { calls, client } = fakeClient();
|
||||
await assert.rejects(() => Promise.resolve(toolNamed(client, "retry_task").execute({})), /task_id is required/);
|
||||
assert.equal(calls.length, 0);
|
||||
});
|
||||
|
||||
test("re-registering replaces the previous registration", async () => {
|
||||
const { registered } = installModelContext();
|
||||
const register = createWebMcpRegistrar(archieWebMcpTools(fakeClient().client));
|
||||
const count = archieWebMcpTools(fakeClient().client).length;
|
||||
|
||||
const first = await register();
|
||||
const second = await register();
|
||||
|
||||
assert.notEqual(first, second);
|
||||
assert.equal(registered.length, count * 2);
|
||||
for (const tool of registered.slice(0, count)) {
|
||||
assert.equal(tool.signal?.aborted, true, "the first registration was left live");
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user