/** * 验证 MCP 的 stdio 传输两种模式结果一致: * 真子进程(电脑默认) vs 进程内仿真(安卓默认;电脑上用 DOUBAO_MCP_INPROC_STDIO=1 强制) * * 用法: node scripts/verify-mcp-stdio.mjs [base] [pythonPath] [serverPy] */ const BASE = process.argv[2] || 'http://127.0.0.1:5399'; const PY = process.argv[3] || 'python'; const SERVER = process.argv[4]; const config = { servers: [ { name: 'py-stdio', enabled: true, transport: 'stdio', command: PY, args: [SERVER], env: {}, }, ], }; const put = async (p, body) => { const res = await fetch(BASE + p, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); return res.json(); }; const post = async (p, body) => { const res = await fetch(BASE + p, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body ?? {}), }); return res.json(); }; const get = async (p) => (await fetch(BASE + p)).json(); console.log(`目标 ${BASE}\n`); console.log('写入 stdio 配置…'); const saved = await put('/api/mcp/config', { text: JSON.stringify(config, null, 2) }); console.log(' ok =', saved.ok, '| 结果 =', JSON.stringify(saved.results)); if (!saved.ok) process.exit(1); const state = await get('/api/state'); const tools = (state.toolNames || []).filter((n) => n.startsWith('mcp__py-stdio__')); console.log(' stdio 暴露的工具 =', tools.join(', ') || '(无)'); let pass = 0; let fail = 0; const check = (label, ok, detail) => { console.log(`${ok ? '✅' : '❌'} ${label}${detail ? ` → ${String(detail).slice(0, 200)}` : ''}`); ok ? (pass += 1) : (fail += 1); }; check('连接成功并列出 3 个工具', tools.length === 3, tools.join(', ')); const wc = await post('/api/tools/run', { name: 'mcp__py-stdio__word_count', args: { text: 'hello 世界 hello' } }); check('tools/call: word_count', wc.ok === true && String(wc.result || '').includes('chars'), JSON.stringify(wc.result ?? wc.error)); const host = await post('/api/tools/run', { name: 'mcp__py-stdio__host_info', args: {} }); check('tools/call: host_info', host.ok === true && String(host.result || '').includes('python'), JSON.stringify(host.result ?? host.error).slice(0, 200)); const net = await post('/api/tools/run', { name: 'mcp__py-stdio__http_get', args: { url: 'https://example.com' } }); check('tools/call: http_get(服务内部联网)', net.ok === true && String(net.result || '').includes('Example'), JSON.stringify(net.result ?? net.error).slice(0, 200)); console.log(`\n结果:${pass} 通过 / ${fail} 失败`); process.exit(fail === 0 ? 0 : 1);