diff --git a/servers/shell_mcp/mcp-rpm.yaml b/servers/shell_mcp/mcp-rpm.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5c2c39b16ea88cac379b55a524d8b09a212956fe --- /dev/null +++ b/servers/shell_mcp/mcp-rpm.yaml @@ -0,0 +1,16 @@ +name: "shell-exec" +summary: "MCP server for shell exec cmd" +description: | + Provides MCP tools for shell command exec. + +dependencies: + system: + - python3 + - python3-mcp + +files: + required: + - mcp_config.json + - src/server.py + optional: + - src/requirements.txt diff --git a/servers/shell_mcp/mcp_config.json b/servers/shell_mcp/mcp_config.json new file mode 100644 index 0000000000000000000000000000000000000000..fb5cc61b73b1522c3e90e78912227ebcd484fe46 --- /dev/null +++ b/servers/shell_mcp/mcp_config.json @@ -0,0 +1,18 @@ +{ + "mcpServers": { + "wget_mcp": { + "command": "uv", + "args": [ + "--directory", + "/opt/mcp-servers/servers/shell_mcp", + "run", + "src/server.py" + ], + "disabled": false, + "autoApprove": [], + "alwaysAllow": [ + "shell_exec" + ] + } + } +} diff --git a/servers/shell_mcp/src/requirements.txt b/servers/shell_mcp/src/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..7211e49a1325ab90f08e21ad5d863873d7079c3d --- /dev/null +++ b/servers/shell_mcp/src/requirements.txt @@ -0,0 +1 @@ +mcp>=1.9.4 \ No newline at end of file diff --git a/servers/shell_mcp/src/server.py b/servers/shell_mcp/src/server.py new file mode 100644 index 0000000000000000000000000000000000000000..894698fdf4162aab755ec665e7cfdcb1c2ebcdae --- /dev/null +++ b/servers/shell_mcp/src/server.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +from pydantic import Field +from typing import Optional, Dict, Any, List +from mcp.server.fastmcp import FastMCP +import subprocess +import json +import os + +mcp = FastMCP("shell_exec") + +def _run_command(args: Dict[str, Any]) -> Dict[str, Any]: + """执行shell命令并返回标准的执行结果 + Execute a shell command and return standardized result + """ + try: + result = subprocess.run( + args, + shell=True, + capture_output=True, + text=True, + check=True, + timeout=360, + ) + return { + "success": True, + "output": result.stdout.strip(), + "error": result.stderr.strip() + } + except subprocess.CalledProcessError as e: + return { + "success": False, + "output": e.stdout.strip(), + "error": e.stderr.strip() + } + +@mcp.tool() +def shell_exec( + cmd: Optional[str] = Field(default=None, description="用户输入的shell命令"), +) -> Dict[str, Any]: + """执行用户下发的shell指令 + Execute the shell command issued by the user + + 参数: + cmd(str): 用户输入的shell指令 + cmd(str): the shell command entered by the uesr + + 返回: + Dict[str, Any]: 字典第一个元素表示是否执行成功,第二个参数表示执行成功时的结果,第三个参数表示执行失败时的结果 + Dict[str, Any]: A dictionary containing the response, first element is true or false, second element is stdout if success, third element is stderr if failed + """ + args = [] + if cmd: + args.append(cmd) + + return _run_command(args) + +if __name__ == "__main__": + mcp.run()