@@ -4,6 +4,7 @@ import { mcpServerOauth } from '@sim/db/schema'
44import { createLogger } from '@sim/logger'
55import { generateId } from '@sim/utils/id'
66import { and , eq , isNull } from 'drizzle-orm'
7+ import { isEqual } from 'es-toolkit'
78import type { NextRequest } from 'next/server'
89import { encryptSecret } from '@/lib/core/security/encryption'
910import {
@@ -84,6 +85,30 @@ export interface PerformMcpServerResult {
8485 server ?: typeof mcpServers . $inferSelect
8586 updated ?: boolean
8687 authType ?: McpAuthType
88+ connectionChanged ?: boolean
89+ }
90+
91+ export function hasMcpServerConnectionChanged (
92+ currentServer : Pick <
93+ typeof mcpServers . $inferSelect ,
94+ 'url' | 'transport' | 'headers' | 'timeout' | 'retries' | 'enabled' | 'authType'
95+ > ,
96+ updates : Pick <
97+ PerformUpdateMcpServerParams ,
98+ 'url' | 'transport' | 'headers' | 'timeout' | 'retries' | 'enabled' | 'authType'
99+ > ,
100+ oauthCredentialsChanged = false
101+ ) : boolean {
102+ return (
103+ oauthCredentialsChanged ||
104+ ( updates . url !== undefined && updates . url !== currentServer . url ) ||
105+ ( updates . transport !== undefined && updates . transport !== currentServer . transport ) ||
106+ ( updates . headers !== undefined && ! isEqual ( updates . headers , currentServer . headers ?? { } ) ) ||
107+ ( updates . timeout !== undefined && updates . timeout !== currentServer . timeout ) ||
108+ ( updates . retries !== undefined && updates . retries !== currentServer . retries ) ||
109+ ( updates . enabled !== undefined && updates . enabled !== currentServer . enabled ) ||
110+ ( updates . authType !== undefined && updates . authType !== currentServer . authType )
111+ )
87112}
88113
89114type ValidateMcpServerUrlResult =
@@ -316,6 +341,11 @@ export async function performUpdateMcpServer(
316341 const [ currentServer ] = await db
317342 . select ( {
318343 url : mcpServers . url ,
344+ transport : mcpServers . transport ,
345+ headers : mcpServers . headers ,
346+ timeout : mcpServers . timeout ,
347+ retries : mcpServers . retries ,
348+ enabled : mcpServers . enabled ,
319349 authType : mcpServers . authType ,
320350 oauthClientId : mcpServers . oauthClientId ,
321351 oauthClientSecret : mcpServers . oauthClientSecret ,
@@ -349,6 +379,11 @@ export async function performUpdateMcpServer(
349379 currentClientId : currentServer . oauthClientId ,
350380 currentEncryptedClientSecret : currentServer . oauthClientSecret ,
351381 } )
382+ const connectionChanged = hasMcpServerConnectionChanged (
383+ currentServer ,
384+ { ...params , authType : updateData . authType as McpAuthType | undefined } ,
385+ credsChanged
386+ )
352387 const shouldClearOauth = urlChanged || credsChanged
353388 const resolvedAuthType = ( updateData . authType ?? currentServer . authType ) as McpAuthType
354389 if ( shouldClearOauth && resolvedAuthType === 'oauth' ) {
@@ -381,15 +416,7 @@ export async function performUpdateMcpServer(
381416
382417 if ( ! server ) return { success : false , error : 'Server not found' , errorCode : 'not_found' }
383418
384- const shouldClearCache =
385- urlChanged ||
386- credsChanged ||
387- params . enabled !== undefined ||
388- params . headers !== undefined ||
389- params . timeout !== undefined ||
390- params . retries !== undefined
391-
392- if ( shouldClearCache ) await mcpService . clearCache ( params . workspaceId )
419+ if ( connectionChanged ) await mcpService . clearCache ( params . workspaceId )
393420
394421 recordAudit ( {
395422 workspaceId : params . workspaceId ,
@@ -410,7 +437,7 @@ export async function performUpdateMcpServer(
410437 request : params . request ,
411438 } )
412439
413- return { success : true , server }
440+ return { success : true , server, connectionChanged }
414441 } catch ( error ) {
415442 logger . error ( 'Failed to update MCP server' , { error } )
416443 return { success : false , error : 'Failed to update MCP server' , errorCode : 'internal' }
0 commit comments