-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
194 lines (154 loc) · 6.29 KB
/
handlers.go
File metadata and controls
194 lines (154 loc) · 6.29 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
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
package main
import (
"context"
"log"
"path/filepath"
"strings"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
type handlerFunc func(ctx context.Context, path string, request mcp.CallToolRequest) (*mcp.CallToolResult, error)
type handlerCfg struct {
baseDir string
dockerMode bool
volumeMapping *VolumeMapping
}
type VolumeMapping struct {
HostPath string
ContainerPath string
}
func (h *handlerCfg) withSafePath(
handler handlerFunc,
) server.ToolHandlerFunc {
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
path := request.Params.Arguments["path"].(string)
if !isSafePath(h.baseDir, path) {
log.Printf("PATH NOT ALLOWED: path is outside of allowed base directory")
return mcp.NewToolResultText("access denied: path is outside of allowed base directory"), nil
}
return handler(ctx, path, request)
}
}
func (h *handlerCfg) withDockerPath(
handler handlerFunc,
) server.ToolHandlerFunc {
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
hostPath := request.Params.Arguments["path"].(string)
// Ensure the path is within the allowed host directory
if !strings.HasPrefix(hostPath, h.volumeMapping.HostPath) {
log.Printf("PATH NOT ALLOWED: %s is outside of %s", hostPath, h.volumeMapping.HostPath)
return mcp.NewToolResultText("PATH NOT ALLOWED: path is outside of allowed directory"), nil
}
// Translate host path to container path
relPath := strings.TrimPrefix(hostPath, h.volumeMapping.HostPath)
containerPath := filepath.Join(h.volumeMapping.ContainerPath, filepath.Clean(relPath))
log.Printf("Path Translation: %s (host) -> %s (container)", hostPath, containerPath)
return handler(ctx, containerPath, request)
}
}
func (h *handlerCfg) handlerListEntries(
ctx context.Context, path string, request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
var depth float64 = 3
if d, ok := request.Params.Arguments["depth"]; ok && d != nil {
depth = d.(float64)
}
operationResult := listEntries(path, depth, "")
if operationResult.Error != nil {
log.Printf("ERROR: %v\n", operationResult.Error)
return mcp.NewToolResultErrorFromErr("", operationResult.Error), operationResult.Error
}
if operationResult.Message != "" {
log.Printf("WARNING: %v\n", operationResult.Message)
return mcp.NewToolResultText(operationResult.Message), nil
}
return mcp.NewToolResultText(operationResult.Content), nil
}
func (h *handlerCfg) handlerReadFile(
ctx context.Context, path string, request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
operationResult := readFile(path)
if operationResult.Error != nil {
log.Printf("ERROR: %v\n", operationResult.Error)
return mcp.NewToolResultErrorFromErr("", operationResult.Error), operationResult.Error
}
if operationResult.Message != "" {
log.Printf("WARNING: %v\n", operationResult.Message)
return mcp.NewToolResultText(operationResult.Message), nil
}
log.Printf("File sucessfully read from: %v\n", path)
return mcp.NewToolResultText(operationResult.Content), nil
}
func (h *handlerCfg) handlerWriteToFile(
ctx context.Context, path string, request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
content := request.Params.Arguments["content"].(string)
operationResult := writeToFile(content, path)
if operationResult.Error != nil {
log.Printf("ERROR: %v\n", operationResult.Error)
return mcp.NewToolResultErrorFromErr("", operationResult.Error), operationResult.Error
}
if operationResult.Message != "" {
log.Printf("WARNING: %v\n", operationResult.Message)
return mcp.NewToolResultText(operationResult.Message), nil
}
log.Printf("File sucessfully written at: %v\n", path)
return mcp.NewToolResultText(operationResult.Content), nil
}
func (h *handlerCfg) handlerGetFileInfo(
ctx context.Context, path string, request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
operationResult := getFileInfo(path)
if operationResult.Error != nil {
log.Printf("ERROR: %v\n", operationResult.Error)
return mcp.NewToolResultErrorFromErr("", operationResult.Error), operationResult.Error
}
if operationResult.Message != "" {
log.Printf("WARNING: %v\n", operationResult.Message)
return mcp.NewToolResultText(operationResult.Message), nil
}
log.Printf("Returning files info from file at: %v\n", path)
return mcp.NewToolResultText(operationResult.Content), nil
}
func (h *handlerCfg) hadlerRenamePath(
ctx context.Context, path string, request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
newPathFinalName := request.Params.Arguments["newPathFinalName"].(string)
operationResult := renamePath(path, newPathFinalName)
if operationResult.Error != nil {
log.Printf("ERROR: %v\n", operationResult.Error)
return mcp.NewToolResultErrorFromErr("", operationResult.Error), operationResult.Error
}
if operationResult.Message != "" {
log.Printf("WARNING: %v\n", operationResult.Message)
return mcp.NewToolResultText(operationResult.Message), nil
}
log.Printf("Returning files info from file at: %v\n", path)
return mcp.NewToolResultText(operationResult.Content), nil
}
func (h *handlerCfg) hadlerCopyFileOrDir(
ctx context.Context, path string, request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
destination := request.Params.Arguments["destination"].(string)
if !h.dockerMode && !isSafePath(h.baseDir, destination) {
log.Printf("PATH NOT ALLOWED: path is outside of allowed base directory")
return mcp.NewToolResultText("access denied: path is outside of allowed base directory"), nil
}
operationResult := copyFileOrDir(path, destination)
if operationResult.Error != nil {
log.Printf("ERROR: %v\n", operationResult.Error)
return mcp.NewToolResultErrorFromErr("", operationResult.Error), operationResult.Error
}
if operationResult.Message != "" {
log.Printf("WARNING: %v\n", operationResult.Message)
return mcp.NewToolResultText(operationResult.Message), nil
}
log.Printf("Returning files info from file at: %v\n", path)
return mcp.NewToolResultText(operationResult.Content), nil
}
func handlersMiddleware(name string, fn server.ToolHandlerFunc) server.ToolHandlerFunc {
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Printf("'%s' called with params: %v", name, request.Params.Arguments)
return fn(ctx, request)
}
}