@scoheart/x-search-mcp 0.2.0 → 0.4.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 (105) hide show
  1. package/README.md +59 -75
  2. package/TOOL.md +28 -17
  3. package/dist/package.json +8 -1
  4. package/dist/src/auth.d.ts +14 -5
  5. package/dist/src/auth.d.ts.map +1 -1
  6. package/dist/src/auth.js +180 -118
  7. package/dist/src/auth.js.map +1 -1
  8. package/dist/src/call-log.d.ts.map +1 -1
  9. package/dist/src/call-log.js +52 -33
  10. package/dist/src/call-log.js.map +1 -1
  11. package/dist/src/cli.d.ts.map +1 -1
  12. package/dist/src/cli.js +9 -4
  13. package/dist/src/cli.js.map +1 -1
  14. package/dist/src/config.d.ts +3 -1
  15. package/dist/src/config.d.ts.map +1 -1
  16. package/dist/src/config.js +4 -4
  17. package/dist/src/config.js.map +1 -1
  18. package/dist/src/device-login.d.ts +46 -0
  19. package/dist/src/device-login.d.ts.map +1 -0
  20. package/dist/src/device-login.js +72 -0
  21. package/dist/src/device-login.js.map +1 -0
  22. package/dist/src/http-config.d.ts +9 -0
  23. package/dist/src/http-config.d.ts.map +1 -1
  24. package/dist/src/http-config.js +10 -5
  25. package/dist/src/http-config.js.map +1 -1
  26. package/dist/src/http-server.d.ts +10 -2
  27. package/dist/src/http-server.d.ts.map +1 -1
  28. package/dist/src/http-server.js +46 -15
  29. package/dist/src/http-server.js.map +1 -1
  30. package/dist/src/index.d.ts +3 -0
  31. package/dist/src/index.d.ts.map +1 -1
  32. package/dist/src/index.js +3 -0
  33. package/dist/src/index.js.map +1 -1
  34. package/dist/src/multi-user/bindings.d.ts +21 -0
  35. package/dist/src/multi-user/bindings.d.ts.map +1 -0
  36. package/dist/src/multi-user/bindings.js +129 -0
  37. package/dist/src/multi-user/bindings.js.map +1 -0
  38. package/dist/src/multi-user/cli.d.ts +23 -0
  39. package/dist/src/multi-user/cli.d.ts.map +1 -0
  40. package/dist/src/multi-user/cli.js +82 -0
  41. package/dist/src/multi-user/cli.js.map +1 -0
  42. package/dist/src/multi-user/identity.d.ts +17 -0
  43. package/dist/src/multi-user/identity.d.ts.map +1 -0
  44. package/dist/src/multi-user/identity.js +78 -0
  45. package/dist/src/multi-user/identity.js.map +1 -0
  46. package/dist/src/multi-user/keys.d.ts +17 -0
  47. package/dist/src/multi-user/keys.d.ts.map +1 -0
  48. package/dist/src/multi-user/keys.js +54 -0
  49. package/dist/src/multi-user/keys.js.map +1 -0
  50. package/dist/src/multi-user/kms.d.ts +10 -0
  51. package/dist/src/multi-user/kms.d.ts.map +1 -0
  52. package/dist/src/multi-user/kms.js +83 -0
  53. package/dist/src/multi-user/kms.js.map +1 -0
  54. package/dist/src/multi-user/service.d.ts +13 -0
  55. package/dist/src/multi-user/service.d.ts.map +1 -0
  56. package/dist/src/multi-user/service.js +92 -0
  57. package/dist/src/multi-user/service.js.map +1 -0
  58. package/dist/src/multi-user/settings.d.ts +9 -0
  59. package/dist/src/multi-user/settings.d.ts.map +1 -0
  60. package/dist/src/multi-user/settings.js +323 -0
  61. package/dist/src/multi-user/settings.js.map +1 -0
  62. package/dist/src/search-input.d.ts +43 -0
  63. package/dist/src/search-input.d.ts.map +1 -0
  64. package/dist/src/search-input.js +105 -0
  65. package/dist/src/search-input.js.map +1 -0
  66. package/dist/src/search-response.d.ts +6 -0
  67. package/dist/src/search-response.d.ts.map +1 -0
  68. package/dist/src/search-response.js +125 -0
  69. package/dist/src/search-response.js.map +1 -0
  70. package/dist/src/search.d.ts +5 -20
  71. package/dist/src/search.d.ts.map +1 -1
  72. package/dist/src/search.js +67 -332
  73. package/dist/src/search.js.map +1 -1
  74. package/dist/src/server.d.ts +9 -1
  75. package/dist/src/server.d.ts.map +1 -1
  76. package/dist/src/server.js +88 -81
  77. package/dist/src/server.js.map +1 -1
  78. package/dist/src/tool-contract.d.ts +150 -0
  79. package/dist/src/tool-contract.d.ts.map +1 -0
  80. package/dist/src/tool-contract.js +83 -0
  81. package/dist/src/tool-contract.js.map +1 -0
  82. package/docs/multi-user.md +108 -0
  83. package/examples/codex.toml +1 -1
  84. package/examples/mcp.json +1 -1
  85. package/package.json +8 -1
  86. package/src/auth.ts +178 -125
  87. package/src/call-log.ts +52 -31
  88. package/src/cli.ts +9 -4
  89. package/src/config.ts +4 -4
  90. package/src/device-login.ts +84 -0
  91. package/src/http-config.ts +11 -5
  92. package/src/http-server.ts +55 -18
  93. package/src/index.ts +3 -0
  94. package/src/multi-user/bindings.ts +135 -0
  95. package/src/multi-user/cli.ts +78 -0
  96. package/src/multi-user/identity.ts +83 -0
  97. package/src/multi-user/keys.ts +66 -0
  98. package/src/multi-user/kms.ts +87 -0
  99. package/src/multi-user/service.ts +92 -0
  100. package/src/multi-user/settings.ts +329 -0
  101. package/src/search-input.ts +113 -0
  102. package/src/search-response.ts +127 -0
  103. package/src/search.ts +130 -426
  104. package/src/server.ts +97 -82
  105. package/src/tool-contract.ts +90 -0
