@@ -112,21 +112,6 @@ export const POST = withRouteHandler(
112112
113113 const serverId = body . url ? generateMcpServerId ( workspaceId , body . url ) : generateId ( )
114114
115- let resolvedAuthType : 'none' | 'headers' | 'oauth' = body . authType ?? 'headers'
116- const hasHeaders = body . headers && Object . keys ( body . headers ) . length > 0
117- if ( ! body . authType && body . url && ! hasHeaders ) {
118- try {
119- resolvedAuthType = await detectMcpAuthType ( body . url )
120- logger . info ( `[${ requestId } ] Probed ${ body . url } : authType=${ resolvedAuthType } ` )
121- } catch ( e ) {
122- logger . warn ( `[${ requestId } ] Probe failed for ${ body . url } , defaulting to headers` , e )
123- resolvedAuthType = 'headers'
124- }
125- }
126-
127- // User-supplied client credentials imply OAuth; pin authType regardless of probe.
128- if ( body . oauthClientId ) resolvedAuthType = 'oauth'
129-
130115 const oauthClientSecretProvided = body . oauthClientSecret !== undefined
131116 const oauthClientSecretEncrypted = body . oauthClientSecret
132117 ? ( await encryptSecret ( body . oauthClientSecret ) ) . encrypted
@@ -139,19 +124,45 @@ export const POST = withRouteHandler(
139124 id : mcpServers . id ,
140125 deletedAt : mcpServers . deletedAt ,
141126 url : mcpServers . url ,
127+ authType : mcpServers . authType ,
142128 oauthClientId : mcpServers . oauthClientId ,
143129 oauthClientSecret : mcpServers . oauthClientSecret ,
144130 } )
145131 . from ( mcpServers )
146132 . where ( and ( eq ( mcpServers . id , serverId ) , eq ( mcpServers . workspaceId , workspaceId ) ) )
147133 . limit ( 1 )
148134
135+ const urlChanged = existingServer ? existingServer . url !== body . url : true
136+ const hasHeaders = body . headers && Object . keys ( body . headers ) . length > 0
137+
138+ let resolvedAuthType : 'none' | 'headers' | 'oauth' = body . authType ?? 'headers'
139+ if ( ! body . authType ) {
140+ if ( existingServer && ! urlChanged ) {
141+ // Preserve existing authType on edits that don't change the URL — re-probing
142+ // can flip a working OAuth+DCR server to 'headers' on a transient 401/timeout.
143+ resolvedAuthType = ( existingServer . authType ?? 'headers' ) as
144+ | 'none'
145+ | 'headers'
146+ | 'oauth'
147+ } else if ( body . url && ! hasHeaders ) {
148+ try {
149+ resolvedAuthType = await detectMcpAuthType ( body . url )
150+ logger . info ( `[${ requestId } ] Probed ${ body . url } : authType=${ resolvedAuthType } ` )
151+ } catch ( e ) {
152+ logger . warn ( `[${ requestId } ] Probe failed for ${ body . url } , defaulting to headers` , e )
153+ resolvedAuthType = 'headers'
154+ }
155+ }
156+ }
157+
158+ // User-supplied client credentials imply OAuth; pin authType regardless of probe.
159+ if ( body . oauthClientId ) resolvedAuthType = 'oauth'
160+
149161 if ( existingServer ) {
150162 logger . info (
151163 `[${ requestId } ] Server with ID ${ serverId } already exists, updating instead of creating`
152164 )
153165
154- const urlChanged = existingServer . url !== body . url
155166 const clientIdChanged =
156167 oauthClientIdProvided &&
157168 ( oauthClientId || null ) !== ( existingServer . oauthClientId ?? null )
0 commit comments