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
4 changes: 4 additions & 0 deletions coderd/x/chatd/mcpclient/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const ToolCallIDMetaKeyForTest = toolCallIDMetaKey
// MaxInlineToolResultBytesForTest exposes the result cap.
const MaxInlineToolResultBytesForTest = maxInlineToolResultBytes

// MaxInProcessToolResultTextBytesForTest exposes the in-process result
// text cap.
const MaxInProcessToolResultTextBytesForTest = maxInProcessToolResultTextBytes

// MaxInlineHTTPResponseBytesForTest exposes the body cap.
const MaxInlineHTTPResponseBytesForTest = maxInlineHTTPResponseBytes

Expand Down
54 changes: 54 additions & 0 deletions coderd/x/chatd/mcpclient/inprocess_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package mcpclient_test

import (
"bytes"
"context"
"slices"
"strings"
"testing"

"charm.land/fantasy"
Expand Down Expand Up @@ -61,3 +64,54 @@ func TestConnectInProcess_RejectsTooManyTools(t *testing.T) {
require.Equal(t, mcpclient.ConnectOutcomeError, summaries[0].Outcome)
require.Contains(t, summaries[0].Error, "maximum is 64")
}

func TestConnectInProcess_ToolResultCap(t *testing.T) {
t.Parallel()

const maxBytes = mcpclient.MaxInProcessToolResultTextBytesForTest
for _, tc := range []struct {
name string
result *mcp.CallToolResult
wantError bool
}{
{name: "MediaOverCap", result: &mcp.CallToolResult{Content: []mcp.Content{
&mcp.TextContent{Text: "report.pdf"},
&mcp.EmbeddedResource{Resource: &mcp.ResourceContents{
URI: "file:///report.pdf",
MIMEType: "application/pdf",
Blob: bytes.Repeat([]byte("a"), maxBytes),
}},
}}},
{name: "TextOverCap", result: textToolResult(strings.Repeat("a", maxBytes+1)), wantError: true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})

srv := mcp.NewServer(&mcp.Implementation{Name: "test-server", Version: "1.0.0"}, nil)
srv.AddTool(
&mcp.Tool{Name: "get_file", InputSchema: map[string]any{"type": "object"}},
func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return tc.result, nil
},
)
cfg := mcpclient.InProcessServer{ID: uuid.New(), Slug: "bot", Server: srv}

tools, _, cleanup := mcpclient.ConnectInProcess(ctx, logger, []mcpclient.InProcessServer{cfg})
t.Cleanup(cleanup)
require.Len(t, tools, 1)

resp, err := tools[0].Run(ctx, fantasy.ToolCall{ID: "call-1", Input: "{}"})
require.NoError(t, err)
require.Equal(t, tc.wantError, resp.IsError)
if tc.wantError {
require.Contains(t, resp.Content, "exceeded maximum size")
return
}
require.Equal(t, "report.pdf", resp.Content)
require.Equal(t, "application/pdf", resp.MediaType)
require.Len(t, resp.Data, maxBytes)
})
}
}
28 changes: 20 additions & 8 deletions coderd/x/chatd/mcpclient/mcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func connectOne(
}

tools = append(
tools, newMCPTool(srv.ID, srv.Slug, mcpTool, session, srv.ModelIntent, redactor, limits.resultBytes),
tools, newMCPTool(srv.ID, srv.Slug, mcpTool, session, srv.ModelIntent, redactor, limits.resultBytes, limits.resultTextOnly),
)
}

Expand Down Expand Up @@ -668,20 +668,23 @@ const (
maxInlineToolResultBytes = 256 << 10
)

// Cap model-visible tool definitions and persisted tool results even
// for in-process servers.
// Cap model-visible tool definitions and persisted tool result text even
// for in-process servers. Their media is trusted, so only the chatloop
// tool media cap bounds it.
const (
maxInProcessTools = 64
maxInProcessToolDefinitionBytes = 64 << 10
maxInProcessToolDefinitionsBytes = 256 << 10
maxInProcessToolResultBytes = 256 << 10
maxInProcessToolResultTextBytes = 256 << 10
)

type toolLimits struct {
tools int
definitionBytes int
definitionsBytes int
resultBytes int
// resultTextOnly excludes media from resultBytes.
resultTextOnly bool
}

func (kind connectionKind) toolLimits() (toolLimits, bool) {
Expand All @@ -698,7 +701,8 @@ func (kind connectionKind) toolLimits() (toolLimits, bool) {
tools: maxInProcessTools,
definitionBytes: maxInProcessToolDefinitionBytes,
definitionsBytes: maxInProcessToolDefinitionsBytes,
resultBytes: maxInProcessToolResultBytes,
resultBytes: maxInProcessToolResultTextBytes,
resultTextOnly: true,
}, true
default:
return toolLimits{}, false
Expand Down Expand Up @@ -1192,7 +1196,9 @@ type mcpToolWrapper struct {
session *mcp.ClientSession
redactor secretRedactor
// maxResultBytes caps the serialized tool result; 0 means no cap.
maxResultBytes int
maxResultBytes int
// resultTextOnly excludes media from maxResultBytes.
resultTextOnly bool
providerOptions fantasy.ProviderOptions
}

Expand All @@ -1212,6 +1218,7 @@ func newMCPTool(
modelIntent bool,
redactor secretRedactor,
maxResultBytes int,
resultTextOnly bool,
) *mcpToolWrapper {
properties, required := splitInputSchema(tool.InputSchema)
// Model-visible fields are redacted once here. originalName is
Expand All @@ -1228,6 +1235,7 @@ func newMCPTool(
session: session,
redactor: redactor,
maxResultBytes: maxResultBytes,
resultTextOnly: resultTextOnly,
}
}

Expand Down Expand Up @@ -1336,8 +1344,12 @@ func (t *mcpToolWrapper) Run(
}
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 {
resultBytes := len(resp.Content)
if !t.resultTextOnly {
// Measure the result as the model receives it: Data is sent base64-encoded.
resultBytes += len(resp.MediaType) + base64.StdEncoding.EncodedLen(len(resp.Data))
}
if t.maxResultBytes > 0 && resultBytes > t.maxResultBytes {
return fantasy.NewTextErrorResponse(fmt.Sprintf(
"tool result exceeded maximum size of %d bytes", t.maxResultBytes,
)), nil
Expand Down
Loading