@scoheart/x-search-mcp 0.4.0 → 0.6.1

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 (105) hide show
  1. package/.env.example +7 -10
  2. package/README.md +18 -76
  3. package/THIRD_PARTY_NOTICES.md +4 -0
  4. package/TOOL.md +32 -268
  5. package/dist/evidence-licenses.txt +791 -0
  6. package/dist/evidence.html +170 -0
  7. package/dist/package.json +10 -3
  8. package/dist/src/auth.d.ts.map +1 -1
  9. package/dist/src/auth.js +6 -11
  10. package/dist/src/auth.js.map +1 -1
  11. package/dist/src/call-log.d.ts +5 -4
  12. package/dist/src/call-log.d.ts.map +1 -1
  13. package/dist/src/call-log.js +38 -29
  14. package/dist/src/call-log.js.map +1 -1
  15. package/dist/src/cli.d.ts.map +1 -1
  16. package/dist/src/cli.js +26 -19
  17. package/dist/src/cli.js.map +1 -1
  18. package/dist/src/config.d.ts.map +1 -1
  19. package/dist/src/config.js +12 -16
  20. package/dist/src/config.js.map +1 -1
  21. package/dist/src/device-login.d.ts +15 -41
  22. package/dist/src/device-login.d.ts.map +1 -1
  23. package/dist/src/device-login.js +101 -63
  24. package/dist/src/device-login.js.map +1 -1
  25. package/dist/src/events.d.ts +16 -0
  26. package/dist/src/events.d.ts.map +1 -0
  27. package/dist/src/events.js +128 -0
  28. package/dist/src/events.js.map +1 -0
  29. package/dist/src/evidence-app.d.ts +4 -0
  30. package/dist/src/evidence-app.d.ts.map +1 -0
  31. package/dist/src/evidence-app.js +17 -0
  32. package/dist/src/evidence-app.js.map +1 -0
  33. package/dist/src/http-server.d.ts +1 -0
  34. package/dist/src/http-server.d.ts.map +1 -1
  35. package/dist/src/http-server.js +6 -4
  36. package/dist/src/http-server.js.map +1 -1
  37. package/dist/src/http.d.ts +12 -15
  38. package/dist/src/http.d.ts.map +1 -1
  39. package/dist/src/http.js +78 -118
  40. package/dist/src/http.js.map +1 -1
  41. package/dist/src/index.d.ts +1 -0
  42. package/dist/src/index.d.ts.map +1 -1
  43. package/dist/src/index.js.map +1 -1
  44. package/dist/src/multi-user/service.d.ts.map +1 -1
  45. package/dist/src/multi-user/service.js +2 -1
  46. package/dist/src/multi-user/service.js.map +1 -1
  47. package/dist/src/search-input.d.ts +5 -17
  48. package/dist/src/search-input.d.ts.map +1 -1
  49. package/dist/src/search-input.js +5 -8
  50. package/dist/src/search-input.js.map +1 -1
  51. package/dist/src/search-response.d.ts +25 -5
  52. package/dist/src/search-response.d.ts.map +1 -1
  53. package/dist/src/search-response.js +97 -94
  54. package/dist/src/search-response.js.map +1 -1
  55. package/dist/src/search.d.ts +6 -3
  56. package/dist/src/search.d.ts.map +1 -1
  57. package/dist/src/search.js +131 -184
  58. package/dist/src/search.js.map +1 -1
  59. package/dist/src/server.d.ts +7 -10
  60. package/dist/src/server.d.ts.map +1 -1
  61. package/dist/src/server.js +109 -95
  62. package/dist/src/server.js.map +1 -1
  63. package/dist/src/tool-contract.d.ts +159 -126
  64. package/dist/src/tool-contract.d.ts.map +1 -1
  65. package/dist/src/tool-contract.js +78 -73
  66. package/dist/src/tool-contract.js.map +1 -1
  67. package/dist/src/types.d.ts +7 -37
  68. package/dist/src/types.d.ts.map +1 -1
  69. package/dist/src/watch-cli.d.ts +2 -0
  70. package/dist/src/watch-cli.d.ts.map +1 -0
  71. package/dist/src/watch-cli.js +41 -0
  72. package/dist/src/watch-cli.js.map +1 -0
  73. package/dist/src/watch-store.d.ts +41 -0
  74. package/dist/src/watch-store.d.ts.map +1 -0
  75. package/dist/src/watch-store.js +171 -0
  76. package/dist/src/watch-store.js.map +1 -0
  77. package/dist/ui/evidence.d.ts +2 -0
  78. package/dist/ui/evidence.d.ts.map +1 -0
  79. package/dist/ui/evidence.js +150 -0
  80. package/dist/ui/evidence.js.map +1 -0
  81. package/docs/events-and-apps.md +28 -0
  82. package/examples/codex.toml +6 -7
  83. package/examples/mcp.json +2 -6
  84. package/package.json +10 -3
  85. package/server.json +34 -0
  86. package/src/auth.ts +6 -11
  87. package/src/call-log.ts +39 -30
  88. package/src/cli.ts +26 -19
  89. package/src/config.ts +11 -13
  90. package/src/device-login.ts +107 -71
  91. package/src/events.ts +124 -0
  92. package/src/evidence-app.ts +19 -0
  93. package/src/http-server.ts +7 -4
  94. package/src/http.ts +83 -162
  95. package/src/index.ts +1 -0
  96. package/src/multi-user/service.ts +2 -1
  97. package/src/search-input.ts +5 -9
  98. package/src/search-response.ts +90 -95
  99. package/src/search.ts +126 -180
  100. package/src/server.ts +115 -104
  101. package/src/tool-contract.ts +82 -76
  102. package/src/types.ts +8 -40
  103. package/src/watch-cli.ts +35 -0
  104. package/src/watch-store.ts +182 -0
  105. package/tsconfig.json +2 -1
