Files
bigwo/dev-assistant-mcp/dist/tools/execCommand.js
2026-03-12 12:47:56 +08:00

62 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { exec } from "child_process";
import { promisify } from "util";
const execAsync = promisify(exec);
export const execCommandTool = {
name: "exec_command",
description: "在本地电脑上执行 Shell 命令。用于运行编译、测试、构建、安装依赖等任何命令。返回 stdout、stderr 和退出码。",
inputSchema: {
type: "object",
properties: {
command: {
type: "string",
description: "要执行的命令(如 npm install, tsc --noEmit, python script.py",
},
cwd: {
type: "string",
description: "工作目录(绝对路径)",
},
timeout: {
type: "number",
description: "超时时间(毫秒),默认 60000",
},
},
required: ["command"],
},
};
export async function executeExecCommand(args) {
const { command, cwd, timeout = 60000 } = args;
const workDir = cwd || process.cwd();
try {
const { stdout, stderr } = await execAsync(command, {
cwd: workDir,
timeout,
maxBuffer: 1024 * 1024 * 10, // 10MB
shell: process.platform === "win32" ? "powershell.exe" : "/bin/bash",
});
const output = [
`$ ${command}`,
`📂 ${workDir}`,
`✅ 退出码: 0`,
];
if (stdout.trim())
output.push(`\n--- stdout ---\n${stdout.trim()}`);
if (stderr.trim())
output.push(`\n--- stderr ---\n${stderr.trim()}`);
return output.join("\n");
}
catch (error) {
const output = [
`$ ${command}`,
`📂 ${workDir}`,
`❌ 退出码: ${error.code ?? "unknown"}`,
];
if (error.stdout?.trim())
output.push(`\n--- stdout ---\n${error.stdout.trim()}`);
if (error.stderr?.trim())
output.push(`\n--- stderr ---\n${error.stderr.trim()}`);
if (error.killed)
output.push(`\n⏰ 命令超时(${timeout}ms被终止`);
return output.join("\n");
}
}
//# sourceMappingURL=execCommand.js.map