Optional Grok-via-OpenRouter integration for the swarm. Lets the orchestrator delegate news/sentiment-heavy personas to Grok 4, which has native web + X (Twitter) search that Claude doesn't.
Note on the directory name: this is called
grok_mcp/rather thanmcp/because Python'smcppackage (the official MCP SDK) gets shadowed by any local directory namedmcp/. Naming itgrok_mcp/avoids the import collision.
grok_swarm_mcp.py— a FastMCP server that exposes one tool,ask_grok. Claude Code loads it via.mcp.jsonat the project root.install.py— one-time setup script. Detects your platform, installs MCP dependencies into the project venv, generates.mcp.jsonwith the right absolute paths, and stubs out.env.__init__.py— empty package marker.
From the project root, after creating the main venv (see top-level README.md):
python grok_mcp/install.pyThat's it. The script:
- Detects (or creates)
.venv/at the project root - Installs
mcp,python-dotenv, andopenaiinto the venv - Smoke-tests the imports
- Writes
.mcp.jsonat the project root with platform-specific absolute paths - Creates
.envfrom.env.exampleif missing - Tells you to add your OpenRouter API key
Then add your key to .env:
# .env
OPENROUTER_API_KEY=sk-or-v1-...Get a key at https://openrouter.ai/keys.
The next time you start a Claude Code session in this folder, you'll see a prompt asking you to approve the project-scoped MCP server. Approve it, and the orchestrator will be able to call Grok agents.
When you ask the orchestrator a question, it checks whether .mcp.json exists and OPENROUTER_API_KEY is set in .env. If both are true, it offers you a mixed cohort: most personas as Claude subagents, with some as Grok agents for lenses where real-time web/X data matters most (e.g. "X Sentiment Watcher", "Real-time News Aggregator", "Geopolitical Newsflow").
You see the proposed cohort once and approve it. Then the orchestrator spawns everything in parallel (Claude subagents via the Agent tool, Grok agents via the mcp__grok-swarm__ask_grok tool) in a single message. From there it runs unattended — you can walk away for 20-30 minutes.
The orchestrator writes Grok agent outputs to the same runs/<run-id>/agents/<NN>_<slug>/ folder structure as Claude agents, so synthesis treats them uniformly.
The MCP server exposes one tool. Signature (auto-generated from the Python type hints):
ask_grok(
prompt: str, # full persona prompt with question, workflow, output format
persona: str = "", # optional persona name for logging
model: str = "x-ai/grok-4", # OpenRouter model id; default is the flagship
enable_x_search: bool = True, # enable both web and X search (xAI native)
max_tokens: int = 8000, # response length cap
) -> strReturns the model's full markdown response with a usage footer (model, persona, token counts). On error, returns a string starting with ERROR: so the orchestrator can detect and handle it.
Grok 4 on OpenRouter as of April 2026:
- Input: $3 / million tokens (≤128K context), $6 / million above
- Output: $15 / million tokens (≤128K), $30 / million above
- Web search: $5 per 1000 calls
- Context window: 256K tokens
A typical 4-Grok-agent run with quality-first depth runs ~$0.60–2.00 in OpenRouter charges, on top of whatever you're paying for Claude Code. Each Grok 4 call uses up to 20000 output tokens by default (the MCP's max_tokens setting), and Grok 4 is a reasoning model — internal reasoning counts against that budget, so deep questions can consume more of the cap than chat-only models would.
Claude Code doesn't see the MCP server
- Make sure you ran
python grok_mcp/install.pyand it printed "Setup complete" - Confirm
.mcp.jsonexists at the project root:ls .mcp.json - Restart your Claude Code session —
.mcp.jsonis loaded at session start - The first time you run it, you should get a trust prompt; approve it
- Run
/mcpinside a Claude Code session to see all loaded MCP servers
OPENROUTER_API_KEY is not set error
- Check
.envexists and contains a real key (not the empty stub) - The key should look like
sk-or-v1-... - After editing
.env, you may need to restart the MCP server (close and reopen the Claude Code session)
OpenRouter error: insufficient credits
- Add credits to your OpenRouter account at https://openrouter.ai/credits
Grok returned an empty response
- Usually means Grok's web search timed out or returned no usable results. Re-spawn the agent.
- If it happens repeatedly, check OpenRouter's status page
Permission denied editing .mcp.json
- The file is gitignored but exists in your working tree. Just edit it in your editor, or re-run
python grok_mcp/install.pyto regenerate it.
I want to switch to Grok-4-Fast for cheaper runs
- The orchestrator can pass
model="x-ai/grok-4-fast"when callingask_grok. Or you can editDEFAULT_MODELingrok_swarm_mcp.pyand restart the session.
There are several open-source OpenRouter MCP servers (th3nolo/openrouter-mcp, overtimepog/OpenrouterMCP, etc.). We built our own because:
- One tool, shaped for our use case. A general-purpose
chat_with_model(model, messages)is too low-level — the orchestrator would need to know the OpenRouter API surface. Ourask_grok(prompt, persona)is exactly the abstraction the swarm needs. - Lives in the repo, version-controlled. No external dependency to break. The whole server is one Python file.
- Uses the venv we already have. No Node.js, no second runtime.
- Auditable. ~150 lines of Python you can read in five minutes.
Built on the official Anthropic-maintained mcp Python SDK using the FastMCP wrapper, which auto-generates tool schemas from type hints.