Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add coder_workspace_edit_file MCP tool#19629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
code-asher merged 5 commits intomainfromasher/mcp-file-edit
Sep 12, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Support editing multiple files
  • Loading branch information
@code-asher
code-asher committedSep 12, 2025
commit276da3437216c9ceaf76623a71a6112da57dac81
2 changes: 1 addition & 1 deletionagent/api.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -62,7 +62,7 @@ func (a *agent) apiHandler() http.Handler {
r.Post("/api/v0/list-directory", a.HandleLS)
r.Get("/api/v0/read-file", a.HandleReadFile)
r.Post("/api/v0/write-file", a.HandleWriteFile)
r.Post("/api/v0/edit-file", a.HandleEditFile)
r.Post("/api/v0/edit-files", a.HandleEditFiles)
r.Get("/debug/logs", a.HandleHTTPDebugLogs)
r.Get("/debug/magicsock", a.HandleHTTPDebugMagicsock)
r.Get("/debug/magicsock/debug-logging/{state}", a.HandleHTTPMagicsockDebugLoggingState)
Expand Down
41 changes: 26 additions & 15 deletionsagent/files.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -170,40 +170,51 @@ func (a *agent) writeFile(ctx context.Context, r *http.Request, path string) (HT
return 0, nil
}

func (a *agent)HandleEditFile(rw http.ResponseWriter, r *http.Request) {
func (a *agent)HandleEditFiles(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()

query := r.URL.Query()
parser := httpapi.NewQueryParamParser().RequiredNotEmpty("path")
path := parser.String(query, "", "path")
parser.ErrorExcessParams(query)
if len(parser.Errors) > 0 {
var req workspacesdk.FileEditRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}

if len(req.Files) == 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Query parameters have invalid values.",
Validations: parser.Errors,
Message: "must specify at least one file",
})
return
}

var edits workspacesdk.FileEditRequest
if !httpapi.Read(ctx, rw, r, &edits) {
return
var combinedErr error
status := http.StatusOK
for _, edit := range req.Files {
s, err := a.editFile(r.Context(), edit.Path, edit.Edits)
// Keep the highest response status, so 500 will be preferred over 400, etc.
if s > status {
status = s
}
if err != nil {
combinedErr = errors.Join(combinedErr, err)
}
}

status, err := a.editFile(r.Context(), path, edits.Edits)
if err != nil {
if combinedErr != nil {
httpapi.Write(ctx, rw, status, codersdk.Response{
Message:err.Error(),
Message:combinedErr.Error(),
})
return
}

httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
Message:fmt.Sprintf("Successfully edited%q", path),
Message: "Successfully editedfile(s)",
})
}

func (a *agent) editFile(ctx context.Context, path string, edits []workspacesdk.FileEdit) (int, error) {
if path == "" {
return http.StatusBadRequest, xerrors.New("\"path\" is required")
}

if !filepath.IsAbs(path) {
return http.StatusBadRequest, xerrors.Errorf("file path must be absolute: %q", path)
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp