Relates to CODAGT-1330 When chatd prepares a turn, it reads a bunch of things from the database: the user's custom prompt, plan mode instructions, personal skills, the advisor config, the user's compaction threshold override, MCP server configs, inline MCP servers, MCP user tokens and the pinned workspace MCP tools. If any of those reads failed, we'd log a warning and run the turn without them, so the model would quietly ignore the user's instructions or be missing tools for that turn. To fix, we'll just return the error instead, so the prepare retry runs, and the chat shows an error if the reads keep failing. The provider switch history sanitizer and the cancel of deleted tool calls also return their database errors now. A model config that's unavailable, e.g. deleted, is still treated as foreign by the sanitizer like it is today. An inline MCP server with invalid stored headers still shows up as a failed connect in the chat debug panel, but a failed read of the inline MCP servers themselves now fails the turn. This PR is in a stack on top of #30449.
69 lines
2.4 KiB
Go
69 lines
2.4 KiB
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/mcpclient"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// inlineMCPServersEnabled reports whether this deployment loads and
|
|
// connects inline MCP servers at turn time. Both the experiment
|
|
// and the caller-supplied tools kill switch must allow it.
|
|
func (server *Server) inlineMCPServersEnabled() bool {
|
|
return !server.disableCallerSuppliedTools &&
|
|
server.experiments.Enabled(codersdk.ExperimentChatInlineMCPServers)
|
|
}
|
|
|
|
// loadInlineMCPServers returns the inline MCP servers that apply to
|
|
// this turn. Child chats read the root chat's rows and keep only those
|
|
// allowed in subagents. A row with invalid stored headers is a connect
|
|
// outcome, so it is returned as a failed ConnectSummary row: the chat
|
|
// debug panel shows it beside connection failures.
|
|
func (server *Server) loadInlineMCPServers(ctx context.Context, chat database.Chat) ([]mcpclient.Server, []mcpclient.ConnectSummary, error) {
|
|
rootChatID := chat.ID
|
|
if chat.RootChatID.Valid {
|
|
rootChatID = chat.RootChatID.UUID
|
|
}
|
|
rows, err := server.db.GetChatMCPServersByChatID(ctx, rootChatID)
|
|
if err != nil {
|
|
return nil, nil, xerrors.Errorf("get inline MCP servers: %w", err)
|
|
}
|
|
|
|
servers := make([]mcpclient.Server, 0, len(rows))
|
|
var failures []mcpclient.ConnectSummary
|
|
for _, row := range rows {
|
|
if chat.ParentChatID.Valid && !row.AllowInSubagents {
|
|
continue
|
|
}
|
|
var headers map[string]string
|
|
if err := json.Unmarshal([]byte(row.Headers), &headers); err != nil {
|
|
server.logger.Warn(ctx, "skipping inline MCP server with invalid stored headers",
|
|
slog.F("chat_id", chat.ID), slog.F("server_slug", row.Slug), slog.Error(err))
|
|
failures = append(failures, mcpclient.ConnectSummary{
|
|
ConfigID: row.ID,
|
|
Slug: row.Slug,
|
|
Outcome: mcpclient.ConnectOutcomeError,
|
|
Error: "invalid stored headers",
|
|
})
|
|
continue
|
|
}
|
|
servers = append(servers, mcpclient.Server{
|
|
ID: row.ID,
|
|
Slug: row.Slug,
|
|
URL: row.Url,
|
|
Transport: mcpclient.TransportStreamableHTTP,
|
|
Headers: headers,
|
|
ForwardCoderHeaders: row.ForwardCoderHeaders,
|
|
ToolAllowList: row.ToolAllowList,
|
|
ToolDenyList: row.ToolDenyList,
|
|
})
|
|
}
|
|
return servers, failures, nil
|
|
}
|