MCPcopy Create free account
hub / github.com/angular/angular / initWebMcpForm

Function initWebMcpForm

packages/forms/signals/src/webmcp/registration.ts:53–109  ·  view source on GitHub ↗
(
  formTree: FieldTree<unknown>,
  options: {name: string; description: string; annotations?: {consequentialHint?: boolean}},
  injector: Injector,
)

Source from the content-addressed store, hash-verified

51};
52
53async 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

Callers 1

registerWebMcpFormFunction · 0.85

Calls 9

submitFunction · 0.90
inferSchemaFromFieldNodeFunction · 0.85
mapMethod · 0.80
errorSummaryMethod · 0.80
setMethod · 0.65
joinMethod · 0.65
pathKeysMethod · 0.45
fieldTreeMethod · 0.45

Tested by

no test coverage detected