@@ -39,39 +39,42 @@ class CustomMcpServerManager(val project: Project) {
39
39
val toolsMap = mutableMapOf<String , List <Tool >>()
40
40
mcpConfig.mcpServers.forEach { entry ->
41
41
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
+ }
45
45
46
- val cmd = GeneralCommandLine (resolvedCommand)
47
- cmd.addParameters(* entry.value.args.toTypedArray())
46
+ cached[mcpServerConfig] = toolsMap
47
+ return toolsMap
48
+ }
48
49
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" ))
52
54
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())
57
57
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
71
60
}
72
61
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
+ }
75
78
}
76
79
77
80
private val json = Json { prettyPrint = true }
0 commit comments