cherry-studio/src/main/services/urlschema/mcp-install.ts
kangfenmao 186f0ed06f feat(MCPSettings): enhance MCP server management and localization
- Added BuiltinMCPServersSection and McpResourcesSection components to display available MCP servers and resources.
- Updated navigation logic to redirect users to the MCP settings upon adding a server.
- Enhanced localization by adding new keys for built-in servers in multiple languages.
- Improved the SettingsPage layout by reordering menu items for better accessibility.
2025-07-10 12:39:01 +08:00

77 lines
2.0 KiB
TypeScript

import { nanoid } from '@reduxjs/toolkit'
import { IpcChannel } from '@shared/IpcChannel'
import { MCPServer } from '@types'
import Logger from 'electron-log'
import { windowService } from '../WindowService'
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.info('install MCP servers from urlschema: ', stringify)
const jsonConfig = JSON.parse(stringify)
Logger.info('install MCP servers from urlschema: ', 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:
console.error(`Unknown MCP protocol URL: ${url}`)
break
}
}