( options: SyncOptions )
| 181 | * @param options.context - Optional context for log messages |
| 182 | */ |
| 183 | export async function syncMcpToolsForWorkflow( |
| 184 | options: SyncOptions |
| 185 | ): Promise<Array<{ serverId: string }>> { |
| 186 | if (!options.tx) { |
| 187 | let state = options.state |
| 188 | if (!state) { |
| 189 | try { |
| 190 | state = await loadDeployedWorkflowState(options.workflowId) |
| 191 | } catch (error) { |
| 192 | logger.error( |
| 193 | `[${options.requestId}] Error loading deployed state for MCP tool sync (${options.context ?? 'sync'}):`, |
| 194 | error |
| 195 | ) |
| 196 | if (options.throwOnError) throw error |
| 197 | return [] |
| 198 | } |
| 199 | } |
| 200 | const resolvedState = state |
| 201 | const tools = await db.transaction((tx) => |
| 202 | syncMcpToolsForWorkflow({ ...options, state: resolvedState, tx, notify: false }) |
| 203 | ) |
| 204 | if (options.notify ?? true) notifyMcpToolServers(tools) |
| 205 | return tools |
| 206 | } |
| 207 | |
| 208 | const { workflowId, requestId, state, context = 'sync', tx, throwOnError = false } = options |
| 209 | |
| 210 | try { |
| 211 | if (!hasValidStartBlockInState(state as WorkflowState | null)) { |
| 212 | return await removeMcpToolsForWorkflow(workflowId, requestId, tx, true) |
| 213 | } |
| 214 | |
| 215 | const generatedParameterSchema = state.blocks |
| 216 | ? generateSchemaFromBlocks(state.blocks) |
| 217 | : EMPTY_SCHEMA |
| 218 | const schemaLimitError = validateMcpToolMetadataForStorage({ |
| 219 | parameterSchema: generatedParameterSchema, |
| 220 | }) |
| 221 | if (schemaLimitError) { |
| 222 | throw new Error(schemaLimitError) |
| 223 | } |
| 224 | const baseParameterSchema = generatedParameterSchema |
| 225 | |
| 226 | const affectedServerIds = new Set<string>() |
| 227 | const lockedServers = await collectWorkflowMcpToolServerIds(tx, workflowId) |
| 228 | if (lockedServers.length === 0) return [] |
| 229 | |
| 230 | for (const { serverId } of lockedServers) { |
| 231 | await acquireWorkflowMcpServerLock(tx, serverId) |
| 232 | affectedServerIds.add(serverId) |
| 233 | } |
| 234 | const lockedServerIds = [...affectedServerIds] |
| 235 | |
| 236 | const usageStateByServer = new Map<string, ServerMetadataUsageState>() |
| 237 | for (const { serverId } of lockedServers) { |
| 238 | const rows = await getMcpServerToolMetadataUsageRows(tx, serverId) |
| 239 | usageStateByServer.set(serverId, { |
| 240 | usageByToolId: new Map(rows.map((row) => [row.id, row])), |
no test coverage detected