Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions agent/x/agentmcp/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agentmcp
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/fs"
Expand Down Expand Up @@ -740,6 +741,13 @@ func (m *Manager) CallTool(ctx context.Context, req workspacesdk.CallMCPToolRequ
return workspacesdk.CallMCPToolResponse{}, xerrors.Errorf("call tool %q on %q: %w", originalName, serverName, err)
}

if req.IncludeResult {
resp, err := rawResult(result)
if err != nil {
return workspacesdk.CallMCPToolResponse{}, xerrors.Errorf("encode result of tool %q on %q: %w", originalName, serverName, err)
}
return resp, nil
}
return convertResult(result), nil
}

Expand Down Expand Up @@ -1013,6 +1021,23 @@ func splitToolName(prefixed string) (serverName, toolName string, err error) {
return server, tool, nil
}

// rawResult returns the whole MCP CallToolResult in MCP wire format, so
// coderd converts every content type itself. Content stays empty so media
// is not sent twice.
func rawResult(result *mcp.CallToolResult) (workspacesdk.CallMCPToolResponse, error) {
if result == nil {
return workspacesdk.CallMCPToolResponse{}, nil
}
raw, err := json.Marshal(result)
if err != nil {
return workspacesdk.CallMCPToolResponse{}, xerrors.Errorf("marshal result: %w", err)
}
return workspacesdk.CallMCPToolResponse{
Result: raw,
IsError: result.IsError,
}, nil
}

// convertResult translates an MCP CallToolResult into a
// workspacesdk.CallMCPToolResponse. It iterates over content
// items and maps each recognized type.
Expand Down
30 changes: 30 additions & 0 deletions agent/x/agentmcp/manager_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,36 @@ func TestConvertResult(t *testing.T) {
}
}

// TestRawResult verifies that the raw path keeps the content types that the
// legacy Content path drops, and does not send media twice.
func TestRawResult(t *testing.T) {
t.Parallel()

got, err := rawResult(&mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "report"},
&mcp.EmbeddedResource{Resource: &mcp.ResourceContents{
URI: "file:///report.pdf",
MIMEType: "application/pdf",
Blob: []byte("pdf-data"),
}},
},
StructuredContent: map[string]any{"pages": 2},
IsError: true,
})
require.NoError(t, err)
assert.Empty(t, got.Content)
assert.True(t, got.IsError)
require.JSONEq(t, `{
"content": [
{"type": "text", "text": "report"},
{"type": "resource", "resource": {"uri": "file:///report.pdf", "mimeType": "application/pdf", "blob": "cGRmLWRhdGE="}}
],
"structuredContent": {"pages": 2},
"isError": true
}`, string(got.Result))
}

// TestConnectServer_StdioProcessSurvivesConnect verifies that a stdio MCP
// server subprocess remains alive after connectServer returns. This is a
// regression test for a bug where the subprocess was tied to a short-lived
Expand Down
80 changes: 36 additions & 44 deletions coderd/x/chatd/chattool/mcpworkspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"strings"

"charm.land/fantasy"
"github.com/modelcontextprotocol/go-sdk/mcp"

"github.com/coder/coder/v2/coderd/x/chatd/mcpclient"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)

Expand Down Expand Up @@ -167,14 +169,21 @@ func (t *WorkspaceMCPTool) Run(
}

resp, err := conn.CallMCPTool(ctx, workspacesdk.CallMCPToolRequest{
ToolName: t.routingName,
Arguments: args,
ToolName: t.routingName,
Arguments: args,
IncludeResult: true,
})
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}

return convertMCPToolResponse(resp), nil
result, err := mcpCallResult(resp)
if err != nil {
return fantasy.NewTextErrorResponse(
"decode workspace MCP tool result: " + err.Error(),
), nil
}
return mcpclient.ConvertCallResult(result), nil
}

