/** * 验证:把 Python 写的 MCP 服务接进内核(stdio 模式)。 * 用法: node scripts/mcp-python-test.mjs [base] [pythonPath] [serverPy] */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const repo = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const BASE = process.argv[2] || 'http://127.0.0.1:5199'; const PYTHON = process.argv[3] || 'python'; const SERVER_PY = process.argv[4] || path.join(repo, 'tools', '_mcp_python_example.py'); const config = { servers: [ { name: 'py-demo', enabled: true, transport: 'stdio', command: PYTHON, args: [SERVER_PY], env: {}, }, { name: 'filesystem', enabled: false, transport: 'stdio', command: 'npx', args: ['-y', '@modelcontextprotocol/server-filesystem', '${DOUBAO_DATA}'], }, { name: 'my-python-mcp-inproc', enabled: false, transport: 'inproc', path: '/sdcard/Android/data/com.doubao.lynxclone/files/tools/my_mcp_server.py', note: '安卓端用:内核直接 import 这个 py,不依赖独立 python 解释器', }, ], }; console.log('写入配置…'); const save = await fetch(`${BASE}/api/mcp/config`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ text: JSON.stringify(config, null, 2) }), }); const saved = await save.json(); console.log('ok=', saved.ok, 'count=', saved.count, 'error=', saved.error || '无'); for (const r of saved.results || []) console.log(' ', r.ok ? '✓' : '✗', r.name, r.error || ''); console.log('\n工具列表:'); const state = await (await fetch(`${BASE}/api/state`)).json(); const mcpTools = state.toolNames.filter((n) => n.startsWith('mcp__py-demo__')); console.log(' py-demo 暴露:', mcpTools.join(', ') || '(无)'); console.log('\n跑一下 word_count:'); const run = await fetch(`${BASE}/api/tools/run`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'mcp__py-demo__word_count', args: { text: 'hello 世界 hello' } }), }); console.log(' ', JSON.stringify(await run.json())); console.log('\n跑一下 host_info:'); const info = await fetch(`${BASE}/api/tools/run`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'mcp__py-demo__host_info', args: {} }), }); console.log(' ', JSON.stringify(await info.json()).slice(0, 260)); console.log('\n跑一下 http_get(证明 MCP 里的 py 也能联网):'); const net = await fetch(`${BASE}/api/tools/run`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'mcp__py-demo__http_get', args: { url: 'https://example.com' } }), }); const netJson = await net.json(); console.log(' ', JSON.stringify(netJson).slice(0, 200));