Files
homaroma/apps/mcp-server/test_mcp_category_image.ts
T
2026-06-28 20:18:07 -04:00

92 lines
2.3 KiB
TypeScript

import { spawn } from "child_process";
async function test() {
console.log("Starting backend server...");
const backend = spawn("bun", ["src/index.ts"], {
cwd: "../backend",
env: { ...process.env, PORT: "3051" }
});
// Give backend 2 seconds to start
await new Promise(resolve => setTimeout(resolve, 2000));
console.log("Starting MCP server...");
const mcp = spawn("bun", ["index.ts"], {
cwd: ".",
env: { ...process.env, MCP_BACKEND_URL: "http://localhost:3051/api" }
});
mcp.stderr.on("data", (data) => {
console.error(`[MCP STDERR] ${data}`);
});
let responseData = "";
mcp.stdout.on("data", (data) => {
responseData += data.toString();
console.log(`[MCP STDOUT] ${data}`);
});
const send = (msg: any) => {
mcp.stdin.write(JSON.stringify(msg) + "\n");
};
// 1. Send initialize
console.log("Sending initialize request...");
send({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "test-client", version: "1.0.0" }
}
});
await new Promise(resolve => setTimeout(resolve, 1000));
// 2. Send initialized notification
console.log("Sending initialized notification...");
send({
jsonrpc: "2.0",
method: "notifications/initialized"
});
await new Promise(resolve => setTimeout(resolve, 1000));
// 3. Send callTool request to update_category_image
console.log("Sending callTool request for update_category_image...");
responseData = ""; // Clear response data
send({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: "update_category_image",
arguments: {
id: 1,
imageUrl: "https://example.com/mcp-test.png"
}
}
});
await new Promise(resolve => setTimeout(resolve, 3000));
console.log("Cleaning up servers...");
mcp.kill();
backend.kill();
console.log("Verifying response...");
console.log("Full responseData:", responseData);
if (!responseData.includes("https://example.com/mcp-test.png")) {
throw new Error("MCP response did not contain the updated imageUrl!");
}
console.log("MCP tool test passed successfully!");
}
test().catch(err => {
console.error("Test failed:", err);
process.exit(1);
});