Files

215 lines
6.9 KiB
JavaScript

// Real MCP SDK end-to-end smoke. Requires a running disposable Shacraft server.
// node sdk-smoke.mjs --binary /abs/shacraft-mcp --data /abs/test-data [--server URL]
// SHACRAFT_MCP_SDK may point at an installed @modelcontextprotocol/sdk directory.
import assert from "node:assert/strict";
import path from "node:path";
import { pathToFileURL, fileURLToPath } from "node:url";
import { existsSync } from "node:fs";
const args = {};
for (let i = 2; i < process.argv.length; i += 2)
args[process.argv[i].replace(/^--/, "")] = process.argv[i + 1];
const root = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../..",
);
const sdk =
process.env.SHACRAFT_MCP_SDK ||
args.sdk ||
path.resolve(
root,
"../minecraft-builder-mcp/bridge/node_modules/@modelcontextprotocol/sdk",
);
if (!existsSync(sdk))
throw Error(
"Set SHACRAFT_MCP_SDK to an installed @modelcontextprotocol/sdk directory.",
);
const { Client } = await import(
pathToFileURL(path.join(sdk, "dist/esm/client/index.js"))
);
const { StdioClientTransport } = await import(
pathToFileURL(path.join(sdk, "dist/esm/client/stdio.js"))
);
const binary = args.binary || path.join(root, "target/debug/shacraft-mcp");
assert.ok(args.data, "--data is required: use disposable server data");
const client = new Client(
{ name: "shacraft-sdk-smoke", version: "1.0.0" },
{ capabilities: {} },
);
const transport = new StdioClientTransport({
command: binary,
args: [
"--server",
args.server || "http://127.0.0.1:4000",
"--data",
path.resolve(args.data),
],
stderr: "pipe",
});
let stderr = "";
transport.stderr?.on("data", (chunk) => (stderr += chunk));
async function call(name, arguments_ = {}) {
const r = await client.callTool({ name, arguments: arguments_ });
assert.notEqual(r.isError, true, `${name}: ${JSON.stringify(r.content)}`);
return (
r.structuredContent ||
JSON.parse(r.content.find((c) => c.type === "text").text)
);
}
const report = { sdk: true, checks: [], world: `mcp_sdk_${Date.now()}` };
try {
await client.connect(transport);
assert.equal(client.getServerVersion()?.name, "shacraft-mcp");
await client.ping();
report.checks.push(
"SDK initialize, version negotiation, notification and ping",
);
const listed = await client.listTools();
assert.equal(listed.tools.length, 17);
report.checks.push("17 discoverable tool schemas accepted by SDK");
const resources = await client.listResources();
assert.equal(resources.resources.length, 4);
for (const resource of resources.resources) {
const r = await client.readResource({ uri: resource.uri });
assert.equal(r.contents[0].mimeType, "application/json");
JSON.parse(r.contents[0].text);
}
report.checks.push("4 resources read through SDK");
const prompts = await client.listPrompts();
assert.equal(prompts.prompts[0].name, "construct");
const prompt = await client.getPrompt({
name: "construct",
arguments: {
world: report.world,
request: "Create a small test structure",
},
});
assert.ok(prompt.messages.length);
report.checks.push("Construction prompt and arguments");
const found = await call("catalog_search", {
query: "minecraft:stone",
limit: 32,
});
const stone = found.items.find((x) => x.state === "minecraft:stone");
assert.ok(stone);
await call("world_create", { world: report.world });
const edit = {
world: report.world,
expected_revision: 0,
operation_id: "sdk-edit",
changes: [{ pos: [0, 0, 0], block: stone.id }],
};
const first = await call("world_edit", edit);
assert.equal(first.revision, 1);
assert.equal((await call("world_edit", edit)).replayed, true);
const read = await call("world_read", {
world: report.world,
min: [0, 0, 0],
max: [1, 1, 1],
});
assert.equal(read.blocks.length, 1);
const conflict = await client.callTool({
name: "world_edit",
arguments: { ...edit, operation_id: "sdk-conflict" },
});
assert.equal(conflict.isError, true);
await call("world_undo", {
world: report.world,
expected_revision: 1,
operation_id: "sdk-undo",
target_operation: "sdk-edit",
});
report.checks.push(
"Durable write, read, idempotent retry, conflict content and undo",
);
const plan = await call("build_plan", {
world: report.world,
expected_revision: 2,
operations: [
{ type: "box", min: [0, 0, 0], max: [2, 1, 2], block: stone.id },
],
});
assert.equal(plan.cells, 18);
const committed = await call("build_commit", {
plan_id: plan.plan_id,
operation_id: "sdk-build",
});
assert.equal(committed.changed, 18);
assert.equal(
(
await call("build_commit", {
plan_id: plan.plan_id,
operation_id: "sdk-build",
})
).replayed,
true,
);
report.checks.push("Plan and atomic idempotent commit");
const image = await client.callTool({
name: "camera_capture",
arguments: { world: report.world, min: [-2, -2, -2], max: [4, 4, 4] },
});
assert.notEqual(image.isError, true);
const png = image.content.find((c) => c.type === "image");
assert.ok(png);
assert.equal(png.mimeType, "image/png");
assert.deepEqual(
[...Buffer.from(png.data, "base64").subarray(0, 8)],
[137, 80, 78, 71, 13, 10, 26, 10],
);
report.checks.push("MCP PNG image content");
const entity = await call("entity_spawn", {
world: report.world,
kind: "minecraft:pig",
position: [1, 2, 1],
properties: { name: "SDK test" },
});
const id = entity.entity.id;
assert.ok(id);
const updated = await call("entity_update", {
id,
position: [2, 2, 1],
properties: { name: "SDK updated" },
});
assert.deepEqual(updated.entity.position, [2, 2, 1]);
const entities = await call("entity_list", { world: report.world });
assert.ok(entities.items.some((e) => e.id === id));
await call("entity_delete", { id });
assert.equal(
(await call("entity_list", { world: report.world })).items.length,
0,
);
report.checks.push("Entity spawn/update/list/delete");
const rules = await call("arena_configure", {
world: report.world,
mode: "creative",
spawn: [0, 3, 0],
countdown_seconds: 2,
round_seconds: 120,
min_players: 2,
elimination_y: -5,
spawn_points: [],
spectator_spawn: [13, 3, 0],
floor_block: "minecraft:snow_block",
});
assert.equal(rules.countdown_seconds ?? rules.rules?.countdown_seconds, 2);
const metrics = await call("metrics");
assert.ok(metrics.storage);
report.checks.push("Persisted world configuration and measured metrics");
await call("world_reset", {
world: report.world,
expected_revision: committed.revision,
operation_id: "sdk-reset",
});
const empty = await call("world_read", {
world: report.world,
min: [0, 0, 0],
max: [2, 1, 2],
});
assert.equal(empty.blocks.length, 0);
report.checks.push("World reset");
assert.equal(stderr, "", "MCP server should not emit routine diagnostics");
console.log(JSON.stringify(report, null, 2));
} finally {
await client.close();
}