@scoheart/x-search-mcp 0.0.0-stage → 0.2.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 (76) hide show
  1. package/.env.example +54 -0
  2. package/LICENSE +21 -0
  3. package/README.md +116 -2
  4. package/THIRD_PARTY_NOTICES.md +35 -0
  5. package/TOOL.md +279 -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/package.json +64 -0
  13. package/dist/src/auth.d.ts +42 -0
  14. package/dist/src/auth.d.ts.map +1 -0
  15. package/dist/src/auth.js +446 -0
  16. package/dist/src/auth.js.map +1 -0
  17. package/dist/src/call-log.d.ts +28 -0
  18. package/dist/src/call-log.d.ts.map +1 -0
  19. package/dist/src/call-log.js +159 -0
  20. package/dist/src/call-log.js.map +1 -0
  21. package/dist/src/cli.d.ts +2 -0
  22. package/dist/src/cli.d.ts.map +1 -0
  23. package/dist/src/cli.js +156 -0
  24. package/dist/src/cli.js.map +1 -0
  25. package/dist/src/config.d.ts +8 -0
  26. package/dist/src/config.d.ts.map +1 -0
  27. package/dist/src/config.js +104 -0
  28. package/dist/src/config.js.map +1 -0
  29. package/dist/src/http-config.d.ts +4 -0
  30. package/dist/src/http-config.d.ts.map +1 -0
  31. package/dist/src/http-config.js +65 -0
  32. package/dist/src/http-config.js.map +1 -0
  33. package/dist/src/http-server.d.ts +20 -0
  34. package/dist/src/http-server.d.ts.map +1 -0
  35. package/dist/src/http-server.js +189 -0
  36. package/dist/src/http-server.js.map +1 -0
  37. package/dist/src/http.d.ts +28 -0
  38. package/dist/src/http.d.ts.map +1 -0
  39. package/dist/src/http.js +144 -0
  40. package/dist/src/http.js.map +1 -0
  41. package/dist/src/index.d.ts +12 -0
  42. package/dist/src/index.d.ts.map +1 -0
  43. package/dist/src/index.js +12 -0
  44. package/dist/src/index.js.map +1 -0
  45. package/dist/src/search.d.ts +24 -0
  46. package/dist/src/search.d.ts.map +1 -0
  47. package/dist/src/search.js +480 -0
  48. package/dist/src/search.js.map +1 -0
  49. package/dist/src/server.d.ts +11 -0
  50. package/dist/src/server.d.ts.map +1 -0
  51. package/dist/src/server.js +124 -0
  52. package/dist/src/server.js.map +1 -0
  53. package/dist/src/types.d.ts +113 -0
  54. package/dist/src/types.d.ts.map +1 -0
  55. package/dist/src/types.js +2 -0
  56. package/dist/src/types.js.map +1 -0
  57. package/dist/src/version.d.ts +2 -0
  58. package/dist/src/version.d.ts.map +1 -0
  59. package/dist/src/version.js +3 -0
  60. package/dist/src/version.js.map +1 -0
  61. package/examples/codex.toml +8 -0
  62. package/examples/mcp.json +16 -0
  63. package/package.json +62 -4
  64. package/src/auth.ts +496 -0
  65. package/src/call-log.ts +162 -0
  66. package/src/cli.ts +172 -0
  67. package/src/config.ts +107 -0
  68. package/src/http-config.ts +48 -0
  69. package/src/http-server.ts +188 -0
  70. package/src/http.ts +197 -0
  71. package/src/index.ts +11 -0
  72. package/src/search.ts +519 -0
  73. package/src/server.ts +129 -0
  74. package/src/types.ts +116 -0
  75. package/src/version.ts +3 -0
  76. package/tsconfig.json +26 -0
