Skip to content

Commit 4e6210c

Browse files
committed
improvement(mcp): update server status from discoverServerTools + cap list-tools timeout at 30s
1 parent 8412ea5 commit 4e6210c

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

apps/sim/lib/mcp/client.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ vi.mock('@modelcontextprotocol/sdk/types.js', () => ({
3737

3838
vi.mock('@/lib/core/execution-limits', () => ({
3939
getMaxExecutionTimeout: vi.fn().mockReturnValue(30000),
40+
DEFAULT_EXECUTION_TIMEOUT_MS: 30000,
4041
}))
4142

4243
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'

apps/sim/lib/mcp/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
type McpToolsChangedCallback,
3737
type McpVersionInfo,
3838
} from '@/lib/mcp/types'
39+
import { MCP_CLIENT_CONSTANTS } from '@/lib/mcp/utils'
3940

4041
const logger = createLogger('McpClient')
4142

@@ -167,7 +168,9 @@ export class McpClient {
167168
}
168169

169170
try {
170-
const result: ListToolsResult = await this.client.listTools()
171+
const result: ListToolsResult = await this.client.listTools(undefined, {
172+
timeout: MCP_CLIENT_CONSTANTS.LIST_TOOLS_TIMEOUT_MS,
173+
})
171174

172175
if (!result.tools || !Array.isArray(result.tools)) {
173176
logger.warn(`Invalid tools response from server ${this.config.name}:`, result)

apps/sim/lib/mcp/service.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,17 @@ class McpService {
580580
try {
581581
const tools = await client.listTools()
582582
logger.info(`[${requestId}] Discovered ${tools.length} tools from server ${config.name}`)
583-
// Prime the per-server cache so the next discoverTools call hits it
584-
// instead of re-fetching live (which can stall on slow upstreams).
585-
await this.cacheAdapter
586-
.set(serverCacheKey(workspaceId, serverId), tools, this.cacheTimeout)
587-
.catch((err) =>
588-
logger.warn(`[${requestId}] Cache write failed for ${config.name}:`, err)
589-
)
583+
// Prime the per-server cache and reflect the successful connection on
584+
// the row so the UI doesn't keep showing "Connect with OAuth" or stale
585+
// disconnected/error state.
586+
await Promise.allSettled([
587+
this.cacheAdapter
588+
.set(serverCacheKey(workspaceId, serverId), tools, this.cacheTimeout)
589+
.catch((err) =>
590+
logger.warn(`[${requestId}] Cache write failed for ${config.name}:`, err)
591+
),
592+
this.updateServerStatus(serverId, workspaceId, true, undefined, tools.length),
593+
])
590594
return tools
591595
} finally {
592596
await client.disconnect()

apps/sim/lib/mcp/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export function sanitizeHeaders(
4646
export const MCP_CLIENT_CONSTANTS = {
4747
CLIENT_TIMEOUT: DEFAULT_EXECUTION_TIMEOUT_MS,
4848
AUTO_REFRESH_INTERVAL: 5 * 60 * 1000,
49+
// Cap metadata calls so a slow upstream can't hang the UI for 60s+.
50+
LIST_TOOLS_TIMEOUT_MS: 30_000,
4951
} as const
5052

5153
/**

0 commit comments

Comments
 (0)