Skip to content

Commit 31dd5cb

Browse files
committed
fix(mcp): read-only discovery takes every shared-state guard best-effort discovery takes
1 parent bbc427a commit 31dd5cb

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

‎apps/sim/lib/mcp/service.test.ts‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -591,21 +591,32 @@ describe('McpService best-effort discovery', () => {
591591
expect(tools.map((t) => t.name)).toEqual(['a1'])
592592
})
593593

594-
it('skips an OAuth server nobody authorized without creating its OAuth row or marking it pending', async () => {
595-
mockGetWorkspaceServersRows.mockResolvedValue([dbRow('mcp-a', 'A', { authType: 'oauth' })])
596-
mcpOauthMockFns.mockHasDecryptableOauthTokens.mockResolvedValue(false)
594+
it.each([
595+
['a best-effort', { bestEffort: true }],
596+
['a read-only', { readOnly: true }],
597+
])(
598+
'skips an OAuth server nobody authorized in %s discovery without creating its OAuth row or marking it pending',
599+
async (_kind, options) => {
600+
mockGetWorkspaceServersRows.mockResolvedValue([dbRow('mcp-a', 'A', { authType: 'oauth' })])
601+
mcpOauthMockFns.mockHasDecryptableOauthTokens.mockResolvedValue(false)
597602

598-
await expect(
599-
mcpService.discoverServerTools(MEMBER_B, 'mcp-a', WORKSPACE_ID, 'cache-aside', undefined, {
600-
bestEffort: true,
601-
})
602-
).rejects.toBeInstanceOf(McpOauthAuthorizationRequiredError)
603-
expect(mcpOauthMockFns.mockGetOrCreateOauthRow).not.toHaveBeenCalled()
604-
// loadOauthRow clears a column it cannot decrypt; the gate must not risk that.
605-
expect(mcpOauthMockFns.mockLoadOauthRow).not.toHaveBeenCalled()
606-
expect(mockConnect).not.toHaveBeenCalled()
607-
expectNoSharedStateWrites()
608-
})
603+
await expect(
604+
mcpService.discoverServerTools(
605+
MEMBER_B,
606+
'mcp-a',
607+
WORKSPACE_ID,
608+
'cache-aside',
609+
undefined,
610+
options
611+
)
612+
).rejects.toBeInstanceOf(McpOauthAuthorizationRequiredError)
613+
expect(mcpOauthMockFns.mockGetOrCreateOauthRow).not.toHaveBeenCalled()
614+
// loadOauthRow clears a column it cannot decrypt; the gate must not risk that.
615+
expect(mcpOauthMockFns.mockLoadOauthRow).not.toHaveBeenCalled()
616+
expect(mockConnect).not.toHaveBeenCalled()
617+
expectNoSharedStateWrites()
618+
}
619+
)
609620

610621
it.each([
611622
['a best-effort', { bestEffort: true }],

‎apps/sim/lib/mcp/service.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,8 +1254,10 @@ class McpService {
12541254
const discoveryStartedAt = new Date()
12551255
const maxRetries = 2
12561256

1257+
// Best-effort and read-only discoveries both leave the server's shared state as they found it.
1258+
const preservesSharedState = bestEffort || readOnly
12571259
// Before the cache: another member's cached tools never reach a requester who cannot call them.
1258-
const requesterConfig = bestEffort
1260+
const requesterConfig = preservesSharedState
12591261
? await this.loadAuthenticatableServerConfig(serverId, userId, workspaceId)
12601262
: undefined
12611263

@@ -1298,7 +1300,7 @@ class McpService {
12981300
onResolvedSecretTraceProvenance,
12991301
signal,
13001302
requireComplete,
1301-
bestEffort || readOnly
1303+
preservesSharedState
13021304
)
13031305
logger.info(`Discovered ${tools.length} tools from server ${config.name}`, { requestId })
13041306
if (readOnly) return tools
@@ -1334,7 +1336,7 @@ class McpService {
13341336
signal?.throwIfAborted()
13351337
continue
13361338
}
1337-
if (bestEffort || readOnly) throw error
1339+
if (preservesSharedState) throw error
13381340
// Drop positive cache so a follow-up doesn't return stale tools.
13391341
const statusApplied = isOauthAuthorizationError(error, authType)
13401342
? await this.markServerOauthPending(serverId, workspaceId, discoveryStartedAt)

0 commit comments

Comments
 (0)