feat(mcp): add automatic MCP server registration for auto-install tool

- Implemented functionality to automatically register MCP servers when the '@cherry/mcp-auto-install' tool is called.
- Utilized nanoid for unique server ID generation and dispatched the new server to the Redux store.
This commit is contained in:
kangfenmao 2025-04-07 14:28:39 +08:00
parent 8191791036
commit 41b9f8dbd5
2 changed files with 20 additions and 7 deletions

View File

@ -169,13 +169,6 @@ const McpListContainer = styled(VStack)`
height: calc(100vh - var(--navbar-height)); height: calc(100vh - var(--navbar-height));
` `
const McpListHeader = styled(HStack)`
width: 100%;
padding: 10px 12px;
padding-bottom: 0;
justify-content: center;
`
const McpList = styled(Scrollbar)` const McpList = styled(Scrollbar)`
display: flex; display: flex;
flex: 1; flex: 1;

View File

@ -15,7 +15,9 @@ import {
SimpleStringSchema, SimpleStringSchema,
Tool as geminiTool Tool as geminiTool
} from '@google/generative-ai' } from '@google/generative-ai'
import { nanoid } from '@reduxjs/toolkit'
import store from '@renderer/store' import store from '@renderer/store'
import { addMCPServer } from '@renderer/store/mcp'
import { MCPServer, MCPTool, MCPToolResponse } from '@renderer/types' import { MCPServer, MCPTool, MCPToolResponse } from '@renderer/types'
import { ChatCompletionMessageToolCall, ChatCompletionTool } from 'openai/resources' import { ChatCompletionMessageToolCall, ChatCompletionTool } from 'openai/resources'
@ -232,6 +234,24 @@ export async function callMCPTool(tool: MCPTool): Promise<any> {
}) })
console.log(`[MCP] Tool called: ${tool.serverName} ${tool.name}`, resp) console.log(`[MCP] Tool called: ${tool.serverName} ${tool.name}`, resp)
if (tool.serverName === '@cherry/mcp-auto-install') {
if (resp.data) {
const mcpServer: MCPServer = {
id: `f${nanoid()}`,
name: resp.data.name,
description: resp.data.description,
baseUrl: resp.data.baseUrl,
command: resp.data.command,
args: resp.data.args,
env: resp.data.env,
registryUrl: '',
isActive: false
}
store.dispatch(addMCPServer(mcpServer))
}
}
return resp return resp
} catch (e) { } catch (e) {
console.error(`[MCP] Error calling Tool: ${tool.serverName} ${tool.name}`, e) console.error(`[MCP] Error calling Tool: ${tool.serverName} ${tool.name}`, e)