/**
  MCP (Model Context Protocol) integration for the agency agent. Loads tools
  from configured MCP servers and gates every call through the policy handler.
*/
import {
  _isMcpAvailable,
  _readProjectMcpConfig,
  _mergeMcpServers,
  _loadMcpTools,
  _loadMcpToolsWithStatus
} from "agency-lang/stdlib-lib/mcp.js"

/// The result of loadMcpToolsWithStatus: the flat tool list plus a per-server
/// status map ("connected" / "unavailable").
export type McpLoadResult = {
  tools: any[];
  status: Record<string, string>
}

// The effect an MCP tool call raises before its JSON-RPC fires. Raised from TS
// (lib/stdlib/mcpGate.ts) via agency.interrupt with this exact name; declared
// here so policy authors can write `handle mcp::call` blocks inspecting e.args.
effect mcp::call {
  server: string,
  tool: string,
  args: Record<string, any>
}

export def isMcpAvailable(): boolean {
  """True when the @agency-lang/mcp package is installed and reachable."""
  return _isMcpAvailable()
}

export def readProjectMcpConfig(dir: string): Record<string, any> {
  """Read the mcpServers block from the project agency.json under `dir`."""
  return _readProjectMcpConfig(dir)
}

export def mergeMcpServers(global: Record<string, any>, project: Record<string, any>): Record<string, any> {
  """Merge global and project mcpServers configs (project wins on collision)."""
  return _mergeMcpServers(global, project)
}

export def loadMcpTools(merged: Record<string, any>, onOAuthRequired?): any[] {
  """Load gated MCP tools for every server in `merged`. Returns [] when the
  package is absent, incompatible, or no servers are configured."""
  return _loadMcpTools(merged, onOAuthRequired)
}

export def loadMcpToolsWithStatus(merged: Record<string, any>, onOAuthRequired?): McpLoadResult {
  """Like loadMcpTools, but also returns a per-server status map for /mcp."""
  return _loadMcpToolsWithStatus(merged, onOAuthRequired)
}
