|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + declareExperimentalWebMcpTool, |
| 11 | + EnvironmentProviders, |
| 12 | + makeEnvironmentProviders, |
| 13 | + untracked, |
| 14 | +} from '@angular/core'; |
| 15 | +import type {JsonSchemaForInference} from '@mcp-b/webmcp-types'; |
| 16 | +import {submit} from '../api/structure'; |
| 17 | +import {FieldNode} from '../field/node'; |
| 18 | +import {REGISTER_WEBMCP_FORM, RegisterWebMcpForm} from './tokens'; |
| 19 | + |
| 20 | +const registerWebMcpForm: RegisterWebMcpForm = (formTree, options) => { |
| 21 | + untracked(() => { |
| 22 | + const node = formTree() as FieldNode; |
| 23 | + const inputSchema = inferSchemaFromFieldNode(node); |
| 24 | + |
| 25 | + if (!inputSchema) { |
| 26 | + throw new Error( |
| 27 | + `Could not accurately infer WebMCP schema for form "${options.name}". ` + |
| 28 | + `Ensure that the form model does not contain null, undefined, empty arrays, or unsupported types.`, |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + declareExperimentalWebMcpTool({ |
| 33 | + name: options.name, |
| 34 | + description: options.description, |
| 35 | + inputSchema, |
| 36 | + execute: async (args: Record<string, unknown>) => { |
| 37 | + // Populate the form with changes from the agent. |
| 38 | + node.value.set(args); |
| 39 | + |
| 40 | + // Trigger form submission. |
| 41 | + const success = await submit(formTree); |
| 42 | + |
| 43 | + // Report the result to the agent. |
| 44 | + if (success) { |
| 45 | + return {content: [{type: 'text', text: 'Form submitted successfully.'}]}; |
| 46 | + } else { |
| 47 | + const errorMessages = node |
| 48 | + .errorSummary() |
| 49 | + .map((err) => { |
| 50 | + const fieldName = (err.fieldTree() as FieldNode).structure.pathKeys().join('.'); |
| 51 | + return `${fieldName ? `${fieldName}: ` : ''}${err.message || err.kind}`; |
| 52 | + }) |
| 53 | + .join('\n'); |
| 54 | + return {content: [{type: 'text', text: `Form submission failed:\n${errorMessages}`}]}; |
| 55 | + } |
| 56 | + }, |
| 57 | + }); |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +/** Infers the JSON schema from a specific form field. */ |
| 62 | +function inferSchemaFromFieldNode(node: FieldNode): JsonSchemaForInference | undefined { |
| 63 | + const value = node.value(); |
| 64 | + |
| 65 | + // Primitive types. |
| 66 | + if (typeof value === 'string') return {type: 'string'}; |
| 67 | + if (typeof value === 'number') return {type: 'number'}; |
| 68 | + if (typeof value === 'boolean') return {type: 'boolean'}; |
| 69 | + |
| 70 | + // `null` or `undefined` does not hint at the underlying type. |
| 71 | + if (value === null || value === undefined) return undefined; |
| 72 | + |
| 73 | + // Use the type of the first value of an array. |
| 74 | + if (Array.isArray(value)) { |
| 75 | + if (value.length === 0) return undefined; |
| 76 | + |
| 77 | + const firstChild = node.structure.getChild('0'); |
| 78 | + if (!firstChild) return undefined; |
| 79 | + |
| 80 | + const itemSchema = inferSchemaFromFieldNode(firstChild); |
| 81 | + if (!itemSchema) return undefined; |
| 82 | + |
| 83 | + return { |
| 84 | + type: 'array', |
| 85 | + items: itemSchema, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + // Recursively infer the types of all object properties. |
| 90 | + if (typeof value === 'object') { |
| 91 | + const properties: Record<string, JsonSchemaForInference> = {}; |
| 92 | + const required: string[] = []; |
| 93 | + const children = node.structure.children(); |
| 94 | + for (const child of children) { |
| 95 | + const key = child.keyInParent(); |
| 96 | + const childSchema = inferSchemaFromFieldNode(child); |
| 97 | + if (!childSchema) return undefined; |
| 98 | + |
| 99 | + properties[key] = childSchema; |
| 100 | + |
| 101 | + if (child.required()) required.push(key.toString()); |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + type: 'object', |
| 106 | + properties, |
| 107 | + required, |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + return undefined; // Unknown type. |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Creates a provider that configures all signal forms with `experimentalWebMcpTool` |
| 116 | + * to be registered as WebMCP tools. |
| 117 | + * |
| 118 | + * @experimental |
| 119 | + */ |
| 120 | +export function provideExperimentalWebMcpForms(): EnvironmentProviders { |
| 121 | + return makeEnvironmentProviders([ |
| 122 | + { |
| 123 | + provide: REGISTER_WEBMCP_FORM, |
| 124 | + useValue: registerWebMcpForm, |
| 125 | + }, |
| 126 | + ]); |
| 127 | +} |
0 commit comments