package/.env.example CHANGED
@@ -28,11 +28,9 @@ X_SEARCH_MODEL=grok-4.5
28
28
  # X_SEARCH_HTTP_ALLOWED_HOSTS=mcp.example.com
29
29
  # X_SEARCH_HTTP_ALLOWED_ORIGINS=https://app.example.com
30
30
 
31
- # HTTP 超时秒数 (默认 180,范围 5-600)
32
- X_SEARCH_TIMEOUT_SECONDS=180
33
-
34
- # 失败重试次数 (默认 2,范围 0-3,仅针对网络、超时及 5xx)
35
- X_SEARCH_RETRIES=2
31
+ # 搜索总时限和上游响应体上限
32
+ X_SEARCH_DEADLINE_MS=90000
33
+ X_SEARCH_MAX_RESPONSE_BYTES=2097152
36
34
 
37
35
  # 最大轮数 (默认 4,范围 1-20)
38
36
  X_SEARCH_MAX_TURNS=4
@@ -40,13 +38,12 @@ X_SEARCH_MAX_TURNS=4
40
38
  # 思考深度: low | medium | high | xhigh (可选)
41
39
  # X_SEARCH_REASONING_EFFORT=medium
42
40
 
43
- # 调用日志:保存每次请求体和上游原始响应。默认开启。
44
- # X_SEARCH_LOG=on
41
+ # 个人调用日志:metadata、debug 或 off
42
+ # X_SEARCH_LOG=metadata
45
43
  # X_SEARCH_LOG_DIR=/Users/username/.config/x-search-mcp/logs
46
- # 保留最近多少份日志,0 表示不清理(默认 100)
47
44
  # X_SEARCH_LOG_KEEP=100
48
- # 单次原始响应写入上限,超出则截断并标记(默认 8388608,最大 33554432)
49
- # X_SEARCH_LOG_MAX_BYTES=8388608
45
+ # X_SEARCH_LOG_MAX_BYTES=65536
46
+ # X_SEARCH_LOG_TOTAL_BYTES=33554432
50
47
 
51
48
  # 代理支持 (由 Undici EnvHttpProxyAgent 自动读取)
52
49
  # HTTP_PROXY=http://127.0.0.1:7890
