Skip to content

Commit 72d1b82

Browse files
committed
feat(mcp): implement tool collection from custom MCP servers #371
- Add collectServerInfos function to CustomMcpServerManager - Update McpPreviewEditor to use the new function for loading tools- Implement caching for server information to improve performance
1 parent 934c1db commit 72d1b82

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ class CustomMcpServerManager(val project: Project) {
111111
return "No such tool: ${tool.name} or failed to execute"
112112
}
113113

114+
suspend fun collectServerInfos(config: String): Map<String, List<Tool>> {
115+
if (cached.containsKey(config)) return cached[config]!!
116+
val mcpConfig = McpServer.load(config)
117+
if (mcpConfig == null) return emptyMap()
118+
119+
val toolsMap = mutableMapOf<String, List<Tool>>()
120+
mcpConfig.mcpServers.forEach { entry ->
121+
if (entry.value.disabled == true) return@forEach
122+
val tools = collectServerInfo(entry.key, entry.value)
123+
toolsMap[entry.key] = tools
124+
}
125+
126+
cached[config] = toolsMap
127+
return toolsMap
128+
}
129+
114130
companion object {
115131
fun instance(project: Project): CustomMcpServerManager {
116132
return project.getService(CustomMcpServerManager::class.java)

core/src/main/kotlin/cc/unitmesh/devti/mcp/editor/McpPreviewEditor.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.unitmesh.devti.mcp.editor
22

33
import cc.unitmesh.devti.mcp.client.CustomMcpServerManager
4+
import com.intellij.openapi.application.runReadAction
45
import com.intellij.openapi.editor.Editor
56
import com.intellij.openapi.fileEditor.FileEditor
67
import com.intellij.openapi.fileEditor.FileEditorState
@@ -33,6 +34,7 @@ import java.awt.GridLayout
3334
import java.beans.PropertyChangeListener
3435
import javax.swing.*
3536
import javax.swing.border.CompoundBorder
37+
import cc.unitmesh.devti.sketch.ui.patch.readText
3638

3739
/**
3840
* Display shire file render prompt and have a sample file as view
@@ -75,8 +77,9 @@ open class McpPreviewEditor(
7577
}
7678

7779
private fun loadTools() {
80+
val content = runReadAction { virtualFile.readText() }
7881
CoroutineScope(Dispatchers.IO).launch {
79-
allTools.putAll(mcpServerManager.collectServerInfos())
82+
allTools.putAll(mcpServerManager.collectServerInfos(content))
8083
SwingUtilities.invokeLater {
8184
addTools()
8285
}

0 commit comments

Comments
 (0)