func (t *WorkspaceMCPTool) ProviderOptions() fantasy.ProviderOptions {
Expand All @@ -187,55 +196,38 @@ func (t *WorkspaceMCPTool) SetProviderOptions(
t.providerOpts = opts
}

// fantasy permits one media payload per response, so only the first eligible
// binary block is kept alongside the text.
func convertMCPToolResponse(
// mcpCallResult returns the MCP result from an agent response. Agents
// that predate IncludeResult send only Content, which is adapted to MCP
// content values.
func mcpCallResult(
resp workspacesdk.CallMCPToolResponse,
) fantasy.ToolResponse {
var (
textParts []string
binaryResult *fantasy.ToolResponse
)
) (*mcp.CallToolResult, error) {
if len(resp.Result) > 0 {
var result *mcp.CallToolResult
if err := json.Unmarshal(resp.Result, &result); err != nil {
return nil, err
}
return result, nil
}

result := &mcp.CallToolResult{IsError: resp.IsError}
for _, c := range resp.Content {
var item mcp.Content
switch c.Type {
case "text":
textParts = append(textParts, strings.ToValidUTF8(c.Text, "\uFFFD"))
case "image", "audio":
if c.Data == "" || c.MediaType == "" {
continue
}
data, err := base64.StdEncoding.DecodeString(c.Data)
if err != nil {
textParts = append(textParts,
"[binary decode error: "+err.Error()+"]",
)
continue
}
if binaryResult == nil {
responseType := c.Type
if c.Type == "audio" {
// chatloop only recognizes "image" and "media".
responseType = "media"
}
r := fantasy.ToolResponse{
Type: responseType,
Data: data,
MediaType: c.MediaType,
IsError: resp.IsError,
}
binaryResult = &r
switch {
case err != nil:
item = &mcp.TextContent{Text: "[binary decode error: " + err.Error() + "]"}
case c.Type == "image":
item = &mcp.ImageContent{Data: data, MIMEType: c.MediaType}
default:
item = &mcp.AudioContent{Data: data, MIMEType: c.MediaType}
}
default:
textParts = append(textParts, strings.ToValidUTF8(c.Text, "\uFFFD"))
item = &mcp.TextContent{Text: c.Text}
}
result.Content = append(result.Content, item)
}

if binaryResult != nil {
binaryResult.Content = strings.Join(textParts, "\n")
return *binaryResult
}
r := fantasy.NewTextResponse(strings.Join(textParts, "\n"))
r.IsError = resp.IsError
return r
return result, nil
}
26 changes: 25 additions & 1 deletion coderd/x/chatd/chattool/mcpworkspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chattool_test
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -118,6 +119,7 @@ func TestWorkspaceMCPTool_ConvertsMixedContent(t *testing.T) {

image := []byte{0x89, 'P', 'N', 'G', 1, 2, 3}
audio := []byte("wav-bytes")
pdf := []byte("%PDF-1.7 report")
encoded := func(b []byte) string { return base64.StdEncoding.EncodeToString(b) }
text := func(s string) workspacesdk.MCPToolContent { return workspacesdk.MCPToolContent{Type: "text", Text: s} }
png := workspacesdk.MCPToolContent{Type: "image", Data: encoded(image), MediaType: "image/png"}
Expand Down Expand Up @@ -163,6 +165,27 @@ func TestWorkspaceMCPTool_ConvertsMixedContent(t *testing.T) {
resp: workspacesdk.CallMCPToolResponse{IsError: true, Content: []workspacesdk.MCPToolContent{text("boom"), png}},
want: fantasy.ToolResponse{Type: "image", Data: image, MediaType: "image/png", Content: "boom", IsError: true},
},
{
// Agents that predate IncludeResult replace embedded resources
// with a placeholder and drop structured content.
name: "LegacyEmbeddedResourcePlaceholderStaysText",
resp: workspacesdk.CallMCPToolResponse{Content: []workspacesdk.MCPToolContent{
text("Report ready"),
{Type: "resource", Text: "[embedded resource: *mcp.ResourceContents]"},
}},
want: fantasy.ToolResponse{Type: "text", Content: "Report ready\n[embedded resource: *mcp.ResourceContents]"},
},
{
name: "ResultKeepsEmbeddedPDFAndStructuredContent",
resp: workspacesdk.CallMCPToolResponse{Result: json.RawMessage(`{
"content": [
{"type": "text", "text": "Report ready"},
{"type": "resource", "resource": {"uri": "file:///report.pdf", "mimeType": "application/pdf", "blob": "` + encoded(pdf) + `"}}
],
"structuredContent": {"pages": 2}
}`)},
want: fantasy.ToolResponse{Type: "media", Data: pdf, MediaType: "application/pdf", Filename: "report.pdf", Content: "Report ready\n{\"pages\":2}"},
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
Expand All @@ -171,7 +194,8 @@ func TestWorkspaceMCPTool_ConvertsMixedContent(t *testing.T) {
[]workspacesdk.MCPToolInfo{{Name: "browser__take_screenshot"}},
func(context.Context) (workspacesdk.AgentConn, error) {
return &fakeAgentConn{
callMCPToolFunc: func(context.Context, workspacesdk.CallMCPToolRequest) (workspacesdk.CallMCPToolResponse, error) {
callMCPToolFunc: func(_ context.Context, req workspacesdk.CallMCPToolRequest) (workspacesdk.CallMCPToolResponse, error) {
assert.True(t, req.IncludeResult, "coderd must ask for the MCP result")
return tc.resp, nil
},
}, nil
Expand Down
4 changes: 0 additions & 4 deletions coderd/x/chatd/mcpclient/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
"github.com/coder/safedial"
)

// ConvertCallResultForTest exposes convertCallResult for external
// tests.
var ConvertCallResultForTest = convertCallResult

// ConnectAllForTest exposes connectAll with an injectable connect
// timeout and a reaperDone hook that fires after an abandoned
// connect goroutine has been drained and its late session closed.
Expand Down
10 changes: 6 additions & 4 deletions coderd/x/chatd/mcpclient/mcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -1329,12 +1329,12 @@ func (t *mcpToolWrapper) Run(
if err != nil {
resp = fantasy.NewTextErrorResponse(t.redactor.redactString(err.Error()))
} else {
// Structured content is redacted before convertCallResult encodes it
// Structured content is redacted before ConvertCallResult encodes it
// as JSON, where escaping would hide a secret from the string match.
if result != nil {
result.StructuredContent = t.redactor.redactValue(result.StructuredContent)
}
resp = t.redactor.redactResponse(convertCallResult(result))
resp = t.redactor.redactResponse(ConvertCallResult(result))
}
// Measure the result as the model receives it: Data is sent base64-encoded.
if t.maxResultBytes > 0 && len(resp.Content)+len(resp.MediaType)+base64.StdEncoding.EncodedLen(len(resp.Data)) > t.maxResultBytes {
Expand Down Expand Up @@ -1385,9 +1385,11 @@ func unwrapModelIntent(input string) string {
return input
}

// ConvertCallResult converts an MCP tool result into a fantasy tool response.
// fantasy permits one media payload per response, so only the first eligible
// binary block is kept alongside the text.
func convertCallResult(
// binary block is kept alongside the text. Callers apply redaction and size
// limits.
func ConvertCallResult(
result *mcp.CallToolResult,
) fantasy.ToolResponse {
if result == nil {
Expand Down
4 changes: 2 additions & 2 deletions coderd/x/chatd/mcpclient/mcpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ func TestConvertCallResult_MixedContent(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

require.Equal(t, tc.want, mcpclient.ConvertCallResultForTest(tc.result))
require.Equal(t, tc.want, mcpclient.ConvertCallResult(tc.result))
})
}
}
Expand Down Expand Up @@ -1810,7 +1810,7 @@ func TestConvertCallResult_UTF8Sanitization(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

resp := mcpclient.ConvertCallResultForTest(tt.result)
resp := mcpclient.ConvertCallResult(tt.result)

require.True(t, utf8.ValidString(resp.Content),
"response content must be valid UTF-8")
Expand Down
7 changes: 7 additions & 0 deletions codersdk/workspacesdk/agentconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1474,12 +1474,19 @@ type CallMCPToolRequest struct {
ToolName string `json:"tool_name"`
// Arguments is the tool input as key-value pairs.
Arguments map[string]any `json:"arguments"`
// IncludeResult asks the agent to return the MCP result in Result
// instead of Content. Older agents ignore it and return Content.
IncludeResult bool `json:"include_result,omitempty"`
}

// CallMCPToolResponse is the response from a proxied MCP tool call.
type CallMCPToolResponse struct {
Content []MCPToolContent `json:"content"`
IsError bool `json:"is_error"`
// Result is the MCP CallToolResult in MCP wire format. The agent sets
// it, and leaves Content empty, only when the request sets
// IncludeResult.
Result json.RawMessage `json:"result,omitempty"`
}

// MCPToolContent is a single content block in an MCP tool response.
Expand Down
5 changes: 5 additions & 0 deletions docs/coder-agents/extending-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ Every workspace MCP tool call is proxied back through the workspace agent, so a

Tool names are prefixed with the server name as `serverName__toolName` to avoid collisions between servers and with built-in tools.

### Tool results

A tool result can include images and PDFs.
For how the model receives them, refer to [Tool results](./platform-controls/mcp-servers.md#tool-results).

### Timeouts

- **Server connect**:
Expand Down
Loading