From 630fc9671783f3f4a37575e9efcbbb987f3824cf Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Tue, 22 Apr 2025 19:26:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=20mcp=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E8=BF=87=E6=BB=A4=E9=80=BB=E8=BE=91=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../open_ai_service/MCPClient.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt index 681489d2f..de6725b5f 100644 --- a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt +++ b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt @@ -71,13 +71,13 @@ interface FunctionHandler { */ class MCPSubClient( private val serverId: String, - serverUrl: String, + private val config: ServerConfig, private val customClient: HttpClient? = null ) : AutoCloseable { private val TAG = "MCPSubClient[$serverId]" // 保存服务器URL - private val serverUrl: String = serverUrl + private val serverUrl: String = config.url init { Log.d(TAG, "MCPSubClient初始化,URL: $serverUrl") @@ -90,7 +90,6 @@ class MCPSubClient( version = "1.0.0" ) ) - // 服务器提供的工具列表 private var tools: List = emptyList() @@ -166,7 +165,8 @@ class MCPSubClient( * 处理工具列表 */ private fun processToolsList(toolsResult: ListToolsResult?) { - tools = toolsResult?.tools?.map { tool -> + tools = toolsResult?.tools?.filter { tool -> config.tools == null || config.tools.contains(tool.name) == true + }?.map { tool -> // 确保输入模式有效 val inputType = tool.inputSchema?.type ?: "object" val inputProperties = tool.inputSchema?.properties ?: emptyMap() @@ -486,7 +486,7 @@ class MCPClient(private val context: Context? = null) : AutoCloseable { // 清除现有连接 closeAllConnections() - val serverMap = mutableMapOf() + val serverMap = mutableMapOf() var configLoaded = false // 尝试解析传入的JSON配置 @@ -506,7 +506,7 @@ class MCPClient(private val context: Context? = null) : AutoCloseable { configMap.forEach { (serverId, serverConfig) -> val url = serverConfig.url if (!url.isNullOrEmpty()) { - serverMap[serverId] = url + serverMap[serverId] = serverConfig Log.d(TAG, "从配置中添加服务器 [$serverId]: $url") } } @@ -525,10 +525,10 @@ class MCPClient(private val context: Context? = null) : AutoCloseable { // 创建并连接所有子客户端 val connectionResults = mutableMapOf() - for ((serverId, serverUrl) in serverMap) { + for ((serverId, serverConfig) in serverMap) { try { - Log.d(TAG, "为服务器 [$serverId],[$serverUrl] 创建子客户端") - val subClient = MCPSubClient(serverId, serverUrl, sharedHttpClient) + Log.d(TAG, "为服务器 [$serverId],[${serverConfig.url}] 创建子客户端") + val subClient = MCPSubClient(serverId, serverConfig, sharedHttpClient) val connected = subClient.connect() if (connected) {