package/README.md CHANGED
@@ -1,117 +1,101 @@
1
- # @scoheart/x-search-mcp
1
+ # x-search-mcp
2
2
 
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
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)
4
5
 
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
+ 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.
6
7
 
7
- ## Quick Start
8
+ [中文使用手册](./TOOL.md)
8
9
 
9
- ### Installation
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 |
10
15
 
11
- Requires Node.js **22.19.0 or later**.
16
+ ## Quick start
12
17
 
13
- Run the package directly with `npx`.
18
+ Requires Node.js **22.19.0 or later**.
14
19
 
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
20
+ ```sh
21
+ npm install --global @scoheart/x-search-mcp
18
22
  ```
19
23
 
20
- You can also install a locally built npm tarball.
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.
21
25
 
22
- ```bash
23
- npm install --global ./scoheart-x-search-mcp-0.2.0.tgz
24
- x-search-mcp auth status
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?"
25
29
  ```
26
30
 
27
- To build from source:
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.
28
32
 
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
33
+ ```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
36
37
  ```
37
38
 
38
- ### Authentication
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#凭证配置).
39
40
 
40
- Choose between direct API Key or Subscription OAuth:
41
+ ## Connect a local MCP client
41
42
 
42
- ```bash
43
- # 1. Using API Key
44
- export XAI_API_KEY="xai-your-api-key"
43
+ Run the command for your client after installing the package. The client launches `x-search-mcp` over stdio.
45
44
 
46
- # 2. Or using Grok Subscription via Device Code OAuth (source checkout)
47
- node bin/x-search-mcp.js auth login
45
+ ```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
48
49
  ```
49
50
 
50
- ### Run Server
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`.
51
52
 
52
- ```bash
53
- # stdio MCP server for agent clients (Claude Desktop, Cursor, Cline, etc.)
54
- node bin/x-search-mcp.js
55
- ```
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-设备授权)
56
54
 
