main
MartyPM Bridge runtime manifest, source, tests and showcase for 0.1.0 d2ae474
#!/usr/bin/env node
'use strict';
// A stand-in for solidcraft-cli, used only by the tests. It speaks the same newline-delimited
// MCP over stdio for `mcp --headless` / `mcp --connect`, and fakes `run`.
const net = require('net');
const fs = require('fs');
const argv = process.argv.slice(2);

const PNG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
const params = { wall: '2 mm', slots: '7' };
let dirty = false;
const log = [];
// Like SolidCraft 0.49: tools/list gives 8 core tools unless --all-tools; every tool answers by name.
const CORE = ['find_commands', 'call_command', 'batch', 'inspect_design', 'measure', 'body_topology', 'pending', 'skill_reference'];
const EXTRA = ['execute', 'set_parameter', 'undo', 'redo', 'new_design', 'check_parameters', 'screenshot', 'compare_picture',
  'open', 'save', 'export', 'list_commands', 'describe_command', 'running'];
const ALL = [...CORE, ...EXTRA];

if (argv[0] === 'run') {
  const get = (f) => { const i = argv.indexOf(f); return i >= 0 ? argv[i + 1] : null; };
  const ps = argv.flatMap((a, i) => (a === '--param' ? [argv[i + 1]] : []));
  if (ps.some((p) => p.startsWith('missing='))) { process.stderr.write('error: no parameter named "missing"\n'); process.exit(2); }
  if (get('--out')) fs.writeFileSync(get('--out'), `STEP from ${get('--in')} ${ps.join(' ')}`);
  process.stdout.write(`rebuilt ${get('--in')} with ${ps.join(', ')}\n`);
  process.exit(0);
}

if (argv[0] !== 'mcp') { process.stderr.write('usage: mock-cli mcp|run\n'); process.exit(64); }

const mode = argv.includes('--headless') ? 'headless' : 'live';
const allTools = argv.includes('--all-tools');
const structured = argv.includes('--structured');
const TOOLS = (allTools ? ALL : CORE).map((name) => ({ name, description: `mock ${name}`, inputSchema: { type: 'object' } }));
const allow = argv.flatMap((a, i) => (a === '--allow-path' ? [argv[i + 1]] : []));

function serve() {
  let buf = '';
  let pinged = false;
  const send = (o) => process.stdout.write(JSON.stringify(o) + '\n');
  process.stdin.setEncoding('utf8');
  process.stdin.on('data', (d) => {
    buf += d;
    let i;
    while ((i = buf.indexOf('\n')) >= 0) {
      const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
      if (!line) continue;
      const m = JSON.parse(line);
      if (m.id === 'srv-ping') continue; // the client's answer to our ping
      if (m.method === 'initialize') send({ jsonrpc: '2.0', id: m.id, result: { protocolVersion: '2025-06-18', capabilities: { tools: {} }, serverInfo: { name: 'solidcraft-mock', version: '0.49.1', mode, allow } } });
      else if (m.method === 'notifications/initialized') { if (!pinged) { pinged = true; send({ jsonrpc: '2.0', id: 'srv-ping', method: 'ping' }); } }
      else if (m.method === 'tools/list') send({ jsonrpc: '2.0', id: m.id, result: { tools: TOOLS } });
      else if (m.method === 'tools/call') {
        if (!ALL.includes(m.params.name)) send({ jsonrpc: '2.0', id: m.id, error: { code: -32602, message: `unknown tool: ${m.params.name}` } });
        else send({ jsonrpc: '2.0', id: m.id, result: call(m.params.name, m.params.arguments || {}) });
      }
      else if (m.id !== undefined) send({ jsonrpc: '2.0', id: m.id, error: { code: -32601, message: 'no such method' } });
    }
  });
  process.stdin.on('end', () => process.exit(0));
}

function call(name, a) {
  log.push({ name, a });
  const text = (t) => ({ content: [{ type: 'text', text: typeof t === 'string' ? t : JSON.stringify(t) }], ...(structured ? { structuredContent: { result: t, notes: ['mock note'] } } : {}) });
  switch (name) {
    case 'inspect_design': return text({ name: 'MockPart', dirty, bodies: 1, features: ['sketch', 'extrude'], parameters: params, mode });
    case 'running': return text(log);
    case 'screenshot': return { content: [{ type: 'image', data: PNG, mimeType: 'image/png' }] };
    case 'set_parameter':
      if (!(a.name in params)) return { isError: true, content: [{ type: 'text', text: `no parameter named ${a.name}` }] };
      params[a.name] = a.value; dirty = true; return text({ ok: true, parameters: params });
    case 'find_commands': return text([{ id: 'solid.box', label: 'Box', input: '{width, depth, height}' }]);
    case 'call_command': return text({ feature: 'Box1', bodies_created: ['Body1'], id: a.id, params: a.params });
    case 'save': dirty = false; return text({ ok: true, echo: a });
    case 'open': case 'export': return text({ ok: true, echo: a });
    case 'slow': return new Promise(() => {});
    default: return text({ ok: true, tool: name, echo: a });
  }
}

if (mode === 'live') {
  const [host, port] = (argv[argv.indexOf('--connect') + 1] || '').split(':');
  const s = net.connect({ host, port: +port });
  s.once('connect', () => { s.destroy(); serve(); });
  s.once('error', () => { process.stderr.write(`error: could not connect to SolidCraft at ${host}:${port}\n`); process.exit(3); });
} else serve();