|
| 1 | +/** |
| 2 | + * Re-advertises the WebMCP tools registered inside the WordPress document as |
| 3 | + * tools of the Playground page itself. |
| 4 | + * |
| 5 | + * A plugin running in Playground can register tools with |
| 6 | + * `document.modelContext`, but that registry lives in a nested, |
| 7 | + * service-worker-served iframe an agent never sees. This proxy mirrors those |
| 8 | + * tools onto the top-level `document.modelContext` and forwards each call back |
| 9 | + * into the WordPress document: |
| 10 | + * |
| 11 | + * agent → document.modelContext (this page) |
| 12 | + * → PlaygroundClient.callWebMCPTool() (Comlink) |
| 13 | + * → remote frame (postMessage) |
| 14 | + * → WordPress document (the plugin's execute()) |
| 15 | + */ |
| 16 | + |
| 17 | +import type { |
| 18 | + PlaygroundClient, |
| 19 | + WebMCPToolDescriptor, |
| 20 | +} from '@wp-playground/remote'; |
| 21 | +import { logger } from '@php-wasm/logger'; |
| 22 | +import { |
| 23 | + toolDefinitions, |
| 24 | + getSiteToolDefinitions, |
| 25 | + stringifyError, |
| 26 | +} from './tools/tool-definitions'; |
| 27 | +import type { ModelContextTool } from './webmcp'; |
| 28 | +import { getModelContext } from './webmcp'; |
| 29 | + |
| 30 | +export interface WebMCPSiteToolProxy { |
| 31 | + stop(): void; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Starts mirroring `client`'s in-site WebMCP tools onto this page. |
| 36 | + * |
| 37 | + * Every announcement carries the full tool list, so the previous registration |
| 38 | + * is aborted and the new list registered in its place. Returns a handle that |
| 39 | + * unregisters everything it registered. |
| 40 | + */ |
| 41 | +export function startWebMCPSiteToolProxy( |
| 42 | + client: PlaygroundClient |
| 43 | +): WebMCPSiteToolProxy { |
| 44 | + const maybeModelContext = getModelContext(); |
| 45 | + if (!maybeModelContext) { |
| 46 | + // Nothing can be advertised, so there is nothing to observe for. |
| 47 | + return { stop: () => {} }; |
| 48 | + } |
| 49 | + // `register` is hoisted, so it does not inherit the narrowing above. |
| 50 | + const modelContext = maybeModelContext; |
| 51 | + |
| 52 | + const reserved = reservedToolNames(); |
| 53 | + let registration: AbortController | null = null; |
| 54 | + let stopped = false; |
| 55 | + let registeredDescriptors = ''; |
| 56 | + |
| 57 | + async function register(tools: WebMCPToolDescriptor[]) { |
| 58 | + if (stopped) { |
| 59 | + return; |
| 60 | + } |
| 61 | + // Registration is asynchronous, so a burst of announcements could |
| 62 | + // otherwise re-register an unchanged list several times over. |
| 63 | + const descriptors = JSON.stringify(tools); |
| 64 | + if (descriptors === registeredDescriptors) { |
| 65 | + return; |
| 66 | + } |
| 67 | + registeredDescriptors = descriptors; |
| 68 | + |
| 69 | + registration?.abort(); |
| 70 | + registration = new AbortController(); |
| 71 | + const { signal } = registration; |
| 72 | + |
| 73 | + for (const tool of tools) { |
| 74 | + if (reserved.has(tool.name)) { |
| 75 | + logger.warn( |
| 76 | + `Skipping the WebMCP tool "${tool.name}" registered by ` + |
| 77 | + `the site: Playground already provides a tool under ` + |
| 78 | + `that name.` |
| 79 | + ); |
| 80 | + continue; |
| 81 | + } |
| 82 | + try { |
| 83 | + await modelContext.registerTool(proxyTool(client, tool), { |
| 84 | + signal, |
| 85 | + }); |
| 86 | + } catch (error) { |
| 87 | + logger.warn( |
| 88 | + `Failed to register the site's WebMCP tool ` + |
| 89 | + `"${tool.name}": ${stringifyError(error)}` |
| 90 | + ); |
| 91 | + } |
| 92 | + if (signal.aborted) { |
| 93 | + return; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + client |
| 99 | + .onWebMCPToolsChanged((tools: WebMCPToolDescriptor[]) => { |
| 100 | + void register(tools); |
| 101 | + }) |
| 102 | + .catch((error) => { |
| 103 | + logger.warn("Failed to observe the site's WebMCP tools:", error); |
| 104 | + }); |
| 105 | + |
| 106 | + return { |
| 107 | + stop() { |
| 108 | + stopped = true; |
| 109 | + registration?.abort(); |
| 110 | + registration = null; |
| 111 | + registeredDescriptors = ''; |
| 112 | + }, |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Names Playground registers itself. A site tool may not take one of them |
| 118 | + * over, or an agent asking to execute PHP would reach the site's plugin |
| 119 | + * instead. |
| 120 | + */ |
| 121 | +function reservedToolNames(): Set<string> { |
| 122 | + return new Set([ |
| 123 | + ...Object.keys(toolDefinitions), |
| 124 | + ...Object.keys(getSiteToolDefinitions()), |
| 125 | + ]); |
| 126 | +} |
| 127 | + |
| 128 | +function proxyTool( |
| 129 | + client: PlaygroundClient, |
| 130 | + tool: WebMCPToolDescriptor |
| 131 | +): ModelContextTool { |
| 132 | + return { |
| 133 | + name: tool.name, |
| 134 | + description: tool.description, |
| 135 | + inputSchema: tool.inputSchema, |
| 136 | + annotations: tool.annotations, |
| 137 | + execute: async (input) => { |
| 138 | + try { |
| 139 | + return await client.callWebMCPTool(tool.name, input ?? {}); |
| 140 | + } catch (error) { |
| 141 | + return { |
| 142 | + error: |
| 143 | + `Error running the WordPress site's "${tool.name}" ` + |
| 144 | + `tool: ${stringifyError(error)}`, |
| 145 | + }; |
| 146 | + } |
| 147 | + }, |
| 148 | + }; |
| 149 | +} |
0 commit comments