Files
coder/coderd/x/chatd/mcpclient/export_test.go
T
Ethan 661b7db7b5 feat(coderd/x/chatd/mcpclient): count only text toward the in-process tool result cap (#31386)
Part of [CODAGT-1430](https://linear.app/codercom/issue/CODAGT-1430).
Stacked on #31385.

Inline and in-process MCP servers cap each tool result at 256 KiB, and
media counts at its base64 size, so a PDF over about 190 KiB becomes an
error. In-process servers are our own code running in coderd, so we'll
count only their text toward the cap, and leave their media to the 10
MiB tool media cap in chatloop. Inline servers are untrusted, so they
still count base64 media.
2026-11-11 00:06:41 +11:00

91 lines
2.7 KiB
Go

package mcpclient
import (
"context"
"net/netip"
"time"
"charm.land/fantasy"
"github.com/google/uuid"
"cdr.dev/slog/v3"
"github.com/coder/safedial"
)
// 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.
func ConnectAllForTest(
ctx context.Context,
logger slog.Logger,
servers []Server,
timeout time.Duration,
reaperDone func(),
) ([]fantasy.AgentTool, []ConnectSummary, func()) {
// Connect-budget tests serve from loopback, which the guard
// blocks by default, so allow it explicitly.
httpClient := NewHTTPClient(nil, safedial.WithAllowedPrefixes(
netip.MustParsePrefix("127.0.0.0/8"),
netip.MustParsePrefix("::1/128"),
))
return connectAllWithHooks(
ctx, logger, servers, nil, uuid.Nil, nil, nil,
connectOptions{
httpClient: httpClient,
timeout: timeout,
hooks: connectHooks{reaperDone: reaperDone},
kind: connectionKindOrg,
},
)
}
// ConnectInlineForTest exposes the inline connect path
// with an injectable connect timeout and a loopback-permitting client.
func ConnectInlineForTest(
ctx context.Context,
logger slog.Logger,
servers []Server,
coderHeaders map[string]string,
timeout time.Duration,
) ([]fantasy.AgentTool, []ConnectSummary, func()) {
httpClient := NewHTTPClient(nil, safedial.WithAllowedPrefixes(
netip.MustParsePrefix("127.0.0.0/8"),
netip.MustParsePrefix("::1/128"),
))
return connectAllWithHooks(
ctx, logger, servers, nil, uuid.Nil, nil, coderHeaders,
connectOptions{
httpClient: inlineHTTPClient(httpClient),
timeout: timeout,
kind: connectionKindInline,
},
)
}
// ToolCallIDMetaKeyForTest exposes the _meta key for external tests.
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
// BuildAuthHeadersForTest exposes buildAuthHeaders for external
// tests.
var BuildAuthHeadersForTest = buildAuthHeaders
// SummaryErrorForTest renders err as an org server's persisted connect
// error.
func SummaryErrorForTest(err error) string {
return summaryError(connectionKindOrg, secretRedactor{}, err)
}
// MaxSummaryErrorLenForTest exposes the persisted-error byte cap for
// external tests.
const MaxSummaryErrorLenForTest = maxSummaryErrorLen