Textley / app · textley.ai / Scripts / check-mcp-bridge.cjs
| 1 | 'use strict'; |
| 2 | |
| 3 | // Adapted from Jotley’s bridge check. Use a disposable Textley vault for verification. |
| 4 | const { spawn } = require('node:child_process'); |
| 5 | const readline = require('node:readline'); |
| 6 | const assert = require('node:assert/strict'); |
| 7 | |
| 8 | const native = process.env.TEXTLEY_NATIVE_BRIDGE_BINARY; |
| 9 | const script = process.env.TEXTLEY_BRIDGE_SCRIPT; |
| 10 | assert.ok(native || script, 'Choose the native binary or the Claude extension bridge'); |
| 11 | const bridge = spawn(native || process.execPath, native ? ['--mcp-stdio'] : [script], { |
| 12 | env: process.env, |
| 13 | stdio: ['pipe', 'pipe', 'pipe'] |
| 14 | }); |
| 15 | const pending = new Map(); |
| 16 | let nextID = 0; |
| 17 | let hadDiagnostics = false; |
| 18 | bridge.stderr.on('data', () => { hadDiagnostics = true; }); |
| 19 | readline.createInterface({ input: bridge.stdout }).on('line', line => { |
| 20 | const message = JSON.parse(line); |
| 21 | const request = pending.get(message.id); |
| 22 | assert.ok(request, 'Unexpected or duplicate response'); |
| 23 | pending.delete(message.id); |
| 24 | clearTimeout(request.timer); |
| 25 | if (message.error) request.reject(new Error(message.error.message)); |
| 26 | else request.resolve(message.result); |
| 27 | }); |
| 28 | bridge.on('exit', () => { |
| 29 | for (const request of pending.values()) { |
| 30 | clearTimeout(request.timer); |
| 31 | request.reject(new Error('Bridge exited before responding')); |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | function call(method, params) { |
| 36 | return new Promise((resolve, reject) => { |
| 37 | const id = ++nextID; |
| 38 | const timer = setTimeout(() => reject(new Error('Bridge request timed out')), 15000); |
| 39 | pending.set(id, { resolve, reject, timer }); |
| 40 | bridge.stdin.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n'); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | function tool(name, args) { return call('tools/call', { name, arguments: args }); } |
| 45 | |
| 46 | function json(result) { |
| 47 | assert.notEqual(result.isError, true, 'Tool failed'); |
| 48 | return JSON.parse(result.content.filter(item => item.type === 'text').map(item => item.text).join('\n')); |
| 49 | } |
| 50 | |
| 51 | async function main() { |
| 52 | const initialized = await call('initialize', { |
| 53 | protocolVersion: '2025-11-25', |
| 54 | capabilities: { experimental: { 'codex/auth-change': {} } }, |
| 55 | clientInfo: { name: 'Textley packaged bridge test', version: '1.0.0' } |
| 56 | }); |
| 57 | assert.equal(initialized.serverInfo.name, 'Textley'); |
| 58 | assert.match(initialized.instructions, /Codex/); |
| 59 | bridge.stdin.write(JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' }) + '\n'); |
| 60 | const discovery = await call('tools/list', {}); |
| 61 | assert.equal(discovery.tools.length, 27); |
| 62 | const vaults = json(await tool('list_vaults', {})).vaults; |
| 63 | const selected = vaults.find(item => item.name === process.env.TEXTLEY_BRIDGE_VAULT); |
| 64 | assert.ok(selected, 'Disposable vault is missing'); |
| 65 | const sample = json(await tool('read_file', { |
| 66 | vault_id: selected.id, |
| 67 | path: process.env.TEXTLEY_BRIDGE_FILE |
| 68 | })); |
| 69 | assert.ok(sample.text.includes(process.env.TEXTLEY_BRIDGE_TEXT)); |
| 70 | const denied = await tool('new_document', {}); |
| 71 | assert.equal(denied.isError, true); |
| 72 | assert.equal(hadDiagnostics, false, 'Bridge reported diagnostics'); |
| 73 | console.log((native ? 'Native STDIO' : 'Claude chat extension') + ': 27 tools, read and read-only guard verified.'); |
| 74 | } |
| 75 | |
| 76 | main().catch(error => { console.error(error.message); process.exitCode = 1; }).finally(() => { |
| 77 | bridge.stdin.end(); |
| 78 | bridge.kill(); |
| 79 | }); |