diff --git a/src/services/mcp/McpHub.ts b/src/services/mcp/McpHub.ts index caca5ddb39..f5b9dca813 100644 --- a/src/services/mcp/McpHub.ts +++ b/src/services/mcp/McpHub.ts @@ -877,6 +877,22 @@ export class McpHub { connection.server.error = truncatedError } + /** + * Helper method to get timeout from connection configuration + * @param connection The MCP connection to get timeout from + * @returns The timeout in milliseconds, defaults to 60000ms if parsing fails + */ + private getTimeoutFromConnection(connection: McpConnection): number { + try { + const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config)) + return (parsedConfig.timeout ?? 60) * 1000 + } catch (error) { + console.error("Failed to parse server config for timeout:", error) + // Default to 60 seconds if parsing fails + return 60 * 1000 + } + } + /** * Helper method to find a connection by server name and source * @param serverName The name of the server to find @@ -911,7 +927,10 @@ export class McpHub { return [] } - const response = await connection.client.request({ method: "tools/list" }, ListToolsResultSchema) + const timeout = this.getTimeoutFromConnection(connection) + const response = await connection.client.request({ method: "tools/list" }, ListToolsResultSchema, { + timeout, + }) // Determine the actual source of the server const actualSource = connection.server.source || "global" @@ -965,7 +984,11 @@ export class McpHub { if (!connection || connection.type !== "connected") { return [] } - const response = await connection.client.request({ method: "resources/list" }, ListResourcesResultSchema) + + const timeout = this.getTimeoutFromConnection(connection) + const response = await connection.client.request({ method: "resources/list" }, ListResourcesResultSchema, { + timeout, + }) return response?.resources || [] } catch (error) { // console.error(`Failed to fetch resources for ${serverName}:`, error) @@ -982,9 +1005,14 @@ export class McpHub { if (!connection || connection.type !== "connected") { return [] } + + const timeout = this.getTimeoutFromConnection(connection) const response = await connection.client.request( { method: "resources/templates/list" }, ListResourceTemplatesResultSchema, + { + timeout, + }, ) return response?.resourceTemplates || [] } catch (error) { @@ -1564,6 +1592,8 @@ export class McpHub { if (connection.server.disabled) { throw new Error(`Server "${serverName}" is disabled`) } + + const timeout = this.getTimeoutFromConnection(connection) return await connection.client.request( { method: "resources/read", @@ -1572,6 +1602,9 @@ export class McpHub { }, }, ReadResourceResultSchema, + { + timeout, + }, ) } @@ -1591,16 +1624,7 @@ export class McpHub { throw new Error(`Server "${serverName}" is disabled and cannot be used`) } - let timeout: number - try { - const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config)) - timeout = (parsedConfig.timeout ?? 60) * 1000 - } catch (error) { - console.error("Failed to parse server config for timeout:", error) - // Default to 60 seconds if parsing fails - timeout = 60 * 1000 - } - + const timeout = this.getTimeoutFromConnection(connection) return await connection.client.request( { method: "tools/call",