MCPcopy Create free account
hub / github.com/modelscope/ms-agent / LocalCodeExecutionTool

Class LocalCodeExecutionTool

ms_agent/tools/code/local_code_executor.py:228–809  ·  view source on GitHub ↗

Code execution tool that runs entirely on the local machine.

Source from the content-addressed store, hash-verified

226
227
228class LocalCodeExecutionTool(ToolBase):
229 """Code execution tool that runs entirely on the local machine."""
230
231 def __init__(self, config):
232 super().__init__(config)
233 self.output_dir = Path(
234 getattr(config, 'output_dir', DEFAULT_OUTPUT_DIR)).expanduser()
235 self.output_dir.mkdir(parents=True, exist_ok=True)
236
237 self.tool_config = getattr(
238 getattr(config, 'tools', None), 'code_executor', None)
239 self._notebook_timeout = getattr(self.tool_config, 'notebook_timeout',
240 60) if self.tool_config else 60
241 self._python_timeout = getattr(self.tool_config, 'python_timeout',
242 30) if self.tool_config else 30
243 self._shell_timeout = getattr(self.tool_config, 'shell_timeout',
244 60) if self.tool_config else 60
245
246 kernel_env = self._build_env('kernel_env', inherit=False)
247 shell_env = self._build_env('shell_env', inherit=False)
248 self.kernel_session = LocalKernelSession(
249 working_dir=self.output_dir, env=kernel_env)
250 self.shell_env = shell_env
251 self._kernel_lock = asyncio.Lock()
252 self._initialized = False
253
254 self.exclude_func(
255 getattr(getattr(config, 'tools', None), 'code_executor', None))
256 if 'file_operation' not in self.exclude_functions:
257 logger.warning(
258 'file_operation is not suggested to be included in local code execution tool.'
259 )
260
261 results = self._check_dependencies()
262 logger.info(f'Dependency check results: {results}\n'
263 f'Make sure to install the missing dependencies.')
264
265 logger.info('LocalCodeExecutionTool initialized (ipykernel based)')
266
267 def _check_dependencies(self) -> None:
268 import importlib
269
270 deps = {
271 'numpy': 'numpy',
272 'pandas': 'pandas',
273 'matplotlib': 'matplotlib',
274 'seaborn': 'seaborn',
275 'scikit-learn': 'sklearn',
276 'requests': 'requests',
277 'beautifulsoup4': 'bs4',
278 'lxml': 'lxml',
279 'pillow': 'PIL',
280 'tqdm': 'tqdm',
281 'pyarrow': 'pyarrow',
282 }
283
284 results = {}
285 for pip_name, import_name in deps.items():

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected