xai-search is a compact Grok live-search helper that lets the same Hono application power four different integration modes:
- CLI (
xai-search search <input>) - HTTP server (
/searchand/mcpendpoints) - Local MCP server over stdio
- Remote MCP server over HTTP via
StreamableHTTPTransport
The goal is to make xAI's Grok 4 Fast web search reachable from your own agents with minimal glue code.
See docs/design/live-search.md for the internal wiring between the CLI adapter, HTTP routes, and MCP server.
- Node.js 18+ (or Bun)
XAI_API_KEYwith access to Grok live search
Optional:
XAI_MODEL(defaults togrok-4-fast)XAI_BASE_URL(defaults tohttps://api.x.ai/v1)GROK_SEARCH_MODE(auto|on|off, defaults toauto)TZ(forwarded to the CLI adapter, defaults to the host TZ)PORTwhen serving HTTP (defaults to9876)
Run the CLI without installing locally:
# latest published version
XAI_API_KEY=sk-... npx xai-search@latest search "Rust learning roadmap"
# pin a version
XAI_API_KEY=sk-... npx xai-search@0.1.0 search "Next.js image optimization"
# avoid npx prompts
XAI_API_KEY=sk-... npx --yes xai-search@latest search "Supabase RLS basics"Local scripts use Bun by default but work with Node as well:
# via npm script (Bun under the hood)
XAI_API_KEY=sk-... npm run cli -- search "Compare LLMs"
# direct Bun execution
XAI_API_KEY=sk-... bun src/cli.ts search "Vite plugin ideas"
# CLI flags
bun src/cli.ts --help--json pretty-prints the response, and --env lets you point to a .env file or supply ad-hoc key/value overrides recognized by hono-cli-adapter.
To pass request parameters, append -- and supply key=value pairs — they become the JSON body sent to /search. Values stay as strings unless the Grok API coerces them, so quote JSON yourself when needed.
# enable citations and bump max results
XAI_API_KEY=sk-... bun src/cli.ts search "Rust learning roadmap" -- return_citations=true max_search_results=8Scope the query to specific source types with the same shorthand:
# single source type
XAI_API_KEY=sk-... bun src/cli.ts search "codexのアップデート情報" -- sources=x
# multiple types (comma or space separated)
XAI_API_KEY=sk-... bun src/cli.ts search "最新のLLMニュース" -- sources=web,news
# complex objects — quote JSON explicitly
XAI_API_KEY=sk-... bun src/cli.ts search "US AI regulation" \
-- sources='[{"type":"news","country":"US"},{"type":"web","profile":"default"}]'
# build JSON safely from scripts
XAI_API_KEY=sk-... bun src/cli.ts search "xAI roadmap" \
-- sources="$(jq -n '[{type:"x",included_x_handles:["xai"]}]')"The CLI already supplies the default search mode from GROK_SEARCH_MODE (fallback on), so only override it with -- mode=off (or similar) when a specific request needs it.
Expose /search and /mcp over HTTP:
# npm script (alias: serve)
XAI_API_KEY=sk-... npm run cli:serve
# bun direct (flag form)
XAI_API_KEY=sk-... bun src/cli.ts --server
# custom port
PORT=8080 XAI_API_KEY=sk-... npm run cli:serveEndpoints:
POST /search/:input→ returns plain text from Grok. Optional JSON body accepts search parameters (mode,return_citations,max_search_results,from_date,to_date,sources).POST /mcp→ MCP endpoint compatible withStreamableHTTPTransportclients.
Example request with overrides:
curl -sS -X POST http://localhost:9876/search/"Next.js image optimization" \
--json '{
"mode": "on",
"return_citations": true,
"max_search_results": 5,
"sources": [
{ "type": "web", "profile": "default" },
{ "type": "news", "mode": "auto" }
]
}'Run an MCP server over stdio for clients such as local coding agents:
XAI_API_KEY=sk-... npm run cli:stdioSpin up the HTTP server and point your agent to http://localhost:9876/mcp using @hono/mcp's StreamableHTTPTransport:
XAI_API_KEY=sk-... npm run cli:serve
# -> MCP tool id: xai-web-searchnpm run dev— start a local worker (requires Wrangler)npm run deploy— deploy to Cloudflare Workers with minify enablednpm run cf-typegen— regenerate type definitions (worker-configuration.d.ts)
# debug style
npm run build:bin # -> bin/xai-search
# release style
npm run build:bin:release
# invoke compiled binary
XAI_API_KEY=sk-... ./bin/xai-search search "Compare LLMs"# install dependencies
npm install
# or
bun install
# build once (writes dist/cli.js)
npm run build
# format CLI usage
bun src/cli.ts --helpDuring CLI execution the adapter resolves environment values in this order:
process.env- Values supplied through adapter options (
options.env) — unused by this binary --env KEY=VALUEflags or.envfile references
- CLI requests map
search <input>toPOST /search { input }under the hood. runSearchcurrently returns the concatenated text from Grok; structured citations are surfaced whenreturn_citationsis enabled.- MCP server metadata (
name/version) lives insrc/app.ts. - Live search internals and future tuning switches are documented in
docs/design/live-search.md.
- MIT License
- CLI adapter provided by
kiyo-e/hono-cli-adapter