57
- For an npm-installed package, use `x-search-mcp` in place of `node bin/x-search-mcp.js`.
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.
58
56
 
59
- ### Native Streamable HTTP (0.2.0)
57
+ ## Run over HTTP
60
58
 
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.
59
+ Both modes use the official MCP SDK with Streamable HTTP.
62
60
 
63
- Run from a built source checkout:
61
+ ### Single-account HTTP
64
62
 
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
- ```
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.
70
64
 
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>`.
65
+ ```sh
66
+ x-search-mcp http --token-file /absolute/path/to/mcp-token
67
+ ```
72
68
 
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`.
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-服务配置).
74
70
 
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.
71
+ ### Multi-user HTTP
76
72
 
77
- For an HTTPS reverse proxy, keep the listener on loopback and explicitly allow the proxy's public Host hostname:
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.
78
74
 
79
- ```bash
80
- x-search-mcp http --token-file /path/to/private/http-token \
81
- --allowed-host mcp.example.com
75
+ ```sh
76
+ x-search-mcp http --multi-user --port 8000
82
77
  ```
83
78
 
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.
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.
85
80
 
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.
81
+ ## Results and logs
87
82
 
88
- ### MCP Client Configuration
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.
89
86
 
90
- Add this server to Pi's `~/.pi/agent/mcp.json` or another client that accepts `mcpServers`.
87
+ ## Development
91
88
 
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
- }
89
+ ```sh
90
+ git clone https://github.com/Scoheart/x-search-mcp.git
91
+ cd x-search-mcp
92
+ npm ci
93
+ npm test
94
+ npm run verify:package
106
95
  ```
107
96
 
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.
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.
113
98
 
114
99
  ## License
115
100
 
116
- [MIT](./LICENSE) © Scoheart
117
- See [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md) for third-party notices (Hermes Agent).
101
+ [MIT](./LICENSE) © Scoheart. The implementation was inspired by Nous Research's Hermes Agent. See [third-party notices](./THIRD_PARTY_NOTICES.md).
package/TOOL.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # @scoheart/x-search-mcp 工具接入与使用说明
2
2
 
3
- 本工具是一个独立运行的 Node.js/TypeScript MCP 服务,提供只读工具 `x_search`,支持通过 xAI Responses API 对 X(原 Twitter)实时动态与历史发帖进行检索。本服务实现参考 Nous Research 的 Hermes Agent(commit `fcca1740533d59d7a46ab1a3a53d3a45ef3127ff`),但作为独立 MCP 包提供,无需安装或运行 Hermes。
3
+ 本工具是一个独立运行的 Node.js/TypeScript MCP 服务,提供 `x_search` 检索和本机 stdio 的 Grok 设备授权工具。检索使用 xAI Responses API,支持 X(原 Twitter)实时动态与历史发帖。本服务实现参考 Nous Research 的 Hermes Agent(commit `fcca1740533d59d7a46ab1a3a53d3a45ef3127ff`),作为独立 MCP 包运行。
4
4
 
5
5
  ---
6
6
 
7
7
  ## 核心特性
8
8
 
9
9
  - **标准 MCP 协议**:基于 `@modelcontextprotocol/server`,默认运行在 `stdio` 模式,输出标准 JSON-RPC。
10
- - **只读与安全性**:仅提供 `x_search` 检索工具,无发帖、点赞、关注或私信写操作;错误信息经由本地脱敏,不回显 upstream token/key。
10
+ - **只读搜索**。 `x_search` 检索公开帖子。stdio 模式还提供 `x_search_login` 和 `x_search_auth_status`,供 agent 发起授权并检查本机状态。
11
11
  - **双认证模式**:支持按量付费 API Key 与 Grok 订阅 OAuth(设备码登录);支持多进程并发共享令牌并带文件锁(`proper-lockfile`)。
12
12
  - **来源与降级感知**:提取结构化 citations 与正文 inline_citations。若上游无有效引用来源,自动标记 `degraded: true`。
13
13
  - **原生双传输**:默认 stdio;`http` / `--http` 使用官方 SDK 的无状态 Streamable HTTP,兼容 2026 与 2025 两代协议。
