@nite07/websearch-mcp 0.2.4 → 0.2.6
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.
- package/README.md +56 -84
- package/README.zh.md +50 -73
- package/config.example.json +2 -1
- package/dist/images.d.ts +17 -2
- package/dist/images.js +37 -8
- package/dist/live.d.ts +5 -15
- package/dist/live.js +47 -43
- package/dist/manage.d.ts +16 -0
- package/dist/manage.js +164 -0
- package/dist/sdk.js +5 -7
- package/dist/tools.d.ts +8 -4
- package/dist/tools.js +75 -47
- package/dist/ui/page.html +3 -2
- package/dist/ui/state.d.ts +4 -0
- package/dist/ui/state.js +78 -53
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,21 +21,34 @@ run is the caller's decision: one id in `backends` runs one backend, several run
|
|
|
21
21
|
|
|
22
22
|
```sh
|
|
23
23
|
npm install -g @nite07/websearch-mcp
|
|
24
|
+
mkdir -p ~/.config/websearch-mcp
|
|
24
25
|
websearch-mcp --print-config > ~/.config/websearch-mcp/config.json
|
|
25
26
|
```
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
This creates the initial settings with Keenable and Reddit enabled; neither needs an account.
|
|
29
|
+
You do not need to edit the generated file. Use the settings page or ask your agent to make changes.
|
|
30
|
+
|
|
31
|
+
## Connect your MCP client
|
|
32
|
+
|
|
33
|
+
Add a **stdio** MCP server in your client's MCP settings:
|
|
34
|
+
|
|
35
|
+
| Field | Value |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| Name | `websearch` |
|
|
38
|
+
| Command | `websearch-mcp` |
|
|
39
|
+
| Arguments | None |
|
|
40
|
+
|
|
41
|
+
The command must be available to the client. If it cannot find it, use the full path to the installed
|
|
42
|
+
executable. Clients that only accept a configuration file have their own setup format; follow that
|
|
43
|
+
client's instructions for adding a stdio server.
|
|
29
44
|
|
|
30
45
|
## First search
|
|
31
46
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
`list_backends` first: it reports each backend's modes, cost tier, enabled state and whether a
|
|
38
|
-
credential is present. It never enables anything.
|
|
47
|
+
Ask your agent to search using the default backends, or ask it to list the available providers first.
|
|
48
|
+
For example: “Search for recent self-hosting news using Keenable and Reddit.”
|
|
49
|
+
|
|
50
|
+
To choose providers, check costs or change search preferences, use the settings page below or ask the agent to
|
|
51
|
+
manage the settings for you. There is no need to write configuration by hand.
|
|
39
52
|
|
|
40
53
|
## Search results
|
|
41
54
|
|
|
@@ -64,99 +77,58 @@ To run only the settings page:
|
|
|
64
77
|
websearch-mcp --ui --port 7799 --token replace-with-a-long-random-token
|
|
65
78
|
```
|
|
66
79
|
|
|
67
|
-
## Client configuration
|
|
68
|
-
|
|
69
|
-
```json
|
|
70
|
-
{
|
|
71
|
-
"mcpServers": {
|
|
72
|
-
"websearch": {
|
|
73
|
-
"command": "websearch-mcp",
|
|
74
|
-
"env": { "WEBSEARCH_MCP_CONFIG": "/home/you/.config/websearch-mcp/config.json" }
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
`--config <path>` works too when you would rather pass the file explicitly. Without either, the
|
|
81
|
-
server reads `~/.config/websearch-mcp/config.json`.
|
|
82
|
-
|
|
83
80
|
## Credentials
|
|
84
81
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
Put them where your client can see them:
|
|
90
|
-
|
|
91
|
-
```json
|
|
92
|
-
{
|
|
93
|
-
"mcpServers": {
|
|
94
|
-
"websearch": {
|
|
95
|
-
"command": "websearch-mcp",
|
|
96
|
-
"env": {
|
|
97
|
-
"WEBSEARCH_MCP_CONFIG": "/home/you/.config/websearch-mcp/config.json",
|
|
98
|
-
"DSH_SEARCH_BRAVE_KEY": "…"
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
If an environment value is absent, `credentials.<backend-id>` may hold the value itself inside the
|
|
106
|
-
config file. Values are literal strings: `${...}` is not expanded.
|
|
82
|
+
Enter a provider's API key or account credential on its card in the settings page and save.
|
|
83
|
+
You can also authorize the agent to set or remove a credential with `search_manage`.
|
|
84
|
+
Credential values are never returned by that tool. Do not paste secrets into a shared conversation.
|
|
107
85
|
|
|
108
86
|
## Browser-backed search
|
|
109
87
|
|
|
110
|
-
Xiaohongshu search and note reading need a browser container
|
|
111
|
-
it from [packages/core/image](../core/image/README.md), sign in over VNC, then point the server at it:
|
|
112
|
-
|
|
113
|
-
```json
|
|
114
|
-
{
|
|
115
|
-
"search": {
|
|
116
|
-
"browser": {
|
|
117
|
-
"endpoint": "ws://127.0.0.1:3000/ws",
|
|
118
|
-
"statePath": "/path/to/search-browser/data/state.json",
|
|
119
|
-
"timeoutMs": 60000,
|
|
120
|
-
"maxConcurrentReads": 2
|
|
121
|
-
},
|
|
122
|
-
"backends": {
|
|
123
|
-
"xiaohongshu": { "enabled": true, "mode": "browser" }
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
```
|
|
88
|
+
Xiaohongshu search and note reading need a browser container:
|
|
128
89
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
90
|
+
1. Start the [browser container](../core/image/README.md) and sign in to Xiaohongshu over VNC.
|
|
91
|
+
2. In the settings page's **Browser** section, enter the endpoint and endpoint token shown by the
|
|
92
|
+
container. If you use a saved session file, enter its path there too.
|
|
93
|
+
3. Enable Xiaohongshu in browser mode in **Backends** and save.
|
|
132
94
|
|
|
133
|
-
|
|
134
|
-
to `xiaohongshu_note`.
|
|
95
|
+
Then ask your agent to search Xiaohongshu or read a full share link copied from the app.
|
|
135
96
|
|
|
136
97
|
## Tools
|
|
137
98
|
|
|
138
99
|
| Tool | What it does |
|
|
139
100
|
| --- | --- |
|
|
140
101
|
| `web_search` | Search selected backends with `query`. Optional: `backends`, `allBackends`, `exclude`, `compact`, `maxResults`. One backend id searches only that backend; omitting the selection searches every enabled backend. |
|
|
141
|
-
| `
|
|
102
|
+
| `reddit_thread` | Read a Reddit post, comments and pictures. |
|
|
103
|
+
| `x_thread` | Read an X post, replies and pictures. Requires the X backend in session mode. |
|
|
142
104
|
| `xiaohongshu_note` | Read a note's text, author, engagement, tags and pictures, with optional comments. Requires the Xiaohongshu backend in browser mode and a configured browser. |
|
|
143
105
|
| `list_backends` | List available backends, access modes, costs, enabled state and credential readiness. |
|
|
106
|
+
| `search_manage` | Inspect or change settings and credentials, test a backend, or test page fetching. |
|
|
144
107
|
| `web_extract` | Read one page as Markdown or HTML. Disabled by default; enable it in **Page fetch**. |
|
|
145
108
|
|
|
146
109
|
`compact: true` omits snippets while keeping links, titles, dates and backend provenance.
|
|
147
110
|
|
|
148
|
-
|
|
111
|
+
The former `read_thread` tool has been replaced by `reddit_thread` and `x_thread`; update existing
|
|
112
|
+
calls to use the platform's tool without a `source` argument.
|
|
149
113
|
|
|
150
|
-
|
|
151
|
-
`search.tools.webExtract` to `true` in the configuration file.
|
|
114
|
+
## Agent settings management
|
|
152
115
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
116
|
+
Ask the agent to use `search_manage` to view settings, change backends or page-fetch options, and
|
|
117
|
+
set or remove credentials. It can also test a saved backend or page-fetch setup. Credential values
|
|
118
|
+
are never returned. Changes take effect without restarting the MCP server.
|
|
119
|
+
|
|
120
|
+
For example: “Show me the available free backends, enable Keenable, and save.”
|
|
121
|
+
Tests make real requests and may incur charges. Configuration changes, credential changes and tests
|
|
122
|
+
require your authorization.
|
|
123
|
+
|
|
124
|
+
## Page fetching
|
|
125
|
+
|
|
126
|
+
Enable **web_extract** in the settings page's **Page fetch** section, or ask the agent to enable it.
|
|
127
|
+
Then ask it to read a URL as Markdown, or return HTML when you need the page structure.
|
|
156
128
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
129
|
+
Browser support is available for JavaScript pages. You can choose a fetch provider on the same page;
|
|
130
|
+
check its supported formats and costs, and supply an API key if required. Public URLs are allowed by
|
|
131
|
+
default.
|
|
160
132
|
|
|
161
133
|
## Behaviour worth relying on
|
|
162
134
|
|
|
@@ -173,10 +145,10 @@ an API key if required. Page fetching allows public URLs by default.
|
|
|
173
145
|
|
|
174
146
|
| Symptom | Cause and fix |
|
|
175
147
|
| --- | --- |
|
|
176
|
-
| The client shows no tools | The server failed to start. Run `websearch-mcp
|
|
177
|
-
| Every search fails | No backend is enabled.
|
|
178
|
-
| A keyed backend reports a missing credential |
|
|
179
|
-
| Xiaohongshu reports no endpoint |
|
|
148
|
+
| The client shows no tools | The server failed to start. Run `websearch-mcp` in a terminal and read the startup error. |
|
|
149
|
+
| Every search fails | No backend is enabled. Enable one in the settings page or ask the agent to enable it, then retry. |
|
|
150
|
+
| A keyed backend reports a missing credential | Enter the credential on the provider card, or authorize the agent to set it. |
|
|
151
|
+
| Xiaohongshu reports no endpoint | Fill in the endpoint in the settings page’s Browser section. |
|
|
180
152
|
| Xiaohongshu reports not signed in | The container's session expired. Sign in again over VNC; the next read reloads the state file. |
|
|
181
153
|
|
|
182
154
|
## Development
|
package/README.zh.md
CHANGED
|
@@ -15,19 +15,32 @@
|
|
|
15
15
|
|
|
16
16
|
```sh
|
|
17
17
|
npm install -g @nite07/websearch-mcp
|
|
18
|
+
mkdir -p ~/.config/websearch-mcp
|
|
18
19
|
websearch-mcp --print-config > ~/.config/websearch-mcp/config.json
|
|
19
20
|
```
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
这会生成初始设置,默认启用无需账号的 Keenable 和 Reddit。无需编辑生成的文件,后续在 Web UI 中操作,或让 Agent 帮你修改即可。
|
|
23
|
+
|
|
24
|
+
## 接入 MCP 客户端
|
|
25
|
+
|
|
26
|
+
在客户端的 MCP 设置中添加一个 **stdio** 服务:
|
|
27
|
+
|
|
28
|
+
| 字段 | 填写内容 |
|
|
29
|
+
| --- | --- |
|
|
30
|
+
| 名称 | `websearch` |
|
|
31
|
+
| 命令 | `websearch-mcp` |
|
|
32
|
+
| 参数 | 留空 |
|
|
33
|
+
|
|
34
|
+
客户端需要能找到该命令;找不到时,填安装后的可执行文件完整路径。如果客户端只接受配置文件,
|
|
35
|
+
按该客户端的说明添加 stdio 服务即可。
|
|
22
36
|
|
|
23
37
|
## 第一次搜索
|
|
24
38
|
|
|
25
|
-
|
|
26
|
-
2. 在 `search.backends` 里把想用的那几家改成 `"enabled": true`。匿名档的 Keenable、Exa、You.com、AnySearch、Parallel、Reddit 不需要额外配置
|
|
27
|
-
3. 按「客户端配置」把服务注册到你的 MCP 客户端
|
|
28
|
-
4. 让客户端搜点东西
|
|
39
|
+
让 Agent 使用默认后端搜索,也可以先让它列出可用服务商。例如:
|
|
29
40
|
|
|
30
|
-
|
|
41
|
+
> 用 Keenable 和 Reddit 搜一下最近的自托管新闻。
|
|
42
|
+
|
|
43
|
+
想更换后端、查看费用或调整搜索偏好,可以使用下面的 Web UI,或直接让 Agent 操作,无需手写配置。
|
|
31
44
|
|
|
32
45
|
## 搜索结果
|
|
33
46
|
|
|
@@ -54,91 +67,55 @@ WEBSEARCH_MCP_UI_PORT=7799 WEBSEARCH_MCP_UI_TOKEN=replace-with-a-long-random-tok
|
|
|
54
67
|
websearch-mcp --ui --port 7799 --token replace-with-a-long-random-token
|
|
55
68
|
```
|
|
56
69
|
|
|
57
|
-
## 客户端配置
|
|
58
|
-
|
|
59
|
-
```json
|
|
60
|
-
{
|
|
61
|
-
"mcpServers": {
|
|
62
|
-
"websearch": {
|
|
63
|
-
"command": "websearch-mcp",
|
|
64
|
-
"env": { "WEBSEARCH_MCP_CONFIG": "/home/you/.config/websearch-mcp/config.json" }
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
想显式指定文件时也可以用 `--config <path>`。两者都没有时,服务读取 `~/.config/websearch-mcp/config.json`。
|
|
71
|
-
|
|
72
70
|
## 凭证
|
|
73
71
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
放到客户端能看到的地方:
|
|
77
|
-
|
|
78
|
-
```json
|
|
79
|
-
{
|
|
80
|
-
"mcpServers": {
|
|
81
|
-
"websearch": {
|
|
82
|
-
"command": "websearch-mcp",
|
|
83
|
-
"env": {
|
|
84
|
-
"WEBSEARCH_MCP_CONFIG": "/home/you/.config/websearch-mcp/config.json",
|
|
85
|
-
"DSH_SEARCH_BRAVE_KEY": "…"
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
环境变量没有时,可以用配置文件里的 `credentials.<后端-id>` 直接写值。值是字面字符串,`${...}` 不会被展开。
|
|
72
|
+
在 Web UI 的服务商卡片中填写 API Key 或账号凭证并保存,也可以授权 Agent 通过 `search_manage`
|
|
73
|
+
设置或清除凭证。该工具不会返回凭证值。不要把密钥粘贴到共享对话中。
|
|
93
74
|
|
|
94
75
|
## 需要浏览器的搜索
|
|
95
76
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
```json
|
|
99
|
-
{
|
|
100
|
-
"search": {
|
|
101
|
-
"browser": {
|
|
102
|
-
"endpoint": "ws://127.0.0.1:3000/ws",
|
|
103
|
-
"statePath": "/path/to/search-browser/data/state.json",
|
|
104
|
-
"timeoutMs": 60000,
|
|
105
|
-
"maxConcurrentReads": 2
|
|
106
|
-
},
|
|
107
|
-
"backends": {
|
|
108
|
-
"xiaohongshu": { "enabled": true, "mode": "browser" }
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
```
|
|
77
|
+
小红书搜索和笔记读取需要浏览器容器:
|
|
113
78
|
|
|
114
|
-
|
|
115
|
-
|
|
79
|
+
1. 按[浏览器容器说明](../core/image/README.zh.md)启动容器,在 VNC 中登录小红书。
|
|
80
|
+
2. 在 Web UI 的「浏览器」部分填写容器提供的端点和端点令牌;使用登录态文件时,也在这里填写路径。
|
|
81
|
+
3. 在「后端」中启用小红书的 browser 模式并保存。
|
|
116
82
|
|
|
117
|
-
|
|
83
|
+
随后就可以让 Agent 搜索小红书,或读取从 App 复制的完整分享链接。
|
|
118
84
|
|
|
119
85
|
## 工具
|
|
120
86
|
|
|
121
87
|
| 工具 | 做什么 |
|
|
122
88
|
| --- | --- |
|
|
123
89
|
| `web_search` | 搜索指定后端。必填 `query`,可选 `backends`、`allBackends`、`exclude`、`compact`、`maxResults`。只指定一个后端就只搜这一家,不指定则搜索所有已启用后端。 |
|
|
124
|
-
| `
|
|
90
|
+
| `reddit_thread` | 读取 Reddit 帖子、评论和配图。 |
|
|
91
|
+
| `x_thread` | 读取 X 帖子、回复和配图,需要 X 后端处于会话模式。 |
|
|
125
92
|
| `xiaohongshu_note` | 读取笔记正文、作者、互动数据、标签和配图,可选读取评论。需要启用小红书后端的 browser 模式并配置浏览器。 |
|
|
126
93
|
| `list_backends` | 查看后端、接入模式、费用、启用状态和凭证是否就绪。 |
|
|
94
|
+
| `search_manage` | 查看或修改设置与凭证,测试后端或页面抓取。 |
|
|
127
95
|
| `web_extract` | 读取单个网页,返回 Markdown 或 HTML。默认关闭,在「页面抓取」中启用。 |
|
|
128
96
|
|
|
129
97
|
`compact: true` 不返回摘要,保留链接、标题、日期和来源后端。
|
|
130
98
|
|
|
131
|
-
|
|
99
|
+
原 `read_thread` 已由 `reddit_thread` 和 `x_thread` 替代,已有调用请改用对应平台的工具,不再传入 `source`。
|
|
132
100
|
|
|
133
|
-
|
|
134
|
-
`search.tools.webExtract` 设为 `true`。
|
|
101
|
+
## 让 Agent 管理设置
|
|
135
102
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
103
|
+
可以让 Agent 用 `search_manage` 查看设置、调整后端或页面抓取选项、设置或清除凭证,
|
|
104
|
+
也可以测试已保存的后端与抓取配置。工具不会返回凭证值,修改后无需重启 MCP 服务。
|
|
105
|
+
|
|
106
|
+
例如:
|
|
107
|
+
|
|
108
|
+
> 帮我看看有哪些免费后端,启用 Keenable 并保存。
|
|
109
|
+
|
|
110
|
+
测试会发出真实请求,可能产生费用。修改配置、凭证或运行测试前,都需要你的授权。
|
|
111
|
+
|
|
112
|
+
## 页面抓取
|
|
113
|
+
|
|
114
|
+
在 Web UI 的「页面抓取」中启用 **web_extract**,或让 Agent 帮你启用。
|
|
115
|
+
随后让它读取指定网页,返回 Markdown;需要查看页面结构时,可以要求返回 HTML。
|
|
139
116
|
|
|
140
|
-
|
|
141
|
-
|
|
117
|
+
支持读取需要 JavaScript 的页面,也可以在同一页面选择抓取服务商。使用前请查看支持的格式和费用,
|
|
118
|
+
需要 API Key 的模式先填写凭证。默认只允许公网地址。
|
|
142
119
|
|
|
143
120
|
## 可以依赖的行为
|
|
144
121
|
|
|
@@ -152,10 +129,10 @@ websearch-mcp --ui --port 7799 --token replace-with-a-long-random-token
|
|
|
152
129
|
|
|
153
130
|
| 现象 | 原因与处理 |
|
|
154
131
|
| --- | --- |
|
|
155
|
-
| 客户端里看不到工具 |
|
|
156
|
-
| 每次搜索都失败 |
|
|
157
|
-
| 某个要 Key 的后端报缺凭证 |
|
|
158
|
-
| 小红书报没有端点 |
|
|
132
|
+
| 客户端里看不到工具 | 服务没起来。在终端运行 `websearch-mcp` 查看启动报错。 |
|
|
133
|
+
| 每次搜索都失败 | 没有启用任何后端。在 Web UI 中启用一家,或让 Agent 帮你启用,再重试。 |
|
|
134
|
+
| 某个要 Key 的后端报缺凭证 | 在服务商卡片中填写凭证,或授权 Agent 设置。 |
|
|
135
|
+
| 小红书报没有端点 | 在 Web UI 的「浏览器」部分填写端点。 |
|
|
159
136
|
| 小红书报未登录 | 容器的登录态过期了。去 VNC 里重新登录,下次读取会重新加载会话文件。 |
|
|
160
137
|
|
|
161
138
|
## 开发
|
package/config.example.json
CHANGED
package/dist/images.d.ts
CHANGED
|
@@ -4,16 +4,31 @@
|
|
|
4
4
|
* A path on this server's disk is useless to a client on another machine, and an image URL the model
|
|
5
5
|
* cannot open is not much better, so the bytes are fetched here and returned as MCP image content.
|
|
6
6
|
* That makes the payload the thing to bound: a few pictures per call, a ceiling per file and in
|
|
7
|
-
* total, and the rest reported by
|
|
7
|
+
* total, and the rest reported by number only.
|
|
8
|
+
*
|
|
9
|
+
* Image links may be signed and time-limited, so no entry carries one. A picture is named by its
|
|
10
|
+
* position in the caller's list, which the reader can match to the note it came from, and by a short
|
|
11
|
+
* digest of its link, which tells two pictures apart without revealing either.
|
|
8
12
|
*/
|
|
9
13
|
export interface InlineImage {
|
|
10
|
-
|
|
14
|
+
/** Position among the picture links, counting from 1. */
|
|
15
|
+
index: number;
|
|
16
|
+
/** First 8 hex characters of the sha256 of the link: stable and discriminating, but not the link. */
|
|
17
|
+
digest: string;
|
|
11
18
|
mimeType: string;
|
|
12
19
|
/** Base64 payload, present only when the picture was actually fetched. */
|
|
13
20
|
data?: string;
|
|
14
21
|
bytes?: number;
|
|
15
22
|
error?: string;
|
|
16
23
|
}
|
|
24
|
+
/** A short, stable label for one picture link that can be printed without printing the link. */
|
|
25
|
+
export declare function imageDigest(url: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* The text form of the picture list. Every picture is named by number and digest and the ones that
|
|
28
|
+
* arrived say so; the link is never printed, because it may carry a signature. A client that cannot
|
|
29
|
+
* read image content still learns which pictures exist and why some are missing.
|
|
30
|
+
*/
|
|
31
|
+
export declare function renderImageList(text: string, images: readonly InlineImage[]): string;
|
|
17
32
|
/** Fetch pictures for one response, bounded by what a client is willing to carry. */
|
|
18
33
|
export declare function fetchImages(urls: readonly string[], options?: {
|
|
19
34
|
limit?: number;
|
package/dist/images.js
CHANGED
|
@@ -1,16 +1,45 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
const EXTENSIONS = {
|
|
2
3
|
'image/jpeg': 'jpeg', 'image/jpg': 'jpeg', 'image/png': 'png', 'image/webp': 'webp',
|
|
3
4
|
'image/gif': 'gif', 'image/avif': 'avif',
|
|
4
5
|
};
|
|
6
|
+
/** A short, stable label for one picture link that can be printed without printing the link. */
|
|
7
|
+
export function imageDigest(url) {
|
|
8
|
+
return createHash('sha256').update(url).digest('hex').slice(0, 8);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The text form of the picture list. Every picture is named by number and digest and the ones that
|
|
12
|
+
* arrived say so; the link is never printed, because it may carry a signature. A client that cannot
|
|
13
|
+
* read image content still learns which pictures exist and why some are missing.
|
|
14
|
+
*/
|
|
15
|
+
export function renderImageList(text, images) {
|
|
16
|
+
if (!images.length)
|
|
17
|
+
return text;
|
|
18
|
+
const lines = [text, '', `## Images (${images.length})`];
|
|
19
|
+
for (const image of images) {
|
|
20
|
+
const label = `image ${image.index} (${image.digest})`;
|
|
21
|
+
lines.push(image.data ? `- ${label} attached (${image.mimeType}, ${image.bytes ?? 0} bytes)` : `- ${label} not attached (${image.error ?? 'unknown reason'})`);
|
|
22
|
+
}
|
|
23
|
+
return lines.join('\n');
|
|
24
|
+
}
|
|
5
25
|
/** Fetch pictures for one response, bounded by what a client is willing to carry. */
|
|
6
26
|
export async function fetchImages(urls, options = {}) {
|
|
7
27
|
const limit = Math.min(Math.max(Math.trunc(options.limit ?? 6), 0), 12);
|
|
8
28
|
const maxBytes = options.maxBytes ?? 8 * 1024 * 1024;
|
|
9
29
|
const totalBytes = options.totalBytes ?? 24 * 1024 * 1024;
|
|
10
30
|
const fetchImpl = options.fetch ?? fetch;
|
|
11
|
-
|
|
31
|
+
// The number a picture is reported by is its position in the caller's list, so it stays stable
|
|
32
|
+
// across deduplication and the limit: two pictures are never both called "image 1".
|
|
33
|
+
const numbered = new Map();
|
|
34
|
+
for (const url of urls) {
|
|
35
|
+
if (/^https?:\/\//.test(url) && !numbered.has(url))
|
|
36
|
+
numbered.set(url, numbered.size + 1);
|
|
37
|
+
}
|
|
38
|
+
const unique = [...numbered.keys()].slice(0, limit);
|
|
12
39
|
let spent = 0;
|
|
13
|
-
return Promise.all(unique.map(async (url) => {
|
|
40
|
+
return Promise.all(unique.map(async (url, position) => {
|
|
41
|
+
const index = numbered.get(url) ?? position + 1;
|
|
42
|
+
const digest = imageDigest(url);
|
|
14
43
|
try {
|
|
15
44
|
const response = await fetchImpl(url, {
|
|
16
45
|
redirect: 'follow',
|
|
@@ -18,21 +47,21 @@ export async function fetchImages(urls, options = {}) {
|
|
|
18
47
|
headers: { accept: 'image/avif,image/webp,image/png,image/jpeg,image/gif;q=0.9,*/*;q=0.5' },
|
|
19
48
|
});
|
|
20
49
|
if (!response.ok)
|
|
21
|
-
return {
|
|
50
|
+
return { index, digest, mimeType: '', error: `HTTP ${response.status}` };
|
|
22
51
|
const contentType = (response.headers.get('content-type') ?? '').split(';')[0].trim().toLowerCase();
|
|
23
52
|
const mimeType = EXTENSIONS[contentType];
|
|
24
53
|
if (!mimeType)
|
|
25
|
-
return {
|
|
54
|
+
return { index, digest, mimeType: '', error: `not an image (${contentType || 'unknown type'})` };
|
|
26
55
|
const buffer = Buffer.from(await response.arrayBuffer());
|
|
27
56
|
if (buffer.byteLength > maxBytes)
|
|
28
|
-
return {
|
|
57
|
+
return { index, digest, mimeType, error: `${buffer.byteLength} bytes exceeds the ${maxBytes} byte limit` };
|
|
29
58
|
if (spent + buffer.byteLength > totalBytes)
|
|
30
|
-
return {
|
|
59
|
+
return { index, digest, mimeType, error: `not fetched: the ${totalBytes} byte budget for one call is used up` };
|
|
31
60
|
spent += buffer.byteLength;
|
|
32
|
-
return {
|
|
61
|
+
return { index, digest, mimeType, bytes: buffer.byteLength, data: buffer.toString('base64') };
|
|
33
62
|
}
|
|
34
63
|
catch (error) {
|
|
35
|
-
return {
|
|
64
|
+
return { index, digest, mimeType: '', error: error instanceof Error ? error.message : String(error) };
|
|
36
65
|
}
|
|
37
66
|
}));
|
|
38
67
|
}
|
package/dist/live.d.ts
CHANGED
|
@@ -1,26 +1,16 @@
|
|
|
1
1
|
import { type ServerConfig } from './config.js';
|
|
2
|
-
/**
|
|
3
|
-
* Tools read the configuration through this handle on every call instead of closing over a snapshot,
|
|
4
|
-
* so an edit that lands on disk reaches a server that is already running.
|
|
5
|
-
*/
|
|
2
|
+
/** Tools read through this handle on every call, including immediately after a management write. */
|
|
6
3
|
export interface ConfigSource {
|
|
7
4
|
get(): ServerConfig;
|
|
8
|
-
/** Changes whenever the configuration behind get() changed; the search engine rebuilds on a new value. */
|
|
9
5
|
revision(): string;
|
|
10
|
-
/**
|
|
11
|
-
* Fires after a successful reload. The MCP tool surface uses it to advertise or hide a gated tool
|
|
12
|
-
* without a restart; a fixed source has no changes to report.
|
|
13
|
-
*/
|
|
14
6
|
subscribe?(listener: () => void): () => void;
|
|
7
|
+
/** Reload the original path; failures propagate and leave the last good configuration intact. */
|
|
8
|
+
reload?(): Promise<void>;
|
|
15
9
|
}
|
|
16
|
-
export declare
|
|
10
|
+
export declare function fixedSource(config: ServerConfig): ConfigSource;
|
|
17
11
|
export declare function asSource(value: ServerConfig | ConfigSource): ConfigSource;
|
|
18
12
|
export interface ReloadableSource extends ConfigSource {
|
|
19
13
|
close(): void;
|
|
20
14
|
}
|
|
21
|
-
/**
|
|
22
|
-
* Watches the directory rather than the file: a settings page replaces the file by rename, which
|
|
23
|
-
* detaches a watcher bound to the inode. A failed reload keeps the previous configuration alive and
|
|
24
|
-
* says why, because a half-written file must never take the running server down.
|
|
25
|
-
*/
|
|
15
|
+
/** Watch the directory: atomic rename replaces the file inode. */
|
|
26
16
|
export declare function watchSource(initial: ServerConfig, log: (message: string) => void): ReloadableSource;
|
package/dist/live.js
CHANGED
|
@@ -1,65 +1,69 @@
|
|
|
1
1
|
import { watch } from 'node:fs';
|
|
2
2
|
import { basename, dirname } from 'node:path';
|
|
3
3
|
import { loadConfig } from './config.js';
|
|
4
|
-
export
|
|
4
|
+
export function fixedSource(config) {
|
|
5
|
+
const configPath = config.configPath;
|
|
6
|
+
let current = config;
|
|
7
|
+
let revision = 0;
|
|
8
|
+
let pending = Promise.resolve();
|
|
9
|
+
const listeners = new Set();
|
|
10
|
+
return {
|
|
11
|
+
get: () => current,
|
|
12
|
+
revision: () => revision ? `r${revision}` : 'fixed',
|
|
13
|
+
subscribe: listener => {
|
|
14
|
+
listeners.add(listener);
|
|
15
|
+
return () => { listeners.delete(listener); };
|
|
16
|
+
},
|
|
17
|
+
reload: () => {
|
|
18
|
+
// Serialize loads so an older disk read cannot replace a newer completed reload.
|
|
19
|
+
const next = pending.catch(() => undefined).then(async () => {
|
|
20
|
+
const loaded = await loadConfig({ configPath });
|
|
21
|
+
current = loaded;
|
|
22
|
+
revision += 1;
|
|
23
|
+
for (const listener of listeners) {
|
|
24
|
+
try {
|
|
25
|
+
listener();
|
|
26
|
+
}
|
|
27
|
+
catch { /* Listener failure does not invalidate a successful load. */ }
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
pending = next;
|
|
31
|
+
return next;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
5
35
|
export function asSource(value) {
|
|
6
36
|
return typeof value.get === 'function' ? value : fixedSource(value);
|
|
7
37
|
}
|
|
8
|
-
/**
|
|
9
|
-
* Watches the directory rather than the file: a settings page replaces the file by rename, which
|
|
10
|
-
* detaches a watcher bound to the inode. A failed reload keeps the previous configuration alive and
|
|
11
|
-
* says why, because a half-written file must never take the running server down.
|
|
12
|
-
*/
|
|
38
|
+
/** Watch the directory: atomic rename replaces the file inode. */
|
|
13
39
|
export function watchSource(initial, log) {
|
|
14
|
-
|
|
15
|
-
let revision = 0;
|
|
16
|
-
const listeners = new Set();
|
|
40
|
+
const source = fixedSource(initial);
|
|
17
41
|
let timer;
|
|
18
42
|
let closed = false;
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const watcher = watch(directory, { persistent: false }, (_event, filename) => {
|
|
22
|
-
if (filename && basename(String(filename)) !== name)
|
|
43
|
+
const watcher = watch(dirname(initial.configPath), { persistent: false }, (_event, filename) => {
|
|
44
|
+
if (filename && basename(String(filename)) !== basename(initial.configPath))
|
|
23
45
|
return;
|
|
24
46
|
if (timer)
|
|
25
47
|
clearTimeout(timer);
|
|
26
|
-
timer = setTimeout(() =>
|
|
48
|
+
timer = setTimeout(() => {
|
|
49
|
+
if (closed)
|
|
50
|
+
return;
|
|
51
|
+
void source.reload().then(() => {
|
|
52
|
+
log(`configuration reloaded from ${initial.configPath} (revision ${source.revision()})`);
|
|
53
|
+
}).catch(() => {
|
|
54
|
+
// Parse errors can quote file contents, including credentials. Never log their raw text.
|
|
55
|
+
log(`configuration at ${initial.configPath} is not usable; keeping the previous one`);
|
|
56
|
+
});
|
|
57
|
+
}, 150);
|
|
27
58
|
timer.unref?.();
|
|
28
59
|
});
|
|
29
|
-
watcher.on('error',
|
|
30
|
-
async function reload() {
|
|
31
|
-
if (closed)
|
|
32
|
-
return;
|
|
33
|
-
try {
|
|
34
|
-
current = await loadConfig({ configPath: initial.configPath });
|
|
35
|
-
revision += 1;
|
|
36
|
-
log(`configuration reloaded from ${initial.configPath} (revision ${revision})`);
|
|
37
|
-
for (const listener of listeners) {
|
|
38
|
-
try {
|
|
39
|
-
listener();
|
|
40
|
-
}
|
|
41
|
-
catch {
|
|
42
|
-
// One broken listener must not stop the others or the reload itself.
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
const reason = error instanceof Error ? error.message.split('\n')[0] : String(error);
|
|
48
|
-
log(`configuration at ${initial.configPath} is not usable; keeping the previous one: ${reason}`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
60
|
+
watcher.on('error', () => log('configuration watcher stopped'));
|
|
51
61
|
return {
|
|
52
|
-
|
|
53
|
-
revision: () => `r${revision}`,
|
|
54
|
-
subscribe: listener => {
|
|
55
|
-
listeners.add(listener);
|
|
56
|
-
return () => listeners.delete(listener);
|
|
57
|
-
},
|
|
62
|
+
...source,
|
|
58
63
|
close: () => {
|
|
59
64
|
closed = true;
|
|
60
65
|
if (timer)
|
|
61
66
|
clearTimeout(timer);
|
|
62
|
-
listeners.clear();
|
|
63
67
|
watcher.close();
|
|
64
68
|
},
|
|
65
69
|
};
|
package/dist/manage.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SearchEngine } from '@nite07/search-core';
|
|
2
|
+
import type { PageFetchEngine } from '@nite07/search-core';
|
|
3
|
+
import { type ServerConfig } from './config.js';
|
|
4
|
+
import type { ConfigSource } from './live.js';
|
|
5
|
+
import type { ToolDefinition } from './tools.js';
|
|
6
|
+
/** Optional factories keep tests independent of network and real credentials. */
|
|
7
|
+
export interface ManageDependencies {
|
|
8
|
+
search?(config: ServerConfig): Pick<SearchEngine, 'search'>;
|
|
9
|
+
fetch?(config: ServerConfig): Pick<PageFetchEngine, 'fetchPage'>;
|
|
10
|
+
}
|
|
11
|
+
/** Exactly one saved, enabled backend, with a bounded result limit and safe diagnostics. */
|
|
12
|
+
export declare function testBackend(configPath: string, args: Record<string, unknown>, signal: AbortSignal, dependencies?: ManageDependencies): Promise<unknown>;
|
|
13
|
+
/** Fetch using saved routes and policy only. No endpoint, credential or route overrides are accepted. */
|
|
14
|
+
export declare function testFetch(configPath: string, args: Record<string, unknown>, signal: AbortSignal, dependencies?: ManageDependencies): Promise<unknown>;
|
|
15
|
+
/** Management is tied to the source's initial path, never a caller-selected file. */
|
|
16
|
+
export declare function createManageTool(source: ConfigSource, dependencies?: ManageDependencies): ToolDefinition;
|