|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * A Model Context Protocol server over the catalog (#115), on stdio, with |
| 4 | + * no dependencies: newline-delimited JSON-RPC 2.0, the tools capability |
| 5 | + * only. Tools: search_assets, get_asset, check_license, list_stacks. |
| 6 | + * node site/cli/mcp.mjs [--data <file|https URL>] |
| 7 | + * Client config (Claude Desktop, Cursor and others): |
| 8 | + * { "command": "node", "args": ["/path/to/Free-Game-Dev-Assets/site/cli/mcp.mjs"] } |
| 9 | + */ |
| 10 | +import path from "node:path"; |
| 11 | +import readline from "node:readline"; |
| 12 | +import { fileURLToPath } from "node:url"; |
| 13 | +import { checkLicense, getAsset, listStacks, searchAssets } from "../lib/catalog-query.mjs"; |
| 14 | +import { loadData } from "./data.mjs"; |
| 15 | + |
| 16 | +export const TOOLS = [ |
| 17 | + { |
| 18 | + name: "search_assets", |
| 19 | + description: "Search the Free Game Dev Assets catalog of free game assets and tools. Every licence was read at its source; results carry the licence, whether commercial use is OK, the credit owed and the date the licence was last checked.", |
| 20 | + inputSchema: { |
| 21 | + type: "object", |
| 22 | + properties: { |
| 23 | + query: { type: "string", description: "Words to search for, e.g. \"pixel font\"" }, |
| 24 | + category: { type: "string", description: "3d, 2d, audio, characters, environment, fonts, shaders-vfx, animation, video or tools" }, |
| 25 | + license: { type: "string", description: "A licence value (CC0, CC-BY-4.0, MIT...), SPDX id, or licence family" }, |
| 26 | + commercial: { type: "boolean", description: "Only sources usable in a commercial game (including per-file ones)" }, |
| 27 | + no_attribution: { type: "boolean", description: "Only sources that need no credit line" }, |
| 28 | + engine: { type: "string", description: "godot, unity or unreal: hide sources made only for other engines" }, |
| 29 | + format: { type: "string", description: "A file format, e.g. glb, png, ttf" }, |
| 30 | + limit: { type: "integer", minimum: 1, maximum: 100 }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "get_asset", |
| 36 | + description: "One catalog entry by id: licence, commercial use, credit line, status, verified date and page.", |
| 37 | + inputSchema: { type: "object", properties: { id: { type: "string" } }, required: ["id"] }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "check_license", |
| 41 | + 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.", |
| 42 | + inputSchema: { |
| 43 | + type: "object", |
| 44 | + properties: { id: { type: "string" }, commercial: { type: "boolean", default: true }, engine: { type: "string" } }, |
| 45 | + required: ["id"], |
| 46 | + }, |
| 47 | + }, |
| 48 | + { name: "list_stacks", description: "The starter stacks: one pick per need for a kind of game.", inputSchema: { type: "object", properties: {} } }, |
| 49 | +]; |
| 50 | + |
| 51 | +/** One tool call's result as MCP content. */ |
| 52 | +export function callTool(data, name, args = {}) { |
| 53 | + const text = (v) => ({ content: [{ type: "text", text: JSON.stringify(v, null, 2) }] }); |
| 54 | + if (name === "search_assets") { |
| 55 | + 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 })); |
| 56 | + } |
| 57 | + if (name === "get_asset") { |
| 58 | + const a = getAsset(data, args.id); |
| 59 | + return a ? text(a) : { content: [{ type: "text", text: `No entry "${args.id}".` }], isError: true }; |
| 60 | + } |
| 61 | + if (name === "check_license") return text(checkLicense(data, args.id, { commercial: args.commercial !== false, engine: args.engine })); |
| 62 | + if (name === "list_stacks") return text(listStacks(data)); |
| 63 | + return { content: [{ type: "text", text: `Unknown tool ${name}` }], isError: true }; |
| 64 | +} |
| 65 | + |
| 66 | +/** The answer to one JSON-RPC message, or null for a notification. */ |
| 67 | +export function handle(msg, data) { |
| 68 | + if (msg.id === undefined || msg.id === null) return null; |
| 69 | + const reply = (result) => ({ jsonrpc: "2.0", id: msg.id, result }); |
| 70 | + switch (msg.method) { |
| 71 | + case "initialize": |
| 72 | + return reply({ |
| 73 | + protocolVersion: msg.params?.protocolVersion || "2025-06-18", |
| 74 | + capabilities: { tools: {} }, |
| 75 | + serverInfo: { name: "free-game-dev-assets", version: String(data.version || "0") }, |
| 76 | + }); |
| 77 | + case "ping": |
| 78 | + return reply({}); |
| 79 | + case "tools/list": |
| 80 | + return reply({ tools: TOOLS }); |
| 81 | + case "tools/call": |
| 82 | + return reply(callTool(data, msg.params?.name, msg.params?.arguments || {})); |
| 83 | + default: |
| 84 | + return { jsonrpc: "2.0", id: msg.id, error: { code: -32601, message: `Method not found: ${msg.method}` } }; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +async function main() { |
| 89 | + const i = process.argv.indexOf("--data"); |
| 90 | + const data = await loadData(i === -1 ? undefined : process.argv[i + 1]); |
| 91 | + const rl = readline.createInterface({ input: process.stdin }); |
| 92 | + rl.on("line", (raw) => { |
| 93 | + if (!raw.trim()) return; |
| 94 | + let msg; |
| 95 | + try { |
| 96 | + msg = JSON.parse(raw); |
| 97 | + } catch { |
| 98 | + process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } })}\n`); |
| 99 | + return; |
| 100 | + } |
| 101 | + const answer = handle(msg, data); |
| 102 | + if (answer) process.stdout.write(`${JSON.stringify(answer)}\n`); |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 107 | + main().catch((err) => { |
| 108 | + console.error(err.message); |
| 109 | + process.exit(1); |
| 110 | + }); |
| 111 | +} |
0 commit comments