Merge pull request #6305 from menloresearch/fix/mcp-edit-and-sort

fix: mcp cleanup dropodown tool availabel and sort list
This commit is contained in:
Faisal Amir 2025-08-27 20:33:05 +07:00 committed by GitHub
commit a7a5a5ab67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 8 deletions

View File

@ -27,6 +27,11 @@ type MCPServerStoreState = {
setLeftPanel: (value: boolean) => void
addServer: (key: string, config: MCPServerConfig) => void
editServer: (key: string, config: MCPServerConfig) => void
renameServer: (
oldKey: string,
newKey: string,
config: MCPServerConfig
) => void
deleteServer: (key: string) => void
setServers: (servers: MCPServers) => void
syncServers: () => Promise<void>
@ -47,7 +52,10 @@ export const useMCPServers = create<MCPServerStoreState>()((set, get) => ({
// Add a new MCP server or update if the key already exists
addServer: (key, config) =>
set((state) => {
const mcpServers = { ...state.mcpServers, [key]: config }
// Remove the key first if it exists to maintain insertion order
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { [key]: _, ...restServers } = state.mcpServers
const mcpServers = { [key]: config, ...restServers }
return { mcpServers }
}),
@ -60,6 +68,27 @@ export const useMCPServers = create<MCPServerStoreState>()((set, get) => ({
const mcpServers = { ...state.mcpServers, [key]: config }
return { mcpServers }
}),
// Rename a server while preserving its position
renameServer: (oldKey, newKey, config) =>
set((state) => {
// Only proceed if the server exists
if (!state.mcpServers[oldKey]) return state
const entries = Object.entries(state.mcpServers)
const mcpServers: MCPServers = {}
// Rebuild the object with the same order, replacing the old key with the new key
entries.forEach(([key, serverConfig]) => {
if (key === oldKey) {
mcpServers[newKey] = config
} else {
mcpServers[key] = serverConfig
}
})
return { mcpServers }
}),
setServers: (servers) =>
set((state) => {
const mcpServers = { ...state.mcpServers, ...servers }

View File

@ -93,6 +93,7 @@ function MCPServers() {
mcpServers,
addServer,
editServer,
renameServer,
deleteServer,
syncServers,
syncServersAndRestart,
@ -137,22 +138,27 @@ function MCPServers() {
}
const handleSaveServer = async (name: string, config: MCPServerConfig) => {
toggleServer(name, false)
if (editingKey) {
// If server name changed, delete old one and add new one
// If server name changed, rename it while preserving position
if (editingKey !== name) {
deleteServer(editingKey)
addServer(name, config)
toggleServer(editingKey, false)
renameServer(editingKey, name, config)
toggleServer(name, true)
// Restart servers to update tool references with new server name
syncServersAndRestart()
} else {
toggleServer(editingKey, false)
editServer(editingKey, config)
toggleServer(editingKey, true)
syncServers()
}
} else {
// Add new server
toggleServer(name, false)
addServer(name, config)
}
syncServers()
toggleServer(name, true)
syncServers()
}
}
const handleEdit = (serverKey: string) => {