shutdown_digitalocean_droplet
Shut down a DigitalOcean Droplet safely through the Cloud Manage MCP Server. Requires three confirmations (ID, IP, and name) to ensure accuracy and prevent unintended actions.
Instructions
优雅关闭DigitalOcean Droplet(需要三次确认)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| droplet_id | Yes | ||
| ip_confirmation | No | ||
| name_confirmation | No | ||
| operation_confirmation | No |
Input Schema (JSON Schema)
{
"properties": {
"droplet_id": {
"title": "Droplet Id",
"type": "integer"
},
"ip_confirmation": {
"default": "",
"title": "Ip Confirmation",
"type": "string"
},
"name_confirmation": {
"default": "",
"title": "Name Confirmation",
"type": "string"
},
"operation_confirmation": {
"default": "",
"title": "Operation Confirmation",
"type": "string"
}
},
"required": [
"droplet_id"
],
"title": "shutdown_digitalocean_dropletArguments",
"type": "object"
}
Implementation Reference
- main.py:432-444 (handler)MCP tool registration and primary handler for 'shutdown_digitalocean_droplet'. This function is decorated with @mcp.tool() and delegates to the DigitalOcean provider's shutdown_droplet method after receiving parameters.@mcp.tool() def shutdown_digitalocean_droplet( droplet_id: int, ip_confirmation: str = "", name_confirmation: str = "", operation_confirmation: str = "" ) -> Dict: """ 优雅关闭DigitalOcean Droplet(需要三次确认) """ return digitalocean_provider.shutdown_droplet( droplet_id, ip_confirmation, name_confirmation, operation_confirmation )
- DigitalOceanProvider class method that implements the core shutdown logic by invoking the shared _execute_power_operation helper with operation='shutdown'.def shutdown_droplet( self, droplet_id: int, ip_confirmation: str = "", name_confirmation: str = "", operation_confirmation: str = "" ) -> Dict: """ 优雅关闭Droplet(需要三次确认) """ return self._execute_power_operation( droplet_id, 'shutdown', ip_confirmation, name_confirmation, operation_confirmation )
- Core helper function shared across power operations (power_on, power_off, shutdown, reboot). Performs triple confirmation validation using utils.security.SecurityConfirmation, fetches droplet info, and executes the DigitalOcean API call: client.droplet_actions.post with body={'type': 'shutdown'} for graceful shutdown.def _execute_power_operation( self, droplet_id: int, operation: str, ip_confirmation: str, name_confirmation: str, operation_confirmation: str ) -> Dict: """ 执行电源操作的通用函数 """ if not self.available: return { 'error': f'DigitalOcean服务不可用: {getattr(self, "error", "未知错误")}', 'provider': 'digitalocean' } # 首先获取droplet信息 try: droplet_response = self.client.droplets.get(droplet_id) droplet = droplet_response.get("droplet", {}) if not droplet: return { 'error': f'未找到ID为 {droplet_id} 的Droplet', 'provider': 'digitalocean' } # 格式化droplet信息用于确认 droplet_info = self._format_droplet_for_confirmation(droplet) except Exception as e: return { 'error': f'获取Droplet信息时发生错误: {str(e)}', 'provider': 'digitalocean' } # 检查是否提供了确认信息 if not ip_confirmation or not name_confirmation or not operation_confirmation: # 返回确认要求 return require_triple_confirmation(droplet_info, operation) # 验证确认信息 security = SecurityConfirmation() is_valid, error_message = security.validate_power_operation( droplet_info, operation, ip_confirmation, name_confirmation, operation_confirmation ) if not is_valid: return { 'error': f'确认验证失败: {error_message}', 'provider': 'digitalocean', 'requires_confirmation': True } # 执行实际操作 try: action_data = {"type": operation} response = self.client.droplet_actions.post(droplet_id=droplet_id, body=action_data) action = response.get("action", {}) return { 'provider': 'digitalocean', 'droplet_id': droplet_id, 'operation_success': True, 'action': { 'id': action.get("id"), 'status': action.get("status"), 'type': action.get("type"), 'started_at': action.get("started_at"), 'resource_id': action.get("resource_id") }, 'message': f'已成功提交 {operation} 操作,操作ID: {action.get("id")}', 'confirmation_validated': True } except Exception as e: return { 'error': f'执行 {operation} 操作时发生错误: {str(e)}', 'provider': 'digitalocean' }