refactor: extract timeout configuration logic into helper method

- Created getTimeoutFromConnection helper method to reduce code duplication
- Applied helper method to all 5 locations where timeout is extracted
- Improves maintainability and reduces repetitive code
This commit is contained in:
Roo Code 2025-09-10 02:28:11 +00:00
parent b57252210e
commit c76fbde280

View file

@ -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,16 +927,7 @@ export class McpHub {
return []
}
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)
const response = await connection.client.request({ method: "tools/list" }, ListToolsResultSchema, {
timeout,
})
@ -978,16 +985,7 @@ export class McpHub {
return []
}
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)
const response = await connection.client.request({ method: "resources/list" }, ListResourcesResultSchema, {
timeout,
})
@ -1008,16 +1006,7 @@ export class McpHub {
return []
}
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)
const response = await connection.client.request(
{ method: "resources/templates/list" },
ListResourceTemplatesResultSchema,
@ -1604,16 +1593,7 @@ export class McpHub {
throw new Error(`Server "${serverName}" is disabled`)
}
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: "resources/read",
@ -1644,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",