Files
dify/web/app/components/workflow/block-selector/context/mcp-tool-availability-context.tsx
yyh 2e0661aa90 refactor(web): align MCP availability context migration
- move MCP availability context to block-selector/context and update imports

- preserve sandbox gating, parent-provider inheritance, and blockedBy semantics

- add context tests on top of refactor baseline cases

- regenerate and prune eslint suppressions
2026-02-23 22:46:31 +08:00

64 lines
1.7 KiB
TypeScript

'use client'
import type { ReactNode } from 'react'
import { createContext, useContext, useMemo } from 'react'
type MCPToolAvailabilityContextValue = {
versionSupported?: boolean
sandboxEnabled?: boolean
}
const MCPToolAvailabilityContext = createContext<MCPToolAvailabilityContextValue | undefined>(undefined)
export type MCPToolAvailability = {
allowed: boolean
blockedBy?: 'version' | 'sandbox'
}
type ProviderProps = {
versionSupported?: boolean
sandboxEnabled?: boolean
children: ReactNode
}
export function MCPToolAvailabilityProvider({
versionSupported,
sandboxEnabled,
children,
}: ProviderProps): ReactNode {
const parent = useContext(MCPToolAvailabilityContext)
const value = useMemo<MCPToolAvailabilityContextValue>(() => ({
versionSupported: versionSupported ?? parent?.versionSupported,
sandboxEnabled: sandboxEnabled ?? parent?.sandboxEnabled,
}), [versionSupported, sandboxEnabled, parent])
return (
<MCPToolAvailabilityContext.Provider value={value}>
{children}
</MCPToolAvailabilityContext.Provider>
)
}
// eslint-disable-next-line react-refresh/only-export-components
export function useMCPToolAvailability(): MCPToolAvailability {
const context = useContext(MCPToolAvailabilityContext)
if (!context)
return { allowed: true }
const versionAllowed = context.versionSupported ?? true
const sandboxAllowed = context.sandboxEnabled ?? true
const allowed = versionAllowed && sandboxAllowed
let blockedBy: MCPToolAvailability['blockedBy']
if (!versionAllowed)
blockedBy = 'version'
else if (!sandboxAllowed)
blockedBy = 'sandbox'
if (blockedBy)
return { allowed, blockedBy }
return { allowed }
}