@@ -17,7 +17,7 @@
17
17
  ## 约束与边界说明(重要)
18
18
 
19
19
  1. **非官方项目**:本项目为独立开源 MCP 实现,并非 xAI 或 Nous Research / Hermes 官方 MCP。
20
- 2. **传输与访问范围**:默认 stdio 供本机子进程接入;原生 HTTP 默认监听 `127.0.0.1:8000/mcp`,要求独立 Bearer 密钥。公网访问通过 HTTPS 反向代理配置,Host/Origin 使用显式白名单。HTTP 接口支持配置请求头的客户端;此版本不提供 OAuth 自动登录/发现服务。
20
+ 2. **传输与访问范围**。默认 stdio 供本机子进程接入。单账号 HTTP 默认监听 `127.0.0.1:8000/mcp`,客户端使用独立 Bearer 密钥。多人 HTTP 通过身份服务进行 MCP OAuth 登录,每人绑定自己的 xAI API Key。公网接入通过 HTTPS 反向代理配置。 [多人部署说明](./docs/multi-user.md)
21
21
  3. **公共 OAuth Client ID 说明**:默认内置的公开客户端 ID 来自 Hermes Agent(`b1a00492-073a-47ea-816f-4c329264a828`)。在浏览器授权确认页面中可能会显示该客户端标识名称。如需使用专属客户端,可通过 `XAI_OAUTH_CLIENT_ID` 环境变量配置。
22
22
  4. **OAuth 成功不等于搜索可用**:OAuth 设备码登录成功仅代表获得 xAI 账号凭证,实际搜索能力及并发额度由 xAI 云端及订阅权益决定;登录成功不保证账号一定具备 X Search 权限。
23
23
  5. **无引用不可作为确证**:当结果标记为 `has_citations: false` / `degraded: true` 时,表示上游未返回任何经过验证的 URL 引用,该回答不可作为来源充分的 X 检索事实依据。
@@ -25,7 +25,7 @@
25
25
  - API Key 模式下按调用量付费(api_metered)。
26
26
  - 显式 `oauth` 模式下仅使用订阅令牌,**绝不回退**到付费 API Key,避免产生非预期账单。
27
27
  - `auto` 模式优先使用 API Key(如果配置),无 Key 时使用本地保存的 OAuth。
28
- 7. **重试消耗**:网络波动或 5xx 错误时会执行有限次数退避重试,重试请求可能重复消耗上游额度。
28
+ 7. **重试消耗**。stdio 和单账号 HTTP 遇到网络波动或 5xx 错误时可执行有限次数退避重试,可能重复消耗上游额度。多人 HTTP 每次搜索只发起一次上游请求。
29
29
  8. **过滤作用域限制**:xAI 接口的 `allowed_x_handles`、`excluded_x_handles`、`from_date` 和 `to_date` 过滤主要作用于关键词与语义搜索阶段,并不严格保证限制上游服务端内部的级联线程抓取(thread fetch)。
30
30
 
31
31
  ---
@@ -35,14 +35,14 @@
35
35
  需要 Node.js 22.19.0 或更高版本。可以用 `npx` 直接运行。
36
36
 
37
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
38
+ npx --yes @scoheart/x-search-mcp@0.4.0 auth login
39
+ npx --yes @scoheart/x-search-mcp@0.4.0 auth status
40
40
  ```
41
41
 
42
42
  也可以在源码仓库运行 `npm ci` 和 `npm pack` 生成安装包。`prepack` 会先编译当前源码,安装端无需 TypeScript 或开发依赖。
43
43
 
44
44
  ```bash
45
- npm install --global ./scoheart-x-search-mcp-0.2.0.tgz
45
+ npm install --global ./scoheart-x-search-mcp-0.4.0.tgz
46
46
  x-search-mcp auth status
47
47
  ```
48
48
 
@@ -79,6 +79,14 @@ Waiting for browser authorization...
79
79
  - 默认路径:`~/.config/x-search-mcp/auth.json`(权限 `0600`,目录权限 `0700`)
80
80
  - 自定义路径:可通过 `X_SEARCH_AUTH_FILE` 覆盖。
81
81
 
82
+ ### 在 agent 内完成 Grok 设备授权
83
+
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
+
82
90
  ### 凭证状态与注销
83
91
 
84
92
  ```bash
