UNPKG

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.

587 lines • 28.3 kB
/** * The tool-declaration extractor: source in, declarations out. * * A function file or an Astro endpoint file declares MCP tools beside its * handler with `export const tool = { … }` (one) or * `export const tools = { name: { … }, … }` (several), as static literals. * This module reads that export by PARSING the file. It never imports, * evaluates or runs the source, so a file whose top level throws, reads * secrets or opens sockets is read exactly like any other. * * It is the only extractor: `@run402/sdk/node` calls it for every function * source a deploy reads, and `@run402/astro` calls it for every * server-rendered endpoint file at build. The gateway reads no source. * * How a file is read: * * 1. As JavaScript, with acorn. A plain or bundled file parses here. * 2. Otherwise types are erased with `module.stripTypeScriptTypes` in strip * mode, which replaces them with spaces, so every line stays where it * was and a reported line is exact. * 3. A file with syntax that cannot be erased (an `enum`) is read in * transform mode, where lines move, so its refusals carry no line. * * The export's initializer is then evaluated as a JSON-literal subset: object, * array, string, number, boolean and null literals only. An identifier, a * call, a spread, a computed key or a template substitution is * `MCP_TOOL_NOT_STATIC`. */ import * as nodeModule from "node:module"; import { parse as parseJs } from "acorn"; import { checkFunctionToolEntry, isValidMcpToolName, toolDeclarationError, } from "../function-tools.js"; import { FUNCTION_TOOL_METHODS } from "../namespaces/deploy.types.js"; /** Thrown and caught inside this module only. */ class Refusal extends Error { code; fieldPath; node; tool; constructor(code, fieldPath, message, node, tool) { super(message); this.code = code; this.fieldPath = fieldPath; this.node = node; this.tool = tool; } /** A refusal already described as data, at the node it is about. */ static at(node, problem, tool) { return new Refusal(problem.code, problem.field_path, problem.message, node, tool); } } const ENDPOINT_HANDLERS = new Set([...FUNCTION_TOOL_METHODS, "ALL"]); // ─── reading the file ──────────────────────────────────────────────────────── /** * Cheap pre-check so the common case (no tool export) never pays for a * parse. A false positive costs only the parse. */ function mayDeclareTools(source) { return /\bexport\b/.test(source) && /\btools?\b/.test(source); } /** Whether an unparseable file still plainly exports `tool` or `tools`. */ function plainlyExportsTools(source) { const declared = /\bexport\s+(?:declare\s+)?(?:const|let|var)\s+(tools?)\b/.exec(source); if (declared) return declared[1]; const clause = /\bexport\s*\{[^}]*?\b(?:as\s+)?(tools?)\s*[,}]/.exec(source); return clause ? clause[1] : null; } function parseModule(source) { return parseJs(source, { ecmaVersion: "latest", sourceType: "module", allowHashBang: true, locations: true }); } /** * `stripTypeScriptTypes` announces itself with a one-time ExperimentalWarning * on stderr. Drop exactly that warning for the duration of the call; every * other warning still reaches the process. */ function stripTypesQuietly(strip, source, mode) { const emit = process.emitWarning; process.emitWarning = function quietStripWarning(warning, ...rest) { const type = typeof rest[0] === "string" ? rest[0] : rest[0]?.type; const message = typeof warning === "string" ? warning : warning?.message ?? ""; const name = typeof warning === "string" ? undefined : warning?.name; if ((type === "ExperimentalWarning" || name === "ExperimentalWarning") && /stripTypeScriptTypes|type strip/i.test(message)) return; return emit.call(process, warning, ...rest); }; try { return strip(source, { mode }); } finally { process.emitWarning = emit; } } function firstLine(err) { const message = err instanceof Error ? err.message : String(err); return message.split("\n")[0].replace(/\s*\(\d+:\d+\)$/, ""); } function readFile(source) { let jsError; try { return { program: parseModule(source), exactLines: true }; } catch (err) { jsError = err; } const strip = nodeModule.stripTypeScriptTypes; if (typeof strip !== "function") { return { unreadable: `${firstLine(jsError)}; this Node (${process.version}) cannot erase TypeScript types, so use Node 22.13 or later` }; } let stripped; let exactLines = true; try { stripped = stripTypesQuietly(strip, source, "strip"); } catch (err) { if (err.code !== "ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX") return { unreadable: firstLine(err) }; try { stripped = stripTypesQuietly(strip, source, "transform"); exactLines = false; } catch (transformErr) { return { unreadable: firstLine(transformErr) }; } } try { return { program: parseModule(stripped), exactLines }; } catch (err) { return { unreadable: firstLine(err) }; } } /** True when the module assigns to `name` anywhere (`=`, `+=`, `++`, destructuring, `for (name of …)`). */ function isReassigned(program, name) { const targets = (node) => { if (!node) return false; switch (node.type) { case "Identifier": return node.name === name; case "ArrayPattern": return node.elements.some(targets); case "ObjectPattern": return node.properties.some((p) => targets(p.type === "RestElement" ? p : p.value)); case "RestElement": return targets(node.argument); case "AssignmentPattern": return targets(node.left); default: return false; } }; const visit = (node) => { if (Array.isArray(node)) return node.some(visit); if (!node || typeof node !== "object" || typeof node.type !== "string") return false; const n = node; if (n.type === "AssignmentExpression" && targets(n.left)) return true; if (n.type === "UpdateExpression" && targets(n.argument)) return true; if ((n.type === "ForInStatement" || n.type === "ForOfStatement") && n.left.type !== "VariableDeclaration" && targets(n.left)) return true; return Object.entries(n).some(([key, value]) => key !== "type" && key !== "loc" && typeof value === "object" && visit(value)); }; return visit(program.body); } /** Names a binding pattern introduces (`{ a, b: [c] }` → a, c). */ function patternNames(pattern, out = []) { if (!pattern) return out; switch (pattern.type) { case "Identifier": out.push(pattern.name); break; case "ObjectPattern": for (const p of pattern.properties) patternNames(p.type === "RestElement" ? p.argument : p.value, out); break; case "ArrayPattern": for (const el of pattern.elements) patternNames(el, out); break; case "RestElement": patternNames(pattern.argument, out); break; case "AssignmentPattern": patternNames(pattern.left, out); break; default: break; } return out; } function specifierName(node) { return node.type === "Identifier" ? node.name : String(node.value); } const writeInline = (name) => `Write it in this file as \`export const ${name} = { … }\` with literal values.`; /** * Walk the module's top level for its exported names and for the `tool` and * `tools` exports. A bundle writes `var t = {…}; export { t as tools }`, so an * export specifier is followed to its local top-level binding; a `let` or * `var` binding counts only when the module never reassigns it. */ function moduleExports(program) { const out = { names: new Set() }; const locals = new Map(); for (const stmt of program.body) { const decl = stmt.type === "ExportNamedDeclaration" && stmt.declaration ? stmt.declaration : stmt; if (decl.type !== "VariableDeclaration") continue; for (const d of decl.declarations) { if (d.id.type === "Identifier") locals.set(d.id.name, { kind: decl.kind, init: d.init, node: d }); } } const record = (name, declared) => { out.names.add(name); if ((name === "tool" || name === "tools") && out[name] === undefined) out[name] = declared; }; const fromLocal = (exported, localName, node) => { const local = locals.get(localName); if (!local) { return { init: null, node, why: `\`${exported}\` is not a literal declared in this file. ${writeInline(exported)}` }; } if (local.kind !== "const" && isReassigned(program, localName)) { return { init: null, node, why: `\`${exported}\` is reassigned, so its value cannot be read without running the file. Declare it once with \`const\`.` }; } if (!local.init) { return { init: null, node, why: `\`${exported}\` has no value. ${writeInline(exported)}` }; } return { init: local.init, node: local.node }; }; for (const stmt of program.body) { if (stmt.type === "ExportAllDeclaration") { if (stmt.exported) { const name = specifierName(stmt.exported); record(name, { init: null, node: stmt, why: `\`${name}\` is re-exported from another module, which is not read. ${writeInline(name)}` }); } continue; } if (stmt.type !== "ExportNamedDeclaration") continue; if (stmt.declaration) { const decl = stmt.declaration; if (decl.type === "VariableDeclaration") { for (const d of decl.declarations) { if (d.id.type === "Identifier") { record(d.id.name, fromLocal(d.id.name, d.id.name, d)); } else { for (const name of patternNames(d.id)) { record(name, { init: null, node: d, why: `\`${name}\` is destructured from another value. ${writeInline(name)}` }); } } } } else if (decl.id?.name) { const kind = decl.type === "ClassDeclaration" ? "a class" : "a function"; record(decl.id.name, { init: null, node: decl, why: `\`${decl.id.name}\` is ${kind}; a tool declaration is an object literal. ${writeInline(decl.id.name)}` }); } continue; } for (const spec of stmt.specifiers) { const exported = specifierName(spec.exported); if (stmt.source) { record(exported, { init: null, node: spec, why: `\`${exported}\` is re-exported from another module, which is not read. ${writeInline(exported)}` }); } else { record(exported, fromLocal(exported, specifierName(spec.local), spec)); } } } return out; } // ─── evaluating the literal ────────────────────────────────────────────────── function joinPath(base, key) { return typeof key === "number" ? `${base}[${key}]` : `${base}.${key}`; } function propertyKey(prop, path) { if (prop.type === "SpreadElement") throw new Refusal("MCP_TOOL_NOT_STATIC", path, "A spread (`...`) is not allowed in a tool declaration; write the fields inline.", prop); if (prop.computed) throw new Refusal("MCP_TOOL_NOT_STATIC", path, "A computed key (`[key]: …`) is not allowed in a tool declaration; write the key itself.", prop); if (prop.kind !== "init" || prop.method) throw new Refusal("MCP_TOOL_NOT_STATIC", path, "A getter, setter or method is not allowed in a tool declaration; every field is a literal value.", prop); if (prop.key.type === "Identifier") return prop.key.name; if (prop.key.type === "Literal" && (typeof prop.key.value === "string" || typeof prop.key.value === "number")) return String(prop.key.value); throw new Refusal("MCP_TOOL_NOT_STATIC", path, "An object key must be an identifier or a string.", prop); } /** Evaluate a JSON-literal subset of an ESTree expression, recording the line of every value by path. */ function literalValue(node, path, lines) { lines.set(path, node); const notStatic = (message) => new Refusal("MCP_TOOL_NOT_STATIC", path, message, node); switch (node.type) { case "Literal": { if (node.regex) throw notStatic("A regular expression is not a JSON value; write the pattern as a string."); if (typeof node.value === "bigint" || node.bigint !== undefined) throw notStatic("A BigInt is not a JSON value; write a number."); return node.value; } case "TemplateLiteral": { if (node.expressions.length > 0) throw notStatic("A template literal with a `${…}` substitution is not static; write the whole string."); return node.quasis.map((quasi) => quasi.value.cooked).join(""); } case "UnaryExpression": { const arg = node.argument; if (node.operator === "-" && arg.type === "Literal" && typeof arg.value === "number") return -arg.value; // A minifier writes `true` as `!0` and `false` as `!1`. if (node.operator === "!" && arg.type === "Literal" && (arg.value === 0 || arg.value === 1)) return arg.value === 0; throw notStatic(`The operator \`${node.operator}\` is not allowed in a tool declaration; write the value itself.`); } case "ArrayExpression": { return node.elements.map((element, index) => { const at = joinPath(path, index); if (element === null) throw new Refusal("MCP_TOOL_NOT_STATIC", at, "An array hole is not a JSON value.", node); if (element.type === "SpreadElement") throw new Refusal("MCP_TOOL_NOT_STATIC", at, "A spread (`...`) is not allowed in a tool declaration; write the items inline.", element); return literalValue(element, at, lines); }); } case "ObjectExpression": { const out = {}; for (const prop of node.properties) { const key = propertyKey(prop, path); const at = joinPath(path, key); if (prop.shorthand) throw new Refusal("MCP_TOOL_NOT_STATIC", at, `\`${key}\` refers to a variable; write the value inline.`, prop); if (key === "__proto__") throw new Refusal("MCP_TOOL_NOT_STATIC", at, "`__proto__` is not allowed in a tool declaration.", prop); out[key] = literalValue(prop.value, at, lines); } return out; } case "Identifier": { if (node.name === "undefined") throw notStatic("`undefined` is not a JSON value; leave the field out."); throw notStatic(`\`${node.name}\` refers to a variable; write the value inline.`); } case "CallExpression": case "NewExpression": throw notStatic("A function call is not static; write the value inline."); default: throw notStatic(`A ${node.type} is not a static value; a tool declaration holds only object, array, string, number, boolean and null literals.`); } } /** The node recorded for `path`, or for its nearest recorded ancestor. */ function nodeAt(lines, path) { let at = path; for (;;) { const node = lines.get(at); if (node) return node; const cut = Math.max(at.lastIndexOf("."), at.lastIndexOf("[")); if (cut <= 0) return undefined; at = at.slice(0, cut); } } // ─── an endpoint's request ─────────────────────────────────────────────────── /** * An Astro route as a tool path: `[param]` becomes `{param}`, and the * project's `base` is applied. A rest segment (`[...rest]`) or a parameter * that is only part of a segment (`post-[id]`) has no tool path. */ export function endpointToolPath(route, opts = {}) { const params = []; const parts = []; for (const segment of route.split("/")) { if (segment === "") continue; const whole = /^\[([^[\]]+)\]$/.exec(segment); if (whole && !whole[1].startsWith("...")) { params.push(whole[1]); parts.push(`{${whole[1]}}`); } else if (/[[\]]/.test(segment)) { return { ok: false, segment }; } else { parts.push(segment); } } const base = (opts.base ?? "/").replace(/\/+$/, ""); const prefix = base === "" || base.startsWith("/") ? base : `/${base}`; let path = `${prefix}${parts.length > 0 ? `/${parts.join("/")}` : ""}`; if (path === "") path = "/"; if (opts.trailingSlash && !path.endsWith("/") && !/\.[^/.]+$/.test(path)) path += "/"; return { ok: true, path, params }; } function endpointMethod(stated, handlers, fieldPath, tool, node) { const exported = handlers.filter((name) => name !== "ALL"); const answersAll = handlers.includes("ALL"); const list = handlers.length > 0 ? handlers.join(", ") : "none"; const at = `${fieldPath}.method`; if (stated !== undefined) { if (exported.includes(stated) || answersAll) return stated; throw new Refusal("MCP_TOOL_SCHEMA_INVALID", at, `\`method\` is "${stated}", but this file exports no \`${stated}\` handler (it exports: ${list}). Name a handler the file exports, or add \`export const ${stated}\`.`, node, tool); } if (exported.length === 1 && !answersAll) return exported[0]; if (handlers.length === 0) { throw new Refusal("MCP_TOOL_SCHEMA_INVALID", at, `This file exports no ${FUNCTION_TOOL_METHODS.join(", ")} or ALL handler, so the tool has no request to make. Export the handler the tool calls.`, node, tool); } throw new Refusal("MCP_TOOL_SCHEMA_INVALID", at, `State \`method\`: this file exports ${list}, so the tool's request is ambiguous.`, node, tool); } // ─── the extractor ─────────────────────────────────────────────────────────── function declarations(program, endpoint) { const exports = moduleExports(program); const { tool, tools } = exports; if (!tool && !tools) return []; if (tool && tools) { const second = tools.node.start > tool.node.start ? "tools" : "tool"; throw new Refusal("MCP_TOOL_SCHEMA_INVALID", second, "A file exports `tool` (one declaration) or `tools` (several, by name), never both. Move the single declaration into `tools`.", exports[second].node); } const exportName = tool ? "tool" : "tools"; const declared = (tool ?? tools); let pathParams; let endpointPath; let handlers = []; if (endpoint) { if (endpoint.prerendered) { throw Refusal.at(declared.node, { code: "MCP_TOOL_ROUTE_ELSEWHERE", field_path: exportName, message: `This endpoint is prerendered, so a static file answers \`${endpoint.route}\` and a tool call would never reach the handler. Add \`export const prerender = false\` to serve it on request, or remove the tool.`, }); } const converted = endpointToolPath(endpoint.route, endpoint); if (!converted.ok) { throw Refusal.at(declared.node, { code: "MCP_TOOL_PATH_UNSUPPORTED", field_path: exportName, message: `The route \`${endpoint.route}\` has the segment \`${converted.segment}\`, which cannot be a tool's path: a tool names one request, so a parameter stands for one whole segment and a rest segment is not allowed. Declare the tool on an endpoint whose route has only \`[param]\` segments.`, }); } endpointPath = converted.path; pathParams = converted.params; handlers = [...ENDPOINT_HANDLERS].filter((name) => exports.names.has(name)); } if (!declared.init) throw new Refusal("MCP_TOOL_NOT_STATIC", exportName, declared.why ?? `\`${exportName}\` must be a literal object.`, declared.node); const lines = new Map(); const finish = (value, fieldPath, name) => { const entry = value; if (endpoint && entry && typeof entry === "object" && !Array.isArray(entry) && "path" in entry) { throw new Refusal("MCP_TOOL_SCHEMA_INVALID", `${fieldPath}.path`, `\`path\` is never written in an endpoint file: the tool's path is the endpoint's own route (\`${endpointPath}\`). Remove \`path\`.`, nodeAt(lines, `${fieldPath}.path`), name); } const checked = checkFunctionToolEntry(value, fieldPath, pathParams ? { pathParams } : {}); if (!checked.ok) throw Refusal.at(nodeAt(lines, checked.problem.field_path) ?? declared.node, checked.problem, name); const { name: ownName, ...rest } = checked.tool; const toolName = name ?? ownName; if (!endpoint) return { ...(toolName !== undefined ? { name: toolName } : {}), ...rest }; if (toolName === undefined) { throw new Refusal("MCP_TOOL_NAME_INVALID", `${fieldPath}.name`, "An endpoint's `tool` must state its `name` (a file's route is not a tool name). Add `name: \"…\"`, or use `tools = { name: { … } }`.", nodeAt(lines, fieldPath)); } const method = endpointMethod(rest.method, handlers, fieldPath, toolName, nodeAt(lines, `${fieldPath}.method`) ?? nodeAt(lines, fieldPath)); return { name: toolName, ...(rest.title !== undefined ? { title: rest.title } : {}), description: rest.description, input: rest.input, ...(rest.annotations !== undefined ? { annotations: rest.annotations } : {}), method, path: endpointPath, }; }; if (exportName === "tool") { return [finish(literalValue(declared.init, "tool", lines), "tool", undefined)]; } if (declared.init.type === "ArrayExpression") { throw new Refusal("MCP_TOOL_SCHEMA_INVALID", "tools", "`tools` must be an object whose keys are the tool names: `export const tools = { list_notes: { … } }`.", declared.init); } if (declared.init.type !== "ObjectExpression") { // Reports the variable, call or other form the same way a field would. literalValue(declared.init, "tools", lines); throw new Refusal("MCP_TOOL_SCHEMA_INVALID", "tools", "`tools` must be an object whose keys are the tool names.", declared.init); } const out = []; const seen = new Set(); lines.set("tools", declared.init); for (const prop of declared.init.properties) { const name = propertyKey(prop, "tools"); const fieldPath = `tools.${name}`; if (!isValidMcpToolName(name)) { throw new Refusal("MCP_TOOL_NAME_INVALID", fieldPath, `\`${name}\` is not a valid MCP tool name: 1 to 128 of A-Z, a-z, 0-9, \`_\`, \`-\` and \`.\`.`, prop, name); } if (seen.has(name)) { throw new Refusal("MCP_TOOL_NAME_DUPLICATE", fieldPath, `\`tools\` names \`${name}\` twice. A tool name is unique in a release.`, prop, name); } seen.add(name); if (prop.shorthand) throw new Refusal("MCP_TOOL_NOT_STATIC", fieldPath, `\`${name}\` refers to a variable; write the declaration inline.`, prop, name); let value; try { value = literalValue(prop.value, fieldPath, lines); } catch (err) { if (err instanceof Refusal && err.tool === undefined) throw new Refusal(err.code, err.fieldPath, err.message, err.node, name); throw err; } if (value !== null && typeof value === "object" && !Array.isArray(value) && "name" in value) { throw new Refusal("MCP_TOOL_SCHEMA_INVALID", `${fieldPath}.name`, `Under \`tools\` the key is the tool's name; remove \`name\` from \`${name}\`.`, nodeAt(lines, `${fieldPath}.name`), name); } out.push(finish(value, fieldPath, name)); } return out; } /** * Read the tools a source file declares, by parsing it. Returns the * declarations as release entries (`[]` when the file declares none), or the * one refusal an author has to fix, with its code, file, exact line when * there is one, and field path. * * A function's entry file: a single `tool` is one entry with the fields as * written; `tools` is one entry per key, the key as `name`. An Astro endpoint * file (`options.endpoint`): every entry carries `name`, `method` (the one * handler the file exports unless stated) and `path` (the endpoint's route). */ export function extractToolDeclarations(source, options = {}) { const file = options.file ?? null; if (!mayDeclareTools(source)) return { ok: true, tools: [] }; const parsed = readFile(source); if ("unreadable" in parsed) { const exported = plainlyExportsTools(source); if (exported === null) return { ok: true, tools: [] }; return { ok: false, problem: { code: "MCP_TOOL_NOT_STATIC", message: `This file could not be parsed (${parsed.unreadable}), so its \`${exported}\` export cannot be read without running it. Keep the declaration in a plain JavaScript or TypeScript file: move JSX, decorators or other compiled syntax into a module this file imports.`, file, field_path: exported, }, }; } try { return { ok: true, tools: declarations(parsed.program, options.endpoint) }; } catch (err) { if (!(err instanceof Refusal)) throw err; const line = parsed.exactLines ? err.node?.loc?.start.line : undefined; return { ok: false, problem: { code: err.code, message: err.message, file, ...(line !== undefined ? { line } : {}), field_path: err.fieldPath, ...(err.tool !== undefined ? { tool: err.tool } : {}), }, }; } } /** * The reader `@run402/sdk/node` installs: the tools one function's entry * source declares, or a {@link Run402DeployError} thrown before any request. */ export function liftFunctionTools(input) { const extraction = extractToolDeclarations(input.source, { file: input.file }); if (!extraction.ok) { throw toolDeclarationError(extraction.problem, { functionName: input.functionName, sourceField: input.sourceField }); } const unnamed = extraction.tools.find((tool) => tool.name === undefined); if (unnamed && !isValidMcpToolName(input.functionName)) { throw toolDeclarationError({ code: "MCP_TOOL_NAME_INVALID", message: `A \`tool\` with no \`name\` is named after its function, and \`${input.functionName}\` is not a valid MCP tool name (1 to 128 of A-Z, a-z, 0-9, \`_\`, \`-\` and \`.\`). Add \`name: "…"\` to the declaration.`, file: input.file, field_path: "tool.name", }, { functionName: input.functionName, sourceField: input.sourceField }); } return extraction.tools; } //# sourceMappingURL=tool-declarations.js.map