@@ -32,15 +32,10 @@ function toSetupError(error: unknown): never {
3232 throw error
3333}
3434
35- /**
36- * Runs before source approval opens its transaction. A provider whose server does not exist yet
37- * has that server checked here, because the check resolves DNS and must not run while the
38- * transaction holds the accounts lock; an already-configured provider needs no check.
39- */
40- export async function prepareSearchMcpProvider (
35+ async function hasProviderServer (
4136 organizationId : string ,
4237 provider : ManagedSearchMcpProvider
43- ) : Promise < SearchMcpProviderSetup > {
38+ ) : Promise < boolean > {
4439 const [ existing ] = await db
4540 . select ( { id : mcpServers . id } )
4641 . from ( mcpServers )
@@ -54,13 +49,27 @@ export async function prepareSearchMcpProvider(
5449 )
5550 )
5651 . limit ( 1 )
57- if ( existing ) return { provider, validated : null }
52+ return existing !== undefined
53+ }
54+
55+ /**
56+ * Runs before source approval opens its transaction. A provider whose server does not exist yet
57+ * has that server checked here, because the check resolves DNS and must not run while the
58+ * transaction holds the accounts lock; an already-configured provider needs no check. A failed
59+ * check looks again first, since a concurrent approval may have created the server meanwhile.
60+ */
61+ export async function prepareSearchMcpProvider (
62+ organizationId : string ,
63+ provider : ManagedSearchMcpProvider
64+ ) : Promise < SearchMcpProviderSetup > {
65+ if ( await hasProviderServer ( organizationId , provider ) ) return { provider, validated : null }
5866 try {
5967 return {
6068 provider,
6169 validated : await validateManagedMcpConnectorInput ( { connectorId : provider } ) ,
6270 }
6371 } catch ( error ) {
72+ if ( await hasProviderServer ( organizationId , provider ) ) return { provider, validated : null }
6473 toSetupError ( error )
6574 }
6675}
0 commit comments