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.
88 lines • 3.95 kB
TypeScript
/**
* 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 { type FunctionToolLiftInput, type ToolDeclarationProblem } from "../function-tools.js";
import type { FunctionToolSpec } from "../namespaces/deploy.types.js";
/** Set for an Astro endpoint file: the request its tools name is the endpoint's own. */
export interface EndpointToolContext {
/** The endpoint's route as Astro writes it, e.g. `/api/notes/[note_id]`. */
route: string;
/** The project's `base`. Default `/`. */
base?: string;
/** True when the route answers only with a trailing slash (`trailingSlash: "always"`). */
trailingSlash?: boolean;
/** True when Astro prerenders the route, so a static file answers its path. */
prerendered?: boolean;
}
export interface ExtractToolDeclarationsOptions {
/** The file's name or path, reported in a refusal. Omit for an inline source. */
file?: string | null;
/** Read the file as an Astro endpoint; omitted, it is a function's entry file. */
endpoint?: EndpointToolContext;
}
export type ToolExtraction = {
ok: true;
tools: FunctionToolSpec[];
} | {
ok: false;
problem: ToolDeclarationProblem;
};
/**
* 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 declare function endpointToolPath(route: string, opts?: {
base?: string;
trailingSlash?: boolean;
}): {
ok: true;
path: string;
params: string[];
} | {
ok: false;
segment: string;
};
/**
* 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 declare function extractToolDeclarations(source: string, options?: ExtractToolDeclarationsOptions): ToolExtraction;
/**
* The reader `@run402/sdk/node` installs: the tools one function's entry
* source declares, or a {@link Run402DeployError} thrown before any request.
*/
export declare function liftFunctionTools(input: FunctionToolLiftInput): FunctionToolSpec[];
//# sourceMappingURL=tool-declarations.d.ts.map