@@ -96,7 +104,8 @@ x-search-mcp auth logout
96
104
  | 命令 | 说明 |
97
105
  |---|---|
98
106
  | `x-search-mcp` 或 `x-search-mcp --stdio` | 默认启动 MCP stdio 服务(供 Agent 客户端调用) |
99
- | `x-search-mcp http [options]` 或 `x-search-mcp --http [options]` | 启动原生 Streamable HTTP;必需独立访问密钥 |
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) |
100
109
  | `x-search-mcp --help` / `x-search-mcp http --help` | 查看命令与 HTTP 参数 |
101
110
  | `x-search-mcp auth login` | 发起 OAuth 设备码登录并在终端交互 |
102
111
  | `x-search-mcp auth status` | 安全输出当前认证模式、Key/OAuth 就绪状态 |
@@ -118,11 +127,11 @@ x-search-mcp auth logout
118
127
  | `XAI_OAUTH_CLIENT_ID` | `b1a00492-073a-47ea-816f-4c329264a828` | OAuth 公开客户端 ID |
119
128
  | `X_SEARCH_MODEL` | `grok-4.5` | 搜索调用模型 |
120
129
  | `X_SEARCH_TIMEOUT_SECONDS` | `180` | 请求超时时间(秒,范围 5–600) |
121
- | `X_SEARCH_RETRIES` | `2` | 失败重试次数(网络/5xx,范围 0–3) |
130
+ | `X_SEARCH_RETRIES` | `2` | stdio 和单账号 HTTP 的失败重试次数(网络/5xx,范围 0–3) |
122
131
  | `X_SEARCH_MAX_TURNS` | `4` | 内部搜索轮数(范围 1–20) |
123
132
  | `X_SEARCH_REASONING_EFFORT`| - | 思考深度:`low` / `medium` / `high` / `xhigh` |
124
133
  | `X_SEARCH_ENV_FILE` | - | 显式指定 `.env` 文件(通过 `process.loadEnvFile` 加载) |
125
- | `X_SEARCH_LOG` | `on` | 是否保存调用日志:`on` / `off` |
134
+ | `X_SEARCH_LOG` | `on` | stdio 和单账号 HTTP 是否保存调用日志(`on` / `off`) |
126
135
  | `X_SEARCH_LOG_DIR` | `~/.config/x-search-mcp/logs` | 调用日志目录 |
127
136
  | `X_SEARCH_LOG_KEEP` | `100` | 保留最近多少份日志;`0` 表示不清理 |
128
137
  | `X_SEARCH_LOG_MAX_BYTES` | `8388608` | 单次原始响应写入上限;超出后截断并标记 |
@@ -150,7 +159,7 @@ HTTP 请求体默认上限为 1 MiB。`SIGINT`/`SIGTERM` 停止服务并取消
150
159
 
151
160
  ## 调用日志
152
161
 
153
- 每次 `x_search` 都会在本地写一份 JSON 日志,里面是发给 xAI 的请求体和上游原始响应。MCP 结果只返回 `log_file` 路径,不把原始响应塞回工具输出。
162
+ stdio 和单账号 HTTP 默认在本地记录发给 xAI 的请求体和上游原始响应,并在工具结果中提供 `log_file` 路径。多人 HTTP 关闭调用日志。
154
163
 
155
164
  - 默认目录:`~/.config/x-search-mcp/logs/`
156
165
  - 文件权限:`0600`,目录权限:`0700`
@@ -163,7 +172,7 @@ HTTP 请求体默认上限为 1 MiB。`SIGINT`/`SIGTERM` 停止服务并取消
163
172
 
164
173
  ## Agent 接入配置
165
174
 
166
- ### 原生 HTTP
175
+ ### 单账号 HTTP
167
176
 
168
177
  源码构建后执行:
169
178
 
