fix(MCPService): clear cache on server close and refactor tool fetching logic
This commit is contained in:
parent
de8dbb2646
commit
f5b675b356
@ -122,6 +122,8 @@ class McpService {
|
|||||||
await client.close()
|
await client.close()
|
||||||
Logger.info(`[MCP] Closed server: ${serverKey}`)
|
Logger.info(`[MCP] Closed server: ${serverKey}`)
|
||||||
this.clients.delete(serverKey)
|
this.clients.delete(serverKey)
|
||||||
|
CacheService.remove(`mcp:list_tool:${serverKey}`)
|
||||||
|
Logger.info(`[MCP] Cleared cache for server: ${serverKey}`)
|
||||||
} else {
|
} else {
|
||||||
Logger.warn(`[MCP] No client found for server: ${serverKey}`)
|
Logger.warn(`[MCP] No client found for server: ${serverKey}`)
|
||||||
}
|
}
|
||||||
@ -150,7 +152,8 @@ class McpService {
|
|||||||
|
|
||||||
async listTools(_: Electron.IpcMainInvokeEvent, server: MCPServer) {
|
async listTools(_: Electron.IpcMainInvokeEvent, server: MCPServer) {
|
||||||
const client = await this.initClient(server)
|
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)) {
|
if (CacheService.has(cacheKey)) {
|
||||||
Logger.info(`[MCP] Tools from ${server.name} loaded from cache`)
|
Logger.info(`[MCP] Tools from ${server.name} loaded from cache`)
|
||||||
const cachedTools = CacheService.get<MCPTool[]>(cacheKey)
|
const cachedTools = CacheService.get<MCPTool[]>(cacheKey)
|
||||||
|
|||||||
@ -82,27 +82,28 @@ const McpSettings: React.FC<Props> = ({ server }) => {
|
|||||||
|
|
||||||
// Load tools on initial mount if server is active
|
// Load tools on initial mount if server is active
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchTools = async () => {
|
|
||||||
if (server.isActive) {
|
|
||||||
try {
|
|
||||||
setLoadingServer(server.id)
|
|
||||||
const localTools = await window.api.mcp.listTools(server)
|
|
||||||
setTools(localTools)
|
|
||||||
// window.message.success(t('settings.mcp.toolsLoaded'))
|
|
||||||
} catch (error) {
|
|
||||||
window.message.error({
|
|
||||||
content: t('settings.mcp.toolsLoadError') + formatError(error),
|
|
||||||
key: 'mcp-tools-error'
|
|
||||||
})
|
|
||||||
} finally {
|
|
||||||
setLoadingServer(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fetchTools()
|
fetchTools()
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const fetchTools = async () => {
|
||||||
|
if (server.isActive) {
|
||||||
|
try {
|
||||||
|
setLoadingServer(server.id)
|
||||||
|
const localTools = await window.api.mcp.listTools(server)
|
||||||
|
setTools(localTools)
|
||||||
|
// window.message.success(t('settings.mcp.toolsLoaded'))
|
||||||
|
} catch (error) {
|
||||||
|
window.message.error({
|
||||||
|
content: t('settings.mcp.toolsLoadError') + formatError(error),
|
||||||
|
key: 'mcp-tools-error'
|
||||||
|
})
|
||||||
|
} finally {
|
||||||
|
setLoadingServer(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Save the form data
|
// Save the form data
|
||||||
const onSave = async () => {
|
const onSave = async () => {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
@ -141,6 +142,7 @@ const McpSettings: React.FC<Props> = ({ server }) => {
|
|||||||
await window.api.mcp.restartServer(mcpServer)
|
await window.api.mcp.restartServer(mcpServer)
|
||||||
updateMCPServer({ ...mcpServer, isActive: true })
|
updateMCPServer({ ...mcpServer, isActive: true })
|
||||||
window.message.success({ content: t('settings.mcp.updateSuccess'), key: 'mcp-update-success' })
|
window.message.success({ content: t('settings.mcp.updateSuccess'), key: 'mcp-update-success' })
|
||||||
|
await fetchTools()
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
setIsFormChanged(false)
|
setIsFormChanged(false)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user