diff --git a/docs/data.md b/docs/data.md index 7c9bc6b..187c40d 100644 --- a/docs/data.md +++ b/docs/data.md @@ -110,6 +110,8 @@ does the same and reads `site/dist/data.json` when a build is there; `--data process.st } if (cmd === "check") { const r = checkLicense(data, rest[0], { commercial: !o["non-commercial"], engine: o.engine }); + if (r.error === "invalid_engine") throw new Error(r.problems[0]); print(r, `${r.ok ? "OK" : "CHECK"}: ${rest[0]}${r.problems.length ? `\n - ${r.problems.join("\n - ")}` : ""}${r.owes.length ? `\n owes: ${r.owes.join("; ")}` : ""}`); return r.ok ? 0 : 1; } diff --git a/site/cli/mcp.mjs b/site/cli/mcp.mjs index 96fde78..aac1985 100644 --- a/site/cli/mcp.mjs +++ b/site/cli/mcp.mjs @@ -9,7 +9,7 @@ * { "command": "npx", "args": ["-y", "github:TMHSDigital/Free-Game-Dev-Assets", "mcp"] } */ import readline from "node:readline"; -import { checkLicense, getAsset, listStacks, searchAssets } from "../lib/catalog-query.mjs"; +import { checkLicense, getAsset, InvalidEngineError, listStacks, searchAssets } from "../lib/catalog-query.mjs"; import { loadData } from "./data.mjs"; import { readArgs, runsAsMain } from "../lib/cli.mjs"; @@ -25,7 +25,7 @@ export const TOOLS = [ license: { type: "string", description: "A licence value (CC0, CC-BY-4.0, MIT...), SPDX id, or licence family" }, commercial: { type: "boolean", description: "Only sources usable in a commercial game (including per-file ones)" }, no_attribution: { type: "boolean", description: "Only sources that need no credit line" }, - engine: { type: "string", description: "godot, unity or unreal: hide sources made only for other engines" }, + engine: { type: "string", enum: ["godot", "unity", "unreal"], description: "Hide sources made only for other engines" }, format: { type: "string", description: "A file format, e.g. glb, png, ttf" }, limit: { type: "integer", minimum: 1, maximum: 100 }, }, @@ -41,7 +41,7 @@ export const TOOLS = [ description: "Whether a catalog entry fits a use: commercial or not, and the engine it ships in. Returns problems to resolve and what it owes.", inputSchema: { type: "object", - properties: { id: { type: "string" }, commercial: { type: "boolean", default: true }, engine: { type: "string" } }, + properties: { id: { type: "string" }, commercial: { type: "boolean", default: true }, engine: { type: "string", enum: ["godot", "unity", "unreal"] } }, required: ["id"], }, }, @@ -54,13 +54,21 @@ export function callTool(data, name, args = {}) { if (name === "search_assets") { // The schema documents 1 to 100; a client that ignores it is clamped. const limit = Number.isFinite(args.limit) ? 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 })); + try { + 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 })); + } catch (err) { + if (!(err instanceof InvalidEngineError)) throw err; + return { content: [{ type: "text", text: err.message }], isError: true }; + } } if (name === "get_asset") { const a = getAsset(data, args.id); return a ? text(a) : { content: [{ type: "text", text: `No entry "${args.id}".` }], isError: true }; } - if (name === "check_license") return text(checkLicense(data, args.id, { commercial: args.commercial !== false, engine: args.engine })); + if (name === "check_license") { + const result = checkLicense(data, args.id, { commercial: args.commercial !== false, engine: args.engine }); + return result.error === "invalid_engine" ? { ...text(result), isError: true } : text(result); + } if (name === "list_stacks") return text(listStacks(data)); return { content: [{ type: "text", text: `Unknown tool ${name}` }], isError: true }; } diff --git a/site/lib/catalog-query.mjs b/site/lib/catalog-query.mjs index 4b2fd1d..4f2ff51 100644 --- a/site/lib/catalog-query.mjs +++ b/site/lib/catalog-query.mjs @@ -6,6 +6,18 @@ import { ENGINES, queryTests, scoreFields, searchFields, SORTS, worksInEngine } from "./search.mjs"; import { isListed } from "./shared.mjs"; +export class InvalidEngineError extends Error {} + +/** An omitted engine is optional; a supplied engine must name a supported engine. */ +function normalizeEngine(engine) { + if (engine === undefined) return undefined; + const supplied = String(engine); + const name = supplied.trim().toLowerCase(); + const normalized = name === "unreal engine" || name === "ue" ? "unreal" : name; + if (!Object.hasOwn(ENGINES, normalized)) throw new InvalidEngineError(`Unknown engine ${JSON.stringify(supplied)}; use godot, unity or unreal`); + return normalized; +} + /** The fields an answer carries: enough to decide, with the page to read. */ export function assetSummary(e, siteUrl = "") { const base = String(siteUrl).replace(/\/+$/, ""); @@ -35,6 +47,7 @@ export function assetSummary(e, siteUrl = "") { * commercial OK or per-file), noAttribution, engine, format, limit }. */ export function searchAssets(data, { q = "", category, license, commercial, noAttribution, engine, format, limit = 20 } = {}) { + const selectedEngine = normalizeEngine(engine); const tests = queryTests(q); const lic = license ? String(license).toLowerCase() : null; const fmt = format ? String(format).toLowerCase() : null; @@ -45,7 +58,7 @@ export function searchAssets(data, { q = "", category, license, commercial, noAt if (lic && String(e.license).toLowerCase() !== lic && String(e.licenseFamily).toLowerCase() !== lic && String(e.license_spdx || "").toLowerCase() !== lic) continue; if (commercial && e.commercial !== true && e.commercial !== "varies") continue; if (noAttribution && e.attribution_required !== false) continue; - if (engine && !worksInEngine(e, engine)) continue; + if (selectedEngine && !worksInEngine(e, selectedEngine)) continue; if (fmt && !(e.formats || []).some((f) => String(f).toLowerCase() === fmt)) continue; const score = scoreFields(searchFields(e), tests); if (score) hits.push({ e, score }); @@ -64,16 +77,24 @@ export function getAsset(data, id) { * Whether an entry fits a use: { ok, problems, owes }. `commercial: true` * asks for a commercial game; `engine` names the engine it ships in. * `owes` is the credit line or the notice to keep; `problems` are reasons - * not to rely on it without reading the source. + * not to rely on it without reading the source. An invalid engine adds an + * `error: "invalid_engine"` so callers can distinguish it from a mismatch. */ export function checkLicense(data, id, { commercial = true, engine } = {}) { + let selectedEngine; + try { + selectedEngine = normalizeEngine(engine); + } catch (err) { + if (!(err instanceof InvalidEngineError)) throw err; + return { ok: false, problems: [err.message], owes: [], error: "invalid_engine" }; + } const e = (data.entries || []).find((x) => x.id === id); if (!e) return { ok: false, problems: [`no entry "${id}" in the catalog`], owes: [] }; const problems = []; if (commercial && e.commercial === false) problems.push("not for commercial use"); if (commercial && e.commercial === "unknown") problems.push("commercial use is not established"); if (e.commercial === "varies") problems.push("terms vary per file: check each file you take"); - if (engine && ENGINES[engine] && !worksInEngine(e, engine)) problems.push(e.engine_lock ? `licensed for ${e.engine_lock} only` : `made for another engine than ${engine}`); + if (selectedEngine && !worksInEngine(e, selectedEngine)) problems.push(e.engine_lock ? `licensed for ${e.engine_lock} only` : `made for another engine than ${selectedEngine}`); if (e.status === "deprecated") problems.push("deprecated in the catalog"); if (e.status === "needs-review") problems.push(`needs review${e.review ? `: ${e.review}` : ""}`); if (e.status === "per-item") problems.push(`licence set per item${e.perItem ? `: ${e.perItem}` : ""}`); diff --git a/site/lib/lib.test.mjs b/site/lib/lib.test.mjs index b40d987..fbd1b53 100644 --- a/site/lib/lib.test.mjs +++ b/site/lib/lib.test.mjs @@ -2876,6 +2876,7 @@ await section("catalog queries, CLI and MCP (#115)", async () => { { id: "c", name: "Pixel NC", page: "entry/c/", category: "2d", license: "CC-BY-NC-4.0", commercial: false, attribution_required: true, formats: ["PNG"], tags: ["pixel"], status: "needs-review", review: "Confirm the NC scope.", verified: "2026-10-01" }, { id: "d", name: "Old pixel", page: "entry/d/", category: "2d", license: "CC0", commercial: true, attribution_required: false, formats: [], tags: ["pixel"], status: "deprecated", verified: null }, { id: "u", name: "UE Pixel", page: "entry/u/", category: "3d", license: "custom", commercial: true, attribution_required: false, engine_lock: "unreal", formats: ["Unreal"], tags: ["pixel"], status: "active", verified: "2026-10-01" }, + { id: "g", name: "Godot Pixel", page: "entry/g/", category: "3d", license: "custom", commercial: true, attribution_required: false, engine_lock: "godot", formats: ["godot-project"], tags: ["pixel"], status: "active", verified: "2026-10-01" }, ], }; eq("query: deprecated entries are not listed", searchAssets(data, { q: "pixel" }).map((a) => a.id).includes("d"), false); @@ -2883,6 +2884,30 @@ await section("catalog queries, CLI and MCP (#115)", async () => { eq("query: licence by value or family", `${searchAssets(data, { license: "sil ofl" }).map((a) => a.id)}|${searchAssets(data, { license: "cc-by" }).map((a) => a.id)}`, "a|b"); eq("query: format, case-insensitive", searchAssets(data, { format: "ttf" }).map((a) => a.id).join(), "a"); eq("query: an engine lock hides the entry for another engine", searchAssets(data, { q: "pixel", engine: "godot" }).map((a) => a.id).includes("u"), false); + for (const engine of ["Godot", " GoDoT "]) { + eq(`query: ${engine} hides an Unreal-only entry`, searchAssets(data, { q: "pixel", engine }).some((a) => a.id === "u"), false); + has(`check: ${engine} reports an engine lock`, checkLicense(data, "u", { engine }).problems.join(), "unreal only"); + } + eq("query: Unity hides an Unreal-only entry", searchAssets(data, { engine: " Unity " }).some((a) => a.id === "u"), false); + has("check: Unity reports an Unreal-only entry", checkLicense(data, "u", { engine: " Unity " }).problems.join(), "unreal only"); + for (const engine of [" Unreal Engine ", "UE"]) { + eq(`query: ${engine} keeps an Unreal-only entry`, searchAssets(data, { engine }).some((a) => a.id === "u"), true); + eq(`check: ${engine} matches an Unreal-only entry`, checkLicense(data, "u", { engine }).ok, true); + eq(`query: ${engine} hides a Godot-only entry`, searchAssets(data, { engine }).some((a) => a.id === "g"), false); + has(`check: ${engine} reports a Godot-only entry`, checkLicense(data, "g", { engine }).problems.join(), "godot only"); + } + const nativeOnly = { ...data, entries: [{ ...data.entries[4], engine_lock: undefined, formats: ["unreal-project"] }] }; + eq("query: Godot hides an Unreal-native entry", searchAssets(nativeOnly, { engine: "Godot" }).length, 0); + has("check: Godot reports an Unreal-native entry", checkLicense(nativeOnly, "u", { engine: "Godot" }).problems.join(), "another engine than godot"); + eq("query: an omitted engine leaves Unreal-only entries visible", searchAssets(data).some((a) => a.id === "u"), true); + eq("check: an omitted engine does not apply the engine lock", checkLicense(data, "u").ok, true); + for (const engine of ["godo", "", " ", "constructor", "__proto__", null]) { + throws(`query: ${JSON.stringify(engine)} is an error even with no entries`, () => searchAssets({ entries: [] }, { engine }), "Unknown engine"); + const invalid = checkLicense(data, "u", { engine }); + eq(`check: ${JSON.stringify(engine)} is not a pass`, invalid.ok, false); + has(`check: ${JSON.stringify(engine)} reports an unknown engine`, invalid.problems.join(), "Unknown engine"); + eq(`check: ${JSON.stringify(engine)} is marked as invalid input`, invalid.error, "invalid_engine"); + } eq("query: page is absolute", getAsset(data, "a").page, "https://x.test/s/entry/a/"); eq("query: unknown id", getAsset(data, "zz"), null); const nc = checkLicense(data, "c"); @@ -2903,6 +2928,22 @@ await section("catalog queries, CLI and MCP (#115)", async () => { has("mcp: a tool answers with JSON text", call.result.content[0].text, '"attribution_string": "Art by B"'); eq("mcp: an unknown id is an error result", handle({ jsonrpc: "2.0", id: 4, method: "tools/call", params: { name: "get_asset", arguments: { id: "zz" } } }, data).result.isError, true); eq("mcp: an unknown method is a JSON-RPC error", handle({ jsonrpc: "2.0", id: 5, method: "resources/list" }, data).error.code, -32601); + const mcpCall = (name, args) => handle({ jsonrpc: "2.0", id: 6, method: "tools/call", params: { name, arguments: args } }, data).result; + for (const name of ["search_assets", "check_license"]) { + eq(`mcp: ${name} advertises canonical engines`, JSON.stringify(TOOLS.find((t) => t.name === name).inputSchema.properties.engine.enum), JSON.stringify(["godot", "unity", "unreal"])); + for (const engine of ["godo", "", "constructor", null]) { + const invalid = mcpCall(name, { id: "u", engine }); + eq(`mcp: ${name} rejects ${JSON.stringify(engine)}`, invalid.isError, true); + has(`mcp: ${name} explains the invalid engine`, invalid.content[0].text, "Unknown engine"); + } + } + const mcpCheck = mcpCall("check_license", { id: "u", engine: " Godot " }); + eq("mcp: Godot reports an Unreal-only entry as unsuitable", JSON.parse(mcpCheck.content[0].text).ok, false); + eq("mcp: a valid engine mismatch is not a tool error", mcpCheck.isError, undefined); + has("mcp: Godot reports the engine lock", mcpCheck.content[0].text, "unreal only"); + eq("mcp: Godot filters Unreal-only search results", JSON.parse(mcpCall("search_assets", { engine: "Godot" }).content[0].text).some((a) => a.id === "u"), false); + eq("mcp: Unreal Engine matches an Unreal-only entry", JSON.parse(mcpCall("check_license", { id: "u", engine: "Unreal Engine" }).content[0].text).ok, true); + eq("mcp: Unreal Engine does not match a Godot-only entry", JSON.parse(mcpCall("check_license", { id: "g", engine: "Unreal Engine" }).content[0].text).ok, false); const { main } = await import("../cli/fgda.mjs"); const fsm = (await import("node:fs")).default; @@ -2910,6 +2951,29 @@ await section("catalog queries, CLI and MCP (#115)", async () => { const file = `${osm.tmpdir()}/fgda-cli-test-${process.pid}.json`; fsm.writeFileSync(file, JSON.stringify(data)); try { + const runCli = async (...args) => { + const output = []; + try { + return { status: await main([...args, "--data", file], (s) => output.push(s)), output: output.join("\n") }; + } catch (error) { + return { error }; + } + }; + const godotCheck = await runCli("check", "u", "--engine", " Godot "); + eq("cli: Godot exits 1 for an Unreal-only entry", godotCheck.status, 1); + has("cli: Godot explains the engine lock", godotCheck.output, "unreal only"); + const godotSearch = await runCli("search", "--engine", "Godot", "--json"); + eq("cli: Godot search succeeds", godotSearch.status, 0); + eq("cli: Godot search filters Unreal-only entries", JSON.parse(godotSearch.output).some((a) => a.id === "u"), false); + eq("cli: UE alias matches an Unreal-only entry", (await runCli("check", "u", "--engine", "UE")).status, 0); + eq("cli: UE alias rejects a Godot-only entry", (await runCli("check", "g", "--engine", "UE")).status, 1); + for (const command of ["search", "check"]) { + for (const engine of ["godo", "", "constructor"]) { + const invalid = await runCli(command, "u", "--engine", engine); + has(`cli: ${command} rejects ${JSON.stringify(engine)} with a usage error`, invalid.error?.message, "Unknown engine"); + } + } + has("cli: invalid engine takes precedence over a missing id", (await runCli("check", "missing", "--engine", "godo")).error?.message, "Unknown engine"); const lines = []; eq("cli: check exits 1 when it does not fit", await main(["check", "c", "--data", file], (s) => lines.push(s)), 1); has("cli: and says why", lines.join("\n"), "not for commercial use"); @@ -2931,6 +2995,9 @@ await section("catalog queries, CLI and MCP (#115)", async () => { const thrown = reply(JSON.stringify({ jsonrpc: "2.0", id: 9, method: "tools/call", params: { name: "search_assets", arguments: {} } }), null); eq("mcp: a throwing tool call is an internal error", thrown.error.code, -32603); eq("mcp: and keeps the request id", thrown.id, 9); + const invalidEngine = reply(JSON.stringify({ jsonrpc: "2.0", id: 10, method: "tools/call", params: { name: "search_assets", arguments: { engine: "godo" } } })); + eq("mcp: an invalid engine is a tool error in JSON-RPC", invalidEngine.result.isError, true); + eq("mcp: an invalid engine keeps the request id", invalidEngine.id, 10); eq("mcp: a valid call still answers", reply(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" })).id, 1); const hits = (limit) => JSON.parse(ct({ ...data, entries: Array.from({ length: 130 }, (_, i) => ({ ...data.entries[0], id: `e${i}` })) }, "search_assets", { limit }).content[0].text).length; eq("mcp: limit is capped at 100", hits(5000), 100);