@@ -173,9 +182,9 @@ export X_SEARCH_HTTP_TOKEN="$(openssl rand -hex 32)"
173
182
  node bin/x-search-mcp.js http --port 8000
174
183
  ```
175
184
 
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。
185
+ 安装后可直接使用 `x-search-mcp http`。客户端选择 **Streamable HTTP**,填写 `http://127.0.0.1:8000/mcp`,请求头设为 `Authorization: Bearer <X_SEARCH_HTTP_TOKEN 的值>`。未带正确密钥返回 401。来源 Host 或 Origin 未获允许时返回 403。
177
186
 
178
- 服务使用同一份本机 Grok 凭证和调用日志;MCP 无状态表示不保存连接会话,不会清除 OAuth 登录。新旧协议都由同一个工具工厂处理,不需要网关子进程。
187
+ 单账号 HTTP 使用同一份本机 Grok 凭证和调用日志。其搜索同时支持新旧 MCP 协议,设备授权可在服务端运行 CLI 进行。
179
188
 
180
189
  若经域名代理,配置 `--allowed-host mcp.example.com`,让客户端连接 `https://mcp.example.com/mcp`。浏览器跨站调用还需显式 `--allowed-origin https://app.example.com`;非浏览器客户端可以不发送 Origin。HTTP 密钥与 Grok 凭证、npm 发布 token 分开管理。
181
190
 
