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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| #!/usr/bin/env python3 """ OPSMCP - Operations MCP Server Internal tool for system operations management """
from flask import Flask, jsonify, request import os
app = Flask(__name__)
# API Key for authentication VALID_API_KEY = "opsmcp_secret_key_4f5a6b7c8d9e0f1a"
# Registered tools (visible) VISIBLE_TOOLS = { "ops.system_status": { "description": "Get system status and health metrics", "parameters": {} }, "ops.list_services": { "description": "List running services", "parameters": {} }, "ops.check_disk": { "description": "Check disk usage", "parameters": {} }, "ops.view_logs": { "description": "View recent system logs", "parameters": {"service": "string"} } }
# Hidden tools (not in /tools/list but callable) HIDDEN_TOOLS = { "ops._admin_dump": { "description": "Emergency credential dump - INTERNAL ONLY", "parameters": {"target": "string", "confirm": "boolean"} }, "ops._debug_mode": { "description": "Enable debug mode", "parameters": {} } }
ALL_TOOLS = {**VISIBLE_TOOLS, **HIDDEN_TOOLS}
def check_auth(): """Check API key authentication""" api_key = request.headers.get('X-API-Key', '') return api_key == VALID_API_KEY
@app.route('/') def index(): return jsonify({ "server": "OPSMCP", "version": "2.1.0", "status": "operational", "endpoints": ["/tools/list", "/tools/call", "/health"], "auth": "Required - X-API-Key header" })
@app.route('/health') def health(): return jsonify({"status": "healthy", "uptime": "14d 3h 22m"})
@app.route('/tools/list') def list_tools(): if not check_auth(): return jsonify({"error": "Unauthorized", "message": "Valid X-API-Key header required"}), 401 return jsonify({ "tools": list(VISIBLE_TOOLS.keys()), "count": len(VISIBLE_TOOLS), "details": VISIBLE_TOOLS })
@app.route('/tools/call', methods=['POST']) def call_tool(): if not check_auth(): return jsonify({"error": "Unauthorized", "message": "Valid X-API-Key header required"}), 401 data = request.get_json() or {} tool_name = data.get('name', '') args = data.get('arguments', {}) if not tool_name: return jsonify({"error": "Tool name required"}), 400 if tool_name not in ALL_TOOLS: return jsonify({"error": f"Unknown tool: {tool_name}"}), 404 # Execute tool if tool_name == "ops.system_status": return jsonify({ "cpu": "23%", "memory": "1.2GB/4GB", "load": "0.45", "status": "nominal" }) elif tool_name == "ops.list_services": return jsonify({ "services": [ {"name": "nginx", "status": "running", "pid": 1234}, {"name": "opsmcp", "status": "running", "pid": 5678}, {"name": "jupyter", "status": "running", "pid": 9012}, {"name": "mcpjam", "status": "running", "pid": 3456} ] }) elif tool_name == "ops.check_disk": return jsonify({ "filesystems": [ {"mount": "/", "used": "4.2G", "available": "15G", "percent": "22%"}, {"mount": "/home", "used": "1.1G", "available": "8G", "percent": "12%"} ] }) elif tool_name == "ops.view_logs": service = args.get('service', 'system') return jsonify({ "service": service, "logs": [ "[2026-01-22 10:00:01] Service started", "[2026-01-22 10:00:02] Listening on configured port", "[202 6-01-22 10:15:33] Health check passed", "[2026-01-22 11:00:00] Routine maintenance completed" ] }) elif tool_name == "ops._debug_mode": return jsonify({ "debug": True, "message": "Debug mode enabled", "hidden_tools": list(HIDDEN_TOOLS.keys()), "note": "Debug endpoints now accessible" }) elif tool_name == "ops._admin_dump": target = args.get('target', '') confirm = args.get('confirm', False) if not confirm: return jsonify({ "error": "Confirmation required", "usage": "Set confirm=true to proceed", "warning": "This dumps sensitive credentials" }) if target == "ssh_keys": try: with open('/root/.ssh/id_rsa', 'r') as f: key_data = f.read() return jsonify({ "target": "ssh_keys", "root_private_key": key_data, "note": "Emergency recovery key dump" }) except Exception as e: return jsonify({ "target": "ssh_keys", "error": f"Could not read key: {str(e)}" }) elif target == "passwords": return jsonify({ "target": "passwords", "dump": { "root": "$6$rounds=656000$saltsalt$hashedpassword", "analyst": "JupyterN0tebook!2026", "mcp-dev": "Mcp!Insp3ct0r2026" } }) elif target == "tokens": return jsonify({ "target": "tokens", "api_tokens": { "admin_token": "opsmcp_admin_7f3b9c2d1e4f5a6b", "service_token": "opsmcp_svc_8c9d0e1f2a3b4c5d" } }) else: return jsonify({ "error": "Invalid target", "valid_targets": ["ssh_keys", "passwords", "tokens"] }) return jsonify({"error": "Tool execution failed"}), 500
if __name__ == '__main__': app.run(host='127.0.0.1', port=5000, debug=False)
|