Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

6.6 KB
Raw Blame
import { create } from 'zustand'
import { getServiceHub } from '@/hooks/useServiceHub'
import { EXTENSION_SERVER_PREFIX } from '@/lib/extensions/tools'

// Define the structure of an MCP server configuration
export type MCPServerConfig = {
  command: string
  args: string[]
  env: Record<string, string>
  active?: boolean
  type?: 'stdio' | 'http' | 'sse'
  url?: string
  headers?: Record<string, string>
  timeout?: number
  official?: boolean
  // Capability tags used by the orchestrator to route requests (e.g. ["web", "filesystem"])
  capabilities?: string[]
  // Short description of what the server provides, used by the intent classifier
  description?: string
}

// Define the structure of all MCP servers
export type MCPServers = {
  [key: string]: MCPServerConfig
}

export type MCPSettings = {
  toolCallTimeoutSeconds: number
  baseRestartDelayMs: number
  maxRestartDelayMs: number
  backoffMultiplier: number
  enableSmartToolRouting: boolean
  /** When smart routing is on, use a dedicated (e.g. smaller) model for routing instead of the chat model. */
  useLightweightRouterModel: boolean
  routerModelProvider: string
  routerModelId: string
  /**
   * Per-tool-result character cap applied before the result enters conversation
   * history, so one oversized result cannot exhaust the model's context. `0`
   * disables the cap.
   */
  maxToolOutputChars: number
}

export const DEFAULT_MCP_SETTINGS: MCPSettings = {
  toolCallTimeoutSeconds: 30,
  baseRestartDelayMs: 1000,
  maxRestartDelayMs: 30000,
  backoffMultiplier: 2,
  enableSmartToolRouting: true,
  useLightweightRouterModel: false,
  routerModelProvider: '',
  routerModelId: '',
  maxToolOutputChars: 40000,
}

type MCPServerStoreState = {
  open: boolean
  mcpServers: MCPServers
  settings: MCPSettings
  loading: boolean
  deletedServerKeys: string[]
  getServerConfig: (key: string) => MCPServerConfig | undefined
  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
  setSettings: (settings: MCPSettings) => void
  updateSettings: (partial: Partial<MCPSettings>) => void
  saveSettings: (partial: Partial<MCPSettings>) => Promise<void>
  syncServers: () => Promise<void>
  syncServersAndRestart: () => Promise<void>
}

/**
 * `ext:` names belong to extensions' tool servers. An MCP server under one
 * could be taken for that extension wherever tools are shown or trusted by
 * server, so the dialog refuses such names and the store, which the JSON
 * editors and rename reach directly, drops them too.
 */
export const isReservedServerName = (name: string): boolean =>
  name.trim().startsWith(EXTENSION_SERVER_PREFIX)

const refusedName = (name: string): boolean => {
  if (!isReservedServerName(name)) return false
  console.warn(`MCP server name "${name}" is reserved for extensions`)
  return true
}

export const useMCPServers = create<MCPServerStoreState>()((set, get) => ({
  open: true,
  mcpServers: {}, // Start with empty object
  settings: { ...DEFAULT_MCP_SETTINGS },
  loading: false,
  deletedServerKeys: [],
  setLeftPanel: (value) => set({ open: value }),
  getServerConfig: (key) => {
    const mcpServers = get().mcpServers
    // Return the server configuration if it exists, otherwise return undefined
    return mcpServers[key] ? mcpServers[key] : undefined
  },
  // Add a new MCP server or update if the key already exists
  addServer: (key, config) =>
    set((state) => {
      if (refusedName(key)) return state
      // 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 }
    }),

  // Edit an existing MCP server configuration
  editServer: (key, config) =>
    set((state) => {
      // Only proceed if the server exists
      if (!state.mcpServers[key]) return state

      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
      if (refusedName(newKey)) 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 }
      return { mcpServers }
    }),
  setSettings: (settings) =>
    set(() => ({
      settings: {
        ...DEFAULT_MCP_SETTINGS,
        ...settings,
      },
    })),
  updateSettings: (partial) =>
    set((state) => ({
      settings: {
        ...state.settings,
        ...partial,
      },
    })),
  // Delete an MCP server by key
  deleteServer: (key) =>
    set((state) => {
      // Create a copy of the current state
      const updatedServers = { ...state.mcpServers }

      // Delete the server if it exists
      if (updatedServers[key]) {
        delete updatedServers[key]
      }
      return {
        mcpServers: updatedServers,
        deletedServerKeys: [...state.deletedServerKeys, key],
      }
    }),
  // Commit settings to the UI only after persistence succeeds. A failed write
  // must remain retryable, rather than appearing saved in the store.
  saveSettings: async (partial) => {
    const settings = { ...get().settings, ...partial }
    await getServiceHub().mcp().updateMCPConfig(
      JSON.stringify({ mcpServers: get().mcpServers, mcpSettings: settings })
    )
    set({ settings })
  },
  syncServers: async () => {
    const { mcpServers, settings } = get()
    await getServiceHub().mcp().updateMCPConfig(
      JSON.stringify({
        mcpServers,
        mcpSettings: settings,
      })
    )
  },
  syncServersAndRestart: async () => {
    const { mcpServers, settings } = get()
    await getServiceHub().mcp().updateMCPConfig(
      JSON.stringify({
        mcpServers,
        mcpSettings: settings,
      })
    ).then(() => getServiceHub().mcp().restartMCPServers())
  },
}))