@lmcc-dev/mult-fetch-mcp-server 1.0.2 → 1.1.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.
- package/README.md +14 -2
- package/README.zh.md +3 -2
- package/dist/src/client.d.ts +6 -0
- package/dist/src/client.js +26 -28
- package/dist/src/client.js.map +1 -0
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib/BrowserFetcher.d.ts +175 -0
- package/dist/src/lib/BrowserFetcher.js +99 -65
- package/dist/src/lib/BrowserFetcher.js.map +1 -0
- package/dist/src/lib/NodeFetcher.d.ts +92 -0
- package/dist/src/lib/NodeFetcher.js +150 -86
- package/dist/src/lib/NodeFetcher.js.map +1 -0
- package/dist/src/lib/i18n/index.js.map +1 -0
- package/dist/src/lib/i18n/keys.js +135 -0
- package/dist/src/lib/i18n/locales/en/browser.d.ts +61 -0
- package/dist/src/lib/i18n/locales/en/browser.js +68 -45
- package/dist/src/lib/i18n/locales/en/browser.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/client.d.ts +39 -0
- package/dist/src/lib/i18n/locales/en/client.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/errors.d.ts +17 -0
- package/dist/src/lib/i18n/locales/en/errors.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/fetcher.d.ts +66 -0
- package/dist/src/lib/i18n/locales/en/fetcher.js +60 -59
- package/dist/src/lib/i18n/locales/en/fetcher.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/index.d.ts +344 -0
- package/dist/src/lib/i18n/locales/en/index.js +1 -1
- package/dist/src/lib/i18n/locales/en/index.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/node.d.ts +41 -0
- package/dist/src/lib/i18n/locales/en/node.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/prompts.d.ts +65 -0
- package/dist/src/lib/i18n/locales/en/prompts.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/resources.d.ts +41 -0
- package/dist/src/lib/i18n/locales/en/resources.js +30 -15
- package/dist/src/lib/i18n/locales/en/resources.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/server.d.ts +30 -0
- package/dist/src/lib/i18n/locales/en/server.js.map +1 -0
- package/dist/src/lib/i18n/locales/en/tools.d.ts +24 -0
- package/dist/src/lib/i18n/locales/en/tools.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/browser.d.ts +61 -0
- package/dist/src/lib/i18n/locales/zh/browser.js +68 -45
- package/dist/src/lib/i18n/locales/zh/browser.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/client.d.ts +38 -0
- package/dist/src/lib/i18n/locales/zh/client.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/errors.d.ts +17 -0
- package/dist/src/lib/i18n/locales/zh/errors.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/fetcher.d.ts +66 -0
- package/dist/src/lib/i18n/locales/zh/fetcher.js +60 -59
- package/dist/src/lib/i18n/locales/zh/fetcher.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/index.d.ts +343 -0
- package/dist/src/lib/i18n/locales/zh/index.js +1 -1
- package/dist/src/lib/i18n/locales/zh/index.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/node.d.ts +41 -0
- package/dist/src/lib/i18n/locales/zh/node.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/prompts.d.ts +65 -0
- package/dist/src/lib/i18n/locales/zh/prompts.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/resources.d.ts +41 -0
- package/dist/src/lib/i18n/locales/zh/resources.js +28 -13
- package/dist/src/lib/i18n/locales/zh/resources.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/server.d.ts +30 -0
- package/dist/src/lib/i18n/locales/zh/server.js.map +1 -0
- package/dist/src/lib/i18n/locales/zh/tools.d.ts +24 -0
- package/dist/src/lib/i18n/locales/zh/tools.js.map +1 -0
- package/dist/src/lib/i18n/logger.d.ts +38 -0
- package/dist/src/lib/i18n/logger.js.map +1 -0
- package/dist/src/lib/logger.d.ts +32 -0
- package/dist/src/lib/logger.js.map +1 -0
- package/dist/src/lib/server/browser.d.ts +34 -0
- package/dist/src/lib/server/browser.js +10 -16
- package/dist/src/lib/server/browser.js.map +1 -0
- package/dist/src/lib/server/fetcher.d.ts +20 -0
- package/dist/src/lib/server/fetcher.js.map +1 -0
- package/dist/src/lib/server/index.d.ts +10 -0
- package/dist/src/lib/server/index.js.map +1 -0
- package/dist/src/lib/server/prompts.d.ts +11 -0
- package/dist/src/lib/server/prompts.js +6 -6
- package/dist/src/lib/server/prompts.js.map +1 -0
- package/dist/src/lib/server/resources.d.ts +11 -0
- package/dist/src/lib/server/resources.js.map +1 -0
- package/dist/src/lib/server/tools.d.ts +12 -0
- package/dist/src/lib/server/tools.js +369 -309
- package/dist/src/lib/server/tools.js.map +1 -0
- package/dist/src/lib/server/types.d.ts +71 -0
- package/dist/src/lib/server/types.js.map +1 -0
- package/dist/src/lib/types.d.ts +119 -0
- package/dist/src/lib/types.js.map +1 -0
- package/dist/src/lib/utils/errorDetection.js +41 -0
- package/dist/src/lib/utils/errors.js +279 -0
- package/dist/src/mcp-server.d.ts +6 -0
- package/dist/src/mcp-server.js.map +1 -0
- package/dist/tests/BrowserFetcher.test.js +951 -0
- package/dist/tests/NodeFetcher.test.js +263 -0
- package/dist/tests/client.test.js +250 -0
- package/dist/tests/i18n.test.js +179 -0
- package/dist/tests/jest.setup.d.ts +6 -0
- package/dist/tests/jest.setup.js +30 -0
- package/dist/tests/jest.setup.js.map +1 -0
- package/dist/tests/logger.test.js +99 -0
- package/dist/tests/mcp-server.test.js +113 -0
- package/dist/tests/server/browser.test.js +197 -0
- package/dist/tests/server/fetcher.test.js +216 -0
- package/dist/tests/server/index.test.js +122 -0
- package/dist/tests/server/prompts.test.js +243 -0
- package/dist/tests/server/resources.test.js +359 -0
- package/dist/tests/server/tools.test.js +222 -0
- package/dist/tests/test-direct-client.d.ts +6 -0
- package/dist/tests/test-direct-client.js.map +1 -0
- package/dist/tests/test-mcp-methods.d.ts +6 -0
- package/dist/tests/test-mcp-methods.js.map +1 -0
- package/dist/tests/test-mcp.d.ts +6 -0
- package/dist/tests/test-mcp.js.map +1 -0
- package/dist/tests/test-mini4k.d.ts +6 -0
- package/dist/tests/test-mini4k.js.map +1 -0
- package/dist/tests/types.test.js +57 -0
- package/package.json +12 -3
|
@@ -6,341 +6,401 @@
|
|
|
6
6
|
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
7
7
|
import { log, COMPONENTS } from '../logger.js';
|
|
8
8
|
import { fetchWithAutoDetect } from './fetcher.js';
|
|
9
|
+
import { closeBrowserInstance } from './browser.js';
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @param server MCP服务器实例 (MCP server instance)
|
|
11
|
+
* 获取HTML获取工具定义 (Get HTML fetch tool definition)
|
|
12
|
+
* @returns HTML获取工具定义 (HTML fetch tool definition)
|
|
13
13
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
properties: {
|
|
25
|
-
url: {
|
|
26
|
-
type: "string",
|
|
27
|
-
description: "URL of the website to fetch",
|
|
28
|
-
},
|
|
29
|
-
headers: {
|
|
30
|
-
type: "object",
|
|
31
|
-
description: "Optional headers to include in the request",
|
|
32
|
-
},
|
|
33
|
-
proxy: {
|
|
34
|
-
type: "string",
|
|
35
|
-
description: "Optional proxy server to use (format: http://host:port or https://host:port)",
|
|
36
|
-
},
|
|
37
|
-
noDelay: {
|
|
38
|
-
type: "boolean",
|
|
39
|
-
description: "Optional flag to disable random delay between requests (default: false)",
|
|
40
|
-
},
|
|
41
|
-
timeout: {
|
|
42
|
-
type: "number",
|
|
43
|
-
description: "Optional timeout in milliseconds (default: 30000)",
|
|
44
|
-
},
|
|
45
|
-
maxRedirects: {
|
|
46
|
-
type: "number",
|
|
47
|
-
description: "Optional maximum number of redirects to follow (default: 10)",
|
|
48
|
-
},
|
|
49
|
-
useSystemProxy: {
|
|
50
|
-
type: "boolean",
|
|
51
|
-
description: "Optional flag to use system proxy environment variables (default: true)",
|
|
52
|
-
},
|
|
53
|
-
debug: {
|
|
54
|
-
type: "boolean",
|
|
55
|
-
description: "Optional flag to enable detailed debug logging (default: false)",
|
|
56
|
-
},
|
|
57
|
-
useBrowser: {
|
|
58
|
-
type: "boolean",
|
|
59
|
-
description: "Optional flag to use headless browser for fetching (default: false)",
|
|
60
|
-
},
|
|
61
|
-
autoDetectMode: {
|
|
62
|
-
type: "boolean",
|
|
63
|
-
description: "Optional flag to automatically switch to browser mode if standard fetch fails (default: true)",
|
|
64
|
-
},
|
|
65
|
-
waitForSelector: {
|
|
66
|
-
type: "string",
|
|
67
|
-
description: "Optional CSS selector to wait for when using browser mode",
|
|
68
|
-
},
|
|
69
|
-
waitForTimeout: {
|
|
70
|
-
type: "number",
|
|
71
|
-
description: "Optional timeout to wait after page load in browser mode (default: 5000)",
|
|
72
|
-
},
|
|
73
|
-
scrollToBottom: {
|
|
74
|
-
type: "boolean",
|
|
75
|
-
description: "Optional flag to scroll to bottom of page in browser mode (default: false)",
|
|
76
|
-
},
|
|
77
|
-
saveCookies: {
|
|
78
|
-
type: "boolean",
|
|
79
|
-
description: "Optional flag to save cookies for future requests to the same domain (default: true)",
|
|
80
|
-
},
|
|
81
|
-
closeBrowser: {
|
|
82
|
-
type: "boolean",
|
|
83
|
-
description: "Optional flag to close the browser after fetching (default: false)",
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
required: ["url"],
|
|
87
|
-
},
|
|
14
|
+
function getHtmlFetchToolDefinition() {
|
|
15
|
+
return {
|
|
16
|
+
name: "fetch_html",
|
|
17
|
+
description: "Fetch a website and return the content as HTML",
|
|
18
|
+
inputSchema: {
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
url: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description: "URL of the website to fetch",
|
|
88
24
|
},
|
|
89
|
-
{
|
|
90
|
-
|
|
91
|
-
description: "
|
|
92
|
-
inputSchema: {
|
|
93
|
-
type: "object",
|
|
94
|
-
properties: {
|
|
95
|
-
url: {
|
|
96
|
-
type: "string",
|
|
97
|
-
description: "URL of the JSON to fetch",
|
|
98
|
-
},
|
|
99
|
-
headers: {
|
|
100
|
-
type: "object",
|
|
101
|
-
description: "Optional headers to include in the request",
|
|
102
|
-
},
|
|
103
|
-
proxy: {
|
|
104
|
-
type: "string",
|
|
105
|
-
description: "Optional proxy server to use (format: http://host:port or https://host:port)",
|
|
106
|
-
},
|
|
107
|
-
noDelay: {
|
|
108
|
-
type: "boolean",
|
|
109
|
-
description: "Optional flag to disable random delay between requests (default: false)",
|
|
110
|
-
},
|
|
111
|
-
timeout: {
|
|
112
|
-
type: "number",
|
|
113
|
-
description: "Optional timeout in milliseconds (default: 30000)",
|
|
114
|
-
},
|
|
115
|
-
maxRedirects: {
|
|
116
|
-
type: "number",
|
|
117
|
-
description: "Optional maximum number of redirects to follow (default: 10)",
|
|
118
|
-
},
|
|
119
|
-
useSystemProxy: {
|
|
120
|
-
type: "boolean",
|
|
121
|
-
description: "Optional flag to use system proxy environment variables (default: true)",
|
|
122
|
-
},
|
|
123
|
-
debug: {
|
|
124
|
-
type: "boolean",
|
|
125
|
-
description: "Optional flag to enable detailed debug logging (default: false)",
|
|
126
|
-
},
|
|
127
|
-
useBrowser: {
|
|
128
|
-
type: "boolean",
|
|
129
|
-
description: "Optional flag to use headless browser for fetching (default: false)",
|
|
130
|
-
},
|
|
131
|
-
waitForSelector: {
|
|
132
|
-
type: "string",
|
|
133
|
-
description: "Optional CSS selector to wait for when using browser mode",
|
|
134
|
-
},
|
|
135
|
-
waitForTimeout: {
|
|
136
|
-
type: "number",
|
|
137
|
-
description: "Optional timeout to wait after page load in browser mode (default: 5000)",
|
|
138
|
-
},
|
|
139
|
-
closeBrowser: {
|
|
140
|
-
type: "boolean",
|
|
141
|
-
description: "Optional flag to close the browser after fetching (default: false)",
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
required: ["url"],
|
|
145
|
-
},
|
|
25
|
+
headers: {
|
|
26
|
+
type: "object",
|
|
27
|
+
description: "Optional headers to include in the request",
|
|
146
28
|
},
|
|
147
|
-
{
|
|
148
|
-
|
|
149
|
-
description: "
|
|
150
|
-
inputSchema: {
|
|
151
|
-
type: "object",
|
|
152
|
-
properties: {
|
|
153
|
-
url: {
|
|
154
|
-
type: "string",
|
|
155
|
-
description: "URL of the website to fetch",
|
|
156
|
-
},
|
|
157
|
-
headers: {
|
|
158
|
-
type: "object",
|
|
159
|
-
description: "Optional headers to include in the request",
|
|
160
|
-
},
|
|
161
|
-
proxy: {
|
|
162
|
-
type: "string",
|
|
163
|
-
description: "Optional proxy server to use (format: http://host:port or https://host:port)",
|
|
164
|
-
},
|
|
165
|
-
noDelay: {
|
|
166
|
-
type: "boolean",
|
|
167
|
-
description: "Optional flag to disable random delay between requests (default: false)",
|
|
168
|
-
},
|
|
169
|
-
timeout: {
|
|
170
|
-
type: "number",
|
|
171
|
-
description: "Optional timeout in milliseconds (default: 30000)",
|
|
172
|
-
},
|
|
173
|
-
maxRedirects: {
|
|
174
|
-
type: "number",
|
|
175
|
-
description: "Optional maximum number of redirects to follow (default: 10)",
|
|
176
|
-
},
|
|
177
|
-
useSystemProxy: {
|
|
178
|
-
type: "boolean",
|
|
179
|
-
description: "Optional flag to use system proxy environment variables (default: true)",
|
|
180
|
-
},
|
|
181
|
-
debug: {
|
|
182
|
-
type: "boolean",
|
|
183
|
-
description: "Optional flag to enable detailed debug logging (default: false)",
|
|
184
|
-
},
|
|
185
|
-
useBrowser: {
|
|
186
|
-
type: "boolean",
|
|
187
|
-
description: "Optional flag to use headless browser for fetching (default: false)",
|
|
188
|
-
},
|
|
189
|
-
waitForSelector: {
|
|
190
|
-
type: "string",
|
|
191
|
-
description: "Optional CSS selector to wait for when using browser mode",
|
|
192
|
-
},
|
|
193
|
-
waitForTimeout: {
|
|
194
|
-
type: "number",
|
|
195
|
-
description: "Optional timeout to wait after page load in browser mode (default: 5000)",
|
|
196
|
-
},
|
|
197
|
-
scrollToBottom: {
|
|
198
|
-
type: "boolean",
|
|
199
|
-
description: "Optional flag to scroll to bottom of page in browser mode (default: false)",
|
|
200
|
-
},
|
|
201
|
-
closeBrowser: {
|
|
202
|
-
type: "boolean",
|
|
203
|
-
description: "Optional flag to close the browser after fetching (default: false)",
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
required: ["url"],
|
|
207
|
-
},
|
|
29
|
+
proxy: {
|
|
30
|
+
type: "string",
|
|
31
|
+
description: "Optional proxy server to use (format: http://host:port or https://host:port)",
|
|
208
32
|
},
|
|
209
|
-
{
|
|
210
|
-
|
|
211
|
-
description: "
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
33
|
+
noDelay: {
|
|
34
|
+
type: "boolean",
|
|
35
|
+
description: "Optional flag to disable random delay between requests (default: false)",
|
|
36
|
+
},
|
|
37
|
+
timeout: {
|
|
38
|
+
type: "number",
|
|
39
|
+
description: "Optional timeout in milliseconds (default: 30000)",
|
|
40
|
+
},
|
|
41
|
+
maxRedirects: {
|
|
42
|
+
type: "number",
|
|
43
|
+
description: "Optional maximum number of redirects to follow (default: 10)",
|
|
44
|
+
},
|
|
45
|
+
useSystemProxy: {
|
|
46
|
+
type: "boolean",
|
|
47
|
+
description: "Optional flag to use system proxy environment variables (default: true)",
|
|
48
|
+
},
|
|
49
|
+
debug: {
|
|
50
|
+
type: "boolean",
|
|
51
|
+
description: "Optional flag to enable detailed debug logging (default: false)",
|
|
52
|
+
},
|
|
53
|
+
useBrowser: {
|
|
54
|
+
type: "boolean",
|
|
55
|
+
description: "Optional flag to use headless browser for fetching (default: false)",
|
|
56
|
+
},
|
|
57
|
+
autoDetectMode: {
|
|
58
|
+
type: "boolean",
|
|
59
|
+
description: "Optional flag to automatically switch to browser mode if standard fetch fails (default: true)",
|
|
60
|
+
},
|
|
61
|
+
waitForSelector: {
|
|
62
|
+
type: "string",
|
|
63
|
+
description: "Optional CSS selector to wait for when using browser mode",
|
|
64
|
+
},
|
|
65
|
+
waitForTimeout: {
|
|
66
|
+
type: "number",
|
|
67
|
+
description: "Optional timeout to wait after page load in browser mode (default: 5000)",
|
|
68
|
+
},
|
|
69
|
+
scrollToBottom: {
|
|
70
|
+
type: "boolean",
|
|
71
|
+
description: "Optional flag to scroll to bottom of page in browser mode (default: false)",
|
|
72
|
+
},
|
|
73
|
+
saveCookies: {
|
|
74
|
+
type: "boolean",
|
|
75
|
+
description: "Optional flag to save cookies for future requests to the same domain (default: true)",
|
|
76
|
+
},
|
|
77
|
+
closeBrowser: {
|
|
78
|
+
type: "boolean",
|
|
79
|
+
description: "Optional flag to close the browser after fetching (default: false)",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
required: ["url"],
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 获取JSON获取工具定义 (Get JSON fetch tool definition)
|
|
88
|
+
* @returns JSON获取工具定义 (JSON fetch tool definition)
|
|
89
|
+
*/
|
|
90
|
+
function getJsonFetchToolDefinition() {
|
|
91
|
+
return {
|
|
92
|
+
name: "fetch_json",
|
|
93
|
+
description: "Fetch a JSON file from a URL",
|
|
94
|
+
inputSchema: {
|
|
95
|
+
type: "object",
|
|
96
|
+
properties: {
|
|
97
|
+
url: {
|
|
98
|
+
type: "string",
|
|
99
|
+
description: "URL of the JSON to fetch",
|
|
100
|
+
},
|
|
101
|
+
headers: {
|
|
102
|
+
type: "object",
|
|
103
|
+
description: "Optional headers to include in the request",
|
|
104
|
+
},
|
|
105
|
+
proxy: {
|
|
106
|
+
type: "string",
|
|
107
|
+
description: "Optional proxy server to use (format: http://host:port or https://host:port)",
|
|
108
|
+
},
|
|
109
|
+
timeout: {
|
|
110
|
+
type: "number",
|
|
111
|
+
description: "Optional timeout in milliseconds (default: 30000)",
|
|
112
|
+
},
|
|
113
|
+
maxRedirects: {
|
|
114
|
+
type: "number",
|
|
115
|
+
description: "Optional maximum number of redirects to follow (default: 10)",
|
|
116
|
+
},
|
|
117
|
+
useSystemProxy: {
|
|
118
|
+
type: "boolean",
|
|
119
|
+
description: "Optional flag to use system proxy environment variables (default: true)",
|
|
120
|
+
},
|
|
121
|
+
debug: {
|
|
122
|
+
type: "boolean",
|
|
123
|
+
description: "Optional flag to enable detailed debug logging (default: false)",
|
|
124
|
+
},
|
|
125
|
+
noDelay: {
|
|
126
|
+
type: "boolean",
|
|
127
|
+
description: "Optional flag to disable random delay between requests (default: false)",
|
|
128
|
+
},
|
|
129
|
+
useBrowser: {
|
|
130
|
+
type: "boolean",
|
|
131
|
+
description: "Optional flag to use headless browser for fetching (default: false)",
|
|
132
|
+
},
|
|
133
|
+
waitForSelector: {
|
|
134
|
+
type: "string",
|
|
135
|
+
description: "Optional CSS selector to wait for when using browser mode",
|
|
136
|
+
},
|
|
137
|
+
waitForTimeout: {
|
|
138
|
+
type: "number",
|
|
139
|
+
description: "Optional timeout to wait after page load in browser mode (default: 5000)",
|
|
140
|
+
},
|
|
141
|
+
closeBrowser: {
|
|
142
|
+
type: "boolean",
|
|
143
|
+
description: "Optional flag to close the browser after fetching (default: false)",
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
required: ["url"],
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 获取文本获取工具定义 (Get text fetch tool definition)
|
|
152
|
+
* @returns 文本获取工具定义 (Text fetch tool definition)
|
|
153
|
+
*/
|
|
154
|
+
function getTextFetchToolDefinition() {
|
|
155
|
+
return {
|
|
156
|
+
name: "fetch_txt",
|
|
157
|
+
description: "Fetch a website, return the content as plain text (no HTML)",
|
|
158
|
+
inputSchema: {
|
|
159
|
+
type: "object",
|
|
160
|
+
properties: {
|
|
161
|
+
url: {
|
|
162
|
+
type: "string",
|
|
163
|
+
description: "URL of the website to fetch",
|
|
164
|
+
},
|
|
165
|
+
headers: {
|
|
166
|
+
type: "object",
|
|
167
|
+
description: "Optional headers to include in the request",
|
|
168
|
+
},
|
|
169
|
+
proxy: {
|
|
170
|
+
type: "string",
|
|
171
|
+
description: "Optional proxy server to use (format: http://host:port or https://host:port)",
|
|
172
|
+
},
|
|
173
|
+
timeout: {
|
|
174
|
+
type: "number",
|
|
175
|
+
description: "Optional timeout in milliseconds (default: 30000)",
|
|
176
|
+
},
|
|
177
|
+
maxRedirects: {
|
|
178
|
+
type: "number",
|
|
179
|
+
description: "Optional maximum number of redirects to follow (default: 10)",
|
|
180
|
+
},
|
|
181
|
+
useSystemProxy: {
|
|
182
|
+
type: "boolean",
|
|
183
|
+
description: "Optional flag to use system proxy environment variables (default: true)",
|
|
184
|
+
},
|
|
185
|
+
debug: {
|
|
186
|
+
type: "boolean",
|
|
187
|
+
description: "Optional flag to enable detailed debug logging (default: false)",
|
|
188
|
+
},
|
|
189
|
+
noDelay: {
|
|
190
|
+
type: "boolean",
|
|
191
|
+
description: "Optional flag to disable random delay between requests (default: false)",
|
|
192
|
+
},
|
|
193
|
+
useBrowser: {
|
|
194
|
+
type: "boolean",
|
|
195
|
+
description: "Optional flag to use headless browser for fetching (default: false)",
|
|
196
|
+
},
|
|
197
|
+
waitForSelector: {
|
|
198
|
+
type: "string",
|
|
199
|
+
description: "Optional CSS selector to wait for when using browser mode",
|
|
200
|
+
},
|
|
201
|
+
waitForTimeout: {
|
|
202
|
+
type: "number",
|
|
203
|
+
description: "Optional timeout to wait after page load in browser mode (default: 5000)",
|
|
204
|
+
},
|
|
205
|
+
scrollToBottom: {
|
|
206
|
+
type: "boolean",
|
|
207
|
+
description: "Optional flag to scroll to bottom of page in browser mode (default: false)",
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
required: ["url"],
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 获取Markdown获取工具定义 (Get Markdown fetch tool definition)
|
|
216
|
+
* @returns Markdown获取工具定义 (Markdown fetch tool definition)
|
|
217
|
+
*/
|
|
218
|
+
function getMarkdownFetchToolDefinition() {
|
|
219
|
+
return {
|
|
220
|
+
name: "fetch_markdown",
|
|
221
|
+
description: "Fetch a website and return the content as Markdown",
|
|
222
|
+
inputSchema: {
|
|
223
|
+
type: "object",
|
|
224
|
+
properties: {
|
|
225
|
+
url: {
|
|
226
|
+
type: "string",
|
|
227
|
+
description: "URL of the website to fetch",
|
|
228
|
+
},
|
|
229
|
+
headers: {
|
|
230
|
+
type: "object",
|
|
231
|
+
description: "Optional headers to include in the request",
|
|
232
|
+
},
|
|
233
|
+
proxy: {
|
|
234
|
+
type: "string",
|
|
235
|
+
description: "Optional proxy server to use (format: http://host:port or https://host:port)",
|
|
236
|
+
},
|
|
237
|
+
timeout: {
|
|
238
|
+
type: "number",
|
|
239
|
+
description: "Optional timeout in milliseconds (default: 30000)",
|
|
240
|
+
},
|
|
241
|
+
maxRedirects: {
|
|
242
|
+
type: "number",
|
|
243
|
+
description: "Optional maximum number of redirects to follow (default: 10)",
|
|
244
|
+
},
|
|
245
|
+
useSystemProxy: {
|
|
246
|
+
type: "boolean",
|
|
247
|
+
description: "Optional flag to use system proxy environment variables (default: true)",
|
|
248
|
+
},
|
|
249
|
+
debug: {
|
|
250
|
+
type: "boolean",
|
|
251
|
+
description: "Optional flag to enable detailed debug logging (default: false)",
|
|
252
|
+
},
|
|
253
|
+
noDelay: {
|
|
254
|
+
type: "boolean",
|
|
255
|
+
description: "Optional flag to disable random delay between requests (default: false)",
|
|
256
|
+
},
|
|
257
|
+
useBrowser: {
|
|
258
|
+
type: "boolean",
|
|
259
|
+
description: "Optional flag to use headless browser for fetching (default: false)",
|
|
260
|
+
},
|
|
261
|
+
waitForSelector: {
|
|
262
|
+
type: "string",
|
|
263
|
+
description: "Optional CSS selector to wait for when using browser mode",
|
|
264
|
+
},
|
|
265
|
+
waitForTimeout: {
|
|
266
|
+
type: "number",
|
|
267
|
+
description: "Optional timeout to wait after page load in browser mode (default: 5000)",
|
|
268
|
+
},
|
|
269
|
+
scrollToBottom: {
|
|
270
|
+
type: "boolean",
|
|
271
|
+
description: "Optional flag to scroll to bottom of page in browser mode (default: false)",
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
required: ["url"],
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* 获取所有工具定义 (Get all tool definitions)
|
|
280
|
+
* @returns 工具定义数组 (Array of tool definitions)
|
|
281
|
+
*/
|
|
282
|
+
function getAllToolDefinitions() {
|
|
283
|
+
return [
|
|
284
|
+
getHtmlFetchToolDefinition(),
|
|
285
|
+
getJsonFetchToolDefinition(),
|
|
286
|
+
getTextFetchToolDefinition(),
|
|
287
|
+
getMarkdownFetchToolDefinition()
|
|
288
|
+
];
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* 注册工具列表处理程序 (Register tool list handler)
|
|
292
|
+
* @param server MCP服务器实例 (MCP server instance)
|
|
293
|
+
*/
|
|
294
|
+
function registerToolListHandler(server) {
|
|
295
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
296
|
+
return {
|
|
297
|
+
tools: getAllToolDefinitions()
|
|
272
298
|
};
|
|
273
299
|
});
|
|
274
|
-
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* 注册工具调用处理程序 (Register tool call handler)
|
|
303
|
+
* @param server MCP服务器实例 (MCP server instance)
|
|
304
|
+
*/
|
|
305
|
+
function registerToolCallHandler(server) {
|
|
275
306
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
276
307
|
const name = request.params.name;
|
|
277
|
-
const
|
|
278
|
-
const
|
|
308
|
+
const args = request.params.arguments || {};
|
|
309
|
+
const debug = args.debug === true;
|
|
310
|
+
log('tools.callReceived', debug, { name, args: JSON.stringify(args) }, COMPONENTS.SERVER);
|
|
279
311
|
try {
|
|
280
|
-
//
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return {
|
|
285
|
-
content: [
|
|
286
|
-
{
|
|
287
|
-
type: "text",
|
|
288
|
-
text: result.content[0].text
|
|
289
|
-
}
|
|
290
|
-
],
|
|
291
|
-
isError: result.isError
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
else if (name === "fetch_json") {
|
|
295
|
-
const result = await fetchWithAutoDetect(params, 'json');
|
|
296
|
-
return {
|
|
297
|
-
content: [
|
|
298
|
-
{
|
|
299
|
-
type: "text",
|
|
300
|
-
text: result.content[0].text
|
|
301
|
-
}
|
|
302
|
-
],
|
|
303
|
-
isError: result.isError
|
|
304
|
-
};
|
|
312
|
+
// 处理不同类型的获取请求 (Handle different types of fetch requests)
|
|
313
|
+
if (name === 'fetch_html' || name === 'fetch_json' || name === 'fetch_txt' || name === 'fetch_markdown') {
|
|
314
|
+
const result = await handleFetchRequest(name, args, debug);
|
|
315
|
+
return result;
|
|
305
316
|
}
|
|
306
|
-
else
|
|
307
|
-
|
|
317
|
+
else {
|
|
318
|
+
// 未知工具 (Unknown tool)
|
|
319
|
+
log('tools.unknownTool', debug, { name }, COMPONENTS.SERVER);
|
|
308
320
|
return {
|
|
321
|
+
isError: true,
|
|
309
322
|
content: [
|
|
310
323
|
{
|
|
311
|
-
|
|
312
|
-
text: result.content[0].text
|
|
324
|
+
text: `Unknown tool: ${name}`
|
|
313
325
|
}
|
|
314
|
-
]
|
|
315
|
-
isError: result.isError
|
|
326
|
+
]
|
|
316
327
|
};
|
|
317
328
|
}
|
|
318
|
-
else if (name === "fetch_markdown") {
|
|
319
|
-
const result = await fetchWithAutoDetect(params, 'markdown');
|
|
320
|
-
return {
|
|
321
|
-
content: [
|
|
322
|
-
{
|
|
323
|
-
type: "text",
|
|
324
|
-
text: result.content[0].text
|
|
325
|
-
}
|
|
326
|
-
],
|
|
327
|
-
isError: result.isError
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
throw new Error(`Unknown tool name: ${name}`);
|
|
331
329
|
}
|
|
332
330
|
catch (error) {
|
|
333
|
-
//
|
|
334
|
-
log('
|
|
331
|
+
// 处理错误 (Handle error)
|
|
332
|
+
log('tools.callError', debug, { name, error: error.message }, COMPONENTS.SERVER);
|
|
335
333
|
return {
|
|
336
334
|
isError: true,
|
|
337
335
|
content: [
|
|
338
336
|
{
|
|
339
|
-
|
|
340
|
-
text: `Error processing request: ${error instanceof Error ? error.message : String(error)}`
|
|
337
|
+
text: error.message
|
|
341
338
|
}
|
|
342
339
|
]
|
|
343
340
|
};
|
|
344
341
|
}
|
|
342
|
+
finally {
|
|
343
|
+
// 如果请求参数中指定了关闭浏览器,则关闭浏览器 (If request parameters specify to close browser, close browser)
|
|
344
|
+
if (args.closeBrowser === true) {
|
|
345
|
+
await closeBrowserInstance(debug);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
345
348
|
});
|
|
346
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* 处理获取请求 (Handle fetch request)
|
|
352
|
+
* @param name 工具名称 (Tool name)
|
|
353
|
+
* @param args 请求参数 (Request parameters)
|
|
354
|
+
* @param debug 是否启用调试模式 (Whether debug mode is enabled)
|
|
355
|
+
* @returns 请求结果 (Request result)
|
|
356
|
+
*/
|
|
357
|
+
async function handleFetchRequest(name, args, debug) {
|
|
358
|
+
// 验证URL参数 (Validate URL parameter)
|
|
359
|
+
if (!args.url) {
|
|
360
|
+
log('tools.missingUrl', debug, {}, COMPONENTS.SERVER);
|
|
361
|
+
return {
|
|
362
|
+
isError: true,
|
|
363
|
+
content: [
|
|
364
|
+
{
|
|
365
|
+
text: "URL parameter is required"
|
|
366
|
+
}
|
|
367
|
+
]
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
// 记录请求 (Log request)
|
|
371
|
+
log('tools.fetchRequest', debug, { url: args.url, type: name }, COMPONENTS.SERVER);
|
|
372
|
+
// 执行获取请求 (Execute fetch request)
|
|
373
|
+
try {
|
|
374
|
+
const params = {
|
|
375
|
+
...args,
|
|
376
|
+
method: name
|
|
377
|
+
};
|
|
378
|
+
// 根据工具名称确定内容类型 (Determine content type based on tool name)
|
|
379
|
+
const type = name.replace('fetch_', '');
|
|
380
|
+
const result = await fetchWithAutoDetect(params, type);
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
// 处理错误 (Handle error)
|
|
385
|
+
log('tools.fetchError', debug, { url: args.url, error: error.message }, COMPONENTS.SERVER);
|
|
386
|
+
return {
|
|
387
|
+
isError: true,
|
|
388
|
+
content: [
|
|
389
|
+
{
|
|
390
|
+
text: `Error fetching ${args.url}: ${error.message}`
|
|
391
|
+
}
|
|
392
|
+
]
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* 注册工具到服务器 (Register tools to server)
|
|
398
|
+
* 设置工具列表和处理程序 (Set up tool list and handlers)
|
|
399
|
+
* @param server MCP服务器实例 (MCP server instance)
|
|
400
|
+
*/
|
|
401
|
+
export function registerTools(server) {
|
|
402
|
+
// 注册工具列表处理程序 (Register tool list handler)
|
|
403
|
+
registerToolListHandler(server);
|
|
404
|
+
// 注册工具调用处理程序 (Register tool call handler)
|
|
405
|
+
registerToolCallHandler(server);
|
|
406
|
+
}
|