fix(MCPService): clear cache on server close and refactor tool fetching logic

This commit is contained in:
Vaayne 2025-03-30 00:48:24 +08:00 committed by 亢奋猫
parent de8dbb2646
commit f5b675b356
2 changed files with 24 additions and 19 deletions

View File

@ -122,6 +122,8 @@ class McpService {
await client.close()
Logger.info(`[MCP] Closed server: ${serverKey}`)
this.clients.delete(serverKey)
CacheService.remove(`mcp:list_tool:${serverKey}`)
Logger.info(`[MCP] Cleared cache for server: ${serverKey}`)
} else {
Logger.warn(`[MCP] No client found for server: ${serverKey}`)
}
@ -150,7 +152,8 @@ class McpService {
async listTools(_: Electron.IpcMainInvokeEvent, server: MCPServer) {
const client = await this.initClient(server)
const cacheKey = `mcp:list_tool:${server.id}`
const serverKey = this.getServerKey(server)
const cacheKey = `mcp:list_tool:${serverKey}`
if (CacheService.has(cacheKey)) {
Logger.info(`[MCP] Tools from ${server.name} loaded from cache`)
const cachedTools = CacheService.get<MCPTool[]>(cacheKey)

View File

@ -82,6 +82,10 @@ const McpSettings: React.FC<Props> = ({ server }) => {
// Load tools on initial mount if server is active
useEffect(() => {
fetchTools()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const fetchTools = async () => {
if (server.isActive) {
try {
@ -100,9 +104,6 @@ const McpSettings: React.FC<Props> = ({ server }) => {
}
}
fetchTools()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
// Save the form data
const onSave = async () => {
setLoading(true)
@ -141,6 +142,7 @@ const McpSettings: React.FC<Props> = ({ server }) => {
await window.api.mcp.restartServer(mcpServer)
updateMCPServer({ ...mcpServer, isActive: true })
window.message.success({ content: t('settings.mcp.updateSuccess'), key: 'mcp-update-success' })
await fetchTools()
setLoading(false)
setIsFormChanged(false)
} catch (error: any) {