MCPcopy
hub / github.com/HKUDS/DeepCode / execute_command_batch

Function execute_command_batch

tools/command_executor.py:116–190  ·  view source on GitHub ↗

执行多个shell命令 / Execute multiple shell commands Args: commands: 命令列表,每行一个命令 / Command list, one command per line working_directory: 工作目录 / Working directory Returns: 执行结果 / Execution results

(
    commands: str, working_directory: str
)

Source from the content-addressed store, hash-verified

114
115
116async def execute_command_batch(
117 commands: str, working_directory: str
118) -> list[types.TextContent]:
119 """
120 执行多个shell命令 / Execute multiple shell commands
121
122 Args:
123 commands: 命令列表,每行一个命令 / Command list, one command per line
124 working_directory: 工作目录 / Working directory
125
126 Returns:
127 执行结果 / Execution results
128 """
129 try:
130 # 确保工作目录存在 / Ensure working directory exists
131 Path(working_directory).mkdir(parents=True, exist_ok=True)
132
133 # 分割命令行 / Split command lines
134 command_lines = [
135 cmd.strip() for cmd in commands.strip().split("\n") if cmd.strip()
136 ]
137
138 if not command_lines:
139 return [
140 types.TextContent(
141 type="text", text="没有提供有效命令 / No valid commands provided"
142 )
143 ]
144
145 results = []
146 stats = {"successful": 0, "failed": 0, "timeout": 0}
147
148 for i, command in enumerate(command_lines, 1):
149 try:
150 # 执行命令 / Execute command
151 result = subprocess.run(
152 command,
153 shell=True,
154 cwd=working_directory,
155 capture_output=True,
156 text=True,
157 timeout=30, # 30秒超时
158 )
159
160 if result.returncode == 0:
161 results.append(f"✅ Command {i}: {command}")
162 if result.stdout.strip():
163 results.append(f" 输出 / Output: {result.stdout.strip()}")
164 stats["successful"] += 1
165 else:
166 results.append(f"❌ Command {i}: {command}")
167 if result.stderr.strip():
168 results.append(f" 错误 / Error: {result.stderr.strip()}")
169 stats["failed"] += 1
170
171 except subprocess.TimeoutExpired:
172 results.append(f"⏱️ Command {i} 超时 / timeout: {command}")
173 stats["timeout"] += 1

Callers 1

handle_call_toolFunction · 0.85

Calls 2

runMethod · 0.80

Tested by

no test coverage detected