diff --git a/site/cli/mcp.mjs b/site/cli/mcp.mjs index 2c2aa9d8..e4db2817 100644 --- a/site/cli/mcp.mjs +++ b/site/cli/mcp.mjs @@ -12,6 +12,7 @@ import readline from "node:readline"; import { fileURLToPath } from "node:url"; import { checkLicense, getAsset, listStacks, searchAssets } from "../lib/catalog-query.mjs"; import { loadData } from "./data.mjs"; +import { cliArgs } from "../lib/cli.mjs"; export const TOOLS = [ { @@ -52,7 +53,9 @@ export const TOOLS = [ export function callTool(data, name, args = {}) { const text = (v) => ({ content: [{ type: "text", text: JSON.stringify(v, null, 2) }] }); if (name === "search_assets") { - return text(searchAssets(data, { q: args.query || "", category: args.category, license: args.license, commercial: args.commercial, noAttribution: args.no_attribution, engine: args.engine, format: args.format, limit: args.limit })); + // Clamp limit to documented range 1..100 + const limit = typeof args.limit === "number" ? Math.min(100, Math.max(1, Math.floor(args.limit))) : undefined; + return text(searchAssets(data, { q: args.query || "", category: args.category, license: args.license, commercial: args.commercial, noAttribution: args.no_attribution, engine: args.engine, format: args.format, limit })); } if (name === "get_asset") { const a = getAsset(data, args.id); @@ -85,22 +88,39 @@ export function handle(msg, data) { } } +/** Process one JSON-RPC message and write the reply (if any) to stdout. */ +function processLine(raw, data) { + if (!raw.trim()) return; + let msg; + try { + msg = JSON.parse(raw); + } catch { + process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } })}\n`); + return; + } + // JSON-RPC 2.0 requires the top-level message to be an object. + if (!msg || typeof msg !== "object" || Array.isArray(msg)) { + process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32600, message: "Invalid Request: top-level message must be an object" } })}\n`); + return; + } + let answer; + try { + answer = handle(msg, data); + } catch (err) { + process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id: msg.id ?? null, error: { code: -32603, message: String(err) } })}\n`); + return; + } + if (answer) process.stdout.write(`${JSON.stringify(answer)}\n`); +} + async function main() { - const i = process.argv.indexOf("--data"); - const data = await loadData(i === -1 ? undefined : process.argv[i + 1]); + const { values: { data: dataPath } } = cliArgs( + { data: { type: "string", short: "d" } }, + "node site/cli/mcp.mjs [--data ]" + ); + const data = await loadData(dataPath); const rl = readline.createInterface({ input: process.stdin }); - rl.on("line", (raw) => { - if (!raw.trim()) return; - let msg; - try { - msg = JSON.parse(raw); - } catch { - process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } })}\n`); - return; - } - const answer = handle(msg, data); - if (answer) process.stdout.write(`${JSON.stringify(answer)}\n`); - }); + rl.on("line", (raw) => processLine(raw, data)); } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { @@ -108,4 +128,4 @@ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.me console.error(err.message); process.exit(1); }); -} +} \ No newline at end of file