|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { promises as fs } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 7 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 8 | +import { z } from "zod"; |
| 9 | + |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | +const DESKTOP_ROOT = __dirname; |
| 12 | +const PROJECT_ROOT = path.resolve(DESKTOP_ROOT, ".."); |
| 13 | +const PYTHON_EXECUTABLE = process.env.QUANTLAB_PYTHON || "python"; |
| 14 | +const MAX_OUTPUT_CHARS = 12000; |
| 15 | + |
| 16 | +function truncateText(text) { |
| 17 | + if (text.length <= MAX_OUTPUT_CHARS) return text; |
| 18 | + return `${text.slice(0, MAX_OUTPUT_CHARS)}\n...[truncated]`; |
| 19 | +} |
| 20 | + |
| 21 | +function runProcess(command, args, options = {}) { |
| 22 | + return new Promise((resolve) => { |
| 23 | + const child = spawn(command, args, { |
| 24 | + cwd: options.cwd || PROJECT_ROOT, |
| 25 | + env: { ...process.env, ...(options.env || {}) }, |
| 26 | + windowsHide: true, |
| 27 | + shell: false, |
| 28 | + }); |
| 29 | + |
| 30 | + let stdout = ""; |
| 31 | + let stderr = ""; |
| 32 | + |
| 33 | + child.stdout.on("data", (chunk) => { |
| 34 | + stdout += String(chunk || ""); |
| 35 | + }); |
| 36 | + child.stderr.on("data", (chunk) => { |
| 37 | + stderr += String(chunk || ""); |
| 38 | + }); |
| 39 | + |
| 40 | + const timeoutMs = options.timeoutMs || 120000; |
| 41 | + const timer = setTimeout(() => { |
| 42 | + child.kill(); |
| 43 | + resolve({ |
| 44 | + exitCode: 124, |
| 45 | + stdout, |
| 46 | + stderr: `${stderr}\nCommand timed out after ${timeoutMs}ms`.trim(), |
| 47 | + }); |
| 48 | + }, timeoutMs); |
| 49 | + |
| 50 | + child.on("error", (error) => { |
| 51 | + clearTimeout(timer); |
| 52 | + resolve({ |
| 53 | + exitCode: 1, |
| 54 | + stdout, |
| 55 | + stderr: error.message || String(error), |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + child.on("exit", (code) => { |
| 60 | + clearTimeout(timer); |
| 61 | + resolve({ |
| 62 | + exitCode: code ?? 1, |
| 63 | + stdout, |
| 64 | + stderr, |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +async function runPythonCli(args, timeoutMs = 120000) { |
| 71 | + return runProcess(PYTHON_EXECUTABLE, ["main.py", ...args], { |
| 72 | + cwd: PROJECT_ROOT, |
| 73 | + timeoutMs, |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +async function formatProcessResult(label, result, commandLine) { |
| 78 | + const sections = [ |
| 79 | + `Command: ${commandLine}`, |
| 80 | + `Exit code: ${result.exitCode}`, |
| 81 | + ]; |
| 82 | + if (result.stdout.trim()) { |
| 83 | + sections.push(`stdout:\n${truncateText(result.stdout.trim())}`); |
| 84 | + } |
| 85 | + if (result.stderr.trim()) { |
| 86 | + sections.push(`stderr:\n${truncateText(result.stderr.trim())}`); |
| 87 | + } |
| 88 | + return { |
| 89 | + content: [ |
| 90 | + { |
| 91 | + type: "text", |
| 92 | + text: `[${label}]\n${sections.join("\n\n")}\n`, |
| 93 | + }, |
| 94 | + ], |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +async function main() { |
| 99 | + const server = new McpServer({ |
| 100 | + name: "quantlab-local", |
| 101 | + version: "0.1.0", |
| 102 | + }); |
| 103 | + |
| 104 | + server.registerTool("quantlab_check", { |
| 105 | + description: "Run the standard QuantLab health check.", |
| 106 | + }, async () => { |
| 107 | + const result = await runPythonCli(["--check"], 120000); |
| 108 | + return formatProcessResult("quantlab_check", result, "python main.py --check"); |
| 109 | + }); |
| 110 | + |
| 111 | + server.registerTool("quantlab_version", { |
| 112 | + description: "Return the QuantLab CLI version.", |
| 113 | + }, async () => { |
| 114 | + const result = await runPythonCli(["--version"], 30000); |
| 115 | + return formatProcessResult("quantlab_version", result, "python main.py --version"); |
| 116 | + }); |
| 117 | + |
| 118 | + server.registerTool("quantlab_runs_list", { |
| 119 | + description: "List indexed QuantLab runs.", |
| 120 | + }, async () => { |
| 121 | + const result = await runPythonCli(["--runs-list"], 120000); |
| 122 | + return formatProcessResult("quantlab_runs_list", result, "python main.py --runs-list"); |
| 123 | + }); |
| 124 | + |
| 125 | + server.registerTool("quantlab_paper_sessions_health", { |
| 126 | + description: "Summarize the health of QuantLab paper sessions.", |
| 127 | + }, async () => { |
| 128 | + const result = await runPythonCli(["--paper-sessions-health"], 120000); |
| 129 | + return formatProcessResult( |
| 130 | + "quantlab_paper_sessions_health", |
| 131 | + result, |
| 132 | + "python main.py --paper-sessions-health", |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + server.registerTool("quantlab_desktop_smoke", { |
| 137 | + description: "Run the QuantLab desktop smoke test.", |
| 138 | + }, async () => { |
| 139 | + const result = await runProcess("node", ["scripts/smoke.js"], { |
| 140 | + cwd: DESKTOP_ROOT, |
| 141 | + timeoutMs: 180000, |
| 142 | + }); |
| 143 | + return formatProcessResult("quantlab_desktop_smoke", result, "node scripts/smoke.js"); |
| 144 | + }); |
| 145 | + |
| 146 | + server.registerTool("quantlab_read_file", { |
| 147 | + description: "Read a text file within the QuantLab repository.", |
| 148 | + inputSchema: { |
| 149 | + relative_path: z.string().describe("Path relative to the QuantLab repository root"), |
| 150 | + }, |
| 151 | + }, async ({ relative_path }) => { |
| 152 | + const resolvedPath = path.resolve(PROJECT_ROOT, relative_path); |
| 153 | + const relative = path.relative(PROJECT_ROOT, resolvedPath); |
| 154 | + if (relative.startsWith("..") || path.isAbsolute(relative)) { |
| 155 | + return { |
| 156 | + content: [ |
| 157 | + { |
| 158 | + type: "text", |
| 159 | + text: `Refusing to read outside the QuantLab repository: ${relative_path}`, |
| 160 | + }, |
| 161 | + ], |
| 162 | + isError: true, |
| 163 | + }; |
| 164 | + } |
| 165 | + |
| 166 | + try { |
| 167 | + const data = await fs.readFile(resolvedPath, "utf8"); |
| 168 | + return { |
| 169 | + content: [ |
| 170 | + { |
| 171 | + type: "text", |
| 172 | + text: truncateText(data), |
| 173 | + }, |
| 174 | + ], |
| 175 | + }; |
| 176 | + } catch (error) { |
| 177 | + return { |
| 178 | + content: [ |
| 179 | + { |
| 180 | + type: "text", |
| 181 | + text: `Failed to read ${relative_path}: ${error.message || String(error)}`, |
| 182 | + }, |
| 183 | + ], |
| 184 | + isError: true, |
| 185 | + }; |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + const transport = new StdioServerTransport(); |
| 190 | + await server.connect(transport); |
| 191 | +} |
| 192 | + |
| 193 | +main().catch((error) => { |
| 194 | + console.error(error); |
| 195 | + process.exit(1); |
| 196 | +}); |
0 commit comments