UNPKG

qiniu-mcp

Version:

A Model Context Protocol server for Qiniu Cloud Storage services with optimized local file upload support

182 lines (147 loc) 4.79 kB
#!/usr/bin/env node import { spawn } from 'child_process'; import fs from 'fs'; import path from 'path'; /** * 测试本地文件上传功能 */ console.log('🚀 测试本地文件上传功能...\n'); // 创建一个测试文件 const testFilePath = path.join(process.cwd(), 'test-image.txt'); const testContent = `这是一个测试文件 创建时间: ${new Date().toISOString()} 文件大小: 约100字节 用于测试七牛云MCP本地文件上传功能`; fs.writeFileSync(testFilePath, testContent, 'utf8'); console.log(`📝 创建测试文件: ${testFilePath}`); /** * 调用MCP工具 */ function callMCPTool(toolName, args) { return new Promise((resolve, reject) => { const child = spawn('node', ['dist/index.js'], { stdio: ['pipe', 'pipe', 'pipe'] }); let stdout = ''; let stderr = ''; child.stdout.on('data', (data) => { stdout += data.toString(); }); child.stderr.on('data', (data) => { stderr += data.toString(); }); child.on('close', (code) => { if (code === 0) { try { // 解析MCP响应 const lines = stdout.trim().split('\n'); const jsonLine = lines.find(line => { try { JSON.parse(line); return true; } catch { return false; } }); if (jsonLine) { resolve(JSON.parse(jsonLine)); } else { resolve({ stdout, stderr }); } } catch (error) { resolve({ stdout, stderr, error: error.message }); } } else { reject(new Error(`Process exited with code ${code}\nStderr: ${stderr}`)); } }); // 发送MCP请求 const request = { jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: toolName, arguments: args } }; child.stdin.write(JSON.stringify(request) + '\n'); child.stdin.end(); }); } /** * 测试本地文件上传 */ async function testLocalFileUpload() { console.log('📤 测试本地文件上传...'); const startTime = Date.now(); try { const result = await callMCPTool('qiniu_upload_local_file', { filePath: testFilePath, key: `test-local-${Date.now()}.txt` }); const endTime = Date.now(); const duration = endTime - startTime; console.log(`✅ 本地文件上传成功!耗时: ${duration}ms`); console.log(`📁 结果: ${result.content?.[0]?.text || JSON.stringify(result)}`); return { success: true, duration, result }; } catch (error) { const endTime = Date.now(); const duration = endTime - startTime; console.log(`❌ 本地文件上传失败!耗时: ${duration}ms`); console.log(`错误: ${error.message}`); return { success: false, duration, error: error.message }; } } /** * 测试file://协议 */ async function testFileProtocol() { console.log('📤 测试file://协议上传...'); const startTime = Date.now(); const fileUrl = `file://${testFilePath}`; try { const result = await callMCPTool('qiniu_upload_local_file', { filePath: fileUrl, key: `test-file-protocol-${Date.now()}.txt` }); const endTime = Date.now(); const duration = endTime - startTime; console.log(`✅ file://协议上传成功!耗时: ${duration}ms`); console.log(`📁 结果: ${result.content?.[0]?.text || JSON.stringify(result)}`); return { success: true, duration, result }; } catch (error) { const endTime = Date.now(); const duration = endTime - startTime; console.log(`❌ file://协议上传失败!耗时: ${duration}ms`); console.log(`错误: ${error.message}`); return { success: false, duration, error: error.message }; } } /** * 运行测试 */ async function runTests() { console.log('🏁 开始本地文件上传测试...\n'); // 测试普通路径 const localResult = await testLocalFileUpload(); console.log(''); // 测试file://协议 const fileProtocolResult = await testFileProtocol(); console.log(''); // 输出测试结果 console.log('📊 测试结果:'); console.log('─'.repeat(50)); console.log(`本地路径上传: ${localResult.success ? '✅' : '❌'} ${localResult.duration}ms`); console.log(`file://协议上传: ${fileProtocolResult.success ? '✅' : '❌'} ${fileProtocolResult.duration}ms`); console.log('─'.repeat(50)); // 清理测试文件 try { fs.unlinkSync(testFilePath); console.log(`🗑️ 清理测试文件: ${testFilePath}`); } catch (error) { console.log(`⚠️ 清理测试文件失败: ${error.message}`); } } // 运行测试 runTests().catch(console.error);