Skip to main content
Glama
lin2000wl
by lin2000wl
shell.py1.83 kB
import os import platform import subprocess from pydantic import BaseModel class ShellCommandResult(BaseModel): stdout: str return_code: int cwd: str stderr: str | None = None def execute_shell_command(command: str, cwd: str | None = None, capture_stderr: bool = False) -> ShellCommandResult: """ Execute a shell command and return the output. :param command: The command to execute. :param cwd: The working directory to execute the command in. If None, the current working directory will be used. :param capture_stderr: Whether to capture the stderr output. :return: The output of the command. """ if cwd is None: cwd = os.getcwd() is_windows = platform.system() == "Windows" process = subprocess.Popen( command, shell=not is_windows, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE if capture_stderr else None, creationflags=subprocess.CREATE_NO_WINDOW if is_windows else 0, # type: ignore text=True, encoding="utf-8", errors="replace", cwd=cwd, ) stdout, stderr = process.communicate() return ShellCommandResult(stdout=stdout, stderr=stderr, return_code=process.returncode, cwd=cwd) def subprocess_check_output(args: list[str], encoding: str = "utf-8", strip: bool = True, timeout: float | None = None) -> str: kwargs = { "stdin": subprocess.DEVNULL, "stderr": subprocess.PIPE, "timeout": timeout, "env": os.environ.copy(), } if platform.system() == "Windows": kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW # type: ignore output = subprocess.check_output(args, **kwargs).decode(encoding) # type: ignore if strip: output = output.strip() return output

Implementation Reference

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/lin2000wl/Serena-cursor-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server