Files
coder/codersdk/toolsdk/workspace.go
T
Kyle Carberry 98332c739b feat: align MCP workspace tools with Coder Agents and add execute tools (#30275)
The control-plane MCP server's workspace tools now share their
arguments, behavior, and result formats with the Coder Agents tools. The
MCP server also exposes the Coder Agents execute and process tools.

The shared logic lives in a new `codersdk/toolsdk/workspacetools`
package that both `coderd/x/chatd/chattool` and `codersdk/toolsdk` call.
The Coder Agents tool schemas, descriptions, and JSON responses are
unchanged and the existing chattool tests pass unmodified. The one
exception is the missing-shell guidance, which now reads "Workspace
commands run with `sh -c`" instead of naming Coder Agents. Golden files
in `coderd/mcp/testdata/workspace_tools` hold the shared contract for
each tool, and a test checks both the Coder Agents and MCP tools against
them, so contract changes show up in review. The test also fails if MCP
text names a tool that only exists in Coder Agents.

The MCP versions differ from Coder Agents in four ways:
- Each tool takes a `workspace` argument.
- Invalid arguments and agent API errors come back as MCP tool errors.
- A single call waits at most 5 minutes, matching the agent's maximum
blocking wait. Longer timeouts are rejected with a pointer to
`run_in_background`.
- There is no `model_intent` argument.

**Breaking changes to `/api/experimental/mcp/http` and `coder exp
mcp`:**

| Before | After |
|---|---|
| `coder_workspace_read_file`: byte offset/limit, base64 `content` +
`mimeType`, binary-safe | Line-based `offset`/`limit`, text only,
returns `{content, file_size, lines_read, total_lines}` |
| `coder_workspace_write_file`: base64 `content`, binary-safe |
Plain-text `content`, returns `{ok: true}` |
| `coder_workspace_edit_file` + `coder_workspace_edit_files` |
`coder_workspace_edit_files` only, returns `{files: [{path, diff}], ok}`
|
| `coder_workspace_bash` over SSH: `timeout_ms` (default 60s, 5m cap),
kills the command on timeout (exit 124), trims output |
`coder_workspace_execute`: `timeout` (default 10s, 5m cap), `workdir`,
`run_in_background`. On timeout the command keeps running and the result
has `background_process_id`. Output is untrimmed and capped at 32KB. |
| None | `coder_workspace_process_output`,
`coder_workspace_process_list`, `coder_workspace_process_signal` |

`--allowed-tools` lists and templates that name the removed tools will
need updating. A code search found `coder_workspace_bash` in a couple of
demo templates (`coder/demo-env-templates`, partner demos).

<details>
<summary>Decision log</summary>

- **Shared core vs. duplication.** The execute lifecycle has the most
logic that could drift: waiting in the foreground, handing off to the
background on timeout, recovering a snapshot after transport errors, and
truncating output. It's extracted rather than copied, along with
edit-file validation, read-file handling, and result types. Each surface
keeps its own protocol handling: fantasy and MCP response envelopes,
plan-turn restrictions, `model_intent`, and workspace resolution and
authorization. Struct-tag descriptions must be literals, so the
golden-file test is what keeps them in sync with the shared constants.
Shared result structs keep their fields in alphabetical order so the
JSON matches what Coder Agents produced from maps before.
- **Replace instead of add alongside.** Keeping `coder_workspace_bash`
next to `coder_workspace_execute` would give models two overlapping ways
to run commands. The read and write contracts change anyway, so keeping
the old names wouldn't preserve compatibility. The endpoint and CLI
command are both experimental.
- **5-minute cap on MCP calls.** Each stateless MCP request has to
return before the client gives up. Otherwise the `background_process_id`
needed to follow up is lost and the command keeps running with no ID the
model knows about. Coder Agents has no cap, so its behavior is
unchanged.
- **No SSH fallback.** `execute` doesn't fall back to SSH on older
agents, because SSH can't support the process management contract. The
agent process API and line-based reads shipped in February 2026.
- **Process scoping.** The agent scopes processes by chat ID, which
coderd sets only for Coder Agents connections. MCP calls carry no chat
ID, so they can't see or signal Coder Agents chat processes. All MCP
callers share one process namespace per workspace, which means anyone
with SSH access can list and signal processes another MCP user started.
That matches what SSH access already allows. The existing `ActionSSH`
check in `coderd/mcp_http.go` still runs before every agent connection.
Nothing reaps MCP processes; they run until they exit, are signaled, or
the agent stops.
- **Errors.** As in Coder Agents, command failures and timeouts come
back as result JSON (`success: false`, `background_process_id`). Agent
API errors have transport metadata removed, the same way Coder Agents
formats them.

</details>

---

🤖 Generated by Coder Agents on behalf of @kylecarbs.
2026-10-04 11:36:48 -05:00

385 lines
13 KiB
Go

package toolsdk
import (
"context"
"strings"
"time"
"golang.org/x/xerrors"
"github.com/coder/aisdk-go"
"github.com/coder/coder/v2/codersdk/toolsdk/workspacetools"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)
// The workspace file and process tools share their arguments, behavior,
// and results with the Coder Agents tools (see workspacetools). The MCP
// versions add a workspace argument because an MCP session is not bound
// to a single workspace, and they report invalid arguments and agent API
// failures as MCP tool errors. A parity test in coderd/mcp keeps the
// schemas aligned.
// workspaceToolNames are the MCP names of the workspace tools, used in
// shared descriptions and follow-up hints.
var workspaceToolNames = workspacetools.ToolNames{
Execute: ToolNameWorkspaceExecute,
ProcessOutput: ToolNameWorkspaceProcessOutput,
ProcessList: ToolNameWorkspaceProcessList,
ReadFile: ToolNameWorkspaceReadFile,
}
// maxWorkspaceToolWait bounds how long an MCP call blocks on a command.
// Each MCP request must return before the client gives up, otherwise the
// background_process_id needed to follow up is lost. It matches the
// agent's maximum blocking wait for process output.
const maxWorkspaceToolWait = 5 * time.Minute
func workspaceProperty() map[string]any {
return map[string]any{
"type": "string",
"description": workspaceAgentDescription,
}
}
// workspaceToolError converts an agent API error to the tool error
// returned to the model, dropping transport metadata.
func workspaceToolError(err error) error {
return xerrors.New(workspacetools.AgentAPIErrorMessage(err))
}
// WorkspaceReadFileArgs are the arguments of WorkspaceReadFile.
type WorkspaceReadFileArgs struct {
Workspace string `json:"workspace"`
Path string `json:"path"`
Offset *int64 `json:"offset,omitempty"`
Limit *int64 `json:"limit,omitempty"`
}
// WorkspaceReadFile reads line-numbered content from a workspace file.
var WorkspaceReadFile = Tool[WorkspaceReadFileArgs, workspacetools.ReadFileResult]{
Tool: aisdk.Tool{
Name: ToolNameWorkspaceReadFile,
Description: workspacetools.ReadFileDescription,
Schema: aisdk.Schema{
Properties: map[string]any{
"workspace": workspaceProperty(),
"path": map[string]any{"type": "string"},
"offset": map[string]any{"type": "integer"},
"limit": map[string]any{"type": "integer"},
},
Required: []string{"workspace", "path"},
},
},
MCPAnnotations: mcpReadOnlyAnnotations,
UserClientOptional: true,
Handler: func(ctx context.Context, deps Deps, args WorkspaceReadFileArgs) (workspacetools.ReadFileResult, error) {
conn, err := openAgentConn(ctx, deps, args.Workspace)
if err != nil {
return workspacetools.ReadFileResult{}, err
}
defer conn.Close()
res, err := workspacetools.ReadFile(ctx, conn, args.Path, args.Offset, args.Limit)
if err != nil {
return workspacetools.ReadFileResult{}, workspaceToolError(err)
}
return res, nil
},
}
// WorkspaceWriteFileArgs are the arguments of WorkspaceWriteFile.
type WorkspaceWriteFileArgs struct {
Workspace string `json:"workspace"`
Path string `json:"path"`
Content string `json:"content"`
}
// WorkspaceWriteFile creates or replaces a workspace file.
var WorkspaceWriteFile = Tool[WorkspaceWriteFileArgs, workspacetools.OKResult]{
Tool: aisdk.Tool{
Name: ToolNameWorkspaceWriteFile,
Description: "Create a file in the workspace or overwrite an existing one with the given content. " +
"Use " + ToolNameWorkspaceEditFiles + " for targeted changes to an existing file.",
Schema: aisdk.Schema{
Properties: map[string]any{
"workspace": workspaceProperty(),
"path": map[string]any{
"type": "string",
"description": "Absolute path of the file to write.",
},
"content": map[string]any{
"type": "string",
"description": workspacetools.WriteFileContentDescription,
},
},
Required: []string{"workspace", "path", "content"},
},
},
MCPAnnotations: mcpDestructiveAnnotations,
UserClientOptional: true,
Handler: func(ctx context.Context, deps Deps, args WorkspaceWriteFileArgs) (workspacetools.OKResult, error) {
path := strings.TrimSpace(args.Path)
if path == "" {
return workspacetools.OKResult{}, xerrors.New("path is required")
}
conn, err := openAgentConn(ctx, deps, args.Workspace)
if err != nil {
return workspacetools.OKResult{}, err
}
defer conn.Close()
if err := conn.WriteFile(ctx, path, strings.NewReader(args.Content)); err != nil {
return workspacetools.OKResult{}, workspaceToolError(err)
}
return workspacetools.OKResult{OK: true}, nil
},
}
// WorkspaceEditFilesArgs are the arguments of WorkspaceEditFiles.
type WorkspaceEditFilesArgs struct {
Workspace string `json:"workspace"`
Files []workspacesdk.FileEdits `json:"files"`
}
// WorkspaceEditFiles applies text replacements to workspace files.
var WorkspaceEditFiles = Tool[WorkspaceEditFilesArgs, workspacetools.EditFilesResult]{
Tool: aisdk.Tool{
Name: ToolNameWorkspaceEditFiles,
Description: workspacetools.EditFilesDescription,
Schema: aisdk.Schema{
Properties: map[string]any{
"workspace": workspaceProperty(),
"files": map[string]any{
"type": "array",
"description": workspacetools.EditFilesFilesDescription,
"items": map[string]any{
"type": "object",
"properties": map[string]any{
"path": map[string]any{
"type": "string",
"description": "The absolute path of the file to edit, for example /home/coder/project/main.go.",
},
"edits": map[string]any{
"type": "array",
"description": "Edits that replace old text with new text, applied to this file in order.",
"items": map[string]any{
"type": "object",
"properties": map[string]any{
"old_text": map[string]any{
"type": "string",
"description": "Existing text in the file to replace. Matching is fuzzy: whitespace and indentation differences are tolerated.",
},
"new_text": map[string]any{
"type": "string",
"description": "Text that replaces old_text.",
},
"replace_all": map[string]any{
"type": "boolean",
"description": "Replace every match of old_text instead of erroring when it matches more than once.",
},
},
"required": []string{"old_text", "new_text"},
},
},
},
"required": []string{"path", "edits"},
},
},
},
Required: []string{"workspace", "files"},
},
},
MCPAnnotations: mcpDestructiveAnnotations,
UserClientOptional: true,
Handler: func(ctx context.Context, deps Deps, args WorkspaceEditFilesArgs) (workspacetools.EditFilesResult, error) {
if err := workspacetools.ValidateFileEdits(args.Files); err != nil {
return workspacetools.EditFilesResult{}, err
}
conn, err := openAgentConn(ctx, deps, args.Workspace)
if err != nil {
return workspacetools.EditFilesResult{}, err
}
defer conn.Close()
res, err := workspacetools.EditFiles(ctx, conn, args.Files)
if err != nil {
return workspacetools.EditFilesResult{}, workspaceToolError(err)
}
return res, nil
},
}
// WorkspaceExecuteArgs are the arguments of WorkspaceExecute.
type WorkspaceExecuteArgs struct {
Workspace string `json:"workspace"`
Command string `json:"command"`
Timeout *string `json:"timeout,omitempty"`
WorkDir *string `json:"workdir,omitempty"`
RunInBackground *bool `json:"run_in_background,omitempty"`
}
// WorkspaceExecute runs a shell command in a workspace.
var WorkspaceExecute = Tool[WorkspaceExecuteArgs, workspacetools.ExecuteResult]{
Tool: aisdk.Tool{
Name: ToolNameWorkspaceExecute,
Description: workspacetools.ExecuteDescription(workspaceToolNames),
Schema: aisdk.Schema{
Properties: map[string]any{
"workspace": workspaceProperty(),
"command": map[string]any{
"type": "string",
"description": workspacetools.ExecuteCommandDescription,
},
"timeout": map[string]any{
"type": "string",
"description": workspacetools.ExecuteTimeoutDescription,
},
"workdir": map[string]any{
"type": "string",
"description": workspacetools.ExecuteWorkDirDescription,
},
"run_in_background": map[string]any{
"type": "boolean",
"description": workspacetools.ExecuteRunInBackgroundDescription(workspaceToolNames),
},
},
Required: []string{"workspace", "command"},
},
},
MCPAnnotations: mcpDestructiveAnnotations,
Handler: func(ctx context.Context, deps Deps, args WorkspaceExecuteArgs) (workspacetools.ExecuteResult, error) {
if args.Command == "" {
return workspacetools.ExecuteResult{}, xerrors.New("command is required")
}
conn, err := openAgentConn(ctx, deps, args.Workspace)
if err != nil {
return workspacetools.ExecuteResult{}, err
}
defer conn.Close()
req := workspacetools.ExecuteRequest{
Command: args.Command,
Timeout: args.Timeout,
RunInBackground: args.RunInBackground != nil && *args.RunInBackground,
MaxTimeout: maxWorkspaceToolWait,
Names: workspaceToolNames,
}
if args.WorkDir != nil {
req.WorkDir = *args.WorkDir
}
return workspacetools.Execute(ctx, conn, req)
},
}
// WorkspaceProcessOutputArgs are the arguments of WorkspaceProcessOutput.
type WorkspaceProcessOutputArgs struct {
Workspace string `json:"workspace"`
ProcessID string `json:"process_id"`
WaitTimeout *string `json:"wait_timeout,omitempty"`
}
// WorkspaceProcessOutput retrieves the output of a workspace process.
var WorkspaceProcessOutput = Tool[WorkspaceProcessOutputArgs, workspacetools.ExecuteResult]{
Tool: aisdk.Tool{
Name: ToolNameWorkspaceProcessOutput,
Description: workspacetools.ProcessOutputDescription(workspaceToolNames),
Schema: aisdk.Schema{
Properties: map[string]any{
"workspace": workspaceProperty(),
"process_id": map[string]any{"type": "string"},
"wait_timeout": map[string]any{
"type": "string",
"description": workspacetools.ProcessOutputWaitTimeoutDescription,
},
},
Required: []string{"workspace", "process_id"},
},
},
MCPAnnotations: mcpReadOnlyAnnotations,
Handler: func(ctx context.Context, deps Deps, args WorkspaceProcessOutputArgs) (workspacetools.ExecuteResult, error) {
if args.ProcessID == "" {
return workspacetools.ExecuteResult{}, xerrors.New("process_id is required")
}
conn, err := openAgentConn(ctx, deps, args.Workspace)
if err != nil {
return workspacetools.ExecuteResult{}, err
}
defer conn.Close()
return workspacetools.ProcessOutput(ctx, conn, workspacetools.ProcessOutputRequest{
ProcessID: args.ProcessID,
WaitTimeout: args.WaitTimeout,
MaxWait: maxWorkspaceToolWait,
})
},
}
// WorkspaceProcessListArgs are the arguments of WorkspaceProcessList.
type WorkspaceProcessListArgs struct {
Workspace string `json:"workspace"`
}
// WorkspaceProcessList lists processes started outside a chat in a
// workspace.
var WorkspaceProcessList = Tool[WorkspaceProcessListArgs, workspacesdk.ListProcessesResponse]{
Tool: aisdk.Tool{
Name: ToolNameWorkspaceProcessList,
Description: workspacetools.ProcessListDescription,
Schema: aisdk.Schema{
Properties: map[string]any{
"workspace": workspaceProperty(),
},
Required: []string{"workspace"},
},
},
MCPAnnotations: mcpReadOnlyAnnotations,
Handler: func(ctx context.Context, deps Deps, args WorkspaceProcessListArgs) (workspacesdk.ListProcessesResponse, error) {
conn, err := openAgentConn(ctx, deps, args.Workspace)
if err != nil {
return workspacesdk.ListProcessesResponse{}, err
}
defer conn.Close()
resp, err := conn.ListProcesses(ctx)
if err != nil {
return workspacesdk.ListProcessesResponse{}, xerrors.Errorf("list processes: %s", workspacetools.AgentAPIErrorMessage(err))
}
return resp, nil
},
}
// WorkspaceProcessSignalArgs are the arguments of WorkspaceProcessSignal.
type WorkspaceProcessSignalArgs struct {
Workspace string `json:"workspace"`
ProcessID string `json:"process_id"`
Signal string `json:"signal"`
}
// WorkspaceProcessSignal terminates or kills a workspace process.
var WorkspaceProcessSignal = Tool[WorkspaceProcessSignalArgs, workspacetools.SignalResult]{
Tool: aisdk.Tool{
Name: ToolNameWorkspaceProcessSignal,
Description: workspacetools.ProcessSignalDescription(workspaceToolNames),
Schema: aisdk.Schema{
Properties: map[string]any{
"workspace": workspaceProperty(),
"process_id": map[string]any{"type": "string"},
"signal": map[string]any{"type": "string"},
},
Required: []string{"workspace", "process_id", "signal"},
},
},
MCPAnnotations: mcpDestructiveAnnotations,
Handler: func(ctx context.Context, deps Deps, args WorkspaceProcessSignalArgs) (workspacetools.SignalResult, error) {
if args.ProcessID == "" {
return workspacetools.SignalResult{}, xerrors.New("process_id is required")
}
if err := workspacetools.ValidateSignal(args.Signal); err != nil {
return workspacetools.SignalResult{}, err
}
conn, err := openAgentConn(ctx, deps, args.Workspace)
if err != nil {
return workspacetools.SignalResult{}, err
}
defer conn.Close()
if err := conn.SignalProcess(ctx, args.ProcessID, args.Signal); err != nil {
return workspacetools.SignalResult{}, xerrors.Errorf("signal process: %s", workspacetools.AgentAPIErrorMessage(err))
}
return workspacetools.SignalSent(args.ProcessID, args.Signal), nil
},
}