Skip to content

Commit efcaabb

Browse files
committed
refactor(devti): improve MCP server information collection
- Extract server info collection into a separate suspend function - Optimize tool collection process for multiple servers- Enhance code readability and maintainability
1 parent b6343d1 commit efcaabb

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

core/src/main/kotlin/cc/unitmesh/devti/mcp/client/CustomMcpServerManager.kt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,42 @@ class CustomMcpServerManager(val project: Project) {
3939
val toolsMap = mutableMapOf<String, List<Tool>>()
4040
mcpConfig.mcpServers.forEach { entry ->
4141
if (entry.value.disabled == true) return@forEach
42-
val resolvedCommand = resolveCommand(entry.value.command)
43-
logger<CustomMcpServerManager>().info("Found MCP command: $resolvedCommand")
44-
val client = Client(clientInfo = Implementation(name = entry.key, version = "1.0.0"))
42+
val tools = collectServerInfo(entry.key, entry.value)
43+
toolsMap[entry.key] = tools
44+
}
4545

46-
val cmd = GeneralCommandLine(resolvedCommand)
47-
cmd.addParameters(*entry.value.args.toTypedArray())
46+
cached[mcpServerConfig] = toolsMap
47+
return toolsMap
48+
}
4849

49-
entry.value.env?.forEach { (key, value) ->
50-
cmd.environment[key] = value
51-
}
50+
suspend fun collectServerInfo(serverKey: String, serverConfig: McpServer): List<Tool> {
51+
val resolvedCommand = resolveCommand(serverConfig.command)
52+
logger<CustomMcpServerManager>().info("Found MCP command for $serverKey: $resolvedCommand")
53+
val client = Client(clientInfo = Implementation(name = serverKey, version = "1.0.0"))
5254

53-
val process = cmd.createProcess()
54-
val input = process.inputStream.asSource().buffered()
55-
val output = process.outputStream.asSink().buffered()
56-
val transport = StdioClientTransport(input, output)
55+
val cmd = GeneralCommandLine(resolvedCommand)
56+
cmd.addParameters(*serverConfig.args.toTypedArray())
5757

58-
val tools = try {
59-
client.connect(transport)
60-
val listTools = client.listTools()
61-
listTools?.tools?.forEach { tool ->
62-
toolClientMap[tool] = client
63-
}
64-
listTools?.tools ?: emptyList()
65-
} catch (e: Exception) {
66-
logger<CustomMcpServerManager>().warn("Failed to list tools from ${entry.key}: $e")
67-
emptyList<Tool>()
68-
}
69-
70-
toolsMap[entry.key] = tools
58+
serverConfig.env?.forEach { (key, value) ->
59+
cmd.environment[key] = value
7160
}
7261

73-
cached[mcpServerConfig] = toolsMap
74-
return toolsMap
62+
val process = cmd.createProcess()
63+
val input = process.inputStream.asSource().buffered()
64+
val output = process.outputStream.asSink().buffered()
65+
val transport = StdioClientTransport(input, output)
66+
67+
return try {
68+
client.connect(transport)
69+
val listTools = client.listTools()
70+
listTools?.tools?.forEach { tool ->
71+
toolClientMap[tool] = client
72+
}
73+
listTools?.tools ?: emptyList()
74+
} catch (e: Exception) {
75+
logger<CustomMcpServerManager>().warn("Failed to list tools from $serverKey: $e")
76+
emptyList()
77+
}
7578
}
7679

7780
private val json = Json { prettyPrint = true }

0 commit comments

Comments
 (0)