Files
runtime/apps/cli/mcp/tools/destroy_sandbox.go
zeekay ea80dc3638 feat: Add new Dockerfiles and improve containerization
- Add main Dockerfile for production builds
- Add Dockerfile.runtime for secure code execution
- Add Dockerfile.runtime.fixed with improved configuration
- Include .dockerignore for build optimization
- Add integration documentation and publishing guides
- Update API and CLI components for better runtime support
- Enhance sandbox and container management features
2025-08-16 23:03:39 -05:00

63 lines
1.7 KiB
Go

// Copyright 2025 Hanzo Industries Inc.
// SPDX-License-Identifier: AGPL-3.0
package tools
import (
"context"
"fmt"
"time"
"github.com/hanzoai/runtime/cli/apiclient"
"github.com/mark3labs/mcp-go/mcp"
log "github.com/sirupsen/logrus"
)
type DestroySandboxArgs struct {
Id *string `json:"id,omitempty"`
}
func GetDestroySandboxTool() mcp.Tool {
return mcp.NewTool("destroy_sandbox",
mcp.WithDescription("Destroy a sandbox with Runtime"),
mcp.WithString("id", mcp.Required(), mcp.Description("ID of the sandbox to destroy.")),
)
}
func DestroySandbox(ctx context.Context, request mcp.CallToolRequest, args DestroySandboxArgs) (*mcp.CallToolResult, error) {
apiClient, err := apiclient.GetApiClient(nil, runtimeMCPHeaders)
if err != nil {
return &mcp.CallToolResult{IsError: true}, err
}
if args.Id == nil || *args.Id == "" {
return &mcp.CallToolResult{IsError: true}, fmt.Errorf("sandbox ID is required")
}
// Destroy sandbox with retries
maxRetries := 3
retryDelay := time.Second * 2
for retry := range maxRetries {
_, err := apiClient.SandboxAPI.DeleteSandbox(ctx, *args.Id).Force(true).Execute()
if err != nil {
if retry == maxRetries-1 {
return &mcp.CallToolResult{IsError: true}, fmt.Errorf("failed to destroy sandbox after %d retries: %v", maxRetries, err)
}
log.Infof("Sandbox creation failed, retrying: %v", err)
time.Sleep(retryDelay)
retryDelay = retryDelay * 3 / 2 // Exponential backoff
continue
}
log.Infof("Destroyed sandbox with ID: %s", *args.Id)
return mcp.NewToolResultText(fmt.Sprintf("Destroyed sandbox with ID %s", *args.Id)), nil
}
return &mcp.CallToolResult{IsError: true}, fmt.Errorf("failed to destroy sandbox after %d retries", maxRetries)
}