diff --git a/client/src/components/SidePanel/Agents/Tools/ToolsSection.tsx b/client/src/components/SidePanel/Agents/Tools/ToolsSection.tsx index d18f8bc2fe..e4bc5a3e5a 100644 --- a/client/src/components/SidePanel/Agents/Tools/ToolsSection.tsx +++ b/client/src/components/SidePanel/Agents/Tools/ToolsSection.tsx @@ -208,39 +208,39 @@ export default function ToolsSection({ agentId }: Props) { mcpServersMap ?? new Map(), ); - /** MCP servers still referenced by the agent's tools but absent from the available - * servers map (removed from config, or a legacy server-only token). The catalog is - * built from available servers, so these would otherwise be invisible and - * unremovable — surface them as removable "needs setup" rows, mirroring the old - * UnconfiguredMCPTool. */ - const orphanedMcpItems = useMemo( - () => - attachedMcpServers - .filter((name) => mcpServersMap?.has(name) !== true) - .map((name) => ({ + /** The attachable catalog omits consume-only servers, but their persisted + * tools still run through shared agents. Show every attached server absent + * from the selected catalog, retaining live metadata when available. */ + const unlistedMcpItems = useMemo(() => { + const listed = new Set(selected.filter((item) => item.kind === 'mcp').map((item) => item.id)); + return attachedMcpServers + .filter((name) => !listed.has(name)) + .map((name) => { + const server = mcpServersMap?.get(name); + return { kind: 'mcp', id: name, - name, - description: '', + name: server?.metadata?.name || name, + description: server?.metadata?.description ?? '', iconKey: 'mcp', - status: 'needs_setup', + status: server?.isConfigured ? undefined : 'needs_setup', toolCount: 0, - server: { + server: server ?? { serverName: name, tools: [], isConfigured: false, isConnected: false, metadata: { name, pluginKey: name, description: '' } as TPlugin, }, - })), - [attachedMcpServers, mcpServersMap], - ); + }; + }); + }, [attachedMcpServers, mcpServersMap, selected]); /** MCP rows show how many of the server's tools are enabled for this agent, not * the total the server exposes, so the count reflects what the agent can use. */ const toolItems = useMemo(() => { const enabled = new Set(tools); - const withCounts = selected + return [...selected, ...unlistedMcpItems] .filter((item) => item.kind !== 'skill') .map((item) => item.kind === 'mcp' @@ -252,8 +252,7 @@ export default function ToolsSection({ agentId }: Props) { } : item, ); - return [...withCounts, ...orphanedMcpItems]; - }, [selected, orphanedMcpItems, tools]); + }, [selected, unlistedMcpItems, tools]); const skillItems = useMemo(() => selected.filter((item) => item.kind === 'skill'), [selected]); return ( diff --git a/client/src/components/SidePanel/Agents/Tools/__tests__/ToolsSection.spec.tsx b/client/src/components/SidePanel/Agents/Tools/__tests__/ToolsSection.spec.tsx index d4226ad31e..5d2dae5ed0 100644 --- a/client/src/components/SidePanel/Agents/Tools/__tests__/ToolsSection.spec.tsx +++ b/client/src/components/SidePanel/Agents/Tools/__tests__/ToolsSection.spec.tsx @@ -2,11 +2,13 @@ import React from 'react'; import '@testing-library/jest-dom/extend-expect'; import { render, screen, fireEvent } from '@testing-library/react'; import type { AgentItem } from '../items/types'; -import { makeSkill } from 'test/itemFactories'; +import type { MCPServerInfo } from '~/common'; +import { makeSkill, makePlugin, makeMcpServer } from 'test/itemFactories'; import ToolsSection from '../ToolsSection'; let mockSelected: AgentItem[] = []; let mockAgentTools: string[] = []; +let mockMcpServers = new Map(); let mockFileEntries: { contextFiles: unknown[]; knowledgeFiles: unknown[]; @@ -33,7 +35,7 @@ jest.mock('~/Providers', () => ({ useAgentPanelContext: () => ({ agentsConfig: { capabilities: ['skills'] }, regularTools: [], - mcpServersMap: new Map(), + mcpServersMap: mockMcpServers, actions: [], }), })); @@ -45,7 +47,7 @@ jest.mock('~/data-provider', () => ({ jest.mock('~/hooks/MCP', () => ({ useRemoveMCPTool: () => ({ removeTool: jest.fn() }), - useVisibleTools: () => ({ toolIds: [], mcpServerNames: [] }), + useVisibleTools: jest.requireActual('~/hooks/MCP/useVisibleTools').useVisibleTools, })); jest.mock('../hooks', () => ({ @@ -58,7 +60,12 @@ jest.mock('../hooks', () => ({ jest.mock('../ToolRow', () => ({ __esModule: true, default: ({ item, onRemove }: { item: AgentItem; onRemove: (item: AgentItem) => void }) => ( - @@ -171,6 +178,7 @@ const skillItem: AgentItem = { beforeEach(() => { mockSelected = []; mockAgentTools = []; + mockMcpServers = new Map(); mockFileEntries = { contextFiles: [], knowledgeFiles: [], codeFiles: [] }; mockFormValues = {}; mockSetValue.mockClear(); @@ -230,6 +238,54 @@ describe('ToolsSection', () => { expect(screen.getByText('com_ui_tools_empty')).toBeInTheDocument(); }); + test.each([ + ['search_mcp_shared', '1'], + ['sys__server__sys_mcp_shared', '0'], + ['sys__all__sys_mcp_shared', '2'], + ['shared', '0'], + ['mcp_shared', '0'], + ])('shows an attached consume-only server referenced by %s', (token, count) => { + mockAgentTools = [token]; + mockMcpServers.set( + 'shared', + makeMcpServer({ + serverName: 'shared', + consumeOnly: true, + isConfigured: true, + tools: [ + { + tool_id: 'search_mcp_shared', + metadata: makePlugin({ pluginKey: 'search_mcp_shared' }), + }, + { tool_id: 'read_mcp_shared', metadata: makePlugin({ pluginKey: 'read_mcp_shared' }) }, + ], + }), + ); + + render(); + + const row = screen.getByRole('button', { name: 'remove-shared' }); + expect(row).toHaveTextContent(count); + expect(row).not.toHaveAttribute('data-status', 'needs_setup'); + expect(screen.queryByText('com_ui_tools_empty')).not.toBeInTheDocument(); + expect(mockSetValue).not.toHaveBeenCalled(); + }); + + test('does not show an unattached consume-only server', () => { + mockMcpServers.set('shared', makeMcpServer({ serverName: 'shared', consumeOnly: true })); + render(); + expect(screen.queryByRole('button', { name: 'remove-shared' })).not.toBeInTheDocument(); + }); + + test('keeps an attached missing server visible as needing setup', () => { + mockAgentTools = ['search_mcp_missing']; + render(); + expect(screen.getByRole('button', { name: 'remove-missing' })).toHaveAttribute( + 'data-status', + 'needs_setup', + ); + }); + test('counts every enumerable MCP tool when the server is attached by wildcard', () => { mockAgentTools = ['sys__all__sys_mcp_runtime']; mockSelected = [ diff --git a/client/src/hooks/MCP/__tests__/useVisibleTools.test.ts b/client/src/hooks/MCP/__tests__/useVisibleTools.test.ts index 20afbacd12..91097c1a67 100644 --- a/client/src/hooks/MCP/__tests__/useVisibleTools.test.ts +++ b/client/src/hooks/MCP/__tests__/useVisibleTools.test.ts @@ -63,4 +63,24 @@ describe('useVisibleTools', () => { expect(result.current.toolIds).toEqual(['web_search']); expect(result.current.mcpServerNames).toEqual([]); }); + + it.each(['Connector: Company', 'Connector__Company'])( + 'resolves a legacy prefixed attachment using %s', + (name) => { + const servers = new Map([['Connector: Company', {} as MCPServerInfo]]); + const { result } = renderHook(() => useVisibleTools([`mcp_${name}`], regularTools, servers)); + expect(result.current.mcpServerNames).toEqual(['Connector: Company']); + }, + ); + + it('gives a literal configured name ownership over a normalized alias', () => { + const servers = new Map([ + ['Connector: Company', {} as MCPServerInfo], + ['Connector__Company', {} as MCPServerInfo], + ]); + const { result } = renderHook(() => + useVisibleTools(['mcp_Connector__Company'], regularTools, servers), + ); + expect(result.current.mcpServerNames).toEqual(['Connector__Company']); + }); }); diff --git a/client/src/hooks/MCP/useVisibleTools.ts b/client/src/hooks/MCP/useVisibleTools.ts index 95998948cb..c59dca9560 100644 --- a/client/src/hooks/MCP/useVisibleTools.ts +++ b/client/src/hooks/MCP/useVisibleTools.ts @@ -34,6 +34,17 @@ export function useVisibleTools( const regularToolIds: string[] = []; for (const toolId of selectedToolIds ?? []) { + // Legacy plugin keys use mcp_, with a literal configured name + // taking precedence over another server's normalized alias. + const prefixedName = toolId.startsWith('mcp_') ? toolId.slice(4) : undefined; + let prefixedOwner: string | undefined; + if (prefixedName != null) { + prefixedOwner = mcpServersMap.has(prefixedName) ? prefixedName : aliases.get(prefixedName); + } + if (prefixedOwner != null) { + mcpServers.add(prefixedOwner); + continue; + } // MCP tools/servers if (toolId.includes(Constants.mcp_delimiter)) { const [, serverName] = splitMCPToolKey(toolId, candidates);