'use client' import * as React from 'react' import { useQuery } from '@tanstack/react-query' import { useT } from '@open-mercato/shared/lib/i18n/context' import { apiCall } from '@open-mercato/ui/backend/utils/apiCall' import { CollapsibleSection } from '@open-mercato/ui/backend/SectionHeader' import { EmptyState } from '@open-mercato/ui/backend/EmptyState' import { ErrorMessage, LoadingMessage } from '@open-mercato/ui/backend/detail' import { StatusBadge } from '@open-mercato/ui/primitives/status-badge' import { Badge } from '@open-mercato/ui/primitives/badge' type McpMutationMode = 'direct' | 'deny' | 'approval' type McpHiddenReason = | 'expose_false' | 'denylist' | 'not_allowlisted' | 'mutation_mode' | 'agent_tools_off' | 'agent_unavailable' export type McpExposureTool = { name: string module: string exposed: boolean hiddenReason: McpHiddenReason | null isMutation: boolean agentRef: { agentId: string; engine: string } | null } export type McpExposureView = { mutationMode: McpMutationMode allowlist: string[] | null denylist: string[] agentToolsEnabled: boolean tools: McpExposureTool[] } type TranslateFn = ReturnType async function fetchMcpExposure(): Promise { const result = await apiCall('/api/ai_assistant/tools?view=mcp') if (!result.ok || !result.result) throw new Error('[internal] Failed to fetch MCP exposure') return result.result } function mutationModeLabel(t: TranslateFn, mode: McpMutationMode): string { switch (mode) { case 'deny': return t('ai_assistant.mcp.exposure.mutationMode.deny', 'Deny: mutation tools are hidden and refused') case 'approval': return t('ai_assistant.mcp.exposure.mutationMode.approval', 'Approval: mutation calls wait for human approval') default: return t('ai_assistant.mcp.exposure.mutationMode.direct', 'Direct: mutation tools run without approval') } } function hiddenReasonLabel(t: TranslateFn, reason: McpHiddenReason | null): string { switch (reason) { case 'expose_false': return t('ai_assistant.mcp.exposure.hiddenReason.exposeFalse', 'Opted out of MCP by the tool') case 'denylist': return t('ai_assistant.mcp.exposure.hiddenReason.denylist', 'Matched by the denylist') case 'not_allowlisted': return t('ai_assistant.mcp.exposure.hiddenReason.notAllowlisted', 'Not on the allowlist') case 'mutation_mode': return t('ai_assistant.mcp.exposure.hiddenReason.mutationMode', 'Hidden by the mutation mode') case 'agent_tools_off': return t('ai_assistant.mcp.exposure.hiddenReason.agentToolsOff', 'Agent tools are disabled') case 'agent_unavailable': return t('ai_assistant.mcp.exposure.hiddenReason.agentUnavailable', 'The agent is unavailable') default: return t('ai_assistant.mcp.exposure.hidden', 'Hidden') } } function ConfigRow({ label, value }: { label: string; value: React.ReactNode }) { return (
{label}
{value}
) } export function McpExposureSectionContent({ view }: { view: McpExposureView }) { const t = useT() const none = t('ai_assistant.mcp.exposure.listNone', 'None') return (
0 ? {view.allowlist.join(', ')} : none } /> 0 ? {view.denylist.join(', ')} : none} />
{view.tools.length === 0 ? ( ) : (
    {view.tools.map((tool) => (
  • {tool.name}

    {tool.module}

    {tool.agentRef ? ( {t('ai_assistant.mcp.exposure.agentTool', 'Agent')} ) : null} {tool.isMutation ? ( {t('ai_assistant.mcp.exposure.mutationTool', 'Mutation')} ) : null} {tool.exposed ? ( {t('ai_assistant.mcp.exposure.exposed', 'Exposed')} ) : ( {hiddenReasonLabel(t, tool.hiddenReason)} )}
  • ))}
)}
) } export function McpExposureSection() { const t = useT() const exposureQuery = useQuery({ queryKey: ['ai-assistant', 'tools', 'mcp-exposure'], queryFn: fetchMcpExposure, staleTime: 60000, }) const view = exposureQuery.data const exposedCount = view ? view.tools.filter((tool) => tool.exposed).length : undefined return (

{t( 'ai_assistant.mcp.exposure.description', 'Tools you can use and whether an MCP client sees them, as resolved by this app from the OM_MCP_* environment variables. An MCP server started with its own CLI flags can differ.', )}

{exposureQuery.isLoading ? ( ) : exposureQuery.isError || !view ? ( ) : ( )}
) } export default McpExposureSection