(
formTree: FieldTree<unknown>,
options: {name: string; description: string; annotations?: {consequentialHint?: boolean}},
injector: Injector,
)
| 51 | }; |
| 52 | |
| 53 | async function initWebMcpForm( |
| 54 | formTree: FieldTree<unknown>, |
| 55 | options: {name: string; description: string; annotations?: {consequentialHint?: boolean}}, |
| 56 | injector: Injector, |
| 57 | ) { |
| 58 | const node = formTree() as FieldNode; |
| 59 | const inputSchema = inferSchemaFromFieldNode(node); |
| 60 | |
| 61 | if (!inputSchema) { |
| 62 | throw new Error( |
| 63 | `Could not accurately infer WebMCP schema for form "${options.name}". ` + |
| 64 | `Ensure that the form model does not contain null, undefined, empty arrays, or unsupported types.`, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | await declareExperimentalWebMcpTool( |
| 69 | { |
| 70 | name: options.name, |
| 71 | description: options.description, |
| 72 | inputSchema, |
| 73 | outputSchema: FORM_OUTPUT_SCHEMA, |
| 74 | annotations: { |
| 75 | ...options.annotations, |
| 76 | |
| 77 | // Forms are assumed to implicitly mutate the DOM (otherwise how would a user interact with them?) |
| 78 | // and therefore are _never_ read-only. |
| 79 | readOnlyHint: false, |
| 80 | |
| 81 | // Response text is currently hard-coded by the framework and trusted or derived from application |
| 82 | // errors which are considered trusted. |
| 83 | untrustedContentHint: false, |
| 84 | |
| 85 | // Forms are end-user UI elements, not developer-facing debugging tools. |
| 86 | debugging: false, |
| 87 | }, |
| 88 | execute: async (args: Record<string, unknown> | unknown[]) => { |
| 89 | // Populate the form with changes from the agent. |
| 90 | node.value.set(args); |
| 91 | |
| 92 | // Trigger form submission. |
| 93 | const success = await submit(formTree); |
| 94 | |
| 95 | // Report the result to the agent. |
| 96 | if (success) { |
| 97 | return {success: true}; |
| 98 | } else { |
| 99 | const errors = node.errorSummary().map((err) => { |
| 100 | const fieldName = (err.fieldTree() as FieldNode).structure.pathKeys().join('.'); |
| 101 | return `${fieldName ? `${fieldName}: ` : ''}${err.message || err.kind}`; |
| 102 | }); |
| 103 | return {success: false, errors}; |
| 104 | } |
| 105 | }, |
| 106 | }, |
| 107 | injector, |
| 108 | ); |
| 109 | } |
| 110 |
no test coverage detected