run402-mcp
Version:
MCP server for Run402 — full-stack backend infrastructure for AI agents: Postgres, auth, storage, serverless functions and atomic deploys. Paid with x402/MPP. Includes $0.03 image generation.
84 lines • 3.72 kB
JavaScript
/**
* Tool arguments are strict: an argument a tool does not declare is refused,
* never silently dropped.
*
* Every tool is registered with its strict schema (`strictInput`), so the
* published JSON Schema says `additionalProperties: false`, and the server
* (`mcp-server.ts`) checks each `tools/call` against the same schema before
* the handler runs, answering a failure with the structured error:
* `UNKNOWN_ARGUMENT` (with the closest declared names) or `INVALID_ARGUMENTS`.
*/
import { z } from "./schema.js";
import { closestMembers } from "./sandbox-proxy.js";
/** A tool's input shape as a strict object: unknown keys fail. */
export function strictInput(shape) {
return z.object(shape).strict();
}
/**
* The structured refusal for arguments that fail a tool's strict schema, or null when they pass.
*
* An undeclared key is named by its dotted path from the arguments, so a key
* nested inside an argument (`functions.patch.set.api.klass`) is never
* reported as a top-level argument; its `did_you_mean` searches the keys the
* object holding it declares, and `accepted_at` lists them by that object's path.
*/
export function argumentError(tool, schema, args) {
const parsed = schema.safeParse(args ?? {});
if (parsed.success)
return null;
const declared = Object.keys(schema.shape);
const unknown = [];
const didYouMean = {};
const acceptedAt = {};
const hints = [];
let topLevel = false;
const issues = [];
for (const issue of parsed.issues) {
if (issue.code !== "unrecognized_keys") {
issues.push({ path: issue.path.join(".") || "(arguments)", message: issue.message });
continue;
}
const at = issue.path.join(".");
const beside = at === "" ? declared : (issue.declared ?? []);
if (at === "")
topLevel = true;
else
acceptedAt[at] = beside;
for (const key of issue.keys ?? []) {
const path = at === "" ? key : `${at}.${key}`;
const near = closestMembers(key, beside);
unknown.push(path);
didYouMean[path] = near.map((name) => (at === "" ? name : `${at}.${name}`));
hints.push(near.length > 0 ? `${path} (did you mean ${near.join(" or ")}?)` : path);
}
}
if (unknown.length > 0) {
const nested = Object.keys(acceptedAt);
const noun = nested.length === 0 ? "argument" : "key";
const takes = [
...(topLevel ? [`It takes: ${declared.join(", ") || "no arguments"}.`] : []),
...nested.map((at) => `${at} takes: ${acceptedAt[at].join(", ") || "no keys"}.`),
];
return {
code: "UNKNOWN_ARGUMENT",
message: `${tool} does not take ${unknown.length === 1 ? `the ${noun}` : `the ${noun}s`} ${hints.join(", ")}. ${takes.join(" ")}`,
next_actions: [{
type: "edit_request",
why: `Remove or rename the ${noun}s the tool does not declare, then call it again.`,
tool,
unknown,
did_you_mean: didYouMean,
accepted: declared,
...(nested.length > 0 ? { accepted_at: acceptedAt } : {}),
}],
details: { unknown, ...(issues.length > 0 ? { issues } : {}) },
};
}
return {
code: "INVALID_ARGUMENTS",
message: `${tool}: ${issues.map((i) => `${i.path}: ${i.message}`).join("; ")}`,
next_actions: [{ type: "edit_request", why: "Fix the arguments named in details.issues, then call the tool again.", tool }],
details: { issues },
};
}
//# sourceMappingURL=input-guard.js.map