@scoheart/x-search-mcp 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.env.example +45 -0
  2. package/LICENSE +21 -0
  3. package/README.md +87 -2
  4. package/THIRD_PARTY_NOTICES.md +35 -0
  5. package/TOOL.md +242 -0
  6. package/bin/x-search-mcp.js +29 -0
  7. package/bin/x-search-mcp.ts +7 -0
  8. package/dist/bin/x-search-mcp.d.ts +3 -0
  9. package/dist/bin/x-search-mcp.d.ts.map +1 -0
  10. package/dist/bin/x-search-mcp.js +7 -0
  11. package/dist/bin/x-search-mcp.js.map +1 -0
  12. package/dist/src/auth.d.ts +42 -0
  13. package/dist/src/auth.d.ts.map +1 -0
  14. package/dist/src/auth.js +402 -0
  15. package/dist/src/auth.js.map +1 -0
  16. package/dist/src/call-log.d.ts +28 -0
  17. package/dist/src/call-log.d.ts.map +1 -0
  18. package/dist/src/call-log.js +159 -0
  19. package/dist/src/call-log.js.map +1 -0
  20. package/dist/src/cli.d.ts +2 -0
  21. package/dist/src/cli.d.ts.map +1 -0
  22. package/dist/src/cli.js +117 -0
  23. package/dist/src/cli.js.map +1 -0
  24. package/dist/src/config.d.ts +8 -0
  25. package/dist/src/config.d.ts.map +1 -0
  26. package/dist/src/config.js +104 -0
  27. package/dist/src/config.js.map +1 -0
  28. package/dist/src/http.d.ts +28 -0
  29. package/dist/src/http.d.ts.map +1 -0
  30. package/dist/src/http.js +148 -0
  31. package/dist/src/http.js.map +1 -0
  32. package/dist/src/index.d.ts +9 -0
  33. package/dist/src/index.d.ts.map +1 -0
  34. package/dist/src/index.js +9 -0
  35. package/dist/src/index.js.map +1 -0
  36. package/dist/src/search.d.ts +24 -0
  37. package/dist/src/search.d.ts.map +1 -0
  38. package/dist/src/search.js +478 -0
  39. package/dist/src/search.js.map +1 -0
  40. package/dist/src/server.d.ts +9 -0
  41. package/dist/src/server.d.ts.map +1 -0
  42. package/dist/src/server.js +106 -0
  43. package/dist/src/server.js.map +1 -0
  44. package/dist/src/types.d.ts +113 -0
  45. package/dist/src/types.d.ts.map +1 -0
  46. package/dist/src/types.js +2 -0
  47. package/dist/src/types.js.map +1 -0
  48. package/examples/codex.toml +8 -0
  49. package/examples/mcp.json +16 -0
  50. package/package.json +59 -4
  51. package/src/auth.ts +455 -0
  52. package/src/call-log.ts +162 -0
  53. package/src/cli.ts +131 -0
  54. package/src/config.ts +107 -0
  55. package/src/http.ts +201 -0
  56. package/src/index.ts +8 -0
  57. package/src/search.ts +518 -0
  58. package/src/server.ts +116 -0
  59. package/src/types.ts +116 -0
  60. package/tsconfig.json +25 -0
