Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions site/cli/mcp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -85,27 +88,44 @@ 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 <file|https URL>]"
);
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)) {
main().catch((err) => {
console.error(err.message);
process.exit(1);
});
}
}