package/README.md CHANGED
@@ -1,101 +1,43 @@
1
- # x-search-mcp
1
+ # X Search MCP
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@scoheart/x-search-mcp)](https://www.npmjs.com/package/@scoheart/x-search-mcp)
4
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
3
+ Search public X posts with Grok, review sources, and watch for new evidence.
5
4
 
6
- A read-only MCP server for searching X through the xAI Responses API. Its `x_search` tool supports account and date filters and returns source links when xAI provides them.
5
+ [中文指南](./TOOL.md) · [npm](https://www.npmjs.com/package/@scoheart/x-search-mcp)
7
6
 
8
- [中文使用手册](./TOOL.md)
7
+ ## Install
9
8
 
10
- | If you want to… | Use | xAI credentials |
11
- | --- | --- | --- |
12
- | Search from Codex, Pi, or another local MCP client | [Local stdio](#connect-a-local-mcp-client) | Your API key or eligible Grok subscription |
13
- | Use one account from several devices | [Single-account HTTP](#single-account-http) | One API key or eligible Grok subscription on the server |
14
- | Let invited users share a URL and use their own xAI accounts | [Multi-user HTTP](#multi-user-http) | Each user binds an API key and uses their own xAI API quota |
15
-
16
- ## Quick start
17
-
18
- Requires Node.js **22.19.0 or later**.
19
-
20
- ```sh
21
- npm install --global @scoheart/x-search-mcp
22
- ```
23
-
24
- Save your xAI API key in a private file and set `XAI_API_KEY_FILE`, or supply it through `XAI_API_KEY`. Then run a search from the terminal.
25
-
26
- ```sh
27
- export XAI_API_KEY_FILE=/absolute/path/to/xai-api-key
28
- x-search-mcp search "What did @SpaceX post about Starship this week?"
29
- ```
30
-
31
- For a personal Grok subscription, sign in with the device code and explicitly select OAuth for the test. X Search access depends on your account's subscription entitlements.
9
+ Requires Node.js 22.19+ and an MCP 2026-07-28 client.
32
10
 
33
11
  ```sh
34
- x-search-mcp auth login
35
- x-search-mcp auth status
36
- x-search-mcp search "What did @SpaceX post about Starship this week?" --auth oauth
12
+ npm install -g @scoheart/x-search-mcp
37
13
  ```
38
14
 
39
- `X_SEARCH_AUTH_MODE=oauth` selects subscription credentials for local stdio or single-account HTTP. The default `auto` mode uses a configured API key first, then looks for a local OAuth login. See the [authentication and configuration reference](./TOOL.md#凭证配置).
15
+ Set `XAI_API_KEY_FILE` to a private key file, or run `x-search-mcp auth login` with an eligible Grok subscription.
40
16
 
41
- ## Connect a local MCP client
17
+ ## Connect
42
18
 
43
- Run the command for your client after installing the package. The client launches `x-search-mcp` over stdio.
19
+ Launch `x-search-mcp` for [stdio clients](./examples/mcp.json). For Codex 0.172.0, use [loopback HTTP](./examples/codex.toml) with `mcp_2026_07_28` enabled.
44
20
 
45
21
  ```sh
46
- codex mcp add x-search -- x-search-mcp --stdio
47
- # Or with Pi
48
- pi mcp add x-search -- x-search-mcp --stdio
22
+ export X_SEARCH_HTTP_TOKEN="$(openssl rand -hex 32)"
23
+ x-search-mcp http --interactive-login
49
24
  ```
50
25
 
51
- In your client, ask it to use `x_search` and include links to the source posts. If the client launches in a separate environment, put `XAI_API_KEY_FILE` or `X_SEARCH_AUTH_MODE=oauth` in the server's environment configuration. Use the absolute executable path if your client cannot find `x-search-mcp`.
52
-
53
- When a local search returns `AUTH_REQUIRED`, your agent can call `x_search_login` to start Grok device authorization. Open the verification page and confirm the code and account in your browser. The agent can then call `x_search_auth_status` and retry the search after authorization completes. [Device sign-in details](./TOOL.md#在-agent-内完成-grok-设备授权)
26
+ See [multi-user HTTP](./docs/multi-user.md) for shared deployments.
54
27
 
55
- See the [Codex TOML](./examples/codex.toml) and [generic MCP JSON](./examples/mcp.json) examples for running the package with `npx` from the client.
28
+ ## Apps and Events
56
29
 
57
- ## Run over HTTP
30
+ - **Apps** lets you filter, open, and select sources to add to the conversation.
31
+ - **Events** delivers new post evidence from bounded watches. Start a personal listener with `--events`.
58
32
 
59
- Both modes use the official MCP SDK with Streamable HTTP.
33
+ [Apps and Events guide](./docs/events-and-apps.md)
60
34
 
61
- ### Single-account HTTP
62
-
63
- Configure xAI credentials on the host that runs the service. Save a long, randomly generated MCP bearer token in a separate private file and start the server.
35
+ ## Develop
64
36
 
65
37
  ```sh
66
- x-search-mcp http --token-file /absolute/path/to/mcp-token
67
- ```
68
-
69
- Connect to `http://127.0.0.1:8000/mcp` with `Authorization: Bearer <MCP token>`. All callers use the host's configured xAI account. For remote access, keep the server on loopback, terminate HTTPS at a reverse proxy, and pass its hostname with `--allowed-host mcp.example.com`. See `x-search-mcp http --help` and the [HTTP configuration reference](./TOOL.md#http-服务配置).
70
-
71
- ### Multi-user HTTP
72
-
73
- Deploy behind HTTPS with an MCP OAuth issuer and Alibaba Cloud KMS. Each invited user signs in to the same `/mcp` URL, opens `/settings` with the same identity, and binds their xAI API key. The server stores each binding encrypted and uses it for that user's searches. The settings page supports replacing and removing keys.
74
-
75
- ```sh
76
- x-search-mcp http --multi-user --port 8000
77
- ```
78
-
79
- See the [multi-user deployment guide](./docs/multi-user.md) for issuer setup, KMS permissions, reverse-proxy configuration, and Codex and Pi login commands. Grok subscription login is available in the personal modes.
80
-
81
- ## Results and logs
82
-
83
- - `x_search` accepts a query, optional X account filters, and a date range. See the [tool parameters](./TOOL.md#工具参数规范-x_search).
84
- - Results include the answer, citations, and available usage information. A result marked `degraded: true` lacks source links. Incomplete xAI responses carry a warning.
85
- - By default, stdio and single-account HTTP save local call logs under `~/.config/x-search-mcp/logs/`. Multi-user HTTP disables those logs.
86
-
87
- ## Development
88
-
89
- ```sh
90
- git clone https://github.com/Scoheart/x-search-mcp.git
91
- cd x-search-mcp
92
38
  npm ci
93
39
  npm test
94
40
  npm run verify:package
95
41
  ```
96
42
 
97
- `npm test` builds the project and checks the tool contracts, xAI response handling and HTTP authorization flows. `npm run verify:package` packs and installs the tarball in a temporary directory and connects to the installed stdio entry point over MCP 2025-11-25 and 2026-07-28. It uses an empty auth store and may download package dependencies. Pass an output directory after `--` to keep the verified tarball.
98
-
99
- ## License
100
-
101
- [MIT](./LICENSE) © Scoheart. The implementation was inspired by Nous Research's Hermes Agent. See [third-party notices](./THIRD_PARTY_NOTICES.md).
43
+ [MIT](./LICENSE) · [Third-party notices](./THIRD_PARTY_NOTICES.md)
@@ -33,3 +33,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
33
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
34
  SOFTWARE.
35
35
  ```
36
+
37
+ ## MCP App bundle
38
+
39
+ License notices for the libraries bundled into the evidence view are included in `dist/evidence-licenses.txt`.
package/TOOL.md CHANGED
@@ -1,290 +1,54 @@
1
- # @scoheart/x-search-mcp 工具接入与使用说明
1
+ # X Search MCP
2
2
 
3
- 本工具是一个独立运行的 Node.js/TypeScript MCP 服务,提供 `x_search` 检索和本机 stdio 的 Grok 设备授权工具。检索使用 xAI Responses API,支持 X(原 Twitter)实时动态与历史发帖。本服务实现参考 Nous Research 的 Hermes Agent(commit `fcca1740533d59d7a46ab1a3a53d3a45ef3127ff`),作为独立 MCP 包运行。
3
+ 用 Grok 搜索 X 的公开帖子,核对来源,并观察新证据。
4
4
 
5
- ---
5
+ ## 安装与接入
6
6
 
7
- ## 核心特性
7
+ 需要 Node.js 22.19+ 和 MCP 2026-07-28 客户端。
8
8
 
9
- - **标准 MCP 协议**:基于 `@modelcontextprotocol/server`,默认运行在 `stdio` 模式,输出标准 JSON-RPC。
10
- - **只读搜索**。 `x_search` 检索公开帖子。stdio 模式还提供 `x_search_login` 和 `x_search_auth_status`,供 agent 发起授权并检查本机状态。
11
- - **双认证模式**:支持按量付费 API Key 与 Grok 订阅 OAuth(设备码登录);支持多进程并发共享令牌并带文件锁(`proper-lockfile`)。
12
- - **来源与降级感知**:提取结构化 citations 与正文 inline_citations。若上游无有效引用来源,自动标记 `degraded: true`。
13
- - **原生双传输**:默认 stdio;`http` / `--http` 使用官方 SDK 的无状态 Streamable HTTP,兼容 2026 与 2025 两代协议。
14
-
15
- ---
16
-
17
- ## 约束与边界说明(重要)
18
-
19
- 1. **非官方项目**:本项目为独立开源 MCP 实现,并非 xAI 或 Nous Research / Hermes 官方 MCP。
20
- 2. **传输与访问范围**。默认 stdio 供本机子进程接入。单账号 HTTP 默认监听 `127.0.0.1:8000/mcp`,客户端使用独立 Bearer 密钥。多人 HTTP 通过身份服务进行 MCP OAuth 登录,每人绑定自己的 xAI API Key。公网接入通过 HTTPS 反向代理配置。 [多人部署说明](./docs/multi-user.md)
21
- 3. **公共 OAuth Client ID 说明**:默认内置的公开客户端 ID 来自 Hermes Agent(`b1a00492-073a-47ea-816f-4c329264a828`)。在浏览器授权确认页面中可能会显示该客户端标识名称。如需使用专属客户端,可通过 `XAI_OAUTH_CLIENT_ID` 环境变量配置。
22
- 4. **OAuth 成功不等于搜索可用**:OAuth 设备码登录成功仅代表获得 xAI 账号凭证,实际搜索能力及并发额度由 xAI 云端及订阅权益决定;登录成功不保证账号一定具备 X Search 权限。
23
- 5. **无引用不可作为确证**:当结果标记为 `has_citations: false` / `degraded: true` 时,表示上游未返回任何经过验证的 URL 引用,该回答不可作为来源充分的 X 检索事实依据。
24
- 6. **计费与回退规则**:
25
- - API Key 模式下按调用量付费(api_metered)。
26
- - 显式 `oauth` 模式下仅使用订阅令牌,**绝不回退**到付费 API Key,避免产生非预期账单。
27
- - `auto` 模式优先使用 API Key(如果配置),无 Key 时使用本地保存的 OAuth。
28
- 7. **重试消耗**。stdio 和单账号 HTTP 遇到网络波动或 5xx 错误时可执行有限次数退避重试,可能重复消耗上游额度。多人 HTTP 每次搜索只发起一次上游请求。
29
- 8. **过滤作用域限制**:xAI 接口的 `allowed_x_handles`、`excluded_x_handles`、`from_date` 和 `to_date` 过滤主要作用于关键词与语义搜索阶段,并不严格保证限制上游服务端内部的级联线程抓取(thread fetch)。
30
-
31
- ---
32
-
33
- ## npm 安装与打包
34
-
35
- 需要 Node.js 22.19.0 或更高版本。可以用 `npx` 直接运行。
36
-
37
- ```bash
38
- npx --yes @scoheart/x-search-mcp@0.4.0 auth login
39
- npx --yes @scoheart/x-search-mcp@0.4.0 auth status
9
+ ```sh
10
+ npm install -g @scoheart/x-search-mcp
40
11
  ```
41
12
 
42
- 也可以在源码仓库运行 `npm ci` 和 `npm pack` 生成安装包。`prepack` 会先编译当前源码,安装端无需 TypeScript 或开发依赖。
13
+ 支持新版协议的本地客户端可以用 [stdio 配置](./examples/mcp.json)。Codex 0.172.0 通过回环 HTTP 接入,打开 `mcp_2026_07_28`。服务与 Codex 使用同一份 HTTP Bearer。
43
14
 
44
- ```bash
45
- npm install --global ./scoheart-x-search-mcp-0.4.0.tgz
46
- x-search-mcp auth status
15
+ ```sh
16
+ export X_SEARCH_HTTP_TOKEN="$(openssl rand -hex 32)"
17
+ x-search-mcp http --interactive-login
47
18
  ```
48
19
 
49
- 包通过 `files` 白名单包含代码、示例和文档;OAuth 凭据和调用日志存放在用户配置目录,不随包分发。
20
+ [Codex 配置](./examples/codex.toml) · [多人 HTTP 部署](./docs/multi-user.md)
50
21
 
51
22
  ## 凭证配置
52
23
 
53
- ### 方式 1:API Key(按量计费)
54
-
55
- 直接设置环境变量或指向私有文件:
56
- ```bash
57
- export XAI_API_KEY="xai-xxxxxxxxxxxxxxxxxxxx"
58
- # 或
59
- export XAI_API_KEY_FILE="/path/to/private/xai-key.txt"
60
- ```
61
-
62
- ### 方式 2:Grok 订阅 OAuth(设备码流程)
63
-
64
- 无需购买 API Key,使用包含 X Search 权益的 xAI 账号直接在终端完成登录:
65
-
66
- ```bash
67
- # 执行设备码登录
68
- x-search-mcp auth login
69
- ```
24
+ | 方式 | 用法 |
25
+ | --- | --- |
26
+ | xAI API | 用 `XAI_API_KEY_FILE` 指向私有密钥文件,或设置 `XAI_API_KEY` |
27
+ | Grok 订阅 | 运行 `x-search-mcp auth login`,或在支持表单 elicitation 的宿主里确认登录 |
28
+ | 多人 HTTP | 先登录 MCP 服务,再在 `/settings` 绑定自己的 xAI API key |
70
29
 
71
- 终端将输出授权网址与用户确认码:
72
- ```text
73
- 1. Visit the authorization page: https://auth.x.ai/oauth2/device/code
74
- 2. Confirm code: ABCD-EFGH
75
- 3. Code expires at: 20:30:00
76
- Waiting for browser authorization...
77
- ```
78
- 在浏览器中打开链接、输入确认码并批准后,令牌将原子写入本地配置文件:
79
- - 默认路径:`~/.config/x-search-mcp/auth.json`(权限 `0600`,目录权限 `0700`)
80
- - 自定义路径:可通过 `X_SEARCH_AUTH_FILE` 覆盖。
30
+ `X_SEARCH_AUTH_MODE` 默认为 `auto`,优先使用已配置的 API key。用 `oauth` 显式选择订阅。用 `x-search-mcp auth status` 查看状态,用 `auth logout` 清除本机登录。
81
31
 
82
32
  ### 在 agent 内完成 Grok 设备授权
83
33
 
84
- 1. 当 stdio 的 `x_search` 返回 `error.code: "AUTH_REQUIRED"` 时,结果中的 `login_tool` 指向 `x_search_login`。用户也可以直接请 agent 发起 Grok 登录。
85
- 2. `x_search_login` 返回 `pending`、官方验证链接、用户短码和有效期。用户在浏览器核对账号与短码并批准本次授权。原 stdio 进程在后台轮询,等待期间重复调用复用同一设备码。
86
- 3. 批准后,agent 调用 `x_search_auth_status`。最近一次流程到达 `authorized` 表示令牌已经保存,可以重试原搜索。`oauth_state` 描述本机缓存的凭证有效期,实际搜索仍取决于 xAI 订阅权益。
87
-
88
- 令牌和用于后端换取令牌的 `device_code` 留在服务端。stdio 的这两个工具使用本机 Grok 设备授权。单账号 HTTP 使用独立的 MCP Bearer 密钥,多人 HTTP 使用 MCP OAuth 和独立的 API Key 绑定。 [MCP Authorization](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization) · [RFC 8628](https://www.rfc-editor.org/rfc/rfc8628.html)
89
-
90
- ### 凭证状态与注销
91
-
92
- ```bash
93
- # 查看凭证配置状态(不探测额度、不泄露密钥内容)
94
- x-search-mcp auth status
95
-
96
- # 清除本地 OAuth 令牌文件(不影响云端授权及 API Key)
97
- x-search-mcp auth logout
98
- ```
99
-
100
- ---
101
-
102
- ## CLI 命令一览
103
-
104
- | 命令 | 说明 |
105
- |---|---|
106
- | `x-search-mcp` 或 `x-search-mcp --stdio` | 默认启动 MCP stdio 服务(供 Agent 客户端调用) |
107
- | `x-search-mcp http [options]` 或 `x-search-mcp --http [options]` | 启动单账号 Streamable HTTP,使用独立访问密钥 |
108
- | `x-search-mcp http --multi-user` | 启动受邀多用户模式,各自绑定 xAI API Key,配置见 [多人部署说明](./docs/multi-user.md) |
109
- | `x-search-mcp --help` / `x-search-mcp http --help` | 查看命令与 HTTP 参数 |
110
- | `x-search-mcp auth login` | 发起 OAuth 设备码登录并在终端交互 |
111
- | `x-search-mcp auth status` | 安全输出当前认证模式、Key/OAuth 就绪状态 |
112
- | `x-search-mcp auth logout` | 在文件锁保护下安全删除本地 OAuth 凭据 |
113
- | `x-search-mcp config` | 打印安全的标准 `mcpServers` JSON 配置(不导出密钥) |
114
- | `x-search-mcp search "<query>"` | 直接在命令行测试搜索并输出结构化 JSON |
115
- | `x-search-mcp logs` | 列出最近的本地调用日志路径 |
116
-
117
- ---
118
-
119
- ## 环境变量参考
120
-
121
- | 变量名 | 默认值 | 说明 |
122
- |---|---|---|
123
- | `XAI_API_KEY` | - | xAI API Key(优先级高于文件) |
124
- | `XAI_API_KEY_FILE` | - | 存放 API Key 的私有文件路径 |
125
- | `X_SEARCH_AUTH_MODE` | `auto` | 认证模式:`auto`、`api_key`、`oauth` |
126
- | `X_SEARCH_AUTH_FILE` | `~/.config/x-search-mcp/auth.json` | 本地 OAuth 凭证文件路径 |
127
- | `XAI_OAUTH_CLIENT_ID` | `b1a00492-073a-47ea-816f-4c329264a828` | OAuth 公开客户端 ID |
128
- | `X_SEARCH_MODEL` | `grok-4.5` | 搜索调用模型 |
129
- | `X_SEARCH_TIMEOUT_SECONDS` | `180` | 请求超时时间(秒,范围 5–600) |
130
- | `X_SEARCH_RETRIES` | `2` | stdio 和单账号 HTTP 的失败重试次数(网络/5xx,范围 0–3) |
131
- | `X_SEARCH_MAX_TURNS` | `4` | 内部搜索轮数(范围 1–20) |
132
- | `X_SEARCH_REASONING_EFFORT`| - | 思考深度:`low` / `medium` / `high` / `xhigh` |
133
- | `X_SEARCH_ENV_FILE` | - | 显式指定 `.env` 文件(通过 `process.loadEnvFile` 加载) |
134
- | `X_SEARCH_LOG` | `on` | stdio 和单账号 HTTP 是否保存调用日志(`on` / `off`) |
135
- | `X_SEARCH_LOG_DIR` | `~/.config/x-search-mcp/logs` | 调用日志目录 |
136
- | `X_SEARCH_LOG_KEEP` | `100` | 保留最近多少份日志;`0` 表示不清理 |
137
- | `X_SEARCH_LOG_MAX_BYTES` | `8388608` | 单次原始响应写入上限;超出后截断并标记 |
138
- | `HTTP_PROXY` / `HTTPS_PROXY` | - | 标准 HTTP/HTTPS 代理配置 |
139
-
140
- ### HTTP 服务配置
141
-
142
- | 变量名 | 默认值 | 说明 |
143
- |---|---|---|
144
- | `X_SEARCH_HTTP_TOKEN` | - | HTTP MCP 的独立 Bearer 密钥;不输出到日志 |
145
- | `X_SEARCH_HTTP_TOKEN_FILE` | - | 仅包含 HTTP 密钥的私有文件,启动时读取 |
146
- | `X_SEARCH_HTTP_HOST` | `127.0.0.1` | 监听地址;远端接入通常由本机 HTTPS 反代转发 |
147
- | `X_SEARCH_HTTP_PORT` | `8000` | TCP 端口;`0` 自动分配 |
148
- | `X_SEARCH_HTTP_PATH` | `/mcp` | 规范化 URL 路径,不含查询参数或片段 |
149
- | `X_SEARCH_HTTP_ALLOWED_HOSTS` | - | 额外允许的 Host 主机名,逗号分隔,不含协议或端口 |
150
- | `X_SEARCH_HTTP_ALLOWED_ORIGINS` | - | 明确允许的浏览器 Origin,逗号分隔,匹配协议、主机和端口 |
34
+ 客户端确认后,`x_search` 返回 `authorization_pending`、验证链接和短码。打开 Grok 核对账号和短码,批准后重试搜索。
151
35
 
152
- 同名 CLI 参数覆盖环境默认值。`--allowed-host` 与 `--allowed-origin` 可重复;提供这类参数时使用命令行列表。显式 `--token-file` 优先,否则非空 `X_SEARCH_HTTP_TOKEN` 优先于环境 token 文件。空密钥、非法路径、非法端口或不可读文件会阻止启动。
36
+ ## 工具参数规范(x_search)
153
37
 
154
- HTTP 请求体默认上限为 1 MiB。`SIGINT`/`SIGTERM` 停止服务并取消活动请求;上游超时和客户端取消持续覆盖响应体读取阶段。
38
+ | 参数 | 用法 |
39
+ | --- | --- |
40
+ | `query` | 检索语句,必填 |
41
+ | `allowed_x_handles` / `excluded_x_handles` | 互斥,各最多 10 个账号 |
42
+ | `from_date` / `to_date` | YYYY-MM-DD,包含起始日,截止日不包含 |
43
+ | `enable_image_understanding` / `enable_video_understanding` | 可选媒体分析 |
44
+ | `detail` | 默认 `concise`,设为 `detailed` 可取更长回答和更多证据 |
155
45
 
156
- 退出会取消尚在等待 OAuth 文件锁的请求。已经发出的刷新请求可能轮换 refresh token,因此服务会等待它完成原子保存,再完成退出;该阶段受独立的 30 秒刷新超时约束。取消后不会继续发起搜索。
46
+ `structuredContent` 返回 `answer`、去重的 `sources[]`、用量和耗时。按帖子来源的 `post_id` 与链接核对证据。`partial` 表示上游结果不完整,`unverified` 表示缺少可识别的 X 帖子来源,`truncated` 表示本地截断。
157
47
 
158
- ---
48
+ ## 运行配置
159
49
 
160
- ## 调用日志
50
+ - 搜索默认总时限 90 秒,上游响应上限 2 MiB。参数见 [.env.example](./.env.example)。
51
+ - 个人日志默认只记录耗时和计数。运行 `x-search-mcp logs` 查看,设 `X_SEARCH_LOG=debug` 开启限量调试数据。
52
+ - `x-search-mcp http --help` 查看监听、访问凭据和主机限制选项。交互登录和 Events 面向个人回环监听器。
161
53
 
162
- stdio 和单账号 HTTP 默认在本地记录发给 xAI 的请求体和上游原始响应,并在工具结果中提供 `log_file` 路径。多人 HTTP 关闭调用日志。
163
-
164
- - 默认目录:`~/.config/x-search-mcp/logs/`
165
- - 文件权限:`0600`,目录权限:`0700`
166
- - 字段:`request`、`attempts[].raw_response`、顶层 `raw_response`(最后一次尝试)
167
- - 凭证处理:不记录 `Authorization`;`access_token`、`api_key` 等字段以及 `Bearer` / `xai-` 形态的字符串会替换成 `[redacted]`
168
- - 失败调用也会写日志。HTTP 错误正文只进本地文件,不回传给 MCP 客户端
169
- - 查看最近日志:`x-search-mcp logs`
170
-
171
- 日志写入失败不会让搜索失败;结果的 `warnings` 会说明日志没写上。
172
-
173
- ## Agent 接入配置
174
-
175
- ### 单账号 HTTP
176
-
177
- 源码构建后执行:
178
-
179
- ```bash
180
- export X_SEARCH_AUTH_MODE=oauth
181
- export X_SEARCH_HTTP_TOKEN="$(openssl rand -hex 32)"
182
- node bin/x-search-mcp.js http --port 8000
183
- ```
184
-
185
- 安装后可直接使用 `x-search-mcp http`。客户端选择 **Streamable HTTP**,填写 `http://127.0.0.1:8000/mcp`,请求头设为 `Authorization: Bearer <X_SEARCH_HTTP_TOKEN 的值>`。未带正确密钥返回 401。来源 Host 或 Origin 未获允许时返回 403。
186
-
187
- 单账号 HTTP 使用同一份本机 Grok 凭证和调用日志。其搜索同时支持新旧 MCP 协议,设备授权可在服务端运行 CLI 进行。
188
-
189
- 若经域名代理,配置 `--allowed-host mcp.example.com`,让客户端连接 `https://mcp.example.com/mcp`。浏览器跨站调用还需显式 `--allowed-origin https://app.example.com`;非浏览器客户端可以不发送 Origin。HTTP 密钥与 Grok 凭证、npm 发布 token 分开管理。
190
-
191
- ### 1. 通用 mcpServers JSON(Claude Desktop / Cline / Cursor 等)
192
-
193
- 在目标 Agent 的配置文件中添加:
194
-
195
- ```json
196
- {
197
- "mcpServers": {
198
- "x-search": {
199
- "command": "npx",
200
- "args": ["--yes", "@scoheart/x-search-mcp@0.4.0"],
201
- "env": {
202
- "X_SEARCH_AUTH_MODE": "auto",
203
- "X_SEARCH_MODEL": "grok-4.5"
204
- },
205
- "timeout": 600
206
- }
207
- }
208
- }
209
- ```
210
-
211
- > 若安装的是本地 tarball,可改用 `"command": "x-search-mcp"`、`"args": []`,并确保客户端的 `PATH` 包含安装目录。也可以运行 `x-search-mcp config` 生成当前可执行入口的绝对路径配置。修改配置后请重启或重新加载 Agent 会话。Pi 使用 `--no-mcp` 启动时不会连接 MCP 服务。
212
-
213
- ### 2. Codex 配置 (`~/.codex/config.toml`)
214
-
215
- ```toml
216
- [mcp_servers.x-search]
217
- command = "npx"
218
- args = ["--yes", "@scoheart/x-search-mcp@0.4.0"]
219
- tool_timeout_sec = 600
220
-
221
- [mcp_servers.x-search.env]
222
- X_SEARCH_AUTH_MODE = "auto"
223
- X_SEARCH_MODEL = "grok-4.5"
224
- ```
225
-
226
- ---
227
-
228
- ## 工具参数规范 (`x_search`)
229
-
230
- ```typescript
231
- interface XSearchInput {
232
- query: string; // 必填,检索语句(trim 后非空,最大 16000 字符)
233
- allowed_x_handles?: string[]; // 可选,白名单账号(最多 10 个,无需 @,与 excluded 互斥)
234
- excluded_x_handles?: string[]; // 可选,黑名单账号(最多 10 个,无需 @,与 allowed 互斥)
235
- from_date?: string; // 可选,开始日期 YYYY-MM-DD(不能晚于当天 UTC,需严格早于 to_date)
236
- to_date?: string; // 可选,结束日期 YYYY-MM-DD(不包含当天)
237
- enable_image_understanding?: boolean; // 可选,是否启用推文配图理解
238
- enable_video_understanding?: boolean; // 可选,是否启用推文视频理解
239
- auth_mode?: 'auto' | 'api_key' | 'oauth'; // 可选,仅本地 stdio 和单账号 HTTP
240
- }
241
- ```
242
-
243
- ### 返回结构示例
244
-
245
- 客户端在 `tools/list` 能看到结果 schema。同一结果同时写入 `structuredContent` 和 JSON 文本。`search_execution: "observed"` 表示上游返回了 X Search 调用或 X 帖子/账号获取计数。判断回答的来源覆盖情况时检查 `citations` 和 `degraded`。当上游报告结果不完整时,`warnings` 会提示覆盖范围有限。内联引用的位置来自上游原始文本。
246
-
247
- ```json
248
- {
249
- "success": true,
250
- "provider": "xai",
251
- "tool": "x_search",
252
- "model": "grok-4.5",
253
- "query": "OpenAI Sora news",
254
- "credential_source": "oauth",
255
- "billing_mode": "subscription_oauth",
256
- "answer": "最新动态包括...",
257
- "citations": [
258
- "https://x.com/OpenAI/status/123456789"
259
- ],
260
- "inline_citations": [
261
- {
262
- "url": "https://x.com/OpenAI/status/123456789",
263
- "title": "OpenAI on X",
264
- "start_index": 12,
265
- "end_index": 35
266
- }
267
- ],
268
- "has_citations": true,
269
- "degraded": false,
270
- "degraded_reason": null,
271
- "search_execution": "observed",
272
- "server_side_calls": [
273
- {
274
- "call_id": "xs_123456",
275
- "name": "x_keyword_search",
276
- "arguments": { "query": "OpenAI Sora" }
277
- }
278
- ],
279
- "response_status": "completed",
280
- "incomplete_details": null,
281
- "usage": {
282
- "prompt_tokens": 800,
283
- "completion_tokens": 350
284
- },
285
- "log_file": "/Users/username/.config/x-search-mcp/logs/call-20260814T120000000Z-ab12cd.json",
286
- "warnings": [
287
- "OAuth subscription search availability and quota are determined upstream by xAI; successful login does not guarantee search availability."
288
- ]
289
- }
290
- ```
54
+ [引用审阅和证据观察](./docs/events-and-apps.md)