#!/usr/bin/env node
// 简单的测试脚本,用于验证MCP服务器功能
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log('🧪 Word Document Reader MCP 测试工具');
console.log('=====================================\n');
// 测试配置
const TEST_CONFIG = {
serverPath: path.join(__dirname, 'server-enhanced.js'),
testFilePath: './test-document.docx', // 需要用户提供测试文档
};
async function runTest(testName, request) {
return new Promise((resolve, reject) => {
console.log(`📋 测试: ${testName}`);
console.log(`📤 请求: ${JSON.stringify(request, null, 2)}`);
const child = spawn('node', [TEST_CONFIG.serverPath], {
stdio: ['pipe', 'pipe', 'pipe']
});
let response = '';
let error = '';
child.stdout.on('data', (data) => {
response += data.toString();
});
child.stderr.on('data', (data) => {
error += data.toString();
});
// 发送MCP请求
const mcpRequest = {
jsonrpc: '2.0',
id: Date.now(),
method: 'tools/call',
params: request
};
child.stdin.write(JSON.stringify(mcpRequest) + '\n');
child.on('close', (code) => {
if (code === 0) {
try {
const result = JSON.parse(response);
console.log(`✅ 成功: ${JSON.stringify(result, null, 2)}`);
resolve(result);
} catch (e) {
console.log(`⚠️ 响应解析错误: ${e.message}`);
console.log(`原始响应: ${response}`);
resolve(null);
}
} else {
console.log(`❌ 错误: ${error}`);
reject(new Error(error));
}
});
// 设置超时
setTimeout(() => {
child.kill();
reject(new Error('测试超时'));
}, 30000);
});
}
async function runAllTests() {
const tests = [
{
name: '列出可用工具',
request: {
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
}
},
{
name: '获取缓存统计',
request: {
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: {
name: 'get_cache_stats',
arguments: {}
}
}
},
{
name: '列出存储的文档',
request: {
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: {
name: 'list_stored_documents',
arguments: {}
}
}
},
{
name: '搜索文档',
request: {
jsonrpc: '2.0',
id: 4,
method: 'tools/call',
params: {
name: 'search_documents',
arguments: {
query: '测试',
limit: 5
}
}
}
}
];
console.log('开始运行测试...\n');
for (const test of tests) {
try {
await runTest(test.name, test.request);
} catch (error) {
console.log(`❌ 测试失败: ${error.message}`);
}
console.log('\n' + '='.repeat(50) + '\n');
}
// 如果有测试文档,运行文档读取测试
if (TEST_CONFIG.testFilePath && await fs.pathExists(TEST_CONFIG.testFilePath)) {
console.log('发现测试文档,运行文档读取测试...\n');
try {
await runTest('读取Word文档', {
jsonrpc: '2.0',
id: 5,
method: 'tools/call',
params: {
name: 'read_word_document',
arguments: {
filePath: TEST_CONFIG.testFilePath,
memoryKey: 'test-doc',
documentType: 'other',
extractTables: true,
extractImages: true,
useCache: true
}
}
});
} catch (error) {
console.log(`❌ 文档读取测试失败: ${error.message}`);
}
} else {
console.log('📝 提示: 将测试Word文档放在 ./test-document.docx 来运行完整测试');
}
console.log('\n🎉 测试完成!');
}
// 检查必要的模块
async function checkDependencies() {
try {
await import('@modelcontextprotocol/sdk');
console.log('✅ MCP SDK 已安装');
} catch (e) {
console.log('❌ 请先安装依赖: npm install');
process.exit(1);
}
try {
await import('mammoth');
console.log('✅ Mammoth 已安装');
} catch (e) {
console.log('❌ 请先安装依赖: npm install');
process.exit(1);
}
try {
await import('tesseract.js');
console.log('✅ Tesseract.js 已安装');
} catch (e) {
console.log('⚠️ Tesseract.js 未安装,OCR功能将不可用');
console.log(' 运行: npm install tesseract.js');
}
try {
await import('sharp');
console.log('✅ Sharp 已安装');
} catch (e) {
console.log('⚠️ Sharp 未安装,图片处理功能将不可用');
console.log(' 运行: npm install sharp');
}
}
// 主函数
async function main() {
console.log('检查依赖...\n');
await checkDependencies();
console.log('\n');
await runAllTests();
}
// 运行测试
main().catch(console.error);