@@ -50,7 +50,8 @@ interface ServerMetadataUsageState {
5050async function listWorkflowMcpToolSyncPage (
5151 tx : DbOrTx ,
5252 workflowId : string ,
53- afterToolId ?: string
53+ afterToolId ?: string ,
54+ serverIds ?: string [ ]
5455) : Promise < WorkflowMcpToolSyncRow [ ] > {
5556 return tx
5657 . select ( {
@@ -64,6 +65,9 @@ async function listWorkflowMcpToolSyncPage(
6465 and (
6566 eq ( workflowMcpTool . workflowId , workflowId ) ,
6667 isNull ( workflowMcpTool . archivedAt ) ,
68+ serverIds && serverIds . length > 0
69+ ? inArray ( workflowMcpTool . serverId , serverIds )
70+ : undefined ,
6771 afterToolId ? gt ( workflowMcpTool . id , afterToolId ) : undefined
6872 )
6973 )
@@ -188,10 +192,13 @@ export async function syncMcpToolsForWorkflow(
188192
189193 const affectedServerIds = new Set < string > ( )
190194 const lockedServers = await collectWorkflowMcpToolServerIds ( tx , workflowId )
195+ if ( lockedServers . length === 0 ) return [ ]
196+
191197 for ( const { serverId } of lockedServers ) {
192198 await acquireWorkflowMcpServerLock ( tx , serverId )
193199 affectedServerIds . add ( serverId )
194200 }
201+ const lockedServerIds = [ ...affectedServerIds ]
195202
196203 const usageStateByServer = new Map < string , ServerMetadataUsageState > ( )
197204 for ( const { serverId } of lockedServers ) {
@@ -206,7 +213,7 @@ export async function syncMcpToolsForWorkflow(
206213 let afterToolId : string | undefined
207214
208215 while ( true ) {
209- const page = await listWorkflowMcpToolSyncPage ( tx , workflowId , afterToolId )
216+ const page = await listWorkflowMcpToolSyncPage ( tx , workflowId , afterToolId , lockedServerIds )
210217 if ( page . length === 0 ) break
211218
212219 const pageTools = page . slice ( 0 , MCP_SYNC_TOOLS_PAGE_SIZE )
@@ -222,7 +229,9 @@ export async function syncMcpToolsForWorkflow(
222229 left . localeCompare ( right )
223230 ) ) {
224231 const usageState = usageStateByServer . get ( serverId )
225- if ( ! usageState ) continue
232+ if ( ! usageState ) {
233+ throw new Error ( `Missing locked MCP server usage state for server ${ serverId } ` )
234+ }
226235 const schemaToolIds : string [ ] = [ ]
227236 const emptySchemaToolIds : string [ ] = [ ]
228237
0 commit comments