Skip to content

Latest commit

 

History

History

grok_mcp

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

grok_mcp/

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 than mcp/ because Python's mcp package (the official MCP SDK) gets shadowed by any local directory named mcp/. Naming it grok_mcp/ avoids the import collision.

What's in here

  • grok_swarm_mcp.py — a FastMCP server that exposes one tool, ask_grok. Claude Code loads it via .mcp.json at the project root.
  • install.py — one-time setup script. Detects your platform, installs MCP dependencies into the project venv, generates .mcp.json with the right absolute paths, and stubs out .env.
  • __init__.py — empty package marker.

Installation

From the project root, after creating the main venv (see top-level README.md):

python grok_mcp/install.py

That's it. The script:

  1. Detects (or creates) .venv/ at the project root
  2. Installs mcp, python-dotenv, and openai into the venv
  3. Smoke-tests the imports
  4. Writes .mcp.json at the project root with platform-specific absolute paths
  5. Creates .env from .env.example if missing
  6. 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.

How it's used by the swarm

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 ask_grok tool

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
) -> str

Returns 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.

Cost

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.

Troubleshooting

Claude Code doesn't see the MCP server

  • Make sure you ran python grok_mcp/install.py and it printed "Setup complete"
  • Confirm .mcp.json exists at the project root: ls .mcp.json
  • Restart your Claude Code session — .mcp.json is loaded at session start
  • The first time you run it, you should get a trust prompt; approve it
  • Run /mcp inside a Claude Code session to see all loaded MCP servers

OPENROUTER_API_KEY is not set error

  • Check .env exists 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

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.py to regenerate it.

I want to switch to Grok-4-Fast for cheaper runs

  • The orchestrator can pass model="x-ai/grok-4-fast" when calling ask_grok. Or you can edit DEFAULT_MODEL in grok_swarm_mcp.py and restart the session.

Why a custom MCP and not a third-party one

There are several open-source OpenRouter MCP servers (th3nolo/openrouter-mcp, overtimepog/OpenrouterMCP, etc.). We built our own because:

  1. 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. Our ask_grok(prompt, persona) is exactly the abstraction the swarm needs.
  2. Lives in the repo, version-controlled. No external dependency to break. The whole server is one Python file.
  3. Uses the venv we already have. No Node.js, no second runtime.
  4. 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.