@@ -17,6 +17,7 @@ import kotlinx.coroutines.withContext
17
17
)
18
18
class McpConfigService (private val project : Project ) : PersistentStateComponent<McpConfigService.State> {
19
19
private val selectedTools = mutableMapOf<String , MutableSet <String >>()
20
+ private val cachedTools = mutableMapOf<Pair <String , String >, Tool > () // Cache of (serverName, toolName) -> Tool
20
21
data class State (
21
22
var toolSelections : Map <String , Set <String >> = emptyMap()
22
23
)
@@ -41,12 +42,21 @@ class McpConfigService(private val project: Project) : PersistentStateComponent<
41
42
fun addSelectedTool (serverName : String , toolName : String ) {
42
43
selectedTools.getOrPut(serverName) { mutableSetOf () }.add(toolName)
43
44
}
45
+
46
+ /* *
47
+ * Add a selected tool and cache the Tool object
48
+ */
49
+ fun addSelectedTool (serverName : String , tool : Tool ) {
50
+ addSelectedTool(serverName, tool.name)
51
+ cachedTools[Pair (serverName, tool.name)] = tool
52
+ }
44
53
45
54
fun removeSelectedTool (serverName : String , toolName : String ) {
46
55
selectedTools[serverName]?.remove(toolName)
47
56
if (selectedTools[serverName]?.isEmpty() == true ) {
48
57
selectedTools.remove(serverName)
49
58
}
59
+ cachedTools.remove(Pair (serverName, toolName))
50
60
}
51
61
52
62
fun isToolSelected (serverName : String , toolName : String ): Boolean {
@@ -57,11 +67,33 @@ class McpConfigService(private val project: Project) : PersistentStateComponent<
57
67
return selectedTools.mapValues { it.value.toSet() }
58
68
}
59
69
70
+ /* *
71
+ * Gets the selected tools as actual Tool objects
72
+ * If tools haven't been cached, it will attempt to retrieve them
73
+ */
74
+ suspend fun getSelectedToolObjects (): Map <String , Set <Tool >> {
75
+ val result = mutableMapOf<String , MutableSet <Tool >>()
76
+ // First try to get from cache
77
+ selectedTools.forEach { (serverName, toolNames) ->
78
+ val toolsForServer = mutableSetOf<Tool >()
79
+ toolNames.forEach { toolName ->
80
+ cachedTools[Pair (serverName, toolName)]?.let {
81
+ toolsForServer.add(it)
82
+ }
83
+ }
84
+ if (toolsForServer.isNotEmpty()) {
85
+ result[serverName] = toolsForServer
86
+ }
87
+ }
88
+ return result.mapValues { it.value.toSet() }
89
+ }
90
+
60
91
fun clearSelectedTools () {
61
92
selectedTools.clear()
93
+ cachedTools.clear()
62
94
}
63
95
64
- fun setSelectedTools (tools : Map <String , MutableSet <String >>) {
96
+ fun setSelectedTools (tools : Map <String , MutableSet <String >>, clearCache : Boolean = true ) {
65
97
selectedTools.clear()
66
98
selectedTools.putAll(tools)
67
99
// Update Sketch systemPromptPanel when tools change
@@ -75,6 +107,9 @@ class McpConfigService(private val project: Project) : PersistentStateComponent<
75
107
// }
76
108
// }
77
109
// }
110
+ if (clearCache) {
111
+ cachedTools.clear()
112
+ }
78
113
}
79
114
80
115
/* *
@@ -96,6 +131,10 @@ class McpConfigService(private val project: Project) : PersistentStateComponent<
96
131
try {
97
132
val tools = mcpServerManager.collectServerInfo(serverName, serverConfig)
98
133
allTools[serverName] = tools
134
+ // Cache the tools for later use
135
+ tools.forEach { tool ->
136
+ cachedTools[Pair (serverName, tool.name)] = tool
137
+ }
99
138
} catch (e: Exception ) {
100
139
// Log error but continue with other servers
101
140
allTools[serverName] = emptyList()
0 commit comments