@@ -188,7 +197,7 @@ node bin/x-search-mcp.js http --port 8000
188
197
  "mcpServers": {
189
198
  "x-search": {
190
199
  "command": "npx",
191
- "args": ["--yes", "@scoheart/x-search-mcp@0.2.0"],
200
+ "args": ["--yes", "@scoheart/x-search-mcp@0.4.0"],
192
201
  "env": {
193
202
  "X_SEARCH_AUTH_MODE": "auto",
194
203
  "X_SEARCH_MODEL": "grok-4.5"
@@ -206,7 +215,7 @@ node bin/x-search-mcp.js http --port 8000
206
215
  ```toml
207
216
  [mcp_servers.x-search]
208
217
  command = "npx"
209
- args = ["--yes", "@scoheart/x-search-mcp@0.2.0"]
218
+ args = ["--yes", "@scoheart/x-search-mcp@0.4.0"]
210
219
  tool_timeout_sec = 600
211
220
 
212
221
  [mcp_servers.x-search.env]
@@ -227,12 +236,14 @@ interface XSearchInput {
227
236
  to_date?: string; // 可选,结束日期 YYYY-MM-DD(不包含当天)
228
237
  enable_image_understanding?: boolean; // 可选,是否启用推文配图理解
229
238
  enable_video_understanding?: boolean; // 可选,是否启用推文视频理解
230
- auth_mode?: 'auto' | 'api_key' | 'oauth';// 可选,单次调用的认证覆盖模式
239
+ auth_mode?: 'auto' | 'api_key' | 'oauth'; // 可选,仅本地 stdio 和单账号 HTTP
231
240
  }
232
241
  ```
233
242
 
234
243
  ### 返回结构示例
235
244
 
245
+ 客户端在 `tools/list` 能看到结果 schema。同一结果同时写入 `structuredContent` 和 JSON 文本。`search_execution: "observed"` 表示上游返回了 X Search 调用或 X 帖子/账号获取计数。判断回答的来源覆盖情况时检查 `citations` 和 `degraded`。当上游报告结果不完整时,`warnings` 会提示覆盖范围有限。内联引用的位置来自上游原始文本。
246
+
236
247
  ```json
237
248
  {
238
249
  "success": true,
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scoheart/x-search-mcp",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Independent MCP service for x_search, inspired by Hermes Agent",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,6 +31,7 @@
31
31
  "LICENSE",
32
32
  "THIRD_PARTY_NOTICES.md",
33
33
  "examples",
34
+ "docs/multi-user.md",
34
35
  ".env.example",
35
36
  "README.md",
36
37
  "tsconfig.json"
@@ -41,6 +42,7 @@
41
42
  "scripts": {
42
43
  "build": "tsc",
43
44
  "test": "npm run build && node --test test/*.test.mjs",
45
+ "verify:package": "node test/packaged-cli.check.mjs",
44
46
  "prepack": "npm run build",
45
47
  "start": "node dist/bin/x-search-mcp.js"
46
48
  },
@@ -48,8 +50,13 @@
48
50
  "access": "public"
49
51
  },
50
52
  "dependencies": {
53
+ "@alicloud/credentials": "2.4.7",
54
+ "@alicloud/kms20160120": "3.3.2",
55
+ "@alicloud/openapi-core": "1.0.9",
51
56
  "@modelcontextprotocol/node": "2.1.2",
52
57
  "@modelcontextprotocol/server": "2.4.0",
58
+ "jose": "6.2.17",
59
+ "openid-client": "6.8.9",
53
60
  "proper-lockfile": "4.1.2",
54
61
  "undici": "8.12.4",
55
62
  "zod": "4.6.7"
@@ -1,3 +1,4 @@
1
+ import { HttpError } from './http.js';
1
2
  import type { AuthRecord, AuthStatus, DiscoveryMetadata } from './types.js';
2
3
  /**
3
4
  * Validate that an OAuth URL is secure and points strictly to an authorized x.ai domain.
@@ -6,7 +7,7 @@ export declare function validateOAuthUrl(rawUrl: string): URL;
6
7
  /**
7
8
  * Fetch and validate OIDC discovery metadata from x.ai.
8
9
  */
9
- export declare function getDiscoveryMetadata(): Promise<DiscoveryMetadata>;
10
+ export declare function getDiscoveryMetadata(signal?: AbortSignal): Promise<DiscoveryMetadata>;
10
11
  /**
11
12
  * Extract expiry timestamp from JWT payload exp claim.
12
13
  * Note: Decoding the JWT payload is only used to extract the expiry timestamp;
@@ -15,10 +16,18 @@ export declare function getDiscoveryMetadata(): Promise<DiscoveryMetadata>;
15
16
  export declare function extractJwtExpiry(jwtToken: string | undefined): number | null;
16
17
  export declare function saveAuthDataAtomic(authFilePath: string, data: AuthRecord): void;
17
18
  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
- */
19
+ export declare function oauthRequired(reason: string, status?: number | null): HttpError;
20
+ /** Share the same device grant between CLI and stdio. The polling code stays in this closure. */
21
+ export declare function startDeviceFlow(signal?: AbortSignal): Promise<{
22
+ prompt: {
23
+ user_code: string;
24
+ expires_at: string;
25
+ verification_uri_complete?: string | undefined;
26
+ verification_uri: string;
27
+ };
28
+ waitForAuthorization: () => Promise<void>;
29
+ authFile: string;
30
+ }>;
22
31
  export declare function loginWithDeviceCode(): Promise<void>;
23
32
  /**
24
33
  * Get valid OAuth access token, refreshing preemptively if expiring within 120s.
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AA8C5E;;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;AAyDD;;;GAGG;AACH,wBAAsB,wBAAwB,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAqCpF;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,mBAAmB,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAwB1G;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,UAAU,CAoB1C;AAED;;;GAGG;AACH,wBAAsB,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAkB5C"}
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AAKA,OAAO,EAAmB,SAAS,EAAyB,MAAM,WAAW,CAAC;AAE9E,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AA+C5E;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CA2BpD;AAED;;GAEG;AACH,wBAAsB,oBAAoB,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAqB3F;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAgB5E;AAiBD,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI,CA2B/E;AAED,wBAAgB,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAWpE;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,GAAG,IAAW,GAAG,SAAS,CAErF;AAuDD,iGAAiG;AACjG,wBAAsB,eAAe,CAAC,MAAM,CAAC,EAAE,WAAW;;mBA2C/B,MAAM;;;;;gCASQ,OAAO,CAAC,IAAI,CAAC;;GA6DrD;AAED,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAczD;AAmDD;;;GAGG;AACH,wBAAsB,wBAAwB,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAqCpF;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,mBAAmB,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAwB1G;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,UAAU,CAkB1C;AAED;;;GAGG;AACH,wBAAsB,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAkB5C"}