package/.env.example ADDED
@@ -0,0 +1,45 @@
1
+ # xAI API Key (按量计费,优先于 XAI_API_KEY_FILE)
2
+ # XAI_API_KEY=xai-xxxxxxxxxxxxxxxxxxxx
3
+
4
+ # 私有 API Key 文件路径(如果不想直接在环境变量放置凭证)
5
+ # XAI_API_KEY_FILE=/path/to/private/xai-api-key.txt
6
+
7
+ # 显式指定的 .env 文件路径(默认不扫描其他目录)
8
+ # X_SEARCH_ENV_FILE=/path/to/.env
9
+
10
+ # 认证模式: auto (默认,有 Key 用 Key,无 Key 用 OAuth) | api_key | oauth
11
+ X_SEARCH_AUTH_MODE=auto
12
+
13
+ # 本地 OAuth 凭证文件存放路径(默认 ~/.config/x-search-mcp/auth.json)
14
+ # X_SEARCH_AUTH_FILE=/Users/username/.config/x-search-mcp/auth.json
15
+
16
+ # 公开 OAuth Client ID(默认来自 Hermes 公开客户端)
17
+ # XAI_OAUTH_CLIENT_ID=b1a00492-073a-47ea-816f-4c329264a828
18
+
19
+ # 搜索使用的模型(默认 grok-4.5)
20
+ X_SEARCH_MODEL=grok-4.5
21
+
22
+ # HTTP 超时秒数 (默认 180,范围 5-600)
23
+ X_SEARCH_TIMEOUT_SECONDS=180
24
+
25
+ # 失败重试次数 (默认 2,范围 0-3,仅针对网络、超时及 5xx)
26
+ X_SEARCH_RETRIES=2
27
+
28
+ # 最大轮数 (默认 4,范围 1-20)
29
+ X_SEARCH_MAX_TURNS=4
30
+
31
+ # 思考深度: low | medium | high | xhigh (可选)
32
+ # X_SEARCH_REASONING_EFFORT=medium
33
+
34
+ # 调用日志:保存每次请求体和上游原始响应。默认开启。
35
+ # X_SEARCH_LOG=on
36
+ # X_SEARCH_LOG_DIR=/Users/username/.config/x-search-mcp/logs
37
+ # 保留最近多少份日志,0 表示不清理(默认 100)
38
+ # X_SEARCH_LOG_KEEP=100
39
+ # 单次原始响应写入上限,超出则截断并标记(默认 8388608,最大 33554432)
40
+ # X_SEARCH_LOG_MAX_BYTES=8388608
41
+
42
+ # 代理支持 (由 Undici EnvHttpProxyAgent 自动读取)
43
+ # HTTP_PROXY=http://127.0.0.1:7890
44
+ # HTTPS_PROXY=http://127.0.0.1:7890
45
+ # NO_PROXY=localhost,127.0.0.1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Scoheart
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,88 @@
1
- # Temporary Holding Version
1
+ # @scoheart/x-search-mcp
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
+
5
+ Independent Model Context Protocol (MCP) server providing real-time `x_search` capabilities using xAI's responses API, inspired by Nous Research's Hermes Agent.
6
+
7
+ ## Quick Start
8
+
9
+ ### Installation
10
+
11
+ Requires Node.js **22.19.0 or later**.
12
+
13
+ Once the package is published to npm, run it without a global installation:
14
+
15
+ ```bash
16
+ npx --yes @scoheart/x-search-mcp@0.1.0 auth login
17
+ npx --yes @scoheart/x-search-mcp@0.1.0 auth status
18
+ ```
19
+
20
+ To install a locally built npm tarball before publication:
21
+
22
+ ```bash
23
+ npm install --global ./scoheart-x-search-mcp-0.1.0.tgz
24
+ x-search-mcp auth status
25
+ ```
26
+
27
+ To build from source:
28
+
29
+ ```bash
30
+ git clone https://github.com/Scoheart/x-search-mcp.git
31
+ cd x-search-mcp
32
+ npm ci
33
+ npm run build
34
+ # Build a distributable .tgz; prepack compiles the current source automatically.
35
+ npm pack
36
+ ```
37
+
38
+ ### Authentication
39
+
40
+ Choose between direct API Key or Subscription OAuth:
41
+
42
+ ```bash
43
+ # 1. Using API Key
44
+ export XAI_API_KEY="xai-your-api-key"
45
+
46
+ # 2. Or using Grok Subscription via Device Code OAuth (source checkout)
47
+ node bin/x-search-mcp.js auth login
48
+ ```
49
+
50
+ ### Run Server
51
+
52
+ ```bash
53
+ # stdio MCP server for agent clients (Claude Desktop, Cursor, Cline, etc.)
54
+ node bin/x-search-mcp.js
55
+ ```
56
+
57
+ For an npm-installed package, use `x-search-mcp` in place of `node bin/x-search-mcp.js`.
58
+
59
+ ### MCP Client Configuration
60
+
61
+ After npm publication, add this server to Pi's `~/.pi/agent/mcp.json` or another client accepting `mcpServers`:
62
+
63
+ ```json
64
+ {
65
+ "mcpServers": {
66
+ "x-search": {
67
+ "command": "npx",
68
+ "args": ["--yes", "@scoheart/x-search-mcp@0.1.0"],
69
+ "env": {
70
+ "X_SEARCH_AUTH_MODE": "oauth",
71
+ "X_SEARCH_MODEL": "grok-4.5"
72
+ },
73
+ "timeout": 600
74
+ }
75
+ }
76
+ }
77
+ ```
78
+
79
+ For a globally installed tarball, use `"command": "x-search-mcp"` and `"args": []`. The client must have Node/npm on its `PATH`; otherwise use the executable's absolute path. Restart or reload the client after changing its configuration. Pi sessions started with `--no-mcp` do not connect to MCP servers.
80
+
81
+ OAuth credentials and call logs stay in `~/.config/x-search-mcp/`; they are not included in the npm package. OAuth login does not guarantee X Search entitlement, and responses without source URLs are marked `degraded: true`.
82
+
83
+ See [TOOL.md](./TOOL.md) for full Chinese documentation, parameters, and configuration options.
84
+
85
+ ## License
86
+
87
+ [MIT](./LICENSE) © Scoheart
88
+ See [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md) for third-party notices (Hermes Agent).
@@ -0,0 +1,35 @@
1
+ # Third-Party Notices
2
+
3
+ This project references and adapts architecture and flows from the Hermes Agent project by Nous Research, specifically the `x_search_tool.py`, `auth_xai.py`, and `auth_constants.py` implementations.
4
+
5
+ Reference Commit:
6
+ `fcca1740533d59d7a46ab1a3a53d3a45ef3127ff`
7
+
8
+ Reference Repository:
9
+ https://github.com/NousResearch/hermes-agent
10
+
11
+ The original code is licensed under the MIT License:
12
+
13
+ ```
14
+ MIT License
15
+
16
+ Copyright (c) 2025 Nous Research
17
+
18
+ Permission is hereby granted, free of charge, to any person obtaining a copy
19
+ of this software and associated documentation files (the "Software"), to deal
20
+ in the Software without restriction, including without limitation the rights
21
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
+ copies of the Software, and to permit persons to whom the Software is
23
+ furnished to do so, subject to the following conditions:
24
+
25
+ The above copyright notice and this permission notice shall be included in all
26
+ copies or substantial portions of the Software.
27
+
28
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
+ SOFTWARE.
35
+ ```
package/TOOL.md ADDED
@@ -0,0 +1,242 @@
1
+ # @scoheart/x-search-mcp 工具接入与使用说明
2
+
3
+ 本工具是一个独立运行的 Node.js/TypeScript MCP 服务,提供只读工具 `x_search`,支持通过 xAI Responses API 对 X(原 Twitter)实时动态与历史发帖进行检索。本服务实现参考 Nous Research 的 Hermes Agent(commit `fcca1740533d59d7a46ab1a3a53d3a45ef3127ff`),但作为独立 MCP 包提供,无需安装或运行 Hermes。
4
+
5
+ ---
6
+
7
+ ## 核心特性
8
+
9
+ - **标准 MCP 协议**:基于 `@modelcontextprotocol/server`,默认运行在 `stdio` 模式,输出标准 JSON-RPC。
10
+ - **只读与安全性**:仅提供 `x_search` 检索工具,无发帖、点赞、关注或私信写操作;错误信息经由本地脱敏,不回显 upstream token/key。
11
+ - **双认证模式**:支持按量付费 API Key 与 Grok 订阅 OAuth(设备码登录);支持多进程并发共享令牌并带文件锁(`proper-lockfile`)。
12
+ - **来源与降级感知**:提取结构化 citations 与正文 inline_citations。若上游无有效引用来源,自动标记 `degraded: true`。
13
+
14
+ ---
15
+
16
+ ## 约束与边界说明(重要)
17
+
18
+ 1. **非官方项目**:本项目为独立开源 MCP 实现,并非 xAI 或 Nous Research / Hermes 官方 MCP。
19
+ 2. **仅本地 stdio 传输**:当前版本未开放远程 HTTP/SSE/WebSocket 监听入口,所有接入 Agent 通过启动子进程连接各自的 `stdio`。
20
+ 3. **公共 OAuth Client ID 说明**:默认内置的公开客户端 ID 来自 Hermes Agent(`b1a00492-073a-47ea-816f-4c329264a828`)。在浏览器授权确认页面中可能会显示该客户端标识名称。如需使用专属客户端,可通过 `XAI_OAUTH_CLIENT_ID` 环境变量配置。
21
+ 4. **OAuth 成功不等于搜索可用**:OAuth 设备码登录成功仅代表获得 xAI 账号凭证,实际搜索能力及并发额度由 xAI 云端及订阅权益决定;登录成功不保证账号一定具备 X Search 权限。
22
+ 5. **无引用不可作为确证**:当结果标记为 `has_citations: false` / `degraded: true` 时,表示上游未返回任何经过验证的 URL 引用,该回答不可作为来源充分的 X 检索事实依据。
23
+ 6. **计费与回退规则**:
24
+ - API Key 模式下按调用量付费(api_metered)。
25
+ - 显式 `oauth` 模式下仅使用订阅令牌,**绝不回退**到付费 API Key,避免产生非预期账单。
26
+ - `auto` 模式优先使用 API Key(如果配置),无 Key 时使用本地保存的 OAuth。
27
+ 7. **重试消耗**:网络波动或 5xx 错误时会执行有限次数退避重试,重试请求可能重复消耗上游额度。
28
+ 8. **过滤作用域限制**:xAI 接口的 `allowed_x_handles`、`excluded_x_handles`、`from_date` 和 `to_date` 过滤主要作用于关键词与语义搜索阶段,并不严格保证限制上游服务端内部的级联线程抓取(thread fetch)。
29
+
30
+ ---
31
+
32
+ ## npm 安装与打包
33
+
34
+ 需要 Node.js 22.19.0 或更高版本。发布到 npm 后可以直接运行:
35
+
36
+ ```bash
37
+ npx --yes @scoheart/x-search-mcp@0.1.0 auth login
38
+ npx --yes @scoheart/x-search-mcp@0.1.0 auth status
39
+ ```
40
+
41
+ 发布前可在源码仓库运行 `npm ci`、`npm pack` 生成安装包。`prepack` 会先编译当前源码,安装端无需 TypeScript 或开发依赖:
42
+
43
+ ```bash
44
+ npm install --global ./scoheart-x-search-mcp-0.1.0.tgz
45
+ x-search-mcp auth status
46
+ ```
47
+
48
+ 包通过 `files` 白名单包含代码、示例和文档;OAuth 凭据和调用日志存放在用户配置目录,不随包分发。
49
+
50
+ ## 凭证配置
51
+
52
+ ### 方式 1:API Key(按量计费)
53
+
54
+ 直接设置环境变量或指向私有文件:
55
+ ```bash
56
+ export XAI_API_KEY="xai-xxxxxxxxxxxxxxxxxxxx"
57
+ # 或
58
+ export XAI_API_KEY_FILE="/path/to/private/xai-key.txt"
59
+ ```
60
+
61
+ ### 方式 2:Grok 订阅 OAuth(设备码流程)
62
+
63
+ 无需购买 API Key,使用包含 X Search 权益的 xAI 账号直接在终端完成登录:
64
+
65
+ ```bash
66
+ # 执行设备码登录
67
+ x-search-mcp auth login
68
+ ```
69
+
70
+ 终端将输出授权网址与用户确认码:
71
+ ```text
72
+ 1. Visit the authorization page: https://auth.x.ai/oauth2/device/code
73
+ 2. Confirm code: ABCD-EFGH
74
+ 3. Code expires at: 20:30:00
75
+ Waiting for browser authorization...
76
+ ```
77
+ 在浏览器中打开链接、输入确认码并批准后,令牌将原子写入本地配置文件:
78
+ - 默认路径:`~/.config/x-search-mcp/auth.json`(权限 `0600`,目录权限 `0700`)
79
+ - 自定义路径:可通过 `X_SEARCH_AUTH_FILE` 覆盖。
80
+
81
+ ### 凭证状态与注销
82
+
83
+ ```bash
84
+ # 查看凭证配置状态(不探测额度、不泄露密钥内容)
85
+ x-search-mcp auth status
86
+
87
+ # 清除本地 OAuth 令牌文件(不影响云端授权及 API Key)
88
+ x-search-mcp auth logout
89
+ ```
90
+
91
+ ---
92
+
93
+ ## CLI 命令一览
94
+
95
+ | 命令 | 说明 |
96
+ |---|---|
97
+ | `x-search-mcp` 或 `x-search-mcp --stdio` | 默认启动 MCP stdio 服务(供 Agent 客户端调用) |
98
+ | `x-search-mcp auth login` | 发起 OAuth 设备码登录并在终端交互 |
99
+ | `x-search-mcp auth status` | 安全输出当前认证模式、Key/OAuth 就绪状态 |
100
+ | `x-search-mcp auth logout` | 在文件锁保护下安全删除本地 OAuth 凭据 |
101
+ | `x-search-mcp config` | 打印安全的标准 `mcpServers` JSON 配置(不导出密钥) |
102
+ | `x-search-mcp search "<query>"` | 直接在命令行测试搜索并输出结构化 JSON |
103
+ | `x-search-mcp logs` | 列出最近的本地调用日志路径 |
104
+
105
+ ---
106
+
107
+ ## 环境变量参考
108
+
109
+ | 变量名 | 默认值 | 说明 |
110
+ |---|---|---|
111
+ | `XAI_API_KEY` | - | xAI API Key(优先级高于文件) |
112
+ | `XAI_API_KEY_FILE` | - | 存放 API Key 的私有文件路径 |
113
+ | `X_SEARCH_AUTH_MODE` | `auto` | 认证模式:`auto`、`api_key`、`oauth` |
114
+ | `X_SEARCH_AUTH_FILE` | `~/.config/x-search-mcp/auth.json` | 本地 OAuth 凭证文件路径 |
115
+ | `XAI_OAUTH_CLIENT_ID` | `b1a00492-073a-47ea-816f-4c329264a828` | OAuth 公开客户端 ID |
116
+ | `X_SEARCH_MODEL` | `grok-4.5` | 搜索调用模型 |
117
+ | `X_SEARCH_TIMEOUT_SECONDS` | `180` | 请求超时时间(秒,范围 5–600) |
118
+ | `X_SEARCH_RETRIES` | `2` | 失败重试次数(网络/5xx,范围 0–3) |
119
+ | `X_SEARCH_MAX_TURNS` | `4` | 内部搜索轮数(范围 1–20) |
120
+ | `X_SEARCH_REASONING_EFFORT`| - | 思考深度:`low` / `medium` / `high` / `xhigh` |
121
+ | `X_SEARCH_ENV_FILE` | - | 显式指定 `.env` 文件(通过 `process.loadEnvFile` 加载) |
122
+ | `X_SEARCH_LOG` | `on` | 是否保存调用日志:`on` / `off` |
123
+ | `X_SEARCH_LOG_DIR` | `~/.config/x-search-mcp/logs` | 调用日志目录 |
124
+ | `X_SEARCH_LOG_KEEP` | `100` | 保留最近多少份日志;`0` 表示不清理 |
125
+ | `X_SEARCH_LOG_MAX_BYTES` | `8388608` | 单次原始响应写入上限;超出后截断并标记 |
126
+ | `HTTP_PROXY` / `HTTPS_PROXY` | - | 标准 HTTP/HTTPS 代理配置 |
127
+
128
+ ---
129
+
130
+ ## 调用日志
131
+
132
+ 每次 `x_search` 都会在本地写一份 JSON 日志,里面是发给 xAI 的请求体和上游原始响应。MCP 结果只返回 `log_file` 路径,不把原始响应塞回工具输出。
133
+
134
+ - 默认目录:`~/.config/x-search-mcp/logs/`
135
+ - 文件权限:`0600`,目录权限:`0700`
136
+ - 字段:`request`、`attempts[].raw_response`、顶层 `raw_response`(最后一次尝试)
137
+ - 凭证处理:不记录 `Authorization`;`access_token`、`api_key` 等字段以及 `Bearer` / `xai-` 形态的字符串会替换成 `[redacted]`
138
+ - 失败调用也会写日志。HTTP 错误正文只进本地文件,不回传给 MCP 客户端
139
+ - 查看最近日志:`x-search-mcp logs`
140
+
141
+ 日志写入失败不会让搜索失败;结果的 `warnings` 会说明日志没写上。
142
+
143
+ ## Agent 接入配置
144
+
145
+ ### 1. 通用 mcpServers JSON(Claude Desktop / Cline / Cursor 等)
146
+
147
+ 在目标 Agent 的配置文件中添加:
148
+
149
+ ```json
150
+ {
151
+ "mcpServers": {
152
+ "x-search": {
153
+ "command": "npx",
154
+ "args": ["--yes", "@scoheart/x-search-mcp@0.1.0"],
155
+ "env": {
156
+ "X_SEARCH_AUTH_MODE": "auto",
157
+ "X_SEARCH_MODEL": "grok-4.5"
158
+ },
159
+ "timeout": 600
160
+ }
161
+ }
162
+ }
163
+ ```
164
+
165
+ > **提示**:上述 `npx` 配置需要包已发布到 npm。若安装的是本地 tarball,可改用 `"command": "x-search-mcp"`、`"args": []`,并确保客户端的 `PATH` 包含安装目录;也可以运行 `x-search-mcp config` 生成当前可执行入口的绝对路径配置。修改配置后请重启或重新加载 Agent 会话。Pi 使用 `--no-mcp` 启动时不会连接 MCP 服务。
166
+
167
+ ### 2. Codex 配置 (`~/.codex/config.toml`)
168
+
169
+ ```toml
170
+ [mcp_servers.x-search]
171
+ command = "npx"
172
+ args = ["--yes", "@scoheart/x-search-mcp@0.1.0"]
173
+ tool_timeout_sec = 600
174
+
175
+ [mcp_servers.x-search.env]
176
+ X_SEARCH_AUTH_MODE = "auto"
177
+ X_SEARCH_MODEL = "grok-4.5"
178
+ ```
179
+
180
+ ---
181
+
182
+ ## 工具参数规范 (`x_search`)
183
+
184
+ ```typescript
185
+ interface XSearchInput {
186
+ query: string; // 必填,检索语句(trim 后非空,最大 16000 字符)
187
+ allowed_x_handles?: string[]; // 可选,白名单账号(最多 10 个,无需 @,与 excluded 互斥)
188
+ excluded_x_handles?: string[]; // 可选,黑名单账号(最多 10 个,无需 @,与 allowed 互斥)
189
+ from_date?: string; // 可选,开始日期 YYYY-MM-DD(不能晚于当天 UTC,需严格早于 to_date)
190
+ to_date?: string; // 可选,结束日期 YYYY-MM-DD(不包含当天)
191
+ enable_image_understanding?: boolean; // 可选,是否启用推文配图理解
192
+ enable_video_understanding?: boolean; // 可选,是否启用推文视频理解
193
+ auth_mode?: 'auto' | 'api_key' | 'oauth';// 可选,单次调用的认证覆盖模式
194
+ }
195
+ ```
196
+
197
+ ### 返回结构示例
198
+
199
+ ```json
200
+ {
201
+ "success": true,
202
+ "provider": "xai",
203
+ "tool": "x_search",
204
+ "model": "grok-4.5",
205
+ "query": "OpenAI Sora news",
206
+ "credential_source": "oauth",
207
+ "billing_mode": "subscription_oauth",
208
+ "answer": "最新动态包括...",
209
+ "citations": [
210
+ "https://x.com/OpenAI/status/123456789"
211
+ ],
212
+ "inline_citations": [
213
+ {
214
+ "url": "https://x.com/OpenAI/status/123456789",
215
+ "title": "OpenAI on X",
216
+ "start_index": 12,
217
+ "end_index": 35
218
+ }
219
+ ],
220
+ "has_citations": true,
221
+ "degraded": false,
222
+ "degraded_reason": null,
223
+ "search_execution": "observed",
224
+ "server_side_calls": [
225
+ {
226
+ "call_id": "xs_123456",
227
+ "name": "x_keyword_search",
228
+ "arguments": { "query": "OpenAI Sora" }
229
+ }
230
+ ],
231
+ "response_status": "completed",
232
+ "incomplete_details": null,
233
+ "usage": {
234
+ "prompt_tokens": 800,
235
+ "completion_tokens": 350
236
+ },
237
+ "log_file": "/Users/username/.config/x-search-mcp/logs/call-20260814T120000000Z-ab12cd.json",
238
+ "warnings": [
239
+ "OAuth subscription search availability and quota are determined upstream by xAI; successful login does not guarantee search availability."
240
+ ]
241
+ }
242
+ ```
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const distCli = path.resolve(__dirname, '../dist/src/cli.js');
8
+ const srcCli = path.resolve(__dirname, '../src/cli.js');
9
+ const srcTsCli = path.resolve(__dirname, '../src/cli.ts');
10
+
11
+ async function main() {
12
+ let cliModule;
13
+ if (fs.existsSync(distCli)) {
14
+ cliModule = await import(distCli);
15
+ } else if (fs.existsSync(srcCli)) {
16
+ cliModule = await import(srcCli);
17
+ } else if (fs.existsSync(srcTsCli)) {
18
+ cliModule = await import(srcTsCli);
19
+ } else {
20
+ throw new Error('Could not locate CLI entry point in dist/ or src/');
21
+ }
22
+
23
+ await cliModule.runCli();
24
+ }
25
+
26
+ main().catch((err) => {
27
+ console.error('Fatal x-search-mcp error:', err);
28
+ process.exit(1);
29
+ });
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ import { runCli } from '../src/cli.js';
3
+
4
+ runCli().catch((err) => {
5
+ console.error('Fatal x-search-mcp error:', err);
6
+ process.exit(1);
7
+ });
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=x-search-mcp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"x-search-mcp.d.ts","sourceRoot":"","sources":["../../bin/x-search-mcp.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ import { runCli } from '../src/cli.js';
3
+ runCli().catch((err) => {
4
+ console.error('Fatal x-search-mcp error:', err);
5
+ process.exit(1);
6
+ });
7
+ //# sourceMappingURL=x-search-mcp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"x-search-mcp.js","sourceRoot":"","sources":["../../bin/x-search-mcp.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEvC,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACrB,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAC;IAChD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,42 @@
1
+ import type { AuthRecord, AuthStatus, DiscoveryMetadata } from './types.js';
2
+ /**
3
+ * Validate that an OAuth URL is secure and points strictly to an authorized x.ai domain.
4
+ */
5
+ export declare function validateOAuthUrl(rawUrl: string): URL;
6
+ /**
7
+ * Fetch and validate OIDC discovery metadata from x.ai.
8
+ */
9
+ export declare function getDiscoveryMetadata(): Promise<DiscoveryMetadata>;
10
+ /**
11
+ * Extract expiry timestamp from JWT payload exp claim.
12
+ * Note: Decoding the JWT payload is only used to extract the expiry timestamp;
13
+ * it is NOT cryptographic verification of the token signature.
14
+ */
15
+ export declare function extractJwtExpiry(jwtToken: string | undefined): number | null;
16
+ export declare function saveAuthDataAtomic(authFilePath: string, data: AuthRecord): void;
17
+ export declare function readAuthData(authFilePath: string): AuthRecord | null;
18
+ /**
19
+ * Execute OAuth Device Code login flow.
20
+ * Outputs user_code and verification URL, polls until user authorization is completed.
21
+ */
22
+ export declare function loginWithDeviceCode(): Promise<void>;
23
+ /**
24
+ * Get valid OAuth access token, refreshing preemptively if expiring within 120s.
25
+ * Concurrency-safe: shares auth.json across multiple agent processes using proper-lockfile.
26
+ */
27
+ export declare function getValidOAuthAccessToken(): Promise<string>;
28
+ /**
29
+ * Force refresh token on HTTP 401 error.
30
+ * If another concurrent process has already rotated the token, the new token is reused.
31
+ */
32
+ export declare function forceRefreshToken(rejectedAccessToken: string): Promise<string>;
33
+ /**
34
+ * Retrieve auth status without probing quota or revealing secret tokens.
35
+ */
36
+ export declare function getAuthStatus(): AuthStatus;
37
+ /**
38
+ * Logout by safely removing the local OAuth auth.json file under lock.
39
+ * Does NOT revoke cloud authorization and does NOT modify API keys.
40
+ */
41
+ export declare function logout(): Promise<void>;
42
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAgB5E;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CA2BpD;AAED;;GAEG;AACH,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAoBvE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAe5E;AAyBD,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI,CAkB/E;AAED,wBAAgB,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAWpE;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CA8HzD;AAuDD;;;GAGG;AACH,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,MAAM,CAAC,CAiChE;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,mBAAmB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAoBpF;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,UAAU,CAoB1C;AAED;;;GAGG;AACH,wBAAsB,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAkB5C"}