@nexface/tools 0.1.21-alpha.8 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/webmcp.js +77 -30
  2. package/package.json +1 -1
package/dist/webmcp.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { AgentToolError } from "./shared/errors.js";
1
2
  function getWebMcpModelContext() {
2
3
  if (typeof document === "undefined")
3
4
  return undefined;
@@ -78,6 +79,13 @@ export function connectToolRuntimeToWebMcp(runtime) {
78
79
  queueMicrotask(() => disposeUnusedConnection(runtime, current));
79
80
  };
80
81
  }
82
+ function evaluationConfirmationRequired() {
83
+ return {
84
+ isError: true,
85
+ _meta: { "x-nexface-eval": "host-confirmation-required" },
86
+ content: [{ type: "text", text: "Nexface evaluation requires user confirmation." }],
87
+ };
88
+ }
81
89
  function createConnection(runtime, modelContext) {
82
90
  const registrations = new Map();
83
91
  let disposed = false;
@@ -86,7 +94,12 @@ function createConnection(runtime, modelContext) {
86
94
  const begin = {
87
95
  name: "nxf_beginOperation",
88
96
  description: "Begin a continuous page operation before using page tools. Call nxf_endOperation with the returned operationId when all page work is done, before your final response. Repeated begin returns the same active operation. Omitting begin shows feedback only while each tool executes.",
89
- inputSchema: { type: "object", properties: {}, additionalProperties: false },
97
+ inputSchema: {
98
+ "x-nexface": { control: true },
99
+ type: "object",
100
+ properties: {},
101
+ additionalProperties: false,
102
+ },
90
103
  execute: async (input, options = {}) => {
91
104
  options.signal?.throwIfAborted();
92
105
  controls.signal.throwIfAborted();
@@ -116,6 +129,7 @@ function createConnection(runtime, modelContext) {
116
129
  description: "End the page operation identified by operationId after all page work. Waits for its admitted tools to settle. Does not stop an external agent or undo changes.",
117
130
  inputSchema: {
118
131
  type: "object",
132
+ "x-nexface": { control: true },
119
133
  properties: { operationId: { type: "string" } },
120
134
  required: ["operationId"],
121
135
  additionalProperties: false,
@@ -163,43 +177,76 @@ function createConnection(runtime, modelContext) {
163
177
  registrations.set(definition.name, registration);
164
178
  let result;
165
179
  try {
180
+ const business = wrappedInput
181
+ ? definition.inputSchema.properties.input
182
+ : definition.inputSchema;
183
+ for (const key of ["nxf_progressLabel", "nxf_confirmation"]) {
184
+ if (Object.hasOwn(business.properties ?? {}, key))
185
+ throw new TypeError(`WebMCP reserves the top-level property "${key}".`);
186
+ }
187
+ const { enum: allowedInputs, ...businessSchema } = business;
188
+ const businessValues = allowedInputs?.filter((value) => !!value && typeof value === "object" && !Array.isArray(value));
189
+ const enumConstraint = businessValues
190
+ ? {
191
+ anyOf: businessValues.length
192
+ ? businessValues.map((value) => ({
193
+ properties: {
194
+ ...Object.fromEntries(Object.entries(value).map(([key, item]) => [key, { const: item }])),
195
+ nxf_progressLabel: { type: "string" },
196
+ },
197
+ required: Object.keys(value),
198
+ additionalProperties: false,
199
+ }))
200
+ : [false],
201
+ }
202
+ : {};
203
+ const properties = {
204
+ ...business.properties,
205
+ nxf_progressLabel: {
206
+ type: "string",
207
+ description: "Briefly describe the action in the user's language.",
208
+ },
209
+ };
166
210
  result = modelContext.registerTool({
211
+ annotations: { readOnlyHint: definition.effect === "read" },
167
212
  name: definition.name,
168
213
  description: definition.description,
169
214
  inputSchema: {
170
- type: "object",
171
- properties: {
172
- ...(wrappedInput
173
- ? definition.inputSchema.properties
174
- : {
175
- input: definition.inputSchema,
176
- }),
177
- progressLabel: {
178
- type: "string",
179
- description: "Briefly describe this tool action in the user’s language. Do not expose internal identifiers or claim success before completion.",
180
- },
181
- },
182
- required: ["input"],
183
- additionalProperties: false,
215
+ $schema: "https://json-schema.org/draft/2020-12/schema",
216
+ ...businessSchema,
217
+ properties,
218
+ ...enumConstraint,
219
+ not: { required: ["nxf_confirmation"] },
184
220
  },
185
221
  execute: async (parameters, options = {}) => {
222
+ // Native WebMCP invokes the current registration, which can change after discovery.
223
+ const evaluation = globalThis[Symbol.for("@nexface/evals/phase")];
224
+ if (evaluation === "forbidden")
225
+ throw new Error("Unexpected tool origin.");
226
+ if (evaluation && tool.confirmation)
227
+ return evaluationConfirmationRequired();
228
+ if (evaluation === "read" && definition.effect !== "read")
229
+ throw new Error("Nexface evaluation requires a read-only tool.");
230
+ if (!parameters || typeof parameters !== "object" || Array.isArray(parameters))
231
+ throw new TypeError("WebMCP arguments must be an object.");
186
232
  const args = parameters;
187
- if (!args ||
188
- typeof args !== "object" ||
189
- Array.isArray(args) ||
190
- !args.input ||
191
- typeof args.input !== "object" ||
192
- Array.isArray(args.input) ||
193
- Object.keys(args).some((key) => key !== "input" && key !== "progressLabel") ||
194
- (args.progressLabel !== undefined && typeof args.progressLabel !== "string")) {
195
- throw new TypeError("WebMCP tools require { input: object, progressLabel?: string }.");
233
+ const { nxf_progressLabel: label, ...input } = args;
234
+ if (Object.hasOwn(input, "nxf_confirmation"))
235
+ throw new TypeError("Confirmation must be collected by the host.");
236
+ if (label !== undefined && typeof label !== "string")
237
+ throw new TypeError("nxf_progressLabel must be a string.");
238
+ try {
239
+ return await runtime.execute(tool, wrappedInput ? { input } : input, {
240
+ ...(options.signal ? { signal: options.signal } : {}),
241
+ ...(typeof label === "string" ? { progressLabel: label } : {}),
242
+ });
243
+ }
244
+ catch (error) {
245
+ if (evaluation && wrappedInput && error instanceof AgentToolError &&
246
+ error.code === "TOOL_CONFIRMATION_UNAVAILABLE")
247
+ return evaluationConfirmationRequired();
248
+ throw error;
196
249
  }
197
- return runtime.execute(tool, wrappedInput ? { input: args.input } : args.input, {
198
- ...(options.signal ? { signal: options.signal } : {}),
199
- ...(typeof args.progressLabel === "string"
200
- ? { progressLabel: args.progressLabel }
201
- : {}),
202
- });
203
250
  },
204
251
  }, { signal: controller.signal });
205
252
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexface/tools",
3
- "version": "0.1.21-alpha.8",
3
+ "version": "0.1.22",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {