import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import type { Caller } from './auth.js'
import { registerTools, type ToolDeps } from './registry.js'
import { buildTools } from './tools/index.js'

const instructions = (stock: boolean) => `Read-only access to Znet's Magento store.
- Catalog, pricing${stock ? ', stock' : ''} and data-quality tools read a copy of the catalog that is synced from Magento every few minutes; results carry sync timestamps. Use sync_status if freshness matters.
- Order tools${stock ? ' and live stock checks' : ''} query Magento directly.${stock ? '' : '\n- Stock is not managed in Magento for this store, so there is no stock information.'}
- Store-specific data: pass a store view code (list_stores) — otherwise the default store view is used.
- Nothing here can change the store.`

/** A fresh MCP server per request (stateless Streamable HTTP), exposing only the caller's tools. */
export function createMcpServer(deps: ToolDeps, caller: Caller): McpServer {
  const stock = deps.env.CATALOG_STOCK_ENABLED
  const server = new McpServer({ name: 'znet-magento', title: 'Znet Magento (read-only)', version: '0.1.0' }, { instructions: instructions(stock) })
  registerTools(server, buildTools({ stock }), deps, caller)
  return server
}
