Skip to content

Commit d25d2da

Browse files
committed
feat(customize): add MCP services test functionality
- Implement MCPServicesTestDialog for testing MCP services connection - Add test connection button in the CustomizeConfigurable UI - Use coroutine to asynchronously load and display server information - Handle loading state and exceptions in the UI
1 parent 2a0ef02 commit d25d2da

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

core/src/main/kotlin/cc/unitmesh/devti/settings/customize/CustomizeConfigurable.kt

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cc.unitmesh.devti.custom.schema.CUSTOM_PROMPTS_FILE_NAME
66
import cc.unitmesh.devti.custom.schema.MCP_SERVERS_FILE_NAME
77
import cc.unitmesh.devti.fullHeight
88
import cc.unitmesh.devti.fullWidthCell
9+
import cc.unitmesh.devti.mcp.client.CustomMcpServerManager
910
import cc.unitmesh.devti.provider.local.JsonTextProvider
1011
import cc.unitmesh.devti.settings.locale.LanguageChangedCallback.componentStateChanged
1112
import cc.unitmesh.devti.settings.locale.LanguageChangedCallback.jBLabel
@@ -16,7 +17,20 @@ import com.intellij.openapi.components.service
1617
import com.intellij.openapi.options.BoundConfigurable
1718
import com.intellij.openapi.project.Project
1819
import com.intellij.openapi.ui.DialogPanel
19-
import com.intellij.ui.dsl.builder.*
20+
import com.intellij.openapi.ui.DialogWrapper
21+
import com.intellij.ui.components.JBLoadingPanel
22+
import com.intellij.ui.dsl.builder.bindSelected
23+
import com.intellij.ui.dsl.builder.panel
24+
import com.intellij.ui.dsl.builder.toMutableProperty
25+
import com.intellij.ui.table.JBTable
26+
import io.modelcontextprotocol.kotlin.sdk.Tool
27+
import kotlinx.coroutines.CoroutineScope
28+
import kotlinx.coroutines.Dispatchers
29+
import kotlinx.coroutines.launch
30+
import kotlinx.coroutines.withContext
31+
import java.awt.BorderLayout
32+
import javax.swing.JComponent
33+
import javax.swing.table.DefaultTableModel
2034

2135
class CustomizeConfigurable(val project: Project) : BoundConfigurable(AutoDevBundle.message("customize.title")),
2236
Disposable {
@@ -85,6 +99,11 @@ class CustomizeConfigurable(val project: Project) : BoundConfigurable(AutoDevBun
8599
}
86100
row {
87101
cell(jBLabel("counit.mcp.services.placeholder", 1))
102+
103+
button(AutoDevBundle.message("settings.autodev.coder.testConnectionButton.tips")) {
104+
val dialog = McpServicesTestDialog(project)
105+
dialog.show()
106+
}
88107
}
89108

90109
row {
@@ -115,6 +134,64 @@ class CustomizeConfigurable(val project: Project) : BoundConfigurable(AutoDevBun
115134
}
116135
}
117136

137+
class McpServicesTestDialog(private val project: Project) : DialogWrapper(project) {
138+
private val loadingPanel = JBLoadingPanel(BorderLayout(), this.disposable)
139+
private val tableModel = DefaultTableModel(arrayOf("Server", "Tool Name", "Description"), 0)
140+
private val table = JBTable(tableModel)
141+
142+
init {
143+
title = "MCP Services Test Results"
144+
init()
145+
loadServices()
146+
}
147+
148+
override fun createCenterPanel(): JComponent {
149+
loadingPanel.add(table, BorderLayout.CENTER)
150+
loadingPanel.setLoadingText("Loading MCP services...")
151+
return loadingPanel
152+
}
153+
154+
private fun loadServices() {
155+
loadingPanel.startLoading()
156+
157+
CoroutineScope(Dispatchers.IO).launch {
158+
try {
159+
val serverManager = CustomMcpServerManager.instance(project)
160+
val serverInfos = serverManager.collectServerInfos()
161+
162+
withContext(Dispatchers.IO) {
163+
updateTable(serverInfos)
164+
loadingPanel.stopLoading()
165+
}
166+
} catch (e: Exception) {
167+
withContext(Dispatchers.IO) {
168+
tableModel.addRow(arrayOf("Error", e.message, ""))
169+
loadingPanel.stopLoading()
170+
}
171+
}
172+
}
173+
}
174+
175+
private fun updateTable(serverInfos: Map<String, List<Tool>>) {
176+
tableModel.rowCount = 0
177+
178+
if (serverInfos.isEmpty()) {
179+
tableModel.addRow(arrayOf("No servers found", "", ""))
180+
return
181+
}
182+
183+
serverInfos.forEach { (server, tools) ->
184+
if (tools.isEmpty()) {
185+
tableModel.addRow(arrayOf(server, "No tools found", ""))
186+
} else {
187+
tools.forEach { tool ->
188+
tableModel.addRow(arrayOf(server, tool.name, tool.description))
189+
}
190+
}
191+
}
192+
}
193+
}
194+
118195
override fun dispose() {
119196

120197
}

0 commit comments

Comments
 (0)