Skip to content

Commit c76fbde

Browse files
committed
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
1 parent b572522 commit c76fbde

File tree

1 file changed

+21
-50
lines changed

1 file changed

+21
-50
lines changed

src/services/mcp/McpHub.ts

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,22 @@ export class McpHub {
877877
connection.server.error = truncatedError
878878
}
879879

880+
/**
881+
* Helper method to get timeout from connection configuration
882+
* @param connection The MCP connection to get timeout from
883+
* @returns The timeout in milliseconds, defaults to 60000ms if parsing fails
884+
*/
885+
private getTimeoutFromConnection(connection: McpConnection): number {
886+
try {
887+
const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config))
888+
return (parsedConfig.timeout ?? 60) * 1000
889+
} catch (error) {
890+
console.error("Failed to parse server config for timeout:", error)
891+
// Default to 60 seconds if parsing fails
892+
return 60 * 1000
893+
}
894+
}
895+
880896
/**
881897
* Helper method to find a connection by server name and source
882898
* @param serverName The name of the server to find
@@ -911,16 +927,7 @@ export class McpHub {
911927
return []
912928
}
913929

914-
let timeout: number
915-
try {
916-
const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config))
917-
timeout = (parsedConfig.timeout ?? 60) * 1000
918-
} catch (error) {
919-
console.error("Failed to parse server config for timeout:", error)
920-
// Default to 60 seconds if parsing fails
921-
timeout = 60 * 1000
922-
}
923-
930+
const timeout = this.getTimeoutFromConnection(connection)
924931
const response = await connection.client.request({ method: "tools/list" }, ListToolsResultSchema, {
925932
timeout,
926933
})
@@ -978,16 +985,7 @@ export class McpHub {
978985
return []
979986
}
980987

981-
let timeout: number
982-
try {
983-
const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config))
984-
timeout = (parsedConfig.timeout ?? 60) * 1000
985-
} catch (error) {
986-
console.error("Failed to parse server config for timeout:", error)
987-
// Default to 60 seconds if parsing fails
988-
timeout = 60 * 1000
989-
}
990-
988+
const timeout = this.getTimeoutFromConnection(connection)
991989
const response = await connection.client.request({ method: "resources/list" }, ListResourcesResultSchema, {
992990
timeout,
993991
})
@@ -1008,16 +1006,7 @@ export class McpHub {
10081006
return []
10091007
}
10101008

1011-
let timeout: number
1012-
try {
1013-
const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config))
1014-
timeout = (parsedConfig.timeout ?? 60) * 1000
1015-
} catch (error) {
1016-
console.error("Failed to parse server config for timeout:", error)
1017-
// Default to 60 seconds if parsing fails
1018-
timeout = 60 * 1000
1019-
}
1020-
1009+
const timeout = this.getTimeoutFromConnection(connection)
10211010
const response = await connection.client.request(
10221011
{ method: "resources/templates/list" },
10231012
ListResourceTemplatesResultSchema,
@@ -1604,16 +1593,7 @@ export class McpHub {
16041593
throw new Error(`Server "${serverName}" is disabled`)
16051594
}
16061595

1607-
let timeout: number
1608-
try {
1609-
const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config))
1610-
timeout = (parsedConfig.timeout ?? 60) * 1000
1611-
} catch (error) {
1612-
console.error("Failed to parse server config for timeout:", error)
1613-
// Default to 60 seconds if parsing fails
1614-
timeout = 60 * 1000
1615-
}
1616-
1596+
const timeout = this.getTimeoutFromConnection(connection)
16171597
return await connection.client.request(
16181598
{
16191599
method: "resources/read",
@@ -1644,16 +1624,7 @@ export class McpHub {
16441624
throw new Error(`Server "${serverName}" is disabled and cannot be used`)
16451625
}
16461626

1647-
let timeout: number
1648-
try {
1649-
const parsedConfig = ServerConfigSchema.parse(JSON.parse(connection.server.config))
1650-
timeout = (parsedConfig.timeout ?? 60) * 1000
1651-
} catch (error) {
1652-
console.error("Failed to parse server config for timeout:", error)
1653-
// Default to 60 seconds if parsing fails
1654-
timeout = 60 * 1000
1655-
}
1656-
1627+
const timeout = this.getTimeoutFromConnection(connection)
16571628
return await connection.client.request(
16581629
{
16591630
method: "tools/call",

0 commit comments

Comments
 (0)