fix: strip deleted MCP IDs from chats on delete (#25763)

Adds a database migration that reconciles existing stale chat MCP server
IDs, then installs a `BEFORE DELETE` trigger on `mcp_server_configs` to
remove the deleted ID from `chats.mcp_server_ids`. This keeps chat
continuation from failing with `400 One or more MCP server IDs are
invalid` after an MCP server config is deleted.

This matches the existing repo precedent in
`coderd/database/migrations/000241_delete_user_roles.up.sql`, where
deleting a custom role cleans `organization_members.roles`, a similarly
structured array of references that cannot be protected by a normal
foreign key.

Closes CODAGT-505
This commit is contained in:
Ethan
2026-05-29 16:49:25 +10:00
committed by GitHub
parent a801d996e7
commit eb2c2799ca
4 changed files with 73 additions and 6 deletions
+15 -6
View File
@@ -1396,10 +1396,11 @@ func TestChatWithMCPServerIDs(t *testing.T) {
// Create the chat model config required for creating a chat.
_ = createChatModelConfigForMCP(t, expClient)
// Create an enabled MCP server config.
mcpConfig := createMCPServerConfig(t, client, "chat-mcp-server", true)
// Create enabled MCP server configs.
mcpConfigA := createMCPServerConfig(t, client, "chat-mcp-server-a", true)
mcpConfigB := createMCPServerConfig(t, client, "chat-mcp-server-b", true)
// Create a chat referencing the MCP server.
// Create a chat referencing the MCP servers.
chat, err := expClient.CreateChat(ctx, codersdk.CreateChatRequest{
OrganizationID: firstUser.OrganizationID,
Content: []codersdk.ChatInputPart{
@@ -1408,16 +1409,24 @@ func TestChatWithMCPServerIDs(t *testing.T) {
Text: "hello with mcp server",
},
},
MCPServerIDs: []uuid.UUID{mcpConfig.ID},
MCPServerIDs: []uuid.UUID{mcpConfigA.ID, mcpConfigB.ID},
})
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, chat.ID)
require.Contains(t, chat.MCPServerIDs, mcpConfig.ID)
require.ElementsMatch(t, []uuid.UUID{mcpConfigA.ID, mcpConfigB.ID}, chat.MCPServerIDs)
// Fetch the chat and verify the MCP server IDs persist.
fetched, err := expClient.GetChat(ctx, chat.ID)
require.NoError(t, err)
require.Contains(t, fetched.MCPServerIDs, mcpConfig.ID)
require.ElementsMatch(t, []uuid.UUID{mcpConfigA.ID, mcpConfigB.ID}, fetched.MCPServerIDs)
err = client.DeleteMCPServerConfig(ctx, mcpConfigA.ID)
require.NoError(t, err)
fetched, err = expClient.GetChat(ctx, chat.ID)
require.NoError(t, err)
require.NotContains(t, fetched.MCPServerIDs, mcpConfigA.ID)
require.Contains(t, fetched.MCPServerIDs, mcpConfigB.ID)
}
func createChatModelConfigForMCP(t testing.TB, client *codersdk.ExperimentalClient) codersdk.ChatModelConfig {