mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
* Feature: Dynamic MCP Server with Full UI Management * 🚦 feat: Add MCP Connection Status icons to MCPBuilder panel (#10805) * feature: Add MCP server connection status icons to MCPBuilder panel * refactor: Simplify MCPConfigDialog rendering in MCPBuilderPanel --------- Co-authored-by: Atef Bellaaj <slalom.bellaaj@external.daimlertruck.com> Co-authored-by: Danny Avila <danny@librechat.ai> * fix: address code review feedback for MCP server management - Fix OAuth secret preservation to avoid mutating input parameter by creating a merged config copy in ServerConfigsDB.update() - Improve error handling in getResourcePermissionsMap to propagate critical errors instead of silently returning empty Map - Extract duplicated MCP server filter logic by exposing selectableServers from useMCPServerManager hook and using it in MCPSelect component * test: Update PermissionService tests to throw errors on invalid resource types - Changed the test for handling invalid resource types to ensure it throws an error instead of returning an empty permissions map. - Updated the expectation to check for the specific error message when an invalid resource type is provided. * feat: Implement retry logic for MCP server creation to handle race conditions - Enhanced the createMCPServer method to include retry logic with exponential backoff for handling duplicate key errors during concurrent server creation. - Updated tests to verify that all concurrent requests succeed and that unique server names are generated. - Added a helper function to identify MongoDB duplicate key errors, improving error handling during server creation. * refactor: StatusIcon to use CircleCheck for connected status - Replaced the PlugZap icon with CircleCheck in the ConnectedStatusIcon component to better represent the connected state. - Ensured consistent icon usage across the component for improved visual clarity. * test: Update AccessControlService tests to throw errors on invalid resource types - Modified the test for invalid resource types to ensure it throws an error with a specific message instead of returning an empty permissions map. - This change enhances error handling and improves test coverage for the AccessControlService. * fix: Update error message for missing server name in MCP server retrieval - Changed the error message returned when the server name is not provided from 'MCP ID is required' to 'Server name is required' for better clarity and accuracy in the API response. --------- Co-authored-by: Atef Bellaaj <slalom.bellaaj@external.daimlertruck.com> Co-authored-by: Danny Avila <danny@librechat.ai>
124 lines
4.1 KiB
TypeScript
124 lines
4.1 KiB
TypeScript
import React, { memo, useCallback } from 'react';
|
|
import { PermissionTypes, Permissions } from 'librechat-data-provider';
|
|
import { MultiSelect, MCPIcon } from '@librechat/client';
|
|
import MCPServerStatusIcon from '~/components/MCP/MCPServerStatusIcon';
|
|
import MCPConfigDialog from '~/components/MCP/MCPConfigDialog';
|
|
import { useBadgeRowContext } from '~/Providers';
|
|
import { useHasAccess } from '~/hooks';
|
|
|
|
function MCPSelectContent() {
|
|
const { conversationId, mcpServerManager } = useBadgeRowContext();
|
|
const {
|
|
localize,
|
|
isPinned,
|
|
mcpValues,
|
|
isInitializing,
|
|
placeholderText,
|
|
batchToggleServers,
|
|
getConfigDialogProps,
|
|
getServerStatusIconProps,
|
|
selectableServers,
|
|
} = mcpServerManager;
|
|
|
|
const renderSelectedValues = useCallback(
|
|
(
|
|
values: string[],
|
|
placeholder?: string,
|
|
items?: (string | { label: string; value: string })[],
|
|
) => {
|
|
if (values.length === 0) {
|
|
return placeholder || localize('com_ui_select_placeholder');
|
|
}
|
|
if (values.length === 1) {
|
|
const selectedItem = items?.find((i) => typeof i !== 'string' && i.value == values[0]);
|
|
return selectedItem && typeof selectedItem !== 'string' ? selectedItem.label : values[0];
|
|
}
|
|
return localize('com_ui_x_selected', { 0: values.length });
|
|
},
|
|
[localize],
|
|
);
|
|
|
|
const renderItemContent = useCallback(
|
|
(serverName: string, defaultContent: React.ReactNode) => {
|
|
const statusIconProps = getServerStatusIconProps(serverName);
|
|
const isServerInitializing = isInitializing(serverName);
|
|
|
|
/**
|
|
Common wrapper for the main content (check mark + text).
|
|
Ensures Check & Text are adjacent and the group takes available space.
|
|
*/
|
|
const mainContentWrapper = (
|
|
<button
|
|
type="button"
|
|
className={`flex flex-grow items-center rounded bg-transparent p-0 text-left transition-colors focus:outline-none ${
|
|
isServerInitializing ? 'opacity-50' : ''
|
|
}`}
|
|
tabIndex={0}
|
|
disabled={isServerInitializing}
|
|
>
|
|
{defaultContent}
|
|
</button>
|
|
);
|
|
|
|
const statusIcon = statusIconProps && <MCPServerStatusIcon {...statusIconProps} />;
|
|
|
|
if (statusIcon) {
|
|
return (
|
|
<div className="flex w-full items-center justify-between">
|
|
{mainContentWrapper}
|
|
<div className="ml-2 flex items-center">{statusIcon}</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return mainContentWrapper;
|
|
},
|
|
[getServerStatusIconProps, isInitializing],
|
|
);
|
|
|
|
if (!isPinned && mcpValues?.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const configDialogProps = getConfigDialogProps();
|
|
return (
|
|
<>
|
|
<MultiSelect
|
|
items={selectableServers.map((s) => ({
|
|
label: s.config.title || s.serverName,
|
|
value: s.serverName,
|
|
}))}
|
|
selectedValues={mcpValues ?? []}
|
|
setSelectedValues={batchToggleServers}
|
|
renderSelectedValues={renderSelectedValues}
|
|
renderItemContent={renderItemContent}
|
|
placeholder={placeholderText}
|
|
popoverClassName="min-w-fit"
|
|
className="badge-icon min-w-fit"
|
|
selectIcon={<MCPIcon className="icon-md text-text-primary" />}
|
|
selectItemsClassName="border border-blue-600/50 bg-blue-500/10 hover:bg-blue-700/10"
|
|
selectClassName="group relative inline-flex items-center justify-center md:justify-start gap-1.5 rounded-full border border-border-medium text-sm font-medium transition-all md:w-full size-9 p-2 md:p-3 bg-transparent shadow-sm hover:bg-surface-hover hover:shadow-md active:shadow-inner"
|
|
/>
|
|
{configDialogProps && (
|
|
<MCPConfigDialog {...configDialogProps} conversationId={conversationId} />
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
function MCPSelect() {
|
|
const { mcpServerManager } = useBadgeRowContext();
|
|
const { selectableServers } = mcpServerManager;
|
|
const canUseMcp = useHasAccess({
|
|
permissionType: PermissionTypes.MCP_SERVERS,
|
|
permission: Permissions.USE,
|
|
});
|
|
|
|
if (!canUseMcp || !selectableServers || selectableServers.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return <MCPSelectContent />;
|
|
}
|
|
|
|
export default memo(MCPSelect);
|