MCPcopy Create free account
hub / github.com/astercloud/aster / Execute

Method Execute

pkg/middleware/filesystem_tools.go:143–186  ·  view source on GitHub ↗
(ctx context.Context, input map[string]any, tc *tools.ToolContext)

Source from the content-addressed store, hash-verified

141}
142
143func (t *EditTool) Execute(ctx context.Context, input map[string]any, tc *tools.ToolContext) (any, error) {
144 path, _ := input["path"].(string)
145 oldStr, _ := input["old_string"].(string)
146 newStr, _ := input["new_string"].(string)
147 replaceAll := false
148 if ra, ok := input["replace_all"].(bool); ok {
149 replaceAll = ra
150 }
151
152 // 路径安全验证
153 if t.middleware != nil {
154 validatedPath, err := t.middleware.validatePath(path)
155 if err != nil {
156 return map[string]any{
157 "ok": false,
158 "error": fmt.Sprintf("path validation failed: %v", err),
159 }, nil
160 }
161 path = validatedPath
162 }
163
164 result, err := t.backend.Edit(ctx, path, oldStr, newStr, replaceAll)
165 if err != nil {
166 return map[string]any{
167 "ok": false,
168 "error": fmt.Sprintf("failed to edit file: %v", err),
169 }, nil
170 }
171
172 // 检查 Error 字段判断是否成功
173 if result.Error != "" {
174 return map[string]any{
175 "ok": false,
176 "error": result.Error,
177 }, nil
178 }
179
180 return map[string]any{
181 "ok": true,
182 "path": path,
183 "replacements": result.ReplacementsMade,
184 "replace_all": replaceAll,
185 }, nil
186}
187
188func (t *EditTool) Prompt() string {
189 return `Use this tool for precise file editing via string replacement.

Callers

nothing calls this directly

Calls 2

EditMethod · 0.65
validatePathMethod · 0.45

Tested by

no test coverage detected