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.

162 lines • 7.09 kB
import { argumentError } from "./input-guard.js"; import { toJsonSchema } from "./schema.js"; /** Newest first: the version a client gets when it asks for one this server does not know. */ export const SUPPORTED_PROTOCOL_VERSIONS = ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"]; export const JSONRPC_PARSE_ERROR = -32700; export const JSONRPC_INVALID_REQUEST = -32600; export const JSONRPC_METHOD_NOT_FOUND = -32601; export const JSONRPC_INVALID_PARAMS = -32602; function textError(text) { return { content: [{ type: "text", text }], isError: true }; } function rpcError(id, code, message) { return { jsonrpc: "2.0", id, error: { code, message } }; } function isId(value) { return typeof value === "string" || typeof value === "number"; } export class McpServer { info; #tools = new Map(); constructor(info) { this.info = info; } /** The same call shape the MCP SDK's `registerTool` had, so the registrations in `index.ts` read as before. */ registerTool(name, config, handler) { if (this.#tools.has(name)) throw new Error(`Tool ${name} is already registered`); this.#tools.set(name, { ...config, handler: handler, // The listing entry as the MCP SDK published it (key order included), built once. listing: { name, description: config.description, inputSchema: toJsonSchema(config.inputSchema), execution: { taskSupport: "forbidden" }, outputSchema: toJsonSchema(config.outputSchema), }, }); } /** The tool names, in registration order. */ toolNames() { return [...this.#tools.keys()]; } /** * One decoded JSON-RPC message in, the response out; `undefined` for a * notification or a client response, which are never answered. */ async handle(message) { if (typeof message !== "object" || message === null || Array.isArray(message)) { return rpcError(null, JSONRPC_INVALID_REQUEST, "Invalid Request"); } const m = message; const hasId = isId(m.id); if (typeof m.method !== "string") { // A response to a request this server never sends, or junk: answer junk that carries an id. return "result" in m || "error" in m ? undefined : rpcError(hasId ? m.id : null, JSONRPC_INVALID_REQUEST, "Invalid Request"); } if (!hasId) return undefined; // notifications/initialized, notifications/cancelled, and any other notification const id = m.id; const params = (typeof m.params === "object" && m.params !== null ? m.params : {}); switch (m.method) { case "initialize": return { jsonrpc: "2.0", id, result: this.#initialize(params) }; case "ping": return { jsonrpc: "2.0", id, result: {} }; case "tools/list": return { jsonrpc: "2.0", id, result: { tools: [...this.#tools.values()].map((t) => t.listing) } }; case "tools/call": return { jsonrpc: "2.0", id, result: await this.#call(params) }; default: return rpcError(id, JSONRPC_METHOD_NOT_FOUND, "Method not found"); } } #initialize(params) { const requested = params.protocolVersion; const protocolVersion = SUPPORTED_PROTOCOL_VERSIONS.includes(requested) ? requested : SUPPORTED_PROTOCOL_VERSIONS[0]; return { protocolVersion, capabilities: { tools: { listChanged: true } }, serverInfo: { ...this.info } }; } async #call(params) { const name = typeof params.name === "string" ? params.name : ""; const tool = this.#tools.get(name); if (!tool) return textError(`MCP error ${JSONRPC_INVALID_PARAMS}: Tool ${name} not found`); const refusal = argumentError(name, tool.inputSchema, params.arguments); if (refusal) { return { content: [{ type: "text", text: `Error: ${refusal.message}` }], structuredContent: { status: "error", error: refusal }, isError: true }; } const args = tool.inputSchema.parse(params.arguments ?? {}); let result; try { result = await tool.handler(args); } catch (err) { return textError(err instanceof Error ? err.message : String(err)); } return this.#checkOutput(name, tool, result); } /** A successful result's structuredContent must satisfy the tool's output schema, as the MCP SDK checked it. */ #checkOutput(name, tool, result) { if (result.isError) return result; if (result.structuredContent === undefined) { return textError(`MCP error ${JSONRPC_INVALID_PARAMS}: Output validation error: Tool ${name} has an output schema but no structured content was provided`); } const checked = tool.outputSchema.safeParse(result.structuredContent); if (checked.success) return result; const detail = checked.issues.map((i) => `${i.path.join(".") || "(result)"}: ${i.message}`).join("; "); return textError(`MCP error ${JSONRPC_INVALID_PARAMS}: Output validation error: Invalid structured content for tool ${name}: ${detail}`); } /** * Serve newline-delimited JSON-RPC on `input` and `output` until `input` * ends; resolves once every call in flight has answered. */ listen(input, output) { const inFlight = new Set(); const write = (response) => { if (response) output.write(`${JSON.stringify(response)}\n`); }; const dispatch = (line) => { let message; try { message = JSON.parse(line); } catch { write(rpcError(null, JSONRPC_PARSE_ERROR, "Parse error")); return; } const task = this.handle(message) .then(write) .catch((err) => { console.error(`run402-mcp: ${err instanceof Error ? err.message : String(err)}`); }) .finally(() => inFlight.delete(task)); inFlight.add(task); }; let buffer = ""; input.setEncoding("utf8"); input.on("data", (chunk) => { buffer += chunk; let newline; while ((newline = buffer.indexOf("\n")) >= 0) { const line = buffer.slice(0, newline).replace(/\r$/, ""); buffer = buffer.slice(newline + 1); if (line.trim() !== "") dispatch(line); } }); return new Promise((resolve) => { input.once("end", () => { if (buffer.trim() !== "") dispatch(buffer.replace(/\r$/, "")); buffer = ""; void Promise.allSettled([...inFlight]).then(() => resolve()); }); }); } } //# sourceMappingURL=mcp-server.js.map