cherry-studio/src/main/services/urlschema/mcp-install.ts
fullex c2086fdb15
refactor[Logger]: strict type check for Logger (#8363)
* fix: strict type check of logger

* feat: logger format in renderer

* fix: error type
2025-07-23 13:24:03 +08:00

79 lines
2.1 KiB
TypeScript

import { loggerService } from '@logger'
import { nanoid } from '@reduxjs/toolkit'
import { IpcChannel } from '@shared/IpcChannel'
import { MCPServer } from '@types'
import { windowService } from '../WindowService'
const logger = loggerService.withContext('URLSchema:handleMcpProtocolUrl')
function installMCPServer(server: MCPServer) {
const mainWindow = windowService.getMainWindow()
if (!server.id) {
server.id = nanoid()
}
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send(IpcChannel.Mcp_AddServer, server)
}
}
function installMCPServers(servers: Record<string, MCPServer>) {
for (const name in servers) {
const server = servers[name]
if (!server.name) {
server.name = name
}
installMCPServer(server)
}
}
export function handleMcpProtocolUrl(url: URL) {
const params = new URLSearchParams(url.search)
switch (url.pathname) {
case '/install': {
// jsonConfig example:
// {
// "mcpServers": {
// "everything": {
// "command": "npx",
// "args": [
// "-y",
// "@modelcontextprotocol/server-everything"
// ]
// }
// }
// }
// cherrystudio://mcp/install?servers={base64Encode(JSON.stringify(jsonConfig))}
const data = params.get('servers')
if (data) {
const stringify = Buffer.from(data, 'base64').toString('utf8')
logger.debug(`install MCP servers from urlschema: ${stringify}`)
const jsonConfig = JSON.parse(stringify)
logger.debug(`install MCP servers from urlschema: ${JSON.stringify(jsonConfig)}`)
// support both {mcpServers: [servers]}, [servers] and {server}
if (jsonConfig.mcpServers) {
installMCPServers(jsonConfig.mcpServers)
} else if (Array.isArray(jsonConfig)) {
for (const server of jsonConfig) {
installMCPServer(server)
}
} else {
installMCPServer(jsonConfig)
}
}
windowService.getMainWindow()?.show()
break
}
default:
logger.error(`Unknown MCP protocol URL: ${url}`)
break
}
}