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.
--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):
Other clients (mcpServers):
opencode (opencode.json):
Claude Code:
Codex:
| Variable | Purpose |
|---|---|
AI_GATEWAY_API_KEY | Vercel AI Gateway API key. Required. |
AI_CLI_OUTPUT_DIR | Absolute directory for generated files. Without it, files land in each call's cwd. |
AI_CLI_MCP_DRAIN_TIMEOUT_MS | How long shutdown waits for in-flight tool calls, in milliseconds (default 30000). |
AI_GATEWAY_BASE_URL, AI_GATEWAY_TEAM_ID_OR_SLUG | Optional 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
| Tool | Profile | CLI command |
|---|---|---|
text | core | ai text |
image | core | ai image |
decide | core | ai decide |
models | core | ai models |
video_start | media | ai video start |
video_status | media | ai video status |
audio_speak | media | ai audio speak |
audio_transcribe | media | ai 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:
| Input | Type | Description |
|---|---|---|
cwd | string | Working directory for the run |
extraArgs | string[] | Extra CLI flags appended last |
timeoutMs | integer | Timeout 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.
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.
Documentation search
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.