package/.env.example ADDED
@@ -0,0 +1,54 @@
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
+ # 原生 Streamable HTTP(x-search-mcp http 或 --http);HTTP 访问密钥独立于 xAI 凭证。
23
+ # X_SEARCH_HTTP_TOKEN=replace-with-a-long-random-http-access-token
24
+ # X_SEARCH_HTTP_TOKEN_FILE=/path/to/private/http-token
25
+ # X_SEARCH_HTTP_HOST=127.0.0.1
26
+ # X_SEARCH_HTTP_PORT=8000
27
+ # X_SEARCH_HTTP_PATH=/mcp
28
+ # X_SEARCH_HTTP_ALLOWED_HOSTS=mcp.example.com
29
+ # X_SEARCH_HTTP_ALLOWED_ORIGINS=https://app.example.com
30
+
31
+ # HTTP 超时秒数 (默认 180,范围 5-600)
32
+ X_SEARCH_TIMEOUT_SECONDS=180
33
+
34
+ # 失败重试次数 (默认 2,范围 0-3,仅针对网络、超时及 5xx)
35
+ X_SEARCH_RETRIES=2
36
+
37
+ # 最大轮数 (默认 4,范围 1-20)
38
+ X_SEARCH_MAX_TURNS=4
39
+
40
+ # 思考深度: low | medium | high | xhigh (可选)
41
+ # X_SEARCH_REASONING_EFFORT=medium
42
+
43
+ # 调用日志:保存每次请求体和上游原始响应。默认开启。
44
+ # X_SEARCH_LOG=on
45
+ # X_SEARCH_LOG_DIR=/Users/username/.config/x-search-mcp/logs
46
+ # 保留最近多少份日志,0 表示不清理(默认 100)
47
+ # X_SEARCH_LOG_KEEP=100
48
+ # 单次原始响应写入上限,超出则截断并标记(默认 8388608,最大 33554432)
49
+ # X_SEARCH_LOG_MAX_BYTES=8388608
50
+
51
+ # 代理支持 (由 Undici EnvHttpProxyAgent 自动读取)
52
+ # HTTP_PROXY=http://127.0.0.1:7890
53
+ # HTTPS_PROXY=http://127.0.0.1:7890
54
+ # 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,117 @@
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
+ Run the package directly with `npx`.
14
+
15
+ ```bash
16
+ npx --yes @scoheart/x-search-mcp@0.2.0 auth login
17
+ npx --yes @scoheart/x-search-mcp@0.2.0 auth status
18
+ ```
19
+
20
+ You can also install a locally built npm tarball.
21
+
22
+ ```bash
23
+ npm install --global ./scoheart-x-search-mcp-0.2.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
+ ### Native Streamable HTTP (0.2.0)
60
+
61
+ The server uses the official MCP SDK directly; it can serve both the 2026 protocol and stateless 2025-era Streamable HTTP clients. Default stdio startup remains available.
62
+
63
+ Run from a built source checkout:
64
+
65
+ ```bash
66
+ export X_SEARCH_AUTH_MODE=oauth
67
+ export X_SEARCH_HTTP_TOKEN="$(openssl rand -hex 32)"
68
+ node bin/x-search-mcp.js http --port 8000
69
+ ```
70
+
71
+ For an installed 0.2.0 package, use `x-search-mcp http` (or `x-search-mcp --http`). The default endpoint is `http://127.0.0.1:8000/mcp`. Each client supplies `Authorization: Bearer <the value of X_SEARCH_HTTP_TOKEN>`.
72
+
73
+ For a persistent credential, use `--token-file /path/to/private/http-token` or `X_SEARCH_HTTP_TOKEN_FILE` instead of an inline token. The file contains only the bearer token and should be private (`0600`). It is read at startup. An explicit `--token-file` overrides environment credentials; otherwise `X_SEARCH_HTTP_TOKEN` takes precedence over `X_SEARCH_HTTP_TOKEN_FILE`.
74
+
75
+ HTTP access uses its own credential. Grok subscription OAuth is completed on the server machine using `auth login`; its credential file and search logs remain there. All authorized callers use that service's configured Grok account. HTTP access currently supports clients with configured bearer headers, rather than an OAuth sign-in/discovery service.
76
+
77
+ For an HTTPS reverse proxy, keep the listener on loopback and explicitly allow the proxy's public Host hostname:
78
+
79
+ ```bash
80
+ x-search-mcp http --token-file /path/to/private/http-token \
81
+ --allowed-host mcp.example.com
82
+ ```
83
+
84
+ Point the client at `https://mcp.example.com/mcp`. Configure proxy timeouts for long searches (the default upstream timeout is 180 seconds). If a browser client calls the service directly, add its exact origin, for example `--allowed-origin https://app.example.com`; preflight is enabled only for explicitly allowed origins. Host allowlist entries have no scheme or port. Origin allowlist entries include scheme and port and have no path.
85
+
86
+ `--host`, `--port`, `--path`, `--allowed-host` and `--allowed-origin` configure deployment; the two allowlist flags are repeatable. `SIGINT`/`SIGTERM` stop the listener and cancel active work, including OAuth lock waiters. An already submitted OAuth refresh is allowed to finish its atomic credential save before shutdown completes: aborting that exchange could lose an upstream-rotated refresh token. This phase has its own 30-second deadline. See `x-search-mcp http --help` and [TOOL.md](./TOOL.md) for environment defaults.
87
+
88
+ ### MCP Client Configuration
89
+
90
+ Add this server to Pi's `~/.pi/agent/mcp.json` or another client that accepts `mcpServers`.
91
+
92
+ ```json
93
+ {
94
+ "mcpServers": {
95
+ "x-search": {
96
+ "command": "npx",
97
+ "args": ["--yes", "@scoheart/x-search-mcp@0.2.0"],
98
+ "env": {
99
+ "X_SEARCH_AUTH_MODE": "oauth",
100
+ "X_SEARCH_MODEL": "grok-4.5"
101
+ },
102
+ "timeout": 600
103
+ }
104
+ }
105
+ }
106
+ ```
107
+
108
+ 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.
109
+
110
+ 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`.
111
+
112
+ See [TOOL.md](./TOOL.md) for full Chinese documentation, parameters, and configuration options.
113
+
114
+ ## License
115
+
116
+ [MIT](./LICENSE) © Scoheart
117
+ 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,279 @@
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
+ - **原生双传输**:默认 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 密钥。公网访问通过 HTTPS 反向代理配置,Host/Origin 使用显式白名单。HTTP 接口支持配置请求头的客户端;此版本不提供 OAuth 自动登录/发现服务。
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. **重试消耗**:网络波动或 5xx 错误时会执行有限次数退避重试,重试请求可能重复消耗上游额度。
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.2.0 auth login
39
+ npx --yes @scoheart/x-search-mcp@0.2.0 auth status
40
+ ```
41
+
42
+ 也可以在源码仓库运行 `npm ci` 和 `npm pack` 生成安装包。`prepack` 会先编译当前源码,安装端无需 TypeScript 或开发依赖。
43
+
44
+ ```bash
45
+ npm install --global ./scoheart-x-search-mcp-0.2.0.tgz
46
+ x-search-mcp auth status
47
+ ```
48
+
49
+ 包通过 `files` 白名单包含代码、示例和文档;OAuth 凭据和调用日志存放在用户配置目录,不随包分发。
50
+
51
+ ## 凭证配置
52
+
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
+ ```
70
+
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` 覆盖。
81
+
82
+ ### 凭证状态与注销
83
+
84
+ ```bash
85
+ # 查看凭证配置状态(不探测额度、不泄露密钥内容)
86
+ x-search-mcp auth status
87
+
88
+ # 清除本地 OAuth 令牌文件(不影响云端授权及 API Key)
89
+ x-search-mcp auth logout
90
+ ```
91
+
92
+ ---
93
+
94
+ ## CLI 命令一览
95
+
96
+ | 命令 | 说明 |
97
+ |---|---|
98
+ | `x-search-mcp` 或 `x-search-mcp --stdio` | 默认启动 MCP stdio 服务(供 Agent 客户端调用) |
99
+ | `x-search-mcp http [options]` 或 `x-search-mcp --http [options]` | 启动原生 Streamable HTTP;必需独立访问密钥 |
100
+ | `x-search-mcp --help` / `x-search-mcp http --help` | 查看命令与 HTTP 参数 |
101
+ | `x-search-mcp auth login` | 发起 OAuth 设备码登录并在终端交互 |
102
+ | `x-search-mcp auth status` | 安全输出当前认证模式、Key/OAuth 就绪状态 |
103
+ | `x-search-mcp auth logout` | 在文件锁保护下安全删除本地 OAuth 凭据 |
104
+ | `x-search-mcp config` | 打印安全的标准 `mcpServers` JSON 配置(不导出密钥) |
105
+ | `x-search-mcp search "<query>"` | 直接在命令行测试搜索并输出结构化 JSON |
106
+ | `x-search-mcp logs` | 列出最近的本地调用日志路径 |
107
+
108
+ ---
109
+
110
+ ## 环境变量参考
111
+
112
+ | 变量名 | 默认值 | 说明 |
113
+ |---|---|---|
114
+ | `XAI_API_KEY` | - | xAI API Key(优先级高于文件) |
115
+ | `XAI_API_KEY_FILE` | - | 存放 API Key 的私有文件路径 |
116
+ | `X_SEARCH_AUTH_MODE` | `auto` | 认证模式:`auto`、`api_key`、`oauth` |
117
+ | `X_SEARCH_AUTH_FILE` | `~/.config/x-search-mcp/auth.json` | 本地 OAuth 凭证文件路径 |
118
+ | `XAI_OAUTH_CLIENT_ID` | `b1a00492-073a-47ea-816f-4c329264a828` | OAuth 公开客户端 ID |
119
+ | `X_SEARCH_MODEL` | `grok-4.5` | 搜索调用模型 |
120
+ | `X_SEARCH_TIMEOUT_SECONDS` | `180` | 请求超时时间(秒,范围 5–600) |
121
+ | `X_SEARCH_RETRIES` | `2` | 失败重试次数(网络/5xx,范围 0–3) |
122
+ | `X_SEARCH_MAX_TURNS` | `4` | 内部搜索轮数(范围 1–20) |
123
+ | `X_SEARCH_REASONING_EFFORT`| - | 思考深度:`low` / `medium` / `high` / `xhigh` |
124
+ | `X_SEARCH_ENV_FILE` | - | 显式指定 `.env` 文件(通过 `process.loadEnvFile` 加载) |
125
+ | `X_SEARCH_LOG` | `on` | 是否保存调用日志:`on` / `off` |
126
+ | `X_SEARCH_LOG_DIR` | `~/.config/x-search-mcp/logs` | 调用日志目录 |
127
+ | `X_SEARCH_LOG_KEEP` | `100` | 保留最近多少份日志;`0` 表示不清理 |
128
+ | `X_SEARCH_LOG_MAX_BYTES` | `8388608` | 单次原始响应写入上限;超出后截断并标记 |
129
+ | `HTTP_PROXY` / `HTTPS_PROXY` | - | 标准 HTTP/HTTPS 代理配置 |
130
+
131
+ ### HTTP 服务配置
132
+
133
+ | 变量名 | 默认值 | 说明 |
134
+ |---|---|---|
135
+ | `X_SEARCH_HTTP_TOKEN` | - | HTTP MCP 的独立 Bearer 密钥;不输出到日志 |
136
+ | `X_SEARCH_HTTP_TOKEN_FILE` | - | 仅包含 HTTP 密钥的私有文件,启动时读取 |
137
+ | `X_SEARCH_HTTP_HOST` | `127.0.0.1` | 监听地址;远端接入通常由本机 HTTPS 反代转发 |
138
+ | `X_SEARCH_HTTP_PORT` | `8000` | TCP 端口;`0` 自动分配 |
139
+ | `X_SEARCH_HTTP_PATH` | `/mcp` | 规范化 URL 路径,不含查询参数或片段 |
140
+ | `X_SEARCH_HTTP_ALLOWED_HOSTS` | - | 额外允许的 Host 主机名,逗号分隔,不含协议或端口 |
141
+ | `X_SEARCH_HTTP_ALLOWED_ORIGINS` | - | 明确允许的浏览器 Origin,逗号分隔,匹配协议、主机和端口 |
142
+
143
+ 同名 CLI 参数覆盖环境默认值。`--allowed-host` 与 `--allowed-origin` 可重复;提供这类参数时使用命令行列表。显式 `--token-file` 优先,否则非空 `X_SEARCH_HTTP_TOKEN` 优先于环境 token 文件。空密钥、非法路径、非法端口或不可读文件会阻止启动。
144
+
145
+ HTTP 请求体默认上限为 1 MiB。`SIGINT`/`SIGTERM` 停止服务并取消活动请求;上游超时和客户端取消持续覆盖响应体读取阶段。
146
+
147
+ 退出会取消尚在等待 OAuth 文件锁的请求。已经发出的刷新请求可能轮换 refresh token,因此服务会等待它完成原子保存,再完成退出;该阶段受独立的 30 秒刷新超时约束。取消后不会继续发起搜索。
148
+
149
+ ---
150
+
151
+ ## 调用日志
152
+
153
+ 每次 `x_search` 都会在本地写一份 JSON 日志,里面是发给 xAI 的请求体和上游原始响应。MCP 结果只返回 `log_file` 路径,不把原始响应塞回工具输出。
154
+
155
+ - 默认目录:`~/.config/x-search-mcp/logs/`
156
+ - 文件权限:`0600`,目录权限:`0700`
157
+ - 字段:`request`、`attempts[].raw_response`、顶层 `raw_response`(最后一次尝试)
158
+ - 凭证处理:不记录 `Authorization`;`access_token`、`api_key` 等字段以及 `Bearer` / `xai-` 形态的字符串会替换成 `[redacted]`
159
+ - 失败调用也会写日志。HTTP 错误正文只进本地文件,不回传给 MCP 客户端
160
+ - 查看最近日志:`x-search-mcp logs`
161
+
162
+ 日志写入失败不会让搜索失败;结果的 `warnings` 会说明日志没写上。
163
+
164
+ ## Agent 接入配置
165
+
166
+ ### 原生 HTTP
167
+
168
+ 源码构建后执行:
169
+
170
+ ```bash
171
+ export X_SEARCH_AUTH_MODE=oauth
172
+ export X_SEARCH_HTTP_TOKEN="$(openssl rand -hex 32)"
173
+ node bin/x-search-mcp.js http --port 8000
174
+ ```
175
+
176
+ 安装版 0.2.0 可直接使用 `x-search-mcp http`。客户端选择 **Streamable HTTP**,填写 `http://127.0.0.1:8000/mcp`,请求头设为 `Authorization: Bearer <X_SEARCH_HTTP_TOKEN 的值>`。未带正确密钥返回 401;不允许的 Host/Origin 返回 403。
177
+
178
+ 服务使用同一份本机 Grok 凭证和调用日志;MCP 无状态表示不保存连接会话,不会清除 OAuth 登录。新旧协议都由同一个工具工厂处理,不需要网关子进程。
179
+
180
+ 若经域名代理,配置 `--allowed-host mcp.example.com`,让客户端连接 `https://mcp.example.com/mcp`。浏览器跨站调用还需显式 `--allowed-origin https://app.example.com`;非浏览器客户端可以不发送 Origin。HTTP 密钥与 Grok 凭证、npm 发布 token 分开管理。
181
+
182
+ ### 1. 通用 mcpServers JSON(Claude Desktop / Cline / Cursor 等)
183
+
184
+ 在目标 Agent 的配置文件中添加:
185
+
186
+ ```json
187
+ {
188
+ "mcpServers": {
189
+ "x-search": {
190
+ "command": "npx",
191
+ "args": ["--yes", "@scoheart/x-search-mcp@0.2.0"],
192
+ "env": {
193
+ "X_SEARCH_AUTH_MODE": "auto",
194
+ "X_SEARCH_MODEL": "grok-4.5"
195
+ },
196
+ "timeout": 600
197
+ }
198
+ }
199
+ }
200
+ ```
201
+
202
+ > 若安装的是本地 tarball,可改用 `"command": "x-search-mcp"`、`"args": []`,并确保客户端的 `PATH` 包含安装目录。也可以运行 `x-search-mcp config` 生成当前可执行入口的绝对路径配置。修改配置后请重启或重新加载 Agent 会话。Pi 使用 `--no-mcp` 启动时不会连接 MCP 服务。
203
+
204
+ ### 2. Codex 配置 (`~/.codex/config.toml`)
205
+
206
+ ```toml
207
+ [mcp_servers.x-search]
208
+ command = "npx"
209
+ args = ["--yes", "@scoheart/x-search-mcp@0.2.0"]
210
+ tool_timeout_sec = 600
211
+
212
+ [mcp_servers.x-search.env]
213
+ X_SEARCH_AUTH_MODE = "auto"
214
+ X_SEARCH_MODEL = "grok-4.5"
215
+ ```
216
+
217
+ ---
218
+
219
+ ## 工具参数规范 (`x_search`)
220
+
221
+ ```typescript
222
+ interface XSearchInput {
223
+ query: string; // 必填,检索语句(trim 后非空,最大 16000 字符)
224
+ allowed_x_handles?: string[]; // 可选,白名单账号(最多 10 个,无需 @,与 excluded 互斥)
225
+ excluded_x_handles?: string[]; // 可选,黑名单账号(最多 10 个,无需 @,与 allowed 互斥)
226
+ from_date?: string; // 可选,开始日期 YYYY-MM-DD(不能晚于当天 UTC,需严格早于 to_date)
227
+ to_date?: string; // 可选,结束日期 YYYY-MM-DD(不包含当天)
228
+ enable_image_understanding?: boolean; // 可选,是否启用推文配图理解
229
+ enable_video_understanding?: boolean; // 可选,是否启用推文视频理解
230
+ auth_mode?: 'auto' | 'api_key' | 'oauth';// 可选,单次调用的认证覆盖模式
231
+ }
232
+ ```
233
+
234
+ ### 返回结构示例
235
+
236
+ ```json
237
+ {
238
+ "success": true,
239
+ "provider": "xai",
240
+ "tool": "x_search",
241
+ "model": "grok-4.5",
242
+ "query": "OpenAI Sora news",
243
+ "credential_source": "oauth",
244
+ "billing_mode": "subscription_oauth",
245
+ "answer": "最新动态包括...",
246
+ "citations": [
247
+ "https://x.com/OpenAI/status/123456789"
248
+ ],
249
+ "inline_citations": [
250
+ {
251
+ "url": "https://x.com/OpenAI/status/123456789",
252
+ "title": "OpenAI on X",
253
+ "start_index": 12,
254
+ "end_index": 35
255
+ }
256
+ ],
257
+ "has_citations": true,
258
+ "degraded": false,
259
+ "degraded_reason": null,
260
+ "search_execution": "observed",
261
+ "server_side_calls": [
262
+ {
263
+ "call_id": "xs_123456",
264
+ "name": "x_keyword_search",
265
+ "arguments": { "query": "OpenAI Sora" }
266
+ }
267
+ ],
268
+ "response_status": "completed",
269
+ "incomplete_details": null,
270
+ "usage": {
271
+ "prompt_tokens": 800,
272
+ "completion_tokens": 350
273
+ },
274
+ "log_file": "/Users/username/.config/x-search-mcp/logs/call-20260814T120000000Z-ab12cd.json",
275
+ "warnings": [
276
+ "OAuth subscription search availability and quota are determined upstream by xAI; successful login does not guarantee search availability."
277
+ ]
278
+ }
279
+ ```
@@ -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,64 @@
1
+ {
2
+ "name": "@scoheart/x-search-mcp",
3
+ "version": "0.2.0",
4
+ "description": "Independent MCP service for x_search, inspired by Hermes Agent",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/Scoheart/x-search-mcp.git"
8
+ },
9
+ "homepage": "https://github.com/Scoheart/x-search-mcp#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/Scoheart/x-search-mcp/issues"
12
+ },
13
+ "keywords": [
14
+ "mcp",
15
+ "x-search",
16
+ "xai",
17
+ "grok",
18
+ "oauth"
19
+ ],
20
+ "type": "module",
21
+ "bin": {
22
+ "x-search-mcp": "./bin/x-search-mcp.js"
23
+ },
24
+ "main": "./dist/src/index.js",
25
+ "types": "./dist/src/index.d.ts",
26
+ "files": [
27
+ "bin",
28
+ "src",
29
+ "dist",
30
+ "TOOL.md",
31
+ "LICENSE",
32
+ "THIRD_PARTY_NOTICES.md",
33
+ "examples",
34
+ ".env.example",
35
+ "README.md",
36
+ "tsconfig.json"
37
+ ],
38
+ "engines": {
39
+ "node": ">=22.19.0"
40
+ },
41
+ "scripts": {
42
+ "build": "tsc",
43
+ "test": "npm run build && node --test test/*.test.mjs",
44
+ "prepack": "npm run build",
45
+ "start": "node dist/bin/x-search-mcp.js"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "dependencies": {
51
+ "@modelcontextprotocol/node": "2.1.2",
52
+ "@modelcontextprotocol/server": "2.4.0",
53
+ "proper-lockfile": "4.1.2",
54
+ "undici": "8.12.4",
55
+ "zod": "4.6.7"
56
+ },
57
+ "devDependencies": {
58
+ "@modelcontextprotocol/client": "2.4.0",
59
+ "@types/node": "^22.13.0",
60
+ "@types/proper-lockfile": "^4.1.4",
61
+ "typescript": "^5.8.0"
62
+ },
63
+ "license": "MIT"
64
+ }