import type { ResolvedMcpServer } from '../types.js' import { toManagedClaudeDesktopName } from './claudeDesktop.js' function escapeToml(value: string): string { return value.replaceAll('\\', '\\\\').replaceAll('"', '\\"') } function quoteTomlKey(key: string): string { return `"${escapeToml(key)}"` } export interface RenderResult { content: string warnings: string[] } export interface TomlRenderOptions { /** Sub-table used for HTTP headers: Codex calls it `http_headers`, Grok calls it `headers`. */ headersTable: string /** Emit `type = "sse"` for legacy SSE servers (Grok) instead of a plain url (Codex). */ emitSseType: boolean label: string } const CODEX_TOML_OPTIONS: TomlRenderOptions = { headersTable: 'http_headers', emitSseType: false, label: 'Codex' } const GROK_TOML_OPTIONS: TomlRenderOptions = { headersTable: 'headers', emitSseType: true, label: 'Grok' } /** * Render MCP servers into the `[mcp_servers.*]` TOML dialect shared by Codex and Grok Build. * * @param servers - Resolved MCP servers * @param options - Dialect differences between the two tools */ export function renderMcpToml(servers: ResolvedMcpServer[], options: TomlRenderOptions): RenderResult { const warnings: string[] = [] const lines: string[] = ['# Generated by agents sync. Do not edit manually.', ''] for (const server of servers) { lines.push(`[mcp_servers.${quoteTomlKey(server.name)}]`) if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in ${options.label} output.`) lines.pop() continue } lines.push(`command = "${escapeToml(server.command)}"`) lines.push( `args = [${(server.args ?? []).map((arg) => `"${escapeToml(arg)}"`).join(', ')}]`, ) if (server.cwd) { lines.push(`cwd = "${escapeToml(server.cwd)}"`) } } else { if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in ${options.label} output.`) lines.pop() continue } lines.push(`url = "${escapeToml(server.url)}"`) if (server.transport === 'sse') { if (options.emitSseType) { lines.push('type = "sse"') } else { warnings.push( `Server "${server.name}" uses the deprecated sse transport; rendering as URL for ${options.label} compatibility.`, ) } } if (server.bearerTokenEnvVar) { lines.push(`bearer_token_env_var = "${escapeToml(server.bearerTokenEnvVar)}"`) } } lines.push('enabled = true') if (typeof server.connectTimeout === 'number') { lines.push(`startup_timeout_sec = ${String(Math.max(1, Math.round(server.connectTimeout / 1000)))}`) } if (typeof server.timeout === 'number') { lines.push(`tool_timeout_sec = ${String(Math.max(1, Math.round(server.timeout / 1000)))}`) } lines.push('') if (server.env && Object.keys(server.env).length > 0) { lines.push(`[mcp_servers.${quoteTomlKey(server.name)}.env]`) for (const key of Object.keys(server.env).sort()) { lines.push(`${quoteTomlKey(key)} = "${escapeToml(server.env[key])}"`) } lines.push('') } if (server.headers && Object.keys(server.headers).length > 0) { lines.push(`[mcp_servers.${quoteTomlKey(server.name)}.${options.headersTable}]`) for (const key of Object.keys(server.headers).sort()) { lines.push(`${quoteTomlKey(key)} = "${escapeToml(server.headers[key])}"`) } lines.push('') } } return { content: `${lines.join('\n')}\n`, warnings } } /** Render MCP servers for Codex, whose headers live in an `http_headers` table. */ export function renderCodexToml(servers: ResolvedMcpServer[]): RenderResult { return renderMcpToml(servers, CODEX_TOML_OPTIONS) } /** Render MCP servers for Grok Build, which uses a plain `headers` table and marks sse. */ export function renderGrokToml(servers: ResolvedMcpServer[]): RenderResult { return renderMcpToml(servers, GROK_TOML_OPTIONS) } export function renderGeminiServers(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Gemini output.`) continue } out[server.name] = { type: 'stdio', command: server.command, args: server.args ?? [], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Gemini output.`) continue } out[server.name] = { ...(server.transport === 'http' ? { httpUrl: server.url } : { url: server.url }), ...(server.headers ? { headers: server.headers } : {}) } } return { mcpServers: out, warnings } } /** * Builds a VS Code MCP servers map from a list of resolved MCP servers. * * @param servers - The resolved MCP servers to render * @returns An object with: * - `servers`: a mapping from server name to its VS Code MCP configuration. For `stdio` servers the entry contains `type: 'stdio'`, `command`, `args` (array), and optional `cwd` and `env`. For non-stdio servers the entry contains `type` (transport), `url`, and optional `headers`. * - `warnings`: an array of warnings for servers that were skipped or adjusted during rendering */ export function renderVscodeMcp(servers: ResolvedMcpServer[]): { servers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in VS Code MCP output.`) continue } out[server.name] = { type: 'stdio', command: server.command, args: server.args ?? [], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in VS Code MCP output.`) continue } out[server.name] = { type: server.transport, url: server.url, ...(server.headers ? { headers: server.headers } : {}) } } return { servers: out, warnings } } /** * Produce an Antigravity-compatible mapping of MCP servers and collect conversion warnings. * * @param servers - Array of resolved MCP server entries to convert * @returns An object with `mcpServers` (mapping server name -> Antigravity entry) and `warnings` (messages about skipped or adjusted servers). * `mcpServers` entries: * - For `transport === 'stdio'`: `{ command, args, cwd? , env? }` * - For non-stdio transports: `{ serverUrl, headers? }` * `warnings` includes notices for servers skipped due to missing `command` or `url`, and a note when legacy `sse` transports are rendered as `serverUrl`. */ export function renderAntigravityMcp(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Antigravity output.`) continue } out[server.name] = { command: server.command, args: server.args ?? [], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Antigravity output.`) continue } if (server.transport === 'sse') { warnings.push(`Server "${server.name}" uses the deprecated sse transport; rendering as serverUrl for Antigravity compatibility.`) } out[server.name] = { serverUrl: server.url, ...(server.headers ? { headers: server.headers } : {}) } } return { mcpServers: out, warnings } } /** * Render Copilot CLI's project MCP config. * * @param servers Resolved MCP servers from the shared registry. * @returns A Copilot CLI `mcpServers` map plus warnings for skipped entries. * Stdio servers without commands and remote servers without URLs are omitted. */ export function renderCopilotCliMcp(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Copilot CLI MCP output.`) continue } out[server.name] = { type: 'stdio', command: server.command, args: server.args ?? [], tools: ['*'], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Copilot CLI MCP output.`) continue } out[server.name] = { type: server.transport, url: server.url, tools: ['*'], ...(server.headers ? { headers: server.headers } : {}) } } return { mcpServers: out, warnings } } /** * Render Claude Desktop's local JSON config. Claude Desktop remote MCP servers * are account/UI-managed connectors, so only local stdio servers are emitted. */ export function renderClaudeDesktopMcp(servers: ResolvedMcpServer[], projectRoot: string): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { const name = toManagedClaudeDesktopName(projectRoot, server.name) if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Claude Desktop output.`) continue } out[name] = { type: 'stdio', command: server.command, args: server.args ?? [], ...(server.env ? { env: server.env } : {}) } if (server.cwd) { warnings.push( `Server "${server.name}" sets cwd, but Claude Desktop claude_desktop_config.json does not document cwd support; prefer absolute paths in command/args/env.`, ) } } else { warnings.push( `Server "${server.name}" uses ${server.transport} transport; skipped in Claude Desktop output. Remote MCP servers are managed as Claude custom connectors, not in claude_desktop_config.json.`, ) } } return { mcpServers: out, warnings } } export function renderWindsurfMcp(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Windsurf output.`) continue } out[server.name] = { command: server.command, args: server.args ?? [], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Windsurf output.`) continue } out[server.name] = { serverUrl: server.url, ...(server.headers ? { headers: server.headers } : {}) } } return { mcpServers: out, warnings } } /** A server entry in the OpenCode config, in either the V2 or the legacy shape. */ export type OpencodeServerEntry = Record /** * Render MCP servers in the OpenCode V2 `mcp.servers` shape. * * This is the canonical form kept in the preview. Sync converts each entry with * `toLegacyOpencodeServer` when the target config still uses the legacy shape. */ export function renderOpencodeServers(servers: ResolvedMcpServer[]): { servers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { const connection = renderOpencodeConnection(server, warnings) if (!connection) continue const options = server.opencode ?? {} const timeout = { ...renderOpencodeSharedTimeout(server, warnings), ...options.timeout } out[server.name] = { ...connection, ...(Object.keys(timeout).length > 0 ? { timeout } : {}), ...renderOpencodeOAuth(server, warnings), ...(options.disabled !== undefined ? { disabled: options.disabled } : {}), ...(options.codemode !== undefined ? { codemode: options.codemode } : {}), ...(options.pool !== undefined ? { pool: options.pool } : {}), ...(options.protocol !== undefined ? { protocol: options.protocol } : {}) } } return { servers: out, warnings } } function renderOpencodeConnection(server: ResolvedMcpServer, warnings: string[]): OpencodeServerEntry | null { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in OpenCode output.`) return null } return { type: 'local', command: [server.command, ...(server.args ?? [])], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { environment: server.env } : {}) } } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in OpenCode output.`) return null } return { type: 'remote', url: server.url, ...(server.headers ? { headers: server.headers } : {}) } } /** * OpenCode reads a legacy `timeout` as the startup, catalog and execution timeout * at once, so the V2 entry sets all three. The same project then behaves the same * whichever shape sync writes. */ function renderOpencodeSharedTimeout( server: ResolvedMcpServer, warnings: string[], ): { startup?: number; catalog?: number; execution?: number } { if (server.timeout === undefined) return {} if (!Number.isSafeInteger(server.timeout) || server.timeout <= 0) { warnings.push(`Server "${server.name}": OpenCode requires a positive integer timeout; omitted.`) return {} } return { startup: server.timeout, catalog: server.timeout, execution: server.timeout } } function renderOpencodeOAuth(server: ResolvedMcpServer, warnings: string[]): { oauth?: false | Record } { const disabled = server.opencode?.oauth === false if (server.transport === 'stdio') { if (disabled) warnings.push(`Server "${server.name}": OpenCode OAuth applies only to remote servers; opencode.oauth omitted.`) return {} } const oauth = server.oauth ?? {} const config = { ...(oauth.clientId ? { client_id: oauth.clientId } : {}), ...(oauth.clientSecret ? { client_secret: oauth.clientSecret } : {}), ...(oauth.scopes ? { scope: oauth.scopes } : {}), ...(oauth.authServerMetadataUrl ? { auth_server_metadata_url: oauth.authServerMetadataUrl } : {}) } if (disabled) { if (Object.keys(config).length > 0) { warnings.push(`Server "${server.name}": OpenCode OAuth is disabled; OAuth settings omitted.`) } return { oauth: false } } return Object.keys(config).length > 0 ? { oauth: config } : {} } /** * Whether a V2 entry uses settings the legacy `mcp.` shape cannot express. * * Only the `opencode` options can produce these, so a shared field never moves a * project onto a config shape that OpenCode 1.x cannot read. */ export function needsOpencodeServersLayout(entry: OpencodeServerEntry): boolean { if (entry.codemode !== undefined || entry.pool !== undefined || entry.protocol !== undefined) return true return entry.timeout !== undefined && legacyTimeout(entry.timeout) === undefined } /** The legacy `timeout` that OpenCode expands to this V2 timeout, if there is one. */ function legacyTimeout(timeout: unknown): number | undefined { const { startup, catalog, execution } = (timeout ?? {}) as { startup?: number; catalog?: number; execution?: number } return startup !== undefined && startup === catalog && startup === execution ? startup : undefined } /** * Convert a V2 entry to the legacy `mcp.` shape that OpenCode 1.x reads and * OpenCode 2.x still accepts. Settings with no legacy field are reported in `dropped`. */ export function toLegacyOpencodeServer(entry: OpencodeServerEntry): { entry: OpencodeServerEntry dropped: string[] } { const { type, disabled, timeout, oauth, codemode, pool, protocol, ...connection } = entry const dropped = Object.entries({ codemode, pool, protocol }) .filter(([, value]) => value !== undefined) .map(([key]) => key) const legacy: OpencodeServerEntry = { type, enabled: disabled !== true, ...connection } if (timeout !== undefined) { const single = legacyTimeout(timeout) if (single !== undefined) legacy.timeout = single else dropped.push('timeout') } if (oauth === false) { legacy.oauth = false } else if (oauth && typeof oauth === 'object') { const v2 = oauth as Record const config = { ...(v2.client_id ? { clientId: v2.client_id } : {}), ...(v2.client_secret ? { clientSecret: v2.client_secret } : {}), ...(v2.scope ? { scope: v2.scope } : {}) } if (v2.auth_server_metadata_url) dropped.push('oauth.auth_server_metadata_url') if (Object.keys(config).length > 0) legacy.oauth = config } return { entry: legacy, dropped } } /** Render MCP servers for Junie, which reads a plain `mcpServers` map. */ export function renderJunieMcp(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Junie output.`) continue } out[server.name] = { command: server.command, args: server.args ?? [], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Junie output.`) continue } out[server.name] = { url: server.url, ...(server.headers ? { headers: server.headers } : {}) } } return { mcpServers: out, warnings } } /** * Render the project-scoped `.mcp.json` read by Claude Code and Copilot CLI. * * @param servers - Resolved MCP servers * @param options.tools - Emit the Copilot CLI `tools` allowlist (defaults to `['*']`) */ export function renderProjectMcpJson( servers: ResolvedMcpServer[], options?: { tools?: boolean; label?: string }, ): { mcpServers: Record; warnings: string[] } { const warnings: string[] = [] const out: Record = {} const label = options?.label ?? 'project .mcp.json' for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in ${label} output.`) continue } out[server.name] = { type: 'stdio', command: server.command, args: server.args ?? [], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}), ...(options?.tools ? { tools: server.tools ?? ['*'] } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in ${label} output.`) continue } out[server.name] = { type: server.transport, url: server.url, ...(server.headers ? { headers: server.headers } : {}), ...(server.headersHelper ? { headersHelper: server.headersHelper } : {}), ...(server.oauth ? { oauth: server.oauth } : {}), ...(options?.tools ? { tools: server.tools ?? ['*'] } : {}) } } return { mcpServers: out, warnings } } /** Render Amp's `amp.mcpServers` map (workspace `.amp/settings.json`). */ export function renderAmpMcp(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Amp output.`) continue } out[server.name] = { command: server.command, args: server.args ?? [], ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Amp output.`) continue } out[server.name] = { url: server.url, ...(server.headers ? { headers: server.headers } : {}) } } return { mcpServers: out, warnings } } /** Render Factory Droid's `.factory/mcp.json`. */ export function renderDroidMcp(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { const shared = { ...(typeof server.timeout === 'number' ? { timeout: server.timeout } : {}), ...(typeof server.connectTimeout === 'number' ? { connectTimeout: server.connectTimeout } : {}), ...(server.disabledTools ? { disabledTools: server.disabledTools } : {}) } if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Droid output.`) continue } out[server.name] = { type: 'stdio', command: server.command, args: server.args ?? [], ...(server.env ? { env: server.env } : {}), ...shared } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Droid output.`) continue } out[server.name] = { type: server.transport, url: server.url, ...(server.headers ? { headers: server.headers } : {}), ...(server.oauth ? { oauth: server.oauth } : {}), ...shared } } return { mcpServers: out, warnings } } /** Render Kilo's `mcp` block (`.kilo/kilo.jsonc`), which uses the local/remote shape. */ export function renderKiloMcp(servers: ResolvedMcpServer[]): { mcp: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Kilo output.`) continue } out[server.name] = { type: 'local', enabled: true, command: [server.command, ...(server.args ?? [])], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { environment: server.env } : {}), ...(typeof server.timeout === 'number' ? { timeout: server.timeout } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Kilo output.`) continue } out[server.name] = { type: 'remote', enabled: true, url: server.url, ...(server.headers ? { headers: server.headers } : {}), ...(typeof server.timeout === 'number' ? { timeout: server.timeout } : {}) } } return { mcp: out, warnings } } /** Render Devin CLI's `.devin/mcp_config.json`. */ export function renderDevinMcp(servers: ResolvedMcpServer[]): { mcpServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Devin output.`) continue } out[server.name] = { command: server.command, args: server.args ?? [], ...(server.cwd ? { cwd: server.cwd } : {}), ...(server.env ? { env: server.env } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Devin output.`) continue } out[server.name] = { url: server.url, ...(server.headers ? { headers: server.headers } : {}) } } return { mcpServers: out, warnings } } /** Render Zed's `context_servers` block. */ export function renderZedContextServers(servers: ResolvedMcpServer[]): { contextServers: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Zed output.`) continue } out[server.name] = { source: 'custom', command: server.command, args: server.args ?? [], env: server.env ?? {} } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Zed output.`) continue } out[server.name] = { source: 'custom', url: server.url, ...(server.headers ? { headers: server.headers } : {}) } } return { contextServers: out, warnings } } /** * Render Goose extensions. * * Goose stores secrets as `env_keys` (names resolved from the environment), never as values, * so env values are dropped here and only their keys are passed through. */ export function renderGooseExtensions(servers: ResolvedMcpServer[]): { extensions: Record warnings: string[] } { const warnings: string[] = [] const out: Record = {} for (const server of servers) { const timeout = typeof server.timeout === 'number' ? Math.max(1, Math.round(server.timeout / 1000)) : undefined if (server.transport === 'stdio') { if (!server.command) { warnings.push(`Server "${server.name}" has no command; skipped in Goose output.`) continue } const envKeys = Object.keys(server.env ?? {}).sort() if (envKeys.length > 0) { warnings.push( `Server "${server.name}": Goose reads secrets from the environment, so ${envKeys.join(', ')} are passed as env_keys and must exist in your shell.`, ) } out[server.name] = { name: server.name, type: 'stdio', enabled: true, cmd: server.command, args: server.args ?? [], ...(envKeys.length > 0 ? { env_keys: envKeys } : {}), ...(timeout ? { timeout } : {}) } continue } if (!server.url) { warnings.push(`Server "${server.name}" has no url; skipped in Goose output.`) continue } out[server.name] = { name: server.name, type: server.transport === 'sse' ? 'sse' : 'streamable_http', enabled: true, uri: server.url, ...(server.headers ? { headers: server.headers } : {}), ...(timeout ? { timeout } : {}) } } return { extensions: out, warnings } } /** * Render Cursor's `.cursor/mcp.json`. * * Same shape as VS Code, plus `envFile`, which Cursor documents and VS Code does not. */ export function renderCursorMcp(servers: ResolvedMcpServer[]): { servers: Record warnings: string[] } { const rendered = renderVscodeMcp(servers) const out: Record = {} for (const [name, value] of Object.entries(rendered.servers)) { const server = servers.find((item) => item.name === name) out[name] = server?.envFile ? { ...(value as Record), envFile: server.envFile } : value } return { servers: out, warnings: rendered.warnings } }