Browse Source

补充 mcp服务工具过滤逻辑代码

weicu
liwei1dao 1 year ago
parent
commit
eee4fd3bb9
  1. 18
      local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt

18
local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt

@ -71,13 +71,13 @@ interface FunctionHandler {
*/ */
class MCPSubClient( class MCPSubClient(
private val serverId: String, private val serverId: String,
serverUrl: String, private val config: ServerConfig,
private val customClient: HttpClient? = null private val customClient: HttpClient? = null
) : AutoCloseable { ) : AutoCloseable {
private val TAG = "MCPSubClient[$serverId]" private val TAG = "MCPSubClient[$serverId]"
// 保存服务器URL // 保存服务器URL
private val serverUrl: String = serverUrl private val serverUrl: String = config.url
init { init {
Log.d(TAG, "MCPSubClient初始化,URL: $serverUrl") Log.d(TAG, "MCPSubClient初始化,URL: $serverUrl")
@ -90,7 +90,6 @@ class MCPSubClient(
version = "1.0.0" version = "1.0.0"
) )
) )
// 服务器提供的工具列表 // 服务器提供的工具列表
private var tools: List<ToolUnion> = emptyList() private var tools: List<ToolUnion> = emptyList()
@ -166,7 +165,8 @@ class MCPSubClient(
* 处理工具列表 * 处理工具列表
*/ */
private fun processToolsList(toolsResult: ListToolsResult?) { 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 inputType = tool.inputSchema?.type ?: "object"
val inputProperties = tool.inputSchema?.properties ?: emptyMap() val inputProperties = tool.inputSchema?.properties ?: emptyMap()
@ -486,7 +486,7 @@ class MCPClient(private val context: Context? = null) : AutoCloseable {
// 清除现有连接 // 清除现有连接
closeAllConnections() closeAllConnections()
val serverMap = mutableMapOf<String, String>() val serverMap = mutableMapOf<String, ServerConfig>()
var configLoaded = false var configLoaded = false
// 尝试解析传入的JSON配置 // 尝试解析传入的JSON配置
@ -506,7 +506,7 @@ class MCPClient(private val context: Context? = null) : AutoCloseable {
configMap.forEach { (serverId, serverConfig) -> configMap.forEach { (serverId, serverConfig) ->
val url = serverConfig.url val url = serverConfig.url
if (!url.isNullOrEmpty()) { if (!url.isNullOrEmpty()) {
serverMap[serverId] = url serverMap[serverId] = serverConfig
Log.d(TAG, "从配置中添加服务器 [$serverId]: $url") Log.d(TAG, "从配置中添加服务器 [$serverId]: $url")
} }
} }
@ -525,10 +525,10 @@ class MCPClient(private val context: Context? = null) : AutoCloseable {
// 创建并连接所有子客户端 // 创建并连接所有子客户端
val connectionResults = mutableMapOf<String, Boolean>() val connectionResults = mutableMapOf<String, Boolean>()
for ((serverId, serverUrl) in serverMap) { for ((serverId, serverConfig) in serverMap) {
try { try {
Log.d(TAG, "为服务器 [$serverId],[$serverUrl] 创建子客户端") Log.d(TAG, "为服务器 [$serverId],[${serverConfig.url}] 创建子客户端")
val subClient = MCPSubClient(serverId, serverUrl, sharedHttpClient) val subClient = MCPSubClient(serverId, serverConfig, sharedHttpClient)
val connected = subClient.connect() val connected = subClient.connect()
if (connected) { if (connected) {

Loading…
Cancel
Save