Files
zeekayandHanzo Dev a386271c80 isolation is a value the org sets, and an empty one is not runc
Every sandbox was created privileged under the daemon's default runtime, and
the setting that would have changed it defaulted to an empty string that
silently meant runc. For a service whose whole premise is running code
somebody else wrote, that is not a boundary.

An org sets its isolation and the create request carries it, so a sandbox
records what it was made with: changing the policy moves new sandboxes and
leaves running ones alone. gvisor is the default. firecracker is delivered
through kata's shim, which is why it is one name rather than two. runc is
refused unless the runner names that org, and an empty list names nobody.

Absent means the default, unknown is a refusal, and a runtime the daemon does
not have is a refusal. Nothing substitutes quietly.

The runner took an organization in a field called UserId with a note beside
it saying so. Deciding who may have runc through a field named after
something else is not reviewable.

Privileged is replaced by dropping everything and granting the eight the
daemon uses. Nothing the sandbox does broke; what stopped working is mount,
mknod, raw sockets, chroot, writing to proc, and seeing the host's disks.
AUDIT_WRITE is among them because without it sudo works and complains every
time.

The creation histogram's lowest bucket was a tenth of a second, so the metric
could not observe the claim the product makes about its own speed. It can
now, and it carries the isolation, so comparing them is a query.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-11 13:47:31 -07:00

113 lines
3.1 KiB
Go

// Copyright 2025 Hanzo Industries Inc.
// SPDX-License-Identifier: AGPL-3.0
package tools
import (
"context"
"encoding/json"
"fmt"
"io"
"path/filepath"
"github.com/hanzoai/runtime/apps/cli/apiclient"
"github.com/mark3labs/mcp-go/mcp"
)
type FileDownloadArgs struct {
Id *string `json:"id,omitempty"`
FilePath *string `json:"filePath,omitempty"`
}
type Content struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Data string `json:"data,omitempty"`
}
func GetFileDownloadTool() mcp.Tool {
return mcp.NewTool("file_download",
mcp.WithDescription("Download a file from the Runtime sandbox. Returns the file content either as text or as a base64 encoded image. Handles special cases like matplotlib plots stored as JSON with embedded base64 images."),
mcp.WithString("filePath", mcp.Required(), mcp.Description("Path to the file to download.")),
mcp.WithString("id", mcp.Required(), mcp.Description("ID of the sandbox to download the file from.")),
)
}
func FileDownload(ctx context.Context, request mcp.CallToolRequest, args FileDownloadArgs) (*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")
}
if args.FilePath == nil || *args.FilePath == "" {
return &mcp.CallToolResult{IsError: true}, fmt.Errorf("filePath parameter is required")
}
// Download the file
file, _, err := apiClient.ToolboxAPI.DownloadFile(ctx, *args.Id).Path(*args.FilePath).Execute()
if err != nil {
return &mcp.CallToolResult{IsError: true}, fmt.Errorf("error downloading file: %v", err)
}
defer file.Close()
// Read file content
content, err := io.ReadAll(file)
if err != nil {
return &mcp.CallToolResult{IsError: true}, fmt.Errorf("error reading file content: %v", err)
}
// Process file content based on file type
ext := filepath.Ext(*args.FilePath)
var result []Content
switch ext {
case ".png", ".jpg", ".jpeg", ".gif":
// For image files, return as base64 encoded data
result = []Content{{
Type: "image",
Data: string(content),
}}
case ".json":
// For JSON files, try to parse and handle special cases like matplotlib plots
var jsonData map[string]interface{}
if err := json.Unmarshal(content, &jsonData); err != nil {
// If not valid JSON, return as text
result = []Content{{
Type: "text",
Text: string(content),
}}
} else {
// Check if it's a matplotlib plot
if _, ok := jsonData["data"]; ok {
result = []Content{{
Type: "image",
Data: jsonData["data"].(string),
}}
} else {
result = []Content{{
Type: "text",
Text: string(content),
}}
}
}
default:
// For all other files, return as text
result = []Content{{
Type: "text",
Text: string(content),
}}
}
// Convert result to JSON
resultJSON, err := json.Marshal(result)
if err != nil {
return &mcp.CallToolResult{IsError: true}, fmt.Errorf("error marshaling result: %v", err)
}
return mcp.NewToolResultText(string(resultJSON)), nil
}