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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Linux MCP Server using Gradio's built-in MCP support.
|
| 3 |
+
This creates both the MCP server and a web interface for tool management.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
# Add project root to path
|
| 12 |
+
project_root = Path(__file__).parent
|
| 13 |
+
sys.path.insert(0, str(project_root))
|
| 14 |
+
|
| 15 |
+
# Import our tools
|
| 16 |
+
from tools.linux_tools import (
|
| 17 |
+
add_user,
|
| 18 |
+
create_file,
|
| 19 |
+
change_permission,
|
| 20 |
+
run_safe_command
|
| 21 |
+
)
|
| 22 |
+
from shared.config import config
|
| 23 |
+
import logging
|
| 24 |
+
|
| 25 |
+
# Set up logging
|
| 26 |
+
logging.basicConfig(level=logging.INFO)
|
| 27 |
+
logger = logging.getLogger(__name__)
|
| 28 |
+
|
| 29 |
+
def create_linux_mcp_server():
|
| 30 |
+
"""
|
| 31 |
+
Create the Linux MCP server with Gradio.
|
| 32 |
+
Gradio automatically converts @gr.tool functions into MCP tools.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# Define the tools list for Gradio
|
| 36 |
+
tools = [
|
| 37 |
+
run_safe_command, # Safe operations (low risk)
|
| 38 |
+
add_user, # Sensitive operations (high risk)
|
| 39 |
+
create_file, # Sensitive operations (high risk)
|
| 40 |
+
change_permission # Sensitive operations (high risk)
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
# Create the MCP server interface
|
| 44 |
+
with gr.Blocks(
|
| 45 |
+
title=config.gradio.title,
|
| 46 |
+
theme=config.gradio.theme
|
| 47 |
+
) as demo:
|
| 48 |
+
|
| 49 |
+
gr.Markdown(f"""
|
| 50 |
+
# {config.gradio.title}
|
| 51 |
+
|
| 52 |
+
## π οΈ Available MCP Tools
|
| 53 |
+
|
| 54 |
+
This server exposes Linux administration tools via the MCP (Model Context Protocol):
|
| 55 |
+
|
| 56 |
+
### π’ Safe Operations (Direct Execution)
|
| 57 |
+
- **run_safe_command**: Execute whitelisted read-only commands
|
| 58 |
+
- Whitelist: `{', '.join(config.security.safe_commands[:10])}...`
|
| 59 |
+
|
| 60 |
+
### π΄ Sensitive Operations (Require Validation)
|
| 61 |
+
- **add_user**: Create new system users
|
| 62 |
+
- **create_file**: Create files with specified content and permissions
|
| 63 |
+
- **change_permission**: Modify file/directory permissions
|
| 64 |
+
|
| 65 |
+
## π MCP Connection
|
| 66 |
+
|
| 67 |
+
Connect MCP clients to this server to access these tools programmatically.
|
| 68 |
+
Each tool includes proper validation and security checks.
|
| 69 |
+
|
| 70 |
+
## π Tool Status
|
| 71 |
+
- Total Tools: {len(tools)}
|
| 72 |
+
- Safe Commands: {len(config.security.safe_commands)}
|
| 73 |
+
- Command Timeout: {config.security.command_timeout}s
|
| 74 |
+
""")
|
| 75 |
+
|
| 76 |
+
# Tool testing interface (optional)
|
| 77 |
+
with gr.Tab("Test Tools"):
|
| 78 |
+
gr.Markdown("### Test MCP Tools Directly")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column():
|
| 82 |
+
tool_selector = gr.Dropdown(
|
| 83 |
+
choices=["run_safe_command", "add_user", "create_file", "change_permission"],
|
| 84 |
+
label="Select Tool",
|
| 85 |
+
value="run_safe_command"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Safe command inputs
|
| 89 |
+
with gr.Group(visible=True) as safe_inputs:
|
| 90 |
+
safe_command = gr.Dropdown(
|
| 91 |
+
choices=config.security.safe_commands,
|
| 92 |
+
label="Command",
|
| 93 |
+
value="ls"
|
| 94 |
+
)
|
| 95 |
+
safe_args = gr.Textbox(
|
| 96 |
+
label="Arguments (space-separated)",
|
| 97 |
+
placeholder="-la /home"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# User inputs
|
| 101 |
+
with gr.Group(visible=False) as user_inputs:
|
| 102 |
+
username = gr.Textbox(label="Username", placeholder="john")
|
| 103 |
+
groups = gr.Textbox(label="Groups (comma-separated)", placeholder="sudo,users")
|
| 104 |
+
create_home = gr.Checkbox(label="Create Home Directory", value=True)
|
| 105 |
+
|
| 106 |
+
# File inputs
|
| 107 |
+
with gr.Group(visible=False) as file_inputs:
|
| 108 |
+
filepath = gr.Textbox(label="File Path", placeholder="/tmp/test.txt")
|
| 109 |
+
content = gr.Textbox(label="File Content", lines=3, placeholder="Hello, World!")
|
| 110 |
+
permissions = gr.Textbox(label="Permissions", value="644", placeholder="644")
|
| 111 |
+
|
| 112 |
+
test_button = gr.Button("Test Tool", variant="primary")
|
| 113 |
+
|
| 114 |
+
with gr.Column():
|
| 115 |
+
test_output = gr.JSON(label="Tool Output")
|
| 116 |
+
|
| 117 |
+
# Tool selector event handler
|
| 118 |
+
def update_inputs(tool_name):
|
| 119 |
+
return {
|
| 120 |
+
safe_inputs: gr.update(visible=(tool_name == "run_safe_command")),
|
| 121 |
+
user_inputs: gr.update(visible=(tool_name == "add_user")),
|
| 122 |
+
file_inputs: gr.update(visible=(tool_name in ["create_file", "change_permission"]))
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
tool_selector.change(
|
| 126 |
+
update_inputs,
|
| 127 |
+
inputs=[tool_selector],
|
| 128 |
+
outputs=[safe_inputs, user_inputs, file_inputs]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Test tool execution
|
| 132 |
+
def test_tool(tool_name, cmd, args, user, grps, home, path, cont, perms):
|
| 133 |
+
try:
|
| 134 |
+
if tool_name == "run_safe_command":
|
| 135 |
+
args_list = args.split() if args else []
|
| 136 |
+
return run_safe_command(cmd, args_list)
|
| 137 |
+
|
| 138 |
+
elif tool_name == "add_user":
|
| 139 |
+
groups_list = [g.strip() for g in grps.split(",")] if grps else []
|
| 140 |
+
return add_user(user, groups_list, home)
|
| 141 |
+
|
| 142 |
+
elif tool_name == "create_file":
|
| 143 |
+
return create_file(path, cont, perms)
|
| 144 |
+
|
| 145 |
+
elif tool_name == "change_permission":
|
| 146 |
+
return change_permission(path, perms)
|
| 147 |
+
|
| 148 |
+
else:
|
| 149 |
+
return {"error": f"Unknown tool: {tool_name}"}
|
| 150 |
+
|
| 151 |
+
except Exception as e:
|
| 152 |
+
return {"error": str(e)}
|
| 153 |
+
|
| 154 |
+
test_button.click(
|
| 155 |
+
test_tool,
|
| 156 |
+
inputs=[
|
| 157 |
+
tool_selector, safe_command, safe_args,
|
| 158 |
+
username, groups, create_home,
|
| 159 |
+
filepath, content, permissions
|
| 160 |
+
],
|
| 161 |
+
outputs=[test_output]
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# System info tab
|
| 165 |
+
with gr.Tab("System Info"):
|
| 166 |
+
gr.Markdown(f"""
|
| 167 |
+
### Server Configuration
|
| 168 |
+
|
| 169 |
+
- **MCP Tools**: {len(tools)} available
|
| 170 |
+
- **Safe Commands**: {len(config.security.safe_commands)} whitelisted
|
| 171 |
+
- **Command Timeout**: {config.security.command_timeout} seconds
|
| 172 |
+
- **OpenAI Model**: {config.openai.model}
|
| 173 |
+
|
| 174 |
+
### Security Settings
|
| 175 |
+
|
| 176 |
+
**Safe Commands Whitelist:**
|
| 177 |
+
```
|
| 178 |
+
{', '.join(config.security.safe_commands)}
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
**Sensitive Operations:**
|
| 182 |
+
- User management (add_user)
|
| 183 |
+
- File operations (create_file, change_permission)
|
| 184 |
+
- All require proper validation and logging
|
| 185 |
+
""")
|
| 186 |
+
|
| 187 |
+
# Configure MCP server
|
| 188 |
+
demo.mcp_tools = tools # Gradio automatically exposes these as MCP tools
|
| 189 |
+
|
| 190 |
+
return demo
|
| 191 |
+
|
| 192 |
+
def main():
|
| 193 |
+
"""Main entry point for the Linux MCP server"""
|
| 194 |
+
|
| 195 |
+
logger.info("Starting Linux MCP Server with Gradio")
|
| 196 |
+
logger.info(f"OpenAI API configured: {'β
' if config.openai.api_key else 'β'}")
|
| 197 |
+
logger.info(f"Safe commands: {len(config.security.safe_commands)}")
|
| 198 |
+
|
| 199 |
+
# Create and launch the server
|
| 200 |
+
demo = create_linux_mcp_server()
|
| 201 |
+
|
| 202 |
+
# Launch with MCP server enabled
|
| 203 |
+
demo.launch(
|
| 204 |
+
share=config.gradio.share,
|
| 205 |
+
server_name="0.0.0.0", # For HF Spaces
|
| 206 |
+
server_port=7860, # Standard HF Spaces port
|
| 207 |
+
show_error=True
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
if __name__ == "__main__":
|
| 211 |
+
main()
|