Files
coder/coderd/x/chatd/chattool/mcpworkspace.go
T
Kyle Carberry 58f70b4488 fix(coderd/x/chatd): sanitize workspace MCP tool names (#26928)
## Summary

Workspace MCP tools (servers a workspace declares in `.mcp.json`) take
their model-facing name from the server key joined with the tool name as
`serverName__toolName`. That name reached the model **unsanitized**, so
a server or tool name containing a character outside
`^[a-zA-Z0-9_-]{1,128}$` (for example `@`) produced an invalid tool
name. Anthropic and Bedrock reject the whole request with `HTTP 400`:

```
tools.N.custom.name: String should match pattern '^[a-zA-Z0-9_-]{1,128}$'
```

which fails the entire turn, not just the one tool. The remote MCP path
(`mcpclient`) and the AI Gateway path (`aibridge/mcp`) already sanitize;
the workspace path did not.

Alternative to #26853 (thanks @ibdafna for the report and repro).

## Fix

Sanitize and length-cap the **model-facing** name, and keep the original
`serverName__toolName` as a `routingName` the workspace agent uses to
reach the original server and tool. `NewWorkspaceMCPTools` builds a
whole set and disambiguates names that collide after sanitization (for
example server keys `foo.bar` and `foo_bar` both exposing `echo`) so
every tool stays addressable in the model's name-keyed dispatch map.
Names already within the allowed set are unchanged, so there is no
behavior change for valid names.

The sanitizer is local to `coderd/x/chatd/chattool`; the fix does
**not** touch the `aibridge` package or the remote MCP client.

### Changes
- `coderd/x/chatd/chattool/mcpworkspace.go`: local provider-safe
sanitizer + length cap, `routingName` for the agent proxy, and
`NewWorkspaceMCPTools` for set-level collision disambiguation.
- `coderd/x/chatd/chatd.go`: build the pinned workspace tool set via
`NewWorkspaceMCPTools`.

## Why sanitize here (not at `.mcp.json` / agent parse)?

The agent uses `serverName__toolName` to route to the real downstream
server (it splits on `__` and calls the original tool name), so
sanitizing at parse time would break routing or merely relocate the
original->sanitized mapping. Sanitization is also a provider constraint
the agent has no knowledge of, and coderd/agent version skew means
coderd must sanitize at its own boundary regardless. The model-facing
boundary in chatd is the right place.

## Test plan
- `@` in a name is sanitized for the model while the original routes to
the agent; a valid name is unchanged; an over-length name is truncated;
colliding names in a set are disambiguated while each still routes to
its own original name.
- `go build`, `go vet`, `golangci-lint`, and `go test
./coderd/x/chatd/chattool/...` pass locally.

<details>
<summary>Design notes / decision log</summary>

**Constraint that drives the design.** The tool name is both the
identifier shown to the model (and the key the model layer dispatches
tool calls by) and, for the workspace path, the string the agent splits
on `__` to route back to the original server and tool. Those roles
conflict once sanitization changes the name, so the name is sanitized
for the model while the unsanitized form is kept as `routingName`.

**Options considered.**
1. **Chosen:** sanitize in the workspace path only, with helpers local
to `chattool`. Smallest blast radius; no new cross-package dependency.
This matches the shape of the other MCP paths (`mcpclient` keeps
`originalName` + `configID`) without sharing code.
2. Sanitize at `.mcp.json` parse time or in the agent. Rejected: breaks
routing (the agent needs the original name), pushes a provider concern
into the agent, and coderd must still defend its own boundary because
the agent and coderd version independently. Tool names also come from
the downstream server at list time, not from `.mcp.json`, so parsing
cannot fully validate them.
3. Extract a shared sanitize/truncate/dedupe helper into `aibridge/mcp`
and adopt it in `mcpclient` too (so the remote path also gains collision
disambiguation). This DRYs all paths, but it grows chatd's coupling to
the `aibridge` subsystem and expands scope/behavior/tests in the remote
path for what is a workspace-path bug. Left out deliberately to keep
this change minimal and self-contained; it can be a separate refactor.
4. Sanitize once at the provider serialization boundary (chat loop). The
only truly generic spot, but the model dispatches by name, so it needs a
reverse (sanitized -> original) mapping and set-wide collision handling
in the model layer. Larger, riskier change.

**Notes.**
- The workspace path defines its own sanitizer (`[^a-zA-Z0-9_-]` -> `_`)
and a `maxModelToolNameLen = 64` constant that mirrors the strictest
provider limit (OpenAI 64, Bedrock 128), rather than importing
`aibridge/mcp`, so it carries no new dependency.
- The set builder sorts before assigning suffixes so disambiguation is
stable across turns.

</details>

---

_Opened by Coder Agents on behalf of @kylecarbs. Alternative to #26853._
2026-07-01 20:34:25 +02:00

276 lines
8.6 KiB
Go

package chattool
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"regexp"
"slices"
"strconv"
"strings"
"charm.land/fantasy"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)
// modelToolNameSanitizer matches characters that LLM providers reject in tool
// names. Anthropic and Bedrock require ^[a-zA-Z0-9_-]{1,128}$, and OpenAI
// enforces a 64-character cap over a similar set. A single invalid name would
// otherwise 400 the entire inference request, failing the whole turn.
var modelToolNameSanitizer = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
// maxModelToolNameLen is the strictest provider tool-name length limit
// (OpenAI allows 64, Bedrock 128); we cap at the lower bound so names are safe
// for every provider.
const maxModelToolNameLen = 64
// WorkspaceMCPTool wraps a single MCP tool discovered in a
// workspace, proxying calls through the workspace agent
// connection. It implements fantasy.AgentTool so it can be
// registered alongside built-in chat tools.
type WorkspaceMCPTool struct {
info fantasy.ToolInfo
// routingName is the unsanitized "serverName__toolName" form the
// workspace agent expects: it splits on "__" to locate the server and
// calls the original tool name. info.Name is the sanitized, provider-safe
// name shown to the model, so the two can differ when the server or tool
// name contains characters outside the provider's allowed set.
routingName string
getConn func(context.Context) (workspacesdk.AgentConn, error)
providerOpts fantasy.ProviderOptions
invalidateCache func()
}
// NewWorkspaceMCPTool creates a single tool wrapper from an MCPToolInfo
// discovered on a workspace agent. Each tool proxies calls back through the
// agent connection. The optional invalidateCache callback is invoked when
// CallMCPTool returns a 404 error, indicating that the server was removed and
// the chat's cached tool list should be dropped.
//
// The model-facing name is sanitized to the provider-safe character set and
// length so a server or tool name containing a character such as "@" cannot
// produce an invalid tool name that the provider rejects. The unsanitized name
// is retained as routingName so the workspace agent can still route the call to
// the original server and tool.
//
// Prefer NewWorkspaceMCPTools when building a set of tools, because that path
// also disambiguates names that collide after sanitization. This single-tool
// constructor cannot detect collisions on its own.
func NewWorkspaceMCPTool(
tool workspacesdk.MCPToolInfo,
getConn func(context.Context) (workspacesdk.AgentConn, error),
invalidateCache func(),
) *WorkspaceMCPTool {
return buildWorkspaceMCPTool(tool, sanitizeModelToolName(tool.Name), getConn, invalidateCache)
}
// NewWorkspaceMCPTools builds wrappers for a set of workspace MCP tools.
// Because the model-facing name is sanitized and length-capped, two distinct
// servers or tools can normalize to the same string (for example server keys
// "foo.bar" and "foo_bar" each exposing "echo", or names that share the first
// maxModelToolNameLen bytes). Duplicate names would be sent to the provider,
// which can reject the request, and the model's name-keyed dispatch would make
// one tool unreachable. To keep every tool addressable, colliding model-facing
// names are disambiguated with a numeric suffix while each tool keeps its own
// original routing name. Tools are sorted by routing name first so the suffix
// assignment is stable across turns.
func NewWorkspaceMCPTools(
infos []workspacesdk.MCPToolInfo,
getConn func(context.Context) (workspacesdk.AgentConn, error),
invalidateCache func(),
) []fantasy.AgentTool {
sorted := slices.Clone(infos)
slices.SortFunc(sorted, func(a, b workspacesdk.MCPToolInfo) int {
return strings.Compare(a.Name, b.Name)
})
tools := make([]fantasy.AgentTool, 0, len(sorted))
seen := make(map[string]struct{}, len(sorted))
for _, info := range sorted {
modelName := uniqueModelToolName(sanitizeModelToolName(info.Name), seen)
tools = append(tools, buildWorkspaceMCPTool(info, modelName, getConn, invalidateCache))
}
return tools
}
func buildWorkspaceMCPTool(
tool workspacesdk.MCPToolInfo,
modelName string,
getConn func(context.Context) (workspacesdk.AgentConn, error),
invalidateCache func(),
) *WorkspaceMCPTool {
required := tool.Required
if required == nil {
required = []string{}
}
return &WorkspaceMCPTool{
info: fantasy.ToolInfo{
Name: modelName,
Description: tool.Description,
Parameters: tool.Schema,
Required: required,
Parallel: true,
},
routingName: tool.Name,
getConn: getConn,
invalidateCache: invalidateCache,
}
}
// sanitizeModelToolName returns the provider-safe form of a workspace MCP tool
// name: characters outside [a-zA-Z0-9_-] become "_" and the result is capped
// at maxModelToolNameLen. The "__" server/tool separator survives because
// underscores are already in the allowed set.
func sanitizeModelToolName(name string) string {
sanitized := modelToolNameSanitizer.ReplaceAllString(name, "_")
if len(sanitized) > maxModelToolNameLen {
sanitized = sanitized[:maxModelToolNameLen]
}
return sanitized
}
// uniqueModelToolName returns name when it is unused; otherwise it appends an
// incrementing "_N" suffix (starting at 2), truncating the base so the result
// stays within maxModelToolNameLen, until it finds a name absent from seen.
// The returned name is recorded in seen.
func uniqueModelToolName(name string, seen map[string]struct{}) string {
if _, ok := seen[name]; !ok {
seen[name] = struct{}{}
return name
}
for i := 2; ; i++ {
suffix := "_" + strconv.Itoa(i)
base := name
if len(base)+len(suffix) > maxModelToolNameLen {
cut := maxModelToolNameLen - len(suffix)
if cut < 0 {
cut = 0
}
base = base[:cut]
}
candidate := base + suffix
if _, ok := seen[candidate]; !ok {
seen[candidate] = struct{}{}
return candidate
}
}
}
func (t *WorkspaceMCPTool) Info() fantasy.ToolInfo {
return t.info
}
func (t *WorkspaceMCPTool) Run(
ctx context.Context,
params fantasy.ToolCall,
) (fantasy.ToolResponse, error) {
conn, err := t.getConn(ctx)
if err != nil {
return fantasy.NewTextErrorResponse(
"workspace connection failed: " + err.Error(),
), nil
}
var args map[string]any
if params.Input != "" {
if err := json.Unmarshal(
[]byte(params.Input), &args,
); err != nil {
return fantasy.NewTextErrorResponse(
"invalid JSON input: " + err.Error(),
), nil
}
}
resp, err := conn.CallMCPTool(ctx, workspacesdk.CallMCPToolRequest{
ToolName: t.routingName,
Arguments: args,
})
if err != nil {
// If the agent returns a 404 (ErrUnknownServer), the
// server was removed or renamed. Invalidate the chat's
// cached tool list so the next turn refetches.
var coderErr *codersdk.Error
if errors.As(err, &coderErr) && coderErr.StatusCode() == http.StatusNotFound {
if t.invalidateCache != nil {
t.invalidateCache()
}
}
return fantasy.NewTextErrorResponse(err.Error()), nil
}
return convertMCPToolResponse(resp), nil
}
func (t *WorkspaceMCPTool) ProviderOptions() fantasy.ProviderOptions {
return t.providerOpts
}
func (t *WorkspaceMCPTool) SetProviderOptions(
opts fantasy.ProviderOptions,
) {
t.providerOpts = opts
}
// convertMCPToolResponse translates a workspace agent MCP tool
// response into a fantasy.ToolResponse. Text content blocks are
// collected and joined; binary content (image/media) is returned
// only when no text is available, matching the mcpclient
// conversion strategy.
func convertMCPToolResponse(
resp workspacesdk.CallMCPToolResponse,
) fantasy.ToolResponse {
var (
textParts []string
binaryResult *fantasy.ToolResponse
)
for _, c := range resp.Content {
switch c.Type {
case "text":
textParts = append(textParts, strings.ToValidUTF8(c.Text, "\uFFFD"))
case "image", "audio":
if c.Data == "" {
continue
}
data, err := base64.StdEncoding.DecodeString(c.Data)
if err != nil {
textParts = append(textParts,
"[binary decode error: "+err.Error()+"]",
)
continue
}
if binaryResult == nil {
r := fantasy.ToolResponse{
Type: c.Type,
Data: data,
MediaType: c.MediaType,
IsError: resp.IsError,
}
binaryResult = &r
}
default:
textParts = append(textParts, strings.ToValidUTF8(c.Text, "\uFFFD"))
}
}
// Prefer text content. Only fall back to binary when no
// text was collected.
if len(textParts) > 0 {
r := fantasy.NewTextResponse(
strings.Join(textParts, "\n"),
)
r.IsError = resp.IsError
return r
}
if binaryResult != nil {
return *binaryResult
}
r := fantasy.NewTextResponse("")
r.IsError = resp.IsError
return r
}