MCP

Run ai-cli as a stdio MCP server with typed tools for every command.

ai mcp starts a stdio MCP server that exposes every ai command as a typed tool. It speaks newline-delimited JSON-RPC 2.0 over stdin and stdout, serves the MCP 2026-07-28 protocol, and keeps serving clients that use the initialize handshake from 2025-11-25 or older. Diagnostics go to stderr.

ai mcp
ai mcp --profile media
ai mcp --profile all

--profile core|media|all selects the tools to expose. core is the default.

Setup

The server reads your key from its environment, like every ai command. Set AI_GATEWAY_API_KEY in your MCP client's config, as below. Many clients do not pass your shell environment to MCP servers. The key is never a tool input, so agents cannot read it or send it elsewhere.

fx (~/.fx/mcp.json):

{
  "mcp": {
    "ai": {
      "type": "local",
      "command": ["ai", "mcp"],
      "environment": {
        "AI_GATEWAY_API_KEY": "your-vercel-ai-gateway-api-key",
        "AI_CLI_OUTPUT_DIR": "/absolute/output/dir"
      }
    }
  }
}

Other clients (mcpServers):

{
  "mcpServers": {
    "ai": {
      "command": "ai",
      "args": ["mcp"],
      "env": {
        "AI_GATEWAY_API_KEY": "your-vercel-ai-gateway-api-key",
        "AI_CLI_OUTPUT_DIR": "/absolute/output/dir"
      }
    }
  }
}

opencode (opencode.json):

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "ai": {
      "type": "local",
      "command": ["ai", "mcp"],
      "environment": {
        "AI_GATEWAY_API_KEY": "your-vercel-ai-gateway-api-key",
        "AI_CLI_OUTPUT_DIR": "/absolute/output/dir"
      },
      "timeout": 600000
    }
  }
}

Claude Code:

claude mcp add ai -e AI_GATEWAY_API_KEY=your-vercel-ai-gateway-api-key -e AI_CLI_OUTPUT_DIR=/absolute/output/dir -- ai mcp

Codex:

codex mcp add ai --env AI_GATEWAY_API_KEY=your-vercel-ai-gateway-api-key --env AI_CLI_OUTPUT_DIR=/absolute/output/dir -- ai mcp
VariablePurpose
AI_GATEWAY_API_KEYVercel AI Gateway API key. Required.
AI_CLI_OUTPUT_DIRAbsolute directory for generated files. Without it, files land in each call's cwd.
AI_CLI_MCP_DRAIN_TIMEOUT_MSHow long shutdown waits for in-flight tool calls, in milliseconds (default 30000).
AI_GATEWAY_BASE_URL, AI_GATEWAY_TEAM_ID_OR_SLUGOptional Gateway overrides. Agents cannot set these.

Add --profile all to the command for the video and audio tools. opencode stops tool calls after 30 seconds unless timeout is raised, as in the example.

Profiles

ToolProfileCLI command
textcoreai text
imagecoreai image
decidecoreai decide
modelscoreai models
video_startmediaai video start
video_statusmediaai video status
audio_speakmediaai audio speak
audio_transcribemediaai audio transcribe

media contains the four media tools. all contains all eight tools.

Protocol versions

Modern clients send io.modelcontextprotocol/protocolVersion and io.modelcontextprotocol/clientCapabilities in each request's _meta. server/discover reports the supported versions, the server capabilities, and a one-hour cache hint. Tool lists carry a five-minute cache hint, and every modern result includes resultType and the server identity in _meta. A request that omits _meta after a modern request fails with an error that names the missing field.

ping is the exception. After server/discover, a request without _meta can still be a ping, so clients can check liveness.

Legacy clients opt in by calling initialize first, and keep the handshake they already use. The server answers with 2025-11-25 unless the client asks for 2025-06-18, 2025-03-26, or 2024-11-05. Those sessions receive plain results without the modern fields.

Limits and shutdown

The server accepts requests up to 4 MiB per line and runs at most eight tool calls at once. Extra calls fail with Server busy until a slot frees up. Duplicate in-flight request ids are rejected instead of being answered twice.

When a tool call includes a progressToken, the server sends a notifications/progress message every second until the call finishes. A notifications/cancelled message stops that call and its child process.

shutdown answers immediately and waits for in-flight calls to finish. If the client closes stdin, the server drains the same way. Tool processes receive SIGTERM first and SIGKILL two seconds later when they do not exit. Set AI_CLI_MCP_DRAIN_TIMEOUT_MS to change how long the drain waits (default 30000, range 0 to 2147483647).

Results

Tool results are the command's JSON output. Generation tools return a manifest with the saved file paths. text and audio_transcribe return a file path to read. If you do not pass output, files are written to the call's cwd, or to AI_CLI_OUTPUT_DIR when it is set.

Tool failures return an error result with the command's diagnostic.

Inputs

Each tool's inputs are its command's own flags, named in camelCase (--aspect-ratio becomes aspectRatio) with the same descriptions as --help. Repeatable flags take a string array, and on/off flags take a boolean. Terminal-only flags (--quiet, --no-preview, --no-play, --no-waveform) are left out. Gateway connection flags (--base-url, --headers, --scope, --team-id-or-slug) are rejected, including in extraArgs, so an agent cannot send your Gateway key to another endpoint. Set AI_GATEWAY_BASE_URL and AI_GATEWAY_TEAM_ID_OR_SLUG in the server environment instead.

Every tool also accepts these inputs:

InputTypeDescription
cwdstringWorking directory for the run
extraArgsstring[]Extra CLI flags appended last
timeoutMsintegerTimeout from 1 to 2147483647 (default 600000)

Decide

decide takes a non-empty questions array. Each question has an id, type, and question. type is boolean, choice, or score. A choice requires choices; a score requires levels. IDs must not contain =, and choices and levels must not contain commas.

{
  "questions": [
    {
      "id": "refund",
      "type": "boolean",
      "question": "Refund requested?"
    },
    {
      "id": "team",
      "type": "choice",
      "question": "Which team?",
      "choices": ["billing", "support"]
    },
    {
      "id": "tone",
      "type": "score",
      "question": "How positive?",
      "levels": ["angry", "neutral", "happy"]
    }
  ],
  "state": "Customer asks for a refund."
}

The optional state string is written to stdin. The result is the command's JSON output.

Video

video_start returns immediately and requires output, which receives the operation file. Pass that same path as operationFile to video_status to check the job. Set download: true to save completed videos, and pass output to choose where they are written.

The ai-cli.dev site serves a separate MCP endpoint at https://ai-cli.dev/api/mcp. Its only tool, search_docs, searches these docs. It doesn't run ai commands, so use ai mcp when an agent needs to generate or decide.