|
1 | 1 | import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
2 | 2 | import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
3 | 3 | import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; |
4 | | -import { promisify } from "util"; |
5 | | -import { exec as execCallback } from "child_process"; |
6 | | - |
7 | | -const exec = promisify(execCallback); |
| 4 | +import { spawn } from "node:child_process"; |
8 | 5 |
|
9 | 6 | /** |
10 | 7 | * Strip ANSI escape sequences from a string to make it human-readable. |
@@ -46,6 +43,52 @@ function escapeShellArg(arg: string): string { |
46 | 43 | return `'${arg.replace(/'/g, "'\"'\"'")}'`; |
47 | 44 | } |
48 | 45 |
|
| 46 | +/** |
| 47 | + * Execute a command with isolated streams to prevent external processes |
| 48 | + * from interfering with the output. |
| 49 | + */ |
| 50 | +function exec(command: string): Promise<{ stdout: string; stderr: string }> { |
| 51 | + return new Promise((resolve, reject) => { |
| 52 | + const parts = command.split(" "); |
| 53 | + const program = parts[0]; |
| 54 | + const args = parts.slice(1).filter(arg => arg.length > 0); |
| 55 | + |
| 56 | + // Use spawn with explicit stdio control |
| 57 | + const child = spawn(program, args, { |
| 58 | + shell: true, // Use shell to handle quotes and escaping |
| 59 | + }); |
| 60 | + |
| 61 | + let stdout = ""; |
| 62 | + let stderr = ""; |
| 63 | + |
| 64 | + child.stdout.setEncoding("utf8"); |
| 65 | + child.stderr.setEncoding("utf8"); |
| 66 | + |
| 67 | + child.stdout.on("data", (data) => { |
| 68 | + stdout += data; |
| 69 | + }); |
| 70 | + |
| 71 | + child.stderr.on("data", (data) => { |
| 72 | + stderr += data; |
| 73 | + }); |
| 74 | + |
| 75 | + child.on("close", (code) => { |
| 76 | + if (code === 0 || code === 1) { // Code 1 is "no matches" for ripgrep |
| 77 | + resolve({ stdout, stderr }); |
| 78 | + } else { |
| 79 | + const error = new Error(`Command failed with exit code ${code}`); |
| 80 | + Object.assign(error, { code, stdout, stderr }); |
| 81 | + reject(error); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + // Handle process errors |
| 86 | + child.on("error", (error) => { |
| 87 | + reject(error); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
49 | 92 | // List available tools |
50 | 93 | server.setRequestHandler(ListToolsRequestSchema, async () => { |
51 | 94 | return { |
|
0 commit comments