#!/usr/bin/env node
/**
* Test script for ProposalPilot MCP server
*
* This script spawns the MCP server and sends test requests via JSON-RPC over stdio.
*/
const { spawn } = require('child_process');
const path = require('path');
const serverPath = path.join(__dirname, '..', 'dist', 'index.js');
let requestId = 1;
function sendRequest(proc, method, params = {}) {
const request = {
jsonrpc: "2.0",
id: requestId++,
method,
params,
};
proc.stdin.write(JSON.stringify(request) + '\n');
return request.id;
}
async function runTests() {
console.log("🚀 Starting ProposalPilot MCP server tests...\n");
// Check if ANTHROPIC_API_KEY is set
if (!process.env.ANTHROPIC_API_KEY) {
console.log("⚠️ ANTHROPIC_API_KEY not set - will test server startup and tool listing only\n");
}
const proc = spawn('node', [serverPath], {
stdio: ['pipe', 'pipe', 'pipe'],
env: process.env,
});
let buffer = '';
const responses = [];
proc.stdout.on('data', (data) => {
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);
responses.push(response);
console.log("📥 Response:", JSON.stringify(response, null, 2).substring(0, 500));
if (JSON.stringify(response).length > 500) {
console.log(" ... (truncated)");
}
console.log();
} catch (e) {
// Not JSON, might be debug output
}
}
}
});
proc.stderr.on('data', (data) => {
console.log("ℹ️ Server:", data.toString().trim());
});
// Wait for server to start
await new Promise(r => setTimeout(r, 1000));
// Test 1: Initialize
console.log("📤 Test 1: Initialize connection");
sendRequest(proc, 'initialize', {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "test-client", version: "1.0.0" }
});
await new Promise(r => setTimeout(r, 500));
// Test 2: List tools
console.log("📤 Test 2: List available tools");
sendRequest(proc, 'tools/list', {});
await new Promise(r => setTimeout(r, 500));
// Test 3: Call generate_proposal (only if API key is set)
if (process.env.ANTHROPIC_API_KEY) {
console.log("📤 Test 3: Generate a test proposal");
sendRequest(proc, 'tools/call', {
name: 'generate_proposal',
arguments: {
project_description: "Build a modern e-commerce website with React frontend and Node.js backend. Need product catalog, shopping cart, checkout with Stripe, and admin dashboard.",
client_name: "TechStartup Inc",
budget: "$8,000 - $12,000",
timeline: "6 weeks",
tone: "professional",
services: "Full-stack web development, React, Node.js, PostgreSQL, AWS deployment"
}
});
console.log("⏳ Waiting for proposal generation (this may take 15-30 seconds)...\n");
await new Promise(r => setTimeout(r, 30000));
} else {
console.log("⏭️ Skipping proposal generation test (no API key)\n");
}
// Clean up
proc.kill();
console.log("\n✅ Tests completed!");
console.log(` Received ${responses.length} response(s)`);
// Validate responses
const initResponse = responses.find(r => r.result?.serverInfo);
const toolsResponse = responses.find(r => r.result?.tools);
if (initResponse) {
console.log(" ✓ Server initialized successfully");
console.log(` Server: ${initResponse.result.serverInfo.name} v${initResponse.result.serverInfo.version}`);
}
if (toolsResponse) {
console.log(" ✓ Tools listed successfully");
for (const tool of toolsResponse.result.tools) {
console.log(` - ${tool.name}: ${tool.description.substring(0, 60)}...`);
}
}
const proposalResponse = responses.find(r => r.result?.content?.[0]?.text?.includes('EXECUTIVE SUMMARY'));
if (proposalResponse) {
console.log(" ✓ Proposal generated successfully");
}
process.exit(0);
}
runTests().catch((err) => {
console.error("❌ Test failed:", err);
process.exit(1);
});