Skip to content

Commit 59048e0

Browse files
committed
address comments
1 parent 54af13e commit 59048e0

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

apps/sim/app/api/mcp/serve/[serverId]/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,7 @@ async function handleToolsList(
560560
.orderBy(asc(workflowMcpTool.id))
561561
.limit(MAX_MCP_TOOLS_LIST_COUNT + 1)
562562

563-
const hasNextPage = toolSizes.length > MAX_MCP_TOOLS_LIST_COUNT
564-
const pageSizes = hasNextPage ? toolSizes.slice(0, MAX_MCP_TOOLS_LIST_COUNT) : toolSizes
565-
const nextCursor = hasNextPage ? pageSizes.at(-1)?.id : undefined
563+
const pageSizes = toolSizes.slice(0, MAX_MCP_TOOLS_LIST_COUNT)
566564

567565
let estimatedSchemaBytes = 0
568566
let estimatedMetadataBytes = 0
@@ -602,7 +600,9 @@ async function handleToolsList(
602600
.orderBy(asc(workflowMcpTool.id))
603601
.limit(MAX_MCP_TOOLS_LIST_COUNT + 1)
604602

603+
const hasNextPage = tools.length > MAX_MCP_TOOLS_LIST_COUNT
605604
const pageTools = tools.slice(0, MAX_MCP_TOOLS_LIST_COUNT)
605+
const nextCursor = hasNextPage ? pageTools.at(-1)?.id : undefined
606606
let schemaBytes = 0
607607
const result: ListToolsResult = {
608608
tools: pageTools.map((tool) => {

apps/sim/lib/mcp/server-locks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const MCP_SERVER_LOCK_TIMEOUT_MS = 3_000
66
const LOCK_NOT_AVAILABLE_SQLSTATE = '55P03'
77

88
export async function setWorkflowMcpTransactionLockTimeout(tx: DbOrTx): Promise<void> {
9-
await tx.execute(sql.raw(`SET LOCAL lock_timeout = '${MCP_SERVER_LOCK_TIMEOUT_MS}ms'`))
9+
await tx.execute(
10+
sql`select set_config('lock_timeout', ${`${MCP_SERVER_LOCK_TIMEOUT_MS}ms`}, true)`
11+
)
1012
}
1113

1214
export async function acquireWorkflowMcpServerLock(tx: DbOrTx, serverId: string): Promise<void> {

apps/sim/lib/mcp/workflow-mcp-sync.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ interface ServerMetadataUsageState {
5050
async 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

Comments
 (0)