#!/usr/bin/env node
/**
* Simple test for the SiteScore MCP server
* Run with: npm run test:dev
*/
import { spawn } from "child_process";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
const __dirname = dirname(fileURLToPath(import.meta.url));
interface MCPResponse {
jsonrpc: string;
id: number;
result?: unknown;
error?: { code: number; message: string };
}
async function test() {
console.log("🧪 Testing SiteScore MCP Server\n");
const serverPath = join(__dirname, "index.ts");
const server = spawn("npx", ["tsx", serverPath], {
stdio: ["pipe", "pipe", "pipe"],
});
let buffer = "";
const sendRequest = (request: object): Promise<MCPResponse> => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error("Timeout")), 60000);
const handler = (data: Buffer) => {
buffer += data.toString();
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (line.trim()) {
try {
const response = JSON.parse(line);
clearTimeout(timeout);
server.stdout.off("data", handler);
resolve(response);
} catch {
// Not JSON, continue
}
}
}
};
server.stdout.on("data", handler);
server.stdin.write(JSON.stringify(request) + "\n");
});
};
try {
// Initialize
console.log("1️⃣ Initializing server...");
const initResponse = await sendRequest({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "test-client", version: "1.0.0" },
},
});
console.log(" ✅ Server initialized:", initResponse.result ? "OK" : "FAIL");
// List tools
console.log("\n2️⃣ Listing tools...");
const toolsResponse = await sendRequest({
jsonrpc: "2.0",
id: 2,
method: "tools/list",
params: {},
});
const tools = (toolsResponse.result as { tools: Array<{ name: string }> })?.tools || [];
console.log(` ✅ Found ${tools.length} tool(s):`, tools.map((t) => t.name).join(", "));
// Call analyze_website
console.log("\n3️⃣ Testing analyze_website with https://example.com...");
const analyzeResponse = await sendRequest({
jsonrpc: "2.0",
id: 3,
method: "tools/call",
params: {
name: "analyze_website",
arguments: { url: "https://example.com" },
},
});
if (analyzeResponse.error) {
console.log(" ❌ Error:", analyzeResponse.error.message);
} else {
const result = analyzeResponse.result as { content: Array<{ text: string }> };
const content = result?.content?.[0]?.text;
if (content) {
try {
const parsed = JSON.parse(content);
if (parsed.error) {
console.log(" ❌ Analysis error:", parsed.error);
} else {
console.log(" ✅ Analysis complete!");
console.log(` 📊 Overall Score: ${parsed.overallScore}/100`);
if (parsed.categories) {
console.log(` 📋 Categories:`);
console.log(` - SEO: ${parsed.categories.seo}/100`);
console.log(` - Performance: ${parsed.categories.performance}/100`);
console.log(` - Mobile: ${parsed.categories.mobile}/100`);
console.log(` - Accessibility: ${parsed.categories.accessibility}/100`);
console.log(` - Security: ${parsed.categories.security}/100`);
}
console.log(` 📝 Summary: ${parsed.summary}`);
console.log(` ⚠️ Issues found: ${parsed.issues?.length || 0}`);
}
} catch (e) {
console.log(" ⚠️ Raw response:", content.slice(0, 500));
}
}
}
console.log("\n✨ All tests passed!");
} catch (err) {
console.error("❌ Test failed:", err);
process.exit(1);
} finally {
server.kill();
}
}
test();