@yyaylt/zhipu-web-search-mcp-server 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # zhipu-web-search-mcp-server
2
+
3
+ 基于智谱开放平台 [Web Search API](https://docs.bigmodel.cn/cn/guide/tools/web-search) 实现的 MCP 服务器,为 opencode 等 MCP 客户端提供网络搜索能力。
4
+
5
+ ## 功能特性
6
+
7
+ - 支持 4 种搜索引擎:智谱基础版(`search_std`)、智谱高阶版(`search_pro`)、搜狗(`search_pro_sogou`)、夸克搜索(`search_pro_quark`)
8
+ - 支持搜索意图识别(`search_intent`)
9
+ - 支持结果条数、域名白名单过滤、时间范围过滤、内容长度控制
10
+ - 返回 Markdown 文本 + 结构化 JSON 数据
11
+
12
+ ## 环境要求
13
+
14
+ - Node.js >= 18
15
+ - 智谱 API Key:从 [https://bigmodel.cn/usercenter/proj-mgmt/apikeys](https://bigmodel.cn/usercenter/proj-mgmt/apikeys) 获取
16
+
17
+ ## 安装与构建
18
+
19
+ ```bash
20
+ npm install
21
+ npm run build
22
+ ```
23
+
24
+ ## 配置
25
+
26
+ 通过环境变量 `ZHIPU_API_KEY` 提供 API Key:
27
+
28
+ ```bash
29
+ # PowerShell
30
+ $env:ZHIPU_API_KEY = "your_api_key_here"
31
+ ```
32
+
33
+ ## 在 opencode 中使用
34
+
35
+ 在 `opencode.json` 中添加 MCP 服务器配置:
36
+
37
+ ```json
38
+ {
39
+ "mcp": {
40
+ "zhipu-web-search": {
41
+ "type": "local",
42
+ "command": ["node", "D:\\path\\to\\zhipu-web-search-mcp-server\\dist\\index.js"],
43
+ "environment": {
44
+ "ZHIPU_API_KEY": "your_api_key_here"
45
+ }
46
+ }
47
+ }
48
+ }
49
+ ```
50
+
51
+ 或使用 `opencode mcp add` 命令添加:
52
+
53
+ ```bash
54
+ opencode mcp add zhipu-web-search -- node D:\path\to\zhipu-web-search-mcp-server\dist\index.js
55
+ ```
56
+
57
+ 然后设置环境变量后重启 opencode 即可。
58
+
59
+ ## 可用工具
60
+
61
+ ### zhipu_web_search
62
+
63
+ 执行网络搜索,返回网页标题、摘要、链接、网站名称等信息。
64
+
65
+ | 参数 | 类型 | 默认值 | 说明 |
66
+ |------|------|--------|------|
67
+ | search_query | string | 必填 | 搜索内容,不超过 70 字符 |
68
+ | search_engine | string | search_std | 搜索引擎:search_std / search_pro / search_pro_sogou / search_pro_quark |
69
+ | search_intent | boolean | false | 是否执行搜索意图识别 |
70
+ | count | number | 10 | 返回结果条数,1-50 |
71
+ | search_domain_filter | string | - | 白名单域名过滤(search_std / search_pro / search_pro_sogou 支持) |
72
+ | search_recency_filter | string | noLimit | 时间过滤:oneDay / oneWeek / oneMonth / oneYear / noLimit |
73
+ | content_size | string | medium | 内容长短:medium / high |
74
+ | user_id | string | - | 终端用户 ID,6-128 字符 |
75
+
76
+ ## 隐私与安全提示
77
+
78
+ - `ZHIPU_API_KEY` 仅通过环境变量注入,请勿将其写入代码、配置仓库或提交到 Git 历史。
79
+ - `user_id` 参数会原样透传给智谱开放平台,请仅传入**匿名化/不可逆**的终端用户标识,切勿传入邮箱、手机号、身份证号等真实个人身份信息。
80
+ - 搜索内容(`search_query`)会发送至智谱开放平台进行检索,请避免在搜索词中包含个人敏感信息。
81
+ - 服务器不会记录或存储搜索历史,所有请求均即时转发,不在本地落盘。
82
+
83
+ ## 错误码说明
84
+
85
+ | 错误码 | 含义 | 处理建议 |
86
+ |--------|------|----------|
87
+ | 1701 | 网络搜索并发已达上限 | 稍后重试或减少并发请求 |
88
+ | 1702 | 系统未找到可用的搜索引擎服务 | 检查配置或联系管理员 |
89
+ | 1703 | 搜索引擎未返回有效数据 | 调整查询条件 |
90
+
91
+ ## 开发
92
+
93
+ ```bash
94
+ npm run dev # tsx watch 模式开发
95
+ npm run build # 编译到 dist/
96
+ ```
97
+
98
+ ## 验证
99
+
100
+ 构建成功后启动服务器,若未设置 `ZHIPU_API_KEY` 会提示缺失:
101
+
102
+ ```bash
103
+ node dist/index.js
104
+ # ERROR: ZHIPU_API_KEY 环境变量是必需的
105
+ ```
@@ -0,0 +1,22 @@
1
+ export declare const API_BASE_URL = "https://open.bigmodel.cn/api/paas/v4/web_search";
2
+ export declare const CHARACTER_LIMIT = 25000;
3
+ export declare const SEARCH_ENGINES: {
4
+ readonly STORE: "search_std";
5
+ readonly PRO: "search_pro";
6
+ readonly PRO_SOGOU: "search_pro_sogou";
7
+ readonly PRO_QUARK: "search_pro_quark";
8
+ };
9
+ export declare const RECENCY_FILTERS: {
10
+ readonly ONE_DAY: "oneDay";
11
+ readonly ONE_WEEK: "oneWeek";
12
+ readonly ONE_MONTH: "oneMonth";
13
+ readonly ONE_YEAR: "oneYear";
14
+ readonly NO_LIMIT: "noLimit";
15
+ };
16
+ export declare const CONTENT_SIZES: {
17
+ readonly MEDIUM: "medium";
18
+ readonly HIGH: "high";
19
+ };
20
+ export declare const COUNT_BY_ENGINE: Record<string, number[]>;
21
+ export declare const REQUEST_TIMEOUT_MS = 30000;
22
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY,oDAAoD,CAAC;AAG9E,eAAO,MAAM,eAAe,QAAQ,CAAC;AAGrC,eAAO,MAAM,cAAc;;;;;CAKjB,CAAC;AAGX,eAAO,MAAM,eAAe;;;;;;CAMlB,CAAC;AAGX,eAAO,MAAM,aAAa;;;CAGhB,CAAC;AAGX,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAIpD,CAAC;AAGF,eAAO,MAAM,kBAAkB,QAAQ,CAAC"}
@@ -0,0 +1,34 @@
1
+ // 智谱 Web Search API 相关常量定义
2
+ // API 基础地址(智谱开放平台)
3
+ export const API_BASE_URL = "https://open.bigmodel.cn/api/paas/v4/web_search";
4
+ // 响应内容的最大字符数,防止单次返回内容过大
5
+ export const CHARACTER_LIMIT = 25000;
6
+ // 支持的搜索引擎编码
7
+ export const SEARCH_ENGINES = {
8
+ STORE: "search_std",
9
+ PRO: "search_pro",
10
+ PRO_SOGOU: "search_pro_sogou",
11
+ PRO_QUARK: "search_pro_quark"
12
+ };
13
+ // 时间范围过滤可选值
14
+ export const RECENCY_FILTERS = {
15
+ ONE_DAY: "oneDay",
16
+ ONE_WEEK: "oneWeek",
17
+ ONE_MONTH: "oneMonth",
18
+ ONE_YEAR: "oneYear",
19
+ NO_LIMIT: "noLimit"
20
+ };
21
+ // 内容长度选项
22
+ export const CONTENT_SIZES = {
23
+ MEDIUM: "medium",
24
+ HIGH: "high"
25
+ };
26
+ // 各搜索引擎支持的 count 枚举约束
27
+ export const COUNT_BY_ENGINE = {
28
+ [SEARCH_ENGINES.STORE]: [10, 20, 30, 40, 50],
29
+ [SEARCH_ENGINES.PRO]: [10, 20, 30, 40, 50],
30
+ [SEARCH_ENGINES.PRO_SOGOU]: [10, 20, 30, 40, 50]
31
+ };
32
+ // 请求超时时间(毫秒)
33
+ export const REQUEST_TIMEOUT_MS = 30000;
34
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAE3B,mBAAmB;AACnB,MAAM,CAAC,MAAM,YAAY,GAAG,iDAAiD,CAAC;AAE9E,wBAAwB;AACxB,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAC;AAErC,YAAY;AACZ,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,KAAK,EAAE,YAAY;IACnB,GAAG,EAAE,YAAY;IACjB,SAAS,EAAE,kBAAkB;IAC7B,SAAS,EAAE,kBAAkB;CACrB,CAAC;AAEX,YAAY;AACZ,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,OAAO,EAAE,QAAQ;IACjB,QAAQ,EAAE,SAAS;IACnB,SAAS,EAAE,UAAU;IACrB,QAAQ,EAAE,SAAS;IACnB,QAAQ,EAAE,SAAS;CACX,CAAC;AAEX,SAAS;AACT,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;CACJ,CAAC;AAEX,sBAAsB;AACtB,MAAM,CAAC,MAAM,eAAe,GAA6B;IACvD,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC;IAC5C,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC;IAC1C,CAAC,cAAc,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC;CACjD,CAAC;AAEF,aAAa;AACb,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,CAAC"}
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * 智谱 Web Search MCP Server
4
+ *
5
+ * 基于智谱开放平台 Web Search API(/paas/v4/web_search)实现的 MCP 服务器。
6
+ * 通过标准输入输出(stdio)与客户端通信,提供网络搜索能力。
7
+ *
8
+ * 环境变量:
9
+ * ZHIPU_API_KEY - 智谱 API Key(必填),从 https://bigmodel.cn/usercenter/proj-mgmt/apikeys 获取
10
+ */
11
+ export {};
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA;;;;;;;;GAQG"}
package/dist/index.js ADDED
@@ -0,0 +1,170 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * 智谱 Web Search MCP Server
4
+ *
5
+ * 基于智谱开放平台 Web Search API(/paas/v4/web_search)实现的 MCP 服务器。
6
+ * 通过标准输入输出(stdio)与客户端通信,提供网络搜索能力。
7
+ *
8
+ * 环境变量:
9
+ * ZHIPU_API_KEY - 智谱 API Key(必填),从 https://bigmodel.cn/usercenter/proj-mgmt/apikeys 获取
10
+ */
11
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
12
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
13
+ import { callWebSearch, handleApiError } from "./services/webSearchApi.js";
14
+ import { WebSearchInputSchema } from "./schemas/webSearch.js";
15
+ import { CHARACTER_LIMIT } from "./constants.js";
16
+ // 创建 MCP 服务器实例
17
+ const server = new McpServer({
18
+ name: "zhipu-web-search-mcp-server",
19
+ version: "1.0.0"
20
+ });
21
+ // 将搜索结果格式化为 Markdown 文本
22
+ function formatResultsMarkdown(output) {
23
+ const lines = [];
24
+ if (output.search_intent && output.search_intent.length > 0) {
25
+ const intent = output.search_intent[0];
26
+ lines.push(`## 搜索意图识别`);
27
+ lines.push(`- 原始查询:${intent.query}`);
28
+ lines.push(`- 识别意图:${intent.intent}`);
29
+ if (intent.keywords) {
30
+ lines.push(`- 改写关键词:${intent.keywords}`);
31
+ }
32
+ lines.push("");
33
+ }
34
+ if (output.results.length === 0) {
35
+ lines.push("未找到相关搜索结果。");
36
+ return lines.join("\n");
37
+ }
38
+ lines.push(`# 搜索结果 (${output.count})`);
39
+ lines.push("");
40
+ output.results.forEach((item, index) => {
41
+ lines.push(`## ${index + 1}. ${item.title}`);
42
+ if (item.publish_date) {
43
+ lines.push(`**发布时间:** ${item.publish_date}`);
44
+ }
45
+ if (item.media) {
46
+ lines.push(`**来源:** ${item.media}`);
47
+ }
48
+ if (item.link) {
49
+ lines.push(`**链接:** ${item.link}`);
50
+ }
51
+ lines.push("");
52
+ lines.push(item.content || "(无摘要)");
53
+ lines.push("");
54
+ });
55
+ return lines.join("\n");
56
+ }
57
+ // 注册网络搜索工具
58
+ server.registerTool("zhipu_web_search", {
59
+ title: "智谱网络搜索",
60
+ description: `通过智谱开放平台 Web Search API 执行网络搜索,返回网页标题、摘要、链接、网站名称等信息。
61
+
62
+ 支持四种搜索引擎:search_std(智谱基础版)、search_pro(智谱高阶版)、search_pro_sogou(搜狗)、search_pro_quark(夸克搜索)。
63
+
64
+ Args:
65
+ - search_query (string): 需要搜索的内容,建议不超过 70 个字符
66
+ - search_engine (string): 搜索引擎编码(默认 search_std)
67
+ - search_intent (boolean): 是否执行搜索意图识别(默认 false)
68
+ - count (number): 返回结果条数,1-50(默认 10)
69
+ - search_domain_filter (string): 白名单域名过滤,仅返回指定域名内容(可选)
70
+ - search_recency_filter (string): 时间范围过滤:oneDay/oneWeek/oneMonth/oneYear/noLimit(默认 noLimit)
71
+ - content_size (string): 返回内容长短:medium/high(默认 medium)
72
+ - user_id (string): 终端用户唯一 ID,长度 6-128 字符(可选)
73
+
74
+ Returns:
75
+ Markdown 文本:搜索意图识别结果 + 搜索结果列表(标题、发布时间、来源、链接、摘要)
76
+ 同时返回结构化 JSON 数据。
77
+
78
+ Examples:
79
+ - "帮我搜索今天的天气" -> search_query="今天北京天气"
80
+ - "搜索 GPT-5 相关新闻" -> search_query="GPT-5 新闻", search_recency_filter="oneWeek"
81
+ - "在夸克上搜索 Python 教程" -> search_query="Python 教程", search_engine="search_pro_quark"
82
+
83
+ Error Handling:
84
+ - 1701:网络搜索并发已达上限,稍后重试
85
+ - 1702:系统未找到可用的搜索引擎服务
86
+ - 1703:搜索引擎未返回有效数据,请调整查询条件`,
87
+ inputSchema: WebSearchInputSchema,
88
+ annotations: {
89
+ readOnlyHint: true,
90
+ destructiveHint: false,
91
+ idempotentHint: true,
92
+ openWorldHint: true
93
+ }
94
+ }, async (params) => {
95
+ const apiKey = process.env.ZHIPU_API_KEY;
96
+ if (!apiKey) {
97
+ return {
98
+ isError: true,
99
+ content: [
100
+ {
101
+ type: "text",
102
+ text: "缺少 ZHIPU_API_KEY 环境变量。请在运行前通过环境变量设置智谱 API Key,获取地址:https://bigmodel.cn/usercenter/proj-mgmt/apikeys"
103
+ }
104
+ ]
105
+ };
106
+ }
107
+ try {
108
+ const data = await callWebSearch(apiKey, {
109
+ search_query: params.search_query,
110
+ search_engine: params.search_engine,
111
+ search_intent: params.search_intent,
112
+ count: params.count,
113
+ ...(params.search_domain_filter
114
+ ? { search_domain_filter: params.search_domain_filter }
115
+ : {}),
116
+ search_recency_filter: params.search_recency_filter,
117
+ content_size: params.content_size,
118
+ ...(params.user_id ? { user_id: params.user_id } : {})
119
+ });
120
+ const results = data.search_result ?? [];
121
+ const output = {
122
+ id: data.id,
123
+ created: data.created,
124
+ request_id: data.request_id,
125
+ search_intent: data.search_intent ?? [],
126
+ count: results.length,
127
+ results
128
+ };
129
+ let text = formatResultsMarkdown(output);
130
+ // 超长截断,防止响应过大
131
+ if (text.length > CHARACTER_LIMIT) {
132
+ const keepItems = Math.max(1, Math.floor(results.length / 2));
133
+ const truncated = {
134
+ ...output,
135
+ results: results.slice(0, keepItems)
136
+ };
137
+ text = `${formatResultsMarkdown(truncated)}\n\n> 响应因过长被截断(原 ${results.length} 条),可减少 count 或增加关键词精度以查看更多结果。`;
138
+ }
139
+ return {
140
+ content: [{ type: "text", text }],
141
+ structuredContent: output
142
+ };
143
+ }
144
+ catch (error) {
145
+ return {
146
+ isError: true,
147
+ content: [
148
+ {
149
+ type: "text",
150
+ text: handleApiError(error)
151
+ }
152
+ ]
153
+ };
154
+ }
155
+ });
156
+ // 主函数:通过 stdio 启动服务器
157
+ async function main() {
158
+ if (!process.env.ZHIPU_API_KEY) {
159
+ console.error("ERROR: ZHIPU_API_KEY 环境变量是必需的");
160
+ process.exit(1);
161
+ }
162
+ const transport = new StdioServerTransport();
163
+ await server.connect(transport);
164
+ console.error("智谱 Web Search MCP server 正在运行 (stdio)");
165
+ }
166
+ main().catch((error) => {
167
+ console.error("服务器启动失败:", error);
168
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,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACrB,OAAO,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IACjC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { z } from "zod";
2
+ export declare const WebSearchInputSchema: z.ZodObject<{
3
+ search_query: z.ZodString;
4
+ search_engine: z.ZodDefault<z.ZodNativeEnum<{
5
+ readonly STORE: "search_std";
6
+ readonly PRO: "search_pro";
7
+ readonly PRO_SOGOU: "search_pro_sogou";
8
+ readonly PRO_QUARK: "search_pro_quark";
9
+ }>>;
10
+ search_intent: z.ZodDefault<z.ZodBoolean>;
11
+ count: z.ZodDefault<z.ZodNumber>;
12
+ search_domain_filter: z.ZodOptional<z.ZodString>;
13
+ search_recency_filter: z.ZodDefault<z.ZodNativeEnum<{
14
+ readonly ONE_DAY: "oneDay";
15
+ readonly ONE_WEEK: "oneWeek";
16
+ readonly ONE_MONTH: "oneMonth";
17
+ readonly ONE_YEAR: "oneYear";
18
+ readonly NO_LIMIT: "noLimit";
19
+ }>>;
20
+ content_size: z.ZodDefault<z.ZodNativeEnum<{
21
+ readonly MEDIUM: "medium";
22
+ readonly HIGH: "high";
23
+ }>>;
24
+ user_id: z.ZodOptional<z.ZodString>;
25
+ }, "strict", z.ZodTypeAny, {
26
+ search_query: string;
27
+ search_engine: "search_std" | "search_pro" | "search_pro_sogou" | "search_pro_quark";
28
+ search_intent: boolean;
29
+ count: number;
30
+ search_recency_filter: "oneDay" | "oneWeek" | "oneMonth" | "oneYear" | "noLimit";
31
+ content_size: "medium" | "high";
32
+ search_domain_filter?: string | undefined;
33
+ user_id?: string | undefined;
34
+ }, {
35
+ search_query: string;
36
+ search_engine?: "search_std" | "search_pro" | "search_pro_sogou" | "search_pro_quark" | undefined;
37
+ search_intent?: boolean | undefined;
38
+ count?: number | undefined;
39
+ search_domain_filter?: string | undefined;
40
+ search_recency_filter?: "oneDay" | "oneWeek" | "oneMonth" | "oneYear" | "noLimit" | undefined;
41
+ content_size?: "medium" | "high" | undefined;
42
+ user_id?: string | undefined;
43
+ }>;
44
+ export type WebSearchInput = z.infer<typeof WebSearchInputSchema>;
45
+ //# sourceMappingURL=webSearch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webSearch.d.ts","sourceRoot":"","sources":["../../src/schemas/webSearch.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAOxB,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4DtB,CAAC;AAEZ,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC"}
@@ -0,0 +1,46 @@
1
+ // Web Search 工具的 Zod 输入校验 schema
2
+ import { z } from "zod";
3
+ import { SEARCH_ENGINES, RECENCY_FILTERS, CONTENT_SIZES } from "../constants.js";
4
+ export const WebSearchInputSchema = z
5
+ .object({
6
+ search_query: z
7
+ .string()
8
+ .min(1, "搜索关键词不能为空")
9
+ .max(70, "搜索关键词不能超过 70 个字符")
10
+ .describe("需要搜索的内容,建议不超过 70 个字符"),
11
+ search_engine: z
12
+ .nativeEnum(SEARCH_ENGINES)
13
+ .default(SEARCH_ENGINES.STORE)
14
+ .describe("要调用的搜索引擎编码:search_std(智谱基础版)、search_pro(智谱高阶版)、search_pro_sogou(搜狗)、search_pro_quark(夸克搜索)"),
15
+ search_intent: z
16
+ .boolean()
17
+ .default(false)
18
+ .describe("是否执行搜索意图识别。true:先识别是否有搜索意图再搜索;false:跳过识别直接搜索"),
19
+ count: z
20
+ .number()
21
+ .int()
22
+ .min(1)
23
+ .max(50)
24
+ .default(10)
25
+ .describe("返回结果的条数,范围 1-50。注意:search_pro_sogou 仅支持枚举值 10/20/30/40/50,且同时指定 search_domain_filter 和 search_recency_filter 时 count 不生效"),
26
+ search_domain_filter: z
27
+ .string()
28
+ .optional()
29
+ .describe("限定搜索结果范围的白名单域名(如 www.example.com)。仅 search_std、search_pro、search_pro_sogou 支持"),
30
+ search_recency_filter: z
31
+ .nativeEnum(RECENCY_FILTERS)
32
+ .default(RECENCY_FILTERS.NO_LIMIT)
33
+ .describe("搜索指定时间范围内的网页。可选值:oneDay(一天内)、oneWeek(一周内)、oneMonth(一个月内)、oneYear(一年内)、noLimit(不限,默认)"),
34
+ content_size: z
35
+ .nativeEnum(CONTENT_SIZES)
36
+ .default(CONTENT_SIZES.MEDIUM)
37
+ .describe("控制返回网页内容长短:medium(摘要信息,适合常规问答)、high(最大化上下文,信息量更大,适合需要细节的场景)"),
38
+ user_id: z
39
+ .string()
40
+ .min(6)
41
+ .max(128)
42
+ .optional()
43
+ .describe("终端用户的唯一 ID,用于平台对非法活动的干预,长度 6-128 个字符")
44
+ })
45
+ .strict();
46
+ //# sourceMappingURL=webSearch.js.map