-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_push_file.go
More file actions
64 lines (53 loc) · 1.58 KB
/
tool_push_file.go
File metadata and controls
64 lines (53 loc) · 1.58 KB
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
//go:build linux
package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func registerPushFile(s *server.MCPServer) {
tool := mcp.NewTool("push_file",
mcp.WithDescription(
"Write base64-encoded content to a file on the device. "+
"The content is decoded and written to the specified path.",
),
mcp.WithString("path",
mcp.Required(),
mcp.Description("Destination file path on device"),
),
mcp.WithString("content_base64",
mcp.Required(),
mcp.Description("Base64-encoded file content"),
),
mcp.WithDestructiveHintAnnotation(true),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handlePushFile)
}
func handlePushFile(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handlePushFile")
defer func() { logger.Tracef(ctx, "/handlePushFile") }()
path, err := request.RequireString("path")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
contentB64, err := request.RequireString("content_base64")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
decoded, err := base64.StdEncoding.DecodeString(contentB64)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("base64 decode: %v", err)), nil
}
if err := os.WriteFile(path, decoded, 0644); err != nil {
return mcp.NewToolResultError(fmt.Sprintf("write file: %v", err)), nil
}
return mcp.NewToolResultText(fmt.Sprintf("wrote %d bytes to %s", len(decoded), path)), nil
}