x-search-mcp
Provides read-only access to X (Twitter) search via xAI's Responses API, returning Grok-generated answers with citations from posts, profiles, and threads.
README
x-search-mcp
A self-contained Model Context Protocol (MCP) server that gives Codex, Claude Code, and other MCP clients access to xAI's built-in X Search.
It calls xAI's Responses API with the server-side x_search tool. The result is a Grok-generated research answer with citations from X, rather than a raw firehose or a replacement for the official X API.
Features
- Search current X posts, profiles, and threads with natural-language queries.
- Include or exclude specific X handles.
- Restrict searches to a date range.
- Optionally analyze images and videos attached to matching posts.
- Return both top-level and inline citation metadata.
- Mark filtered responses as
degradedwhen xAI returns no citations. - Authenticate with standalone xAI device-code OAuth or
XAI_API_KEY. - Refresh rotating OAuth tokens automatically with a cross-process file lock.
- Expose a narrow, read-only stdio MCP surface containing one tool:
x_search.
Requirements
- Python 3.11 or newer
- uv
- An xAI account eligible for OAuth API access, or an xAI API key
- An MCP client such as Codex or Claude Code
Installation
Clone the repository and install the command with uv:
git clone https://github.com/kamellperry/x-search-mcp.git
cd x-search-mcp
uv tool install .
For local development, use uv sync instead:
uv sync
Authentication
Device-code OAuth
Run the login command once:
x-search-mcp login
The command opens xAI's device authorization page, waits for approval, and stores the resulting tokens in:
~/.config/x-search-mcp/auth.json
The auth directory and token file are created with user-only permissions. The server refreshes expiring access tokens automatically and persists rotated refresh tokens atomically. It does not read Hermes, Codex, Claude, browser, or shell credential stores.
Check credential state without printing any token:
x-search-mcp status
OAuth access depends on xAI account tier and entitlement. If xAI rejects OAuth API access, use an API key instead.
API key fallback
Set an xAI API key in the environment that launches the MCP server:
export XAI_API_KEY="your-api-key"
Stored OAuth is preferred when both credential methods are present.
Connect an MCP client
Codex
If the command was installed with uv tool install .:
codex mcp add x-search -- x-search-mcp serve
codex mcp get x-search
For a development checkout, use the included launcher:
codex mcp add x-search -- /absolute/path/to/x-search-mcp/run-server
Then start a fresh Codex session so it receives the new tool manifest.
Claude Code
If the command was installed globally:
claude mcp add --scope user x-search -- x-search-mcp serve
claude mcp get x-search
For a development checkout:
claude mcp add --scope user x-search -- /absolute/path/to/x-search-mcp/run-server
Other MCP clients
Configure a stdio server with either of these commands:
x-search-mcp serve
/absolute/path/to/x-search-mcp/run-server
Do not run the stdio server through a wrapper that writes ordinary output to stdout. MCP protocol messages use stdout; diagnostics belong on stderr.
Tool reference
x_search
| Argument | Type | Required | Description |
|---|---|---|---|
query |
string | yes | Natural-language description of what to find on X. |
allowed_x_handles |
string[] | no | Search only these handles, maximum 10. Leading @ is optional. |
excluded_x_handles |
string[] | no | Exclude these handles, maximum 10. Cannot be combined with allowed_x_handles. |
from_date |
string | no | Inclusive start date in YYYY-MM-DD format. |
to_date |
string | no | End date in YYYY-MM-DD format. |
enable_image_understanding |
boolean | no | Ask xAI to inspect images attached to matching posts. |
enable_video_understanding |
boolean | no | Ask xAI to inspect videos attached to matching posts. |
Example tool arguments:
{
"query": "What has xAI announced recently?",
"allowed_x_handles": ["xai"],
"from_date": "2026-07-01"
}
The result includes:
answer: synthesized answer from the configured Grok modelcitations: citations returned at the response levelinline_citations: URL annotations attached to generated textcredential_source:xai-oauthorxai-api-keydegraded: true when narrowing filters were active but no citations were returneddegraded_reason: explanation whendegradedis true
Treat a degraded answer as unsourced model output. Broaden the filters, retry, or verify the claim another way.
Configuration
| Environment variable | Default | Description |
|---|---|---|
X_SEARCH_MCP_HOME |
~/.config/x-search-mcp |
Directory containing the standalone OAuth state and lock. |
X_SEARCH_MODEL |
grok-4.5 |
xAI model used for the Responses API request. |
X_SEARCH_REASONING_EFFORT |
unset | Optional low, medium, high, or xhigh. |
X_SEARCH_TIMEOUT_SECONDS |
180 |
HTTP request timeout, clamped to at least 30 seconds. |
XAI_API_KEY |
unset | Optional API-key fallback when standalone OAuth is unavailable. |
Security notes
auth.json,.envfiles, virtual environments, caches, and build outputs are ignored by Git.- OAuth and inference endpoints are restricted to HTTPS hosts on the
x.aidomain. - The inference bearer is sent only to
https://api.x.ai/v1. - The status command reports metadata only and never prints tokens.
- Refresh tokens may rotate after each refresh. Do not copy one OAuth token store between concurrently running installations.
- Never commit
~/.config/x-search-mcp/auth.jsonor pass its contents to an MCP client configuration.
Troubleshooting
No xAI credentials
Run x-search-mcp login, or provide XAI_API_KEY to the MCP server process.
OAuth login succeeds but API calls are denied
xAI may restrict OAuth API access by subscription tier. Configure XAI_API_KEY if your account is not entitled to OAuth API access.
The MCP client does not show x_search
- Run
x-search-mcp status. - Run the client's MCP inspection command, such as
codex mcp get x-search. - Use an absolute launcher path if the client does not inherit your shell
PATH. - Restart the client so it reloads the MCP tool manifest.
A filtered result is marked degraded
xAI returned a synthesized response without citations matching the requested handle or date filters. Remove or broaden filters and retry.
Development
Install development dependencies and run the complete test suite:
uv sync
uv run ruff check src tests
uv run ruff format --check src tests
uv run pytest
uv run python tests/mcp_smoke.py
The smoke test starts the real stdio server, initializes an MCP client session, and confirms that x_search is discoverable with the expected schema.
Build distribution artifacts with:
uv build
Project scope
This project intentionally exposes X Search only. It does not post, like, follow, send messages, or mutate an X account. It does not provide deterministic raw-post pagination or full X API semantics.
License
MIT. See LICENSE.
Acknowledgments
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。