-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.py
More file actions
60 lines (51 loc) · 1.58 KB
/
Copy pathbash.py
File metadata and controls
60 lines (51 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from mcp.server.fastmcp import FastMCP
import subprocess
import shlex
import os
mcp = FastMCP()
async def execute_bash(script: str) -> str:
"""
Execute an bash script and return the result.
Args:
script (str): The bash command to execute
"""
try:
# Use bash to run the command
result = subprocess.run(
['bash', '-c', script],
capture_output=True,
text=True,
check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
return f"Error executing bash: {e.stderr.strip()}"
except Exception as e:
return f"Unexpected error: {str(e)}"
@mcp.tool()
async def say(text: str) -> str:
"""
Say a given text.
Args:
text (str): The text to be spoken
"""
# Sanitize the input text to prevent code injection
text = shlex.quote(text)
script = f'say {text}'
return await execute_bash(script)
@mcp.tool()
async def whoami() -> str:
"""
Get the name of the current user.
Returns:
str: The username of the current user
"""
script = 'whoami'
return await execute_bash(script)
# If arbitrary execution mode is enabled, we can expose the execute_bash tool.
if os.getenv("MCP_EXECUTION_MODE", "restricted").lower() == "arbitrary":
print("Warning: Arbitrary execution mode is enabled. Be careful with this option and only run it in a safe / sandboxed environment.")
mcp.add_tool(execute_bash)
if __name__ == "__main__":
# Initialize and run the server
mcp.run(transport='stdio')