search1api-mcp 0.10.0 → 0.12.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 +18 -1
- package/build/api.js +33 -6
- package/build/branding.js +1 -1
- package/build/config.js +8 -2
- package/build/http.js +61 -31
- package/build/server.js +1 -1
- package/build/tools/deepcrawl.js +207 -0
- package/build/tools/handlers.js +4 -1
- package/build/tools/index.js +115 -1
- package/build/types.js +35 -0
- package/package.json +1 -1
- package/server.json +3 -3
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ The official MCP server for [Search1API](https://s1.dev/?utm_source=mcp) — web
|
|
|
10
10
|
## Authentication
|
|
11
11
|
|
|
12
12
|
- OAuth-aware clients can connect to the Remote MCP URL directly, then sign in and approve access in the browser.
|
|
13
|
-
- Existing integrations can continue to use an API key from the [Search1API dashboard](https://
|
|
13
|
+
- Existing integrations can continue to use an API key from the [Search1API dashboard](https://app.s1.dev).
|
|
14
14
|
- Every MCP request — including tool discovery (`initialize`, `tools/list`) — requires a credential. Unauthenticated requests draw the OAuth challenge, which is how clients trigger sign-in; pre-connect inspection is served by the static [server card](https://mcp.search1api.com/.well-known/mcp/server-card.json) instead.
|
|
15
15
|
|
|
16
16
|
## Quick Start (Remote MCP)
|
|
@@ -153,6 +153,8 @@ If you prefer to run the server locally, use Node.js 20 or newer with npx — no
|
|
|
153
153
|
}
|
|
154
154
|
```
|
|
155
155
|
|
|
156
|
+
To send requests through another Search1API-compatible address, such as a New API gateway with the Search1API plugin, set `SEARCH1API_API_URL` to that base URL (for example `https://your-gateway.example.com/search1api`) and `SEARCH1API_KEY` to the key that address accepts.
|
|
157
|
+
|
|
156
158
|
For self-hosted HTTP deployments behind a proxy, add any internal hostnames
|
|
157
159
|
that reach the Node.js process to the comma-separated `MCP_ALLOWED_HOSTS`
|
|
158
160
|
environment variable. `mcp.search1api.com` and localhost addresses are allowed
|
|
@@ -233,6 +235,20 @@ Find local businesses and places on Google Maps. Returns one page of up to 20 pl
|
|
|
233
235
|
| `page` | No | 1 | Google Maps page, 1-6; each page is a separate call |
|
|
234
236
|
| `max_results` | No | 20 | Places to return from the page (1-20); fewer places do not lower the price |
|
|
235
237
|
|
|
238
|
+
### deepcrawl
|
|
239
|
+
Read many pages of one site in a single task, such as a documentation section. Finds the site's URLs through its sitemap, keeps those that match the path rules, and returns up to `max_pages` pages as citable results: `title`, `url`, and the page's Markdown in `text`. The result also carries `stats` (discovered, matched, skipped, crawled, failed), `failed` (URLs that could not be read, with the reason), the API `message`, and `result_url`, a JSON file with every page in full. Costs 20 credits per started task, however many pages it reads.
|
|
240
|
+
|
|
241
|
+
The tool waits up to about 45 seconds. If the crawl is still running, it returns `status: "running"` with a `task_id`; call `deepcrawl` again with that `task_id` to collect the pages, which is free and starts no new crawl. Page text is capped at 8,000 characters per page and 80,000 in total; shortened pages have `metadata.truncated: true`, and `result_url` holds the full text.
|
|
242
|
+
|
|
243
|
+
| Parameter | Required | Default | Description |
|
|
244
|
+
|-----------|----------|---------|-------------|
|
|
245
|
+
| `url` | Yes, unless `task_id` is given | - | Site or section to crawl, such as `https://example.com/docs/` |
|
|
246
|
+
| `max_pages` | No | 10 | Most pages to read (1-50), in discovery order after the path rules; the price is the same for any number |
|
|
247
|
+
| `include_paths` | No | - | Up to 20 URL path rules to keep. Without `*` a rule is a path prefix on segment boundaries (`/docs` matches `/docs/api`, not `/docsearch`); with `*` it must match the whole path and `*` also matches `/` |
|
|
248
|
+
| `exclude_paths` | No | - | Up to 20 path rules to skip, same syntax; exclusion wins |
|
|
249
|
+
| `type` | No | sitemap | `sitemap` uses the site's sitemap, or the links on the start page when it has none; `all` uses every link discovered from the start URL |
|
|
250
|
+
| `task_id` | No | - | `task_id` of a crawl that was still running; only checks its status, and the other parameters are ignored |
|
|
251
|
+
|
|
236
252
|
### send_feedback
|
|
237
253
|
Report a wrong, empty or incomplete result, an error you could not resolve, documentation that did not match behavior, or a missing capability to the Search1API team. Free; it spends no credits. When the server has a Search1API credential, the report is linked to that account so the team can follow up. Do not include credentials or private data.
|
|
238
254
|
|
|
@@ -246,6 +262,7 @@ Report a wrong, empty or incomplete result, an error you could not resolve, docu
|
|
|
246
262
|
|
|
247
263
|
## Version History
|
|
248
264
|
|
|
265
|
+
- v0.11.0: New `deepcrawl` tool to read up to 50 pages of one site in a single task (20 credits per task; checking a running task is free)
|
|
249
266
|
- v0.10.0: New `maps` tool for Google Maps place search (3 credits per page of up to 20 places)
|
|
250
267
|
- v0.7.0: New `ask` tool for agentic search. `search` adds the `bingcn`, `yandex`, and `grokipedia` engines and a `page` parameter; `search` and `news` accept `time_range: "week"` and pass `published_date`, GitHub, and Hacker News fields through in result `metadata`
|
|
251
268
|
|
package/build/api.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import { API_KEY, API_CONFIG } from './config.js';
|
|
2
2
|
import { log } from './utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* A non-2xx answer from the API. `status` and `body` let a caller tell, for
|
|
5
|
+
* example, a failed deepcrawl task (a task payload sent with HTTP 500) from
|
|
6
|
+
* an outage.
|
|
7
|
+
*/
|
|
8
|
+
export class ApiResponseError extends Error {
|
|
9
|
+
status;
|
|
10
|
+
body;
|
|
11
|
+
constructor(message, status, body) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.body = body;
|
|
15
|
+
this.name = 'ApiResponseError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
3
18
|
/**
|
|
4
19
|
* Unified API request function
|
|
5
20
|
* @param endpoint API endpoint
|
|
@@ -8,6 +23,16 @@ import { log } from './utils.js';
|
|
|
8
23
|
* @returns API response
|
|
9
24
|
*/
|
|
10
25
|
export async function makeRequest(endpoint, data, apiKey) {
|
|
26
|
+
return request('POST', endpoint, data, apiKey);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* GET an API endpoint, such as a task status, with the same credential and
|
|
30
|
+
* error handling as makeRequest.
|
|
31
|
+
*/
|
|
32
|
+
export async function makeGetRequest(endpoint, apiKey) {
|
|
33
|
+
return request('GET', endpoint, undefined, apiKey);
|
|
34
|
+
}
|
|
35
|
+
async function request(method, endpoint, data, apiKey) {
|
|
11
36
|
const key = apiKey || API_KEY;
|
|
12
37
|
if (!key) {
|
|
13
38
|
throw new Error("API key is required. Provide SEARCH1API_KEY or pass apiKey explicitly.");
|
|
@@ -17,23 +42,25 @@ export async function makeRequest(endpoint, data, apiKey) {
|
|
|
17
42
|
const path = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
|
18
43
|
const url = `${baseUrl}${path}`;
|
|
19
44
|
// Debug log for request details (excluding sensitive info)
|
|
20
|
-
log(`Making request to: ${url}`);
|
|
21
|
-
|
|
45
|
+
log(`Making ${method} request to: ${url}`);
|
|
46
|
+
if (method === 'POST') {
|
|
47
|
+
log(`Request data: ${JSON.stringify(data, null, 2)}`);
|
|
48
|
+
}
|
|
22
49
|
try {
|
|
23
50
|
const response = await fetch(url, {
|
|
24
|
-
method
|
|
51
|
+
method,
|
|
25
52
|
headers: {
|
|
26
53
|
'Authorization': `Bearer ${key}`,
|
|
27
|
-
'Content-Type': 'application/json',
|
|
54
|
+
...(method === 'POST' && { 'Content-Type': 'application/json' }),
|
|
28
55
|
},
|
|
29
|
-
body: JSON.stringify(data)
|
|
56
|
+
...(method === 'POST' && { body: JSON.stringify(data) }),
|
|
30
57
|
});
|
|
31
58
|
// Log response status only
|
|
32
59
|
log(`Response status: ${response.status}`);
|
|
33
60
|
if (!response.ok) {
|
|
34
61
|
const errorData = await response.json().catch(() => ({}));
|
|
35
62
|
log(`Error response: ${JSON.stringify(errorData, null, 2)}`);
|
|
36
|
-
throw new
|
|
63
|
+
throw new ApiResponseError(`API error: ${response.status} ${errorData.message || response.statusText}`, response.status, errorData);
|
|
37
64
|
}
|
|
38
65
|
const result = await response.json();
|
|
39
66
|
const duration = Date.now() - startTime;
|
package/build/branding.js
CHANGED
|
@@ -10,4 +10,4 @@ export const SERVER_ICONS = [
|
|
|
10
10
|
},
|
|
11
11
|
];
|
|
12
12
|
// Same wording as server.json, advertised in the MCP handshake serverInfo.
|
|
13
|
-
export const SERVER_DESCRIPTION = "
|
|
13
|
+
export const SERVER_DESCRIPTION = "Multi-engine web and platform search, news, Google Maps places, page reading, sitemaps, and trends.";
|
package/build/config.js
CHANGED
|
@@ -33,8 +33,12 @@ else {
|
|
|
33
33
|
log('API key located successfully.');
|
|
34
34
|
}
|
|
35
35
|
// API configuration
|
|
36
|
+
// SEARCH1API_API_URL points the tools at another Search1API-compatible base
|
|
37
|
+
// URL, such as a New API gateway with the Search1API plugin
|
|
38
|
+
// (https://your-gateway.example.com/search1api). The remote server reads the
|
|
39
|
+
// same variable.
|
|
36
40
|
export const API_CONFIG = {
|
|
37
|
-
BASE_URL: 'https://api.search1api.com',
|
|
41
|
+
BASE_URL: (process.env.SEARCH1API_API_URL?.trim() || 'https://api.search1api.com').replace(/\/+$/, ''),
|
|
38
42
|
DEFAULT_QUERY: 'latest news in the world',
|
|
39
43
|
ENDPOINTS: {
|
|
40
44
|
SEARCH: '/search',
|
|
@@ -43,7 +47,9 @@ export const API_CONFIG = {
|
|
|
43
47
|
NEWS: '/news',
|
|
44
48
|
TRENDING: '/trending',
|
|
45
49
|
ASK: '/ask',
|
|
46
|
-
MAPS: '/maps'
|
|
50
|
+
MAPS: '/maps',
|
|
51
|
+
DEEPCRAWL: '/deepcrawl',
|
|
52
|
+
DEEPCRAWL_STATUS: '/deepcrawl/status'
|
|
47
53
|
}
|
|
48
54
|
};
|
|
49
55
|
// Agentback project public key for the send_feedback tool. It can only submit
|
package/build/http.js
CHANGED
|
@@ -46,41 +46,71 @@ export async function validateCredential(credential) {
|
|
|
46
46
|
}
|
|
47
47
|
return { credential, principal };
|
|
48
48
|
}
|
|
49
|
-
//
|
|
50
|
-
// again.
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
//
|
|
49
|
+
// How long a validation result is reused before the credential is checked
|
|
50
|
+
// again. Every MCP exchange carries the credential, and connected clients keep
|
|
51
|
+
// exchanging even when idle (re-discovery, reconnects, SSE attempts), so
|
|
52
|
+
// checking each one turned the API's /usage into its busiest endpoint: about
|
|
53
|
+
// 33k checks a day against about 1k tool calls. A minute collapses a client's
|
|
54
|
+
// bursts and most of its idle polling.
|
|
55
|
+
//
|
|
56
|
+
// Rejections: clients holding a dead token keep retrying (one ping every few
|
|
57
|
+
// seconds, indefinitely); matches the gateway's own cache of failed tokens.
|
|
58
|
+
// Acceptances: a revoked key, a disabled account or an expired OAuth token
|
|
59
|
+
// can still open a connection for up to this long. Tool calls are unaffected:
|
|
60
|
+
// the API authenticates and bills every one of them itself.
|
|
61
|
+
const ACCEPTED_CREDENTIAL_TTL_MS = 60_000;
|
|
54
62
|
const REJECTED_CREDENTIAL_TTL_MS = 60_000;
|
|
55
|
-
const
|
|
63
|
+
const CREDENTIAL_CACHE_MAX_ENTRIES = 10_000;
|
|
56
64
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
65
|
+
* Reuse validation results for a short time, keyed by the credential's hash.
|
|
66
|
+
*
|
|
67
|
+
* Both outcomes are cached, each for its own TTL. Validation outages (thrown
|
|
68
|
+
* errors) are never cached. Concurrent checks of the same credential share a
|
|
69
|
+
* single upstream call, since a client's exchanges often arrive together.
|
|
60
70
|
*/
|
|
61
|
-
export function
|
|
62
|
-
const
|
|
71
|
+
export function cacheCredentialValidation(validator, { acceptedTtlMs = ACCEPTED_CREDENTIAL_TTL_MS, rejectedTtlMs = REJECTED_CREDENTIAL_TTL_MS, maxEntries = CREDENTIAL_CACHE_MAX_ENTRIES, now = Date.now, } = {}) {
|
|
72
|
+
const cache = new Map();
|
|
73
|
+
const inFlight = new Map();
|
|
74
|
+
const remember = (key, principal) => {
|
|
75
|
+
const ttlMs = principal === null ? rejectedTtlMs : acceptedTtlMs;
|
|
76
|
+
if (ttlMs <= 0) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
cache.delete(key);
|
|
80
|
+
// Map keeps insertion order, so the first key is the oldest entry.
|
|
81
|
+
if (cache.size >= maxEntries) {
|
|
82
|
+
const oldest = cache.keys().next().value;
|
|
83
|
+
if (oldest !== undefined) {
|
|
84
|
+
cache.delete(oldest);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
cache.set(key, { expiresAt: now() + ttlMs, principal });
|
|
88
|
+
};
|
|
63
89
|
return async (credential) => {
|
|
64
90
|
const key = createHash("sha256").update(credential).digest("hex");
|
|
65
|
-
const
|
|
66
|
-
if (
|
|
67
|
-
if (expiresAt > now()) {
|
|
68
|
-
return null
|
|
91
|
+
const cached = cache.get(key);
|
|
92
|
+
if (cached !== undefined) {
|
|
93
|
+
if (cached.expiresAt > now()) {
|
|
94
|
+
return cached.principal === null
|
|
95
|
+
? null
|
|
96
|
+
: { credential, principal: cached.principal };
|
|
69
97
|
}
|
|
70
|
-
|
|
98
|
+
cache.delete(key);
|
|
71
99
|
}
|
|
72
|
-
const
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
if (rejectedUntil.size >= maxEntries) {
|
|
76
|
-
const oldest = rejectedUntil.keys().next().value;
|
|
77
|
-
if (oldest !== undefined) {
|
|
78
|
-
rejectedUntil.delete(oldest);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
rejectedUntil.set(key, now() + ttlMs);
|
|
100
|
+
const pending = inFlight.get(key);
|
|
101
|
+
if (pending) {
|
|
102
|
+
return pending;
|
|
82
103
|
}
|
|
83
|
-
|
|
104
|
+
const check = validator(credential)
|
|
105
|
+
.then((authenticated) => {
|
|
106
|
+
remember(key, authenticated?.principal ?? null);
|
|
107
|
+
return authenticated;
|
|
108
|
+
})
|
|
109
|
+
.finally(() => {
|
|
110
|
+
inFlight.delete(key);
|
|
111
|
+
});
|
|
112
|
+
inFlight.set(key, check);
|
|
113
|
+
return check;
|
|
84
114
|
};
|
|
85
115
|
}
|
|
86
116
|
/**
|
|
@@ -88,8 +118,8 @@ export function cacheRejectedCredentials(validator, { ttlMs = REJECTED_CREDENTIA
|
|
|
88
118
|
*
|
|
89
119
|
* A single v2 handler serves the 2026-07-28 protocol and the SDK's default
|
|
90
120
|
* stateless 2025 fallback. Every presented credential is validated per
|
|
91
|
-
* exchange, so no credential or principal
|
|
92
|
-
* session. Every exchange requires a credential: the 401 + WWW-Authenticate
|
|
121
|
+
* exchange (through a one-minute result cache), so no credential or principal
|
|
122
|
+
* is retained in a server-side MCP session. Every exchange requires a credential: the 401 + WWW-Authenticate
|
|
93
123
|
* challenge on the first unauthenticated request is what spec-compliant
|
|
94
124
|
* clients treat as the trigger to start the OAuth flow, and serving
|
|
95
125
|
* discovery anonymously would leave clients that equate "tools listed" with
|
|
@@ -97,7 +127,7 @@ export function cacheRejectedCredentials(validator, { ttlMs = REJECTED_CREDENTIA
|
|
|
97
127
|
*/
|
|
98
128
|
export function createHttpApp(options = {}) {
|
|
99
129
|
const app = express();
|
|
100
|
-
const credentialValidator =
|
|
130
|
+
const credentialValidator = cacheCredentialValidation(options.validateCredential ?? validateCredential);
|
|
101
131
|
const authorizationServer = normalizeAuthorizationServer(options.authorizationServer ??
|
|
102
132
|
process.env.OAUTH_AUTHORIZATION_SERVER ??
|
|
103
133
|
DEFAULT_AUTHORIZATION_SERVER);
|
|
@@ -225,7 +255,7 @@ export function createHttpApp(options = {}) {
|
|
|
225
255
|
name: "Search1API",
|
|
226
256
|
version: PACKAGE_VERSION,
|
|
227
257
|
icons: SERVER_ICONS,
|
|
228
|
-
description: "Remote MCP server for Search1API.
|
|
258
|
+
description: "Remote MCP server for Search1API. Gives AI agents live search across Google, Bing, and platforms such as GitHub, Reddit, X, arXiv, WeChat, and Bilibili, plus news, Google Maps places, full-page reading, site link discovery, and GitHub and Hacker News trends.",
|
|
229
259
|
serverUrl: MCP_RESOURCE,
|
|
230
260
|
tools: ALL_TOOLS.map((tool) => ({
|
|
231
261
|
name: tool.name,
|
package/build/server.js
CHANGED
|
@@ -41,7 +41,7 @@ export function createMcpServer(credential) {
|
|
|
41
41
|
icons: SERVER_ICONS,
|
|
42
42
|
websiteUrl: "https://s1.dev",
|
|
43
43
|
description: SERVER_DESCRIPTION,
|
|
44
|
-
}, { instructions: "Use search, news, ask, maps, crawl, sitemap and trending to retrieve data. Call render_results with selected existing results only when the user wants to browse sources or a visual list helps. Rendering never repeats a search or consumes search credits; unsupported UI clients receive a readable text list." });
|
|
44
|
+
}, { instructions: "Use search, news, ask, maps, crawl, deepcrawl, sitemap and trending to retrieve data. Call render_results with selected existing results only when the user wants to browse sources or a visual list helps. Rendering never repeats a search or consumes search credits; unsupported UI clients receive a readable text list." });
|
|
45
45
|
for (const tool of ALL_TOOLS) {
|
|
46
46
|
const inputSchema = fromJsonSchema(tool.inputSchema);
|
|
47
47
|
const outputSchema = tool.outputSchema
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { isValidDeepcrawlArgs, } from '../types.js';
|
|
2
|
+
import { ApiResponseError, makeGetRequest, makeRequest } from '../api.js';
|
|
3
|
+
import { formatError, log } from '../utils.js';
|
|
4
|
+
import { API_CONFIG } from '../config.js';
|
|
5
|
+
import { INVALID_PARAMS, ProtocolError, } from "@modelcontextprotocol/server";
|
|
6
|
+
/**
|
|
7
|
+
* How long one deepcrawl call waits for its task. A 20-page crawl usually
|
|
8
|
+
* takes 10-20 s; anything slower is handed back as a task_id so the call
|
|
9
|
+
* stays well inside common client timeouts. Mutable so tests can shorten it.
|
|
10
|
+
*/
|
|
11
|
+
export const DEEPCRAWL_POLLING = {
|
|
12
|
+
intervalMs: 2_000,
|
|
13
|
+
timeoutMs: 45_000,
|
|
14
|
+
};
|
|
15
|
+
/** Bounds on the page text returned inline; resultUrl holds the rest. */
|
|
16
|
+
export const DEEPCRAWL_TEXT_LIMITS = {
|
|
17
|
+
perPage: 8_000,
|
|
18
|
+
total: 80_000,
|
|
19
|
+
};
|
|
20
|
+
const RUNNING_STATUSES = new Set(["queued", "waiting", "processing", "running", "unknown"]);
|
|
21
|
+
function errorResult(text) {
|
|
22
|
+
return {
|
|
23
|
+
content: [{ type: "text", text }],
|
|
24
|
+
isError: true,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function toolResult(structuredContent) {
|
|
28
|
+
return {
|
|
29
|
+
structuredContent,
|
|
30
|
+
content: [{
|
|
31
|
+
type: "text",
|
|
32
|
+
text: JSON.stringify(structuredContent)
|
|
33
|
+
}]
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
37
|
+
/**
|
|
38
|
+
* Read a task's status. A failed task arrives as HTTP 500 and an unknown one
|
|
39
|
+
* as HTTP 404, both with a task payload, so those bodies are statuses too.
|
|
40
|
+
*/
|
|
41
|
+
async function checkStatus(taskId, apiKey) {
|
|
42
|
+
try {
|
|
43
|
+
const response = await makeGetRequest(`${API_CONFIG.ENDPOINTS.DEEPCRAWL_STATUS}/${encodeURIComponent(taskId)}`, apiKey);
|
|
44
|
+
return { kind: "status", response };
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof ApiResponseError) {
|
|
48
|
+
const body = error.body && typeof error.body === "object" ? error.body : {};
|
|
49
|
+
if (error.status === 404) {
|
|
50
|
+
return { kind: "status", response: { ...body, taskId, status: "not_found" } };
|
|
51
|
+
}
|
|
52
|
+
if (typeof body.status === "string" && body.taskId) {
|
|
53
|
+
return { kind: "status", response: body };
|
|
54
|
+
}
|
|
55
|
+
// Rate limits and server faults can clear up while we wait; a rejected
|
|
56
|
+
// credential or request will not.
|
|
57
|
+
return { kind: "error", error, retryable: error.status === 429 || error.status >= 500 };
|
|
58
|
+
}
|
|
59
|
+
return { kind: "error", error, retryable: true };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Cut at a UTF-16 boundary so a shortened text never ends in half a character. */
|
|
63
|
+
function cutText(text, limit) {
|
|
64
|
+
if (text.length <= limit) {
|
|
65
|
+
return text;
|
|
66
|
+
}
|
|
67
|
+
let cut = text.slice(0, limit);
|
|
68
|
+
const last = cut.charCodeAt(cut.length - 1);
|
|
69
|
+
if (last >= 0xd800 && last <= 0xdbff) {
|
|
70
|
+
cut = cut.slice(0, -1);
|
|
71
|
+
}
|
|
72
|
+
return cut;
|
|
73
|
+
}
|
|
74
|
+
function pagesToResults(pages) {
|
|
75
|
+
let remaining = DEEPCRAWL_TEXT_LIMITS.total;
|
|
76
|
+
let truncated = false;
|
|
77
|
+
const results = pages.map((page, index) => {
|
|
78
|
+
const content = typeof page.content === "string" ? page.content : "";
|
|
79
|
+
const text = cutText(content, Math.min(DEEPCRAWL_TEXT_LIMITS.perPage, remaining));
|
|
80
|
+
remaining -= text.length;
|
|
81
|
+
const cut = text.length < content.length;
|
|
82
|
+
truncated ||= cut;
|
|
83
|
+
return {
|
|
84
|
+
id: page.url,
|
|
85
|
+
title: page.title || page.url,
|
|
86
|
+
url: page.url,
|
|
87
|
+
...(text && { text }),
|
|
88
|
+
metadata: {
|
|
89
|
+
source: "search1api",
|
|
90
|
+
position: index + 1,
|
|
91
|
+
...(cut && { truncated: true, content_length: content.length }),
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
return { results, truncated };
|
|
96
|
+
}
|
|
97
|
+
function completedResult(taskId, response) {
|
|
98
|
+
const pages = Array.isArray(response.pages) ? response.pages : undefined;
|
|
99
|
+
const { results, truncated } = pagesToResults(pages ?? []);
|
|
100
|
+
const resultUrl = response.resultUrl || undefined;
|
|
101
|
+
let note;
|
|
102
|
+
if (truncated) {
|
|
103
|
+
note = `Page text was shortened to fit this response (at most ${DEEPCRAWL_TEXT_LIMITS.perPage} characters per page and ${DEEPCRAWL_TEXT_LIMITS.total} in total); pages marked truncated in metadata are incomplete.${resultUrl ? " result_url holds every page in full;" : ""} read a single page in full with crawl.`;
|
|
104
|
+
}
|
|
105
|
+
else if (!pages && resultUrl) {
|
|
106
|
+
note = "The result was too large to return inline; result_url holds every page.";
|
|
107
|
+
}
|
|
108
|
+
else if (results.length === 0) {
|
|
109
|
+
note = "The crawl finished without pages; message says why.";
|
|
110
|
+
}
|
|
111
|
+
return toolResult({
|
|
112
|
+
task_id: taskId,
|
|
113
|
+
status: "completed",
|
|
114
|
+
...(response.message && { message: response.message }),
|
|
115
|
+
results,
|
|
116
|
+
...(response.stats && { stats: response.stats }),
|
|
117
|
+
failed: Array.isArray(response.failed) ? response.failed : [],
|
|
118
|
+
...(resultUrl && { result_url: resultUrl }),
|
|
119
|
+
truncated,
|
|
120
|
+
...(note && { note }),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function runningResult(taskId, lastError) {
|
|
124
|
+
return toolResult({
|
|
125
|
+
task_id: taskId,
|
|
126
|
+
status: "running",
|
|
127
|
+
results: [],
|
|
128
|
+
note: `The crawl is still running. Call deepcrawl again with {"task_id": "${taskId}"} in a few seconds to collect the pages; checking costs no credits and does not start a new crawl.${lastError ? ` The last status check failed: ${formatError(lastError)}` : ""}`,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Implementation of the deepcrawl tool
|
|
133
|
+
*/
|
|
134
|
+
export async function handleDeepcrawl(args, apiKey) {
|
|
135
|
+
if (!isValidDeepcrawlArgs(args)) {
|
|
136
|
+
throw new ProtocolError(INVALID_PARAMS, "Invalid deepcrawl arguments");
|
|
137
|
+
}
|
|
138
|
+
let taskId;
|
|
139
|
+
const resuming = args.task_id !== undefined;
|
|
140
|
+
if (args.task_id !== undefined) {
|
|
141
|
+
taskId = args.task_id.trim();
|
|
142
|
+
log("Checking deepcrawl task:", taskId);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
log("Starting deepcrawl for:", args.url);
|
|
146
|
+
try {
|
|
147
|
+
const response = await makeRequest(API_CONFIG.ENDPOINTS.DEEPCRAWL, {
|
|
148
|
+
url: args.url,
|
|
149
|
+
...(args.type !== undefined && { type: args.type }),
|
|
150
|
+
max_pages: args.max_pages ?? 10,
|
|
151
|
+
...(args.include_paths !== undefined && { include_paths: args.include_paths }),
|
|
152
|
+
...(args.exclude_paths !== undefined && { exclude_paths: args.exclude_paths }),
|
|
153
|
+
format: "json",
|
|
154
|
+
}, apiKey);
|
|
155
|
+
if (typeof response?.taskId !== "string" || response.taskId.length === 0) {
|
|
156
|
+
throw new Error("the API did not return a task id");
|
|
157
|
+
}
|
|
158
|
+
taskId = response.taskId;
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
log("Deepcrawl start error:", error);
|
|
162
|
+
return errorResult(`Deepcrawl API error: ${formatError(error)}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const deadline = Date.now() + DEEPCRAWL_POLLING.timeoutMs;
|
|
166
|
+
let lastError;
|
|
167
|
+
// A new task needs a moment before it can finish; an earlier one may already have.
|
|
168
|
+
let waitFirst = !resuming;
|
|
169
|
+
while (true) {
|
|
170
|
+
if (waitFirst) {
|
|
171
|
+
if (Date.now() + DEEPCRAWL_POLLING.intervalMs > deadline) {
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
await sleep(DEEPCRAWL_POLLING.intervalMs);
|
|
175
|
+
}
|
|
176
|
+
waitFirst = true;
|
|
177
|
+
const check = await checkStatus(taskId, apiKey);
|
|
178
|
+
if (check.kind === "error") {
|
|
179
|
+
if (!check.retryable) {
|
|
180
|
+
log("Deepcrawl status error:", check.error);
|
|
181
|
+
return errorResult(`Deepcrawl API error: ${formatError(check.error)} (task_id ${taskId})`);
|
|
182
|
+
}
|
|
183
|
+
lastError = check.error;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
lastError = undefined;
|
|
187
|
+
const { response } = check;
|
|
188
|
+
const status = response.status ?? "processing";
|
|
189
|
+
if (status === "completed") {
|
|
190
|
+
return completedResult(taskId, response);
|
|
191
|
+
}
|
|
192
|
+
if (status === "failed") {
|
|
193
|
+
return errorResult(`Deepcrawl task ${taskId} failed: ${response.error || response.message || "no reason given"}`);
|
|
194
|
+
}
|
|
195
|
+
if (status === "not_found") {
|
|
196
|
+
// A task this call just started may not be visible yet; keep waiting.
|
|
197
|
+
if (!resuming) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
return errorResult(`Deepcrawl task ${taskId} was not found; it may have expired. Start a new crawl with url.`);
|
|
201
|
+
}
|
|
202
|
+
if (!RUNNING_STATUSES.has(status)) {
|
|
203
|
+
log(`Unexpected deepcrawl status for ${taskId}: ${status}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return runningResult(taskId, lastError);
|
|
207
|
+
}
|
package/build/tools/handlers.js
CHANGED
|
@@ -8,7 +8,8 @@ import { handleTrending } from "./trending.js";
|
|
|
8
8
|
import { handleRenderResults } from "./render-results.js";
|
|
9
9
|
import { handleAsk } from "./ask.js";
|
|
10
10
|
import { handleMaps } from "./maps.js";
|
|
11
|
-
import {
|
|
11
|
+
import { handleDeepcrawl } from "./deepcrawl.js";
|
|
12
|
+
import { SEARCH_TOOL, CRAWL_TOOL, SITEMAP_TOOL, NEWS_TOOL, TRENDING_TOOL, ASK_TOOL, MAPS_TOOL, DEEPCRAWL_TOOL, RENDER_RESULTS_TOOL, } from "./index.js";
|
|
12
13
|
/**
|
|
13
14
|
* Dispatch request based on tool name
|
|
14
15
|
* @param toolName Name of the tool
|
|
@@ -33,6 +34,8 @@ export async function handleToolCall(toolName, args, apiKey) {
|
|
|
33
34
|
return await handleAsk(args, apiKey);
|
|
34
35
|
case MAPS_TOOL.name:
|
|
35
36
|
return await handleMaps(args, apiKey);
|
|
37
|
+
case DEEPCRAWL_TOOL.name:
|
|
38
|
+
return await handleDeepcrawl(args, apiKey);
|
|
36
39
|
case RENDER_RESULTS_TOOL.name:
|
|
37
40
|
return await handleRenderResults(args);
|
|
38
41
|
default:
|
package/build/tools/index.js
CHANGED
|
@@ -68,7 +68,7 @@ const SEARCH_OUTPUT_SCHEMA = {
|
|
|
68
68
|
export const SEARCH_TOOL = {
|
|
69
69
|
name: "search",
|
|
70
70
|
title: "Web search",
|
|
71
|
-
description: "Search the live
|
|
71
|
+
description: "Search the live web through one chosen engine: Google, Bing, DuckDuckGo, or Yahoo for the open web, or a specialized source such as GitHub, Reddit, X, YouTube, arXiv, Wikipedia, WeChat, or Bilibili when the user wants results from that platform. Use when the user needs current information, sources, or research. Returns citable results with id, title, URL, and text; read any URL in full with crawl.",
|
|
72
72
|
...AUTHENTICATED_READ_ONLY_WEB,
|
|
73
73
|
inputSchema: {
|
|
74
74
|
type: "object",
|
|
@@ -427,6 +427,119 @@ export const MAPS_TOOL = {
|
|
|
427
427
|
required: ["results"],
|
|
428
428
|
},
|
|
429
429
|
};
|
|
430
|
+
const DEEPCRAWL_PATH_RULES = {
|
|
431
|
+
type: "array",
|
|
432
|
+
maxItems: 20,
|
|
433
|
+
items: {
|
|
434
|
+
type: "string",
|
|
435
|
+
minLength: 1,
|
|
436
|
+
maxLength: 200,
|
|
437
|
+
},
|
|
438
|
+
};
|
|
439
|
+
// Deepcrawl tool definition
|
|
440
|
+
export const DEEPCRAWL_TOOL = {
|
|
441
|
+
name: "deepcrawl",
|
|
442
|
+
title: "Crawl a website",
|
|
443
|
+
description: "Read many pages of one website in a single task, such as a documentation section, when the user needs content from across a site rather than one page. Finds the site's URLs through its sitemap, keeps those whose path matches the path rules, and returns up to max_pages pages as citable results with title, URL, and Markdown text, plus page counts and the URLs that failed. Costs 20 credits per started task, however many pages it reads. Waits about 45 seconds; if the crawl is still running, the result holds a task_id, and calling deepcrawl again with that task_id collects the pages at no cost. Use crawl for a single page and sitemap for a list of URLs without their content.",
|
|
444
|
+
...AUTHENTICATED_READ_ONLY_WEB,
|
|
445
|
+
inputSchema: {
|
|
446
|
+
type: "object",
|
|
447
|
+
properties: {
|
|
448
|
+
url: {
|
|
449
|
+
type: "string",
|
|
450
|
+
format: "uri",
|
|
451
|
+
description: "Site or section to crawl, such as https://example.com or https://example.com/docs/; required unless task_id is given",
|
|
452
|
+
},
|
|
453
|
+
max_pages: {
|
|
454
|
+
type: "integer",
|
|
455
|
+
minimum: 1,
|
|
456
|
+
maximum: 50,
|
|
457
|
+
description: "Most pages to read, taken in discovery order after the path rules; the price is the same for any number",
|
|
458
|
+
default: 10,
|
|
459
|
+
},
|
|
460
|
+
include_paths: {
|
|
461
|
+
...DEEPCRAWL_PATH_RULES,
|
|
462
|
+
description: "Only read URLs whose path matches one of these rules. A rule without * is a path prefix on segment boundaries (\"/docs\" matches /docs/api but not /docsearch); a rule with * must match the whole path, and * also matches / (\"/blog/*\")",
|
|
463
|
+
},
|
|
464
|
+
exclude_paths: {
|
|
465
|
+
...DEEPCRAWL_PATH_RULES,
|
|
466
|
+
description: "Skip URLs whose path matches one of these rules, with the same syntax as include_paths; exclusion wins over inclusion",
|
|
467
|
+
},
|
|
468
|
+
type: {
|
|
469
|
+
type: "string",
|
|
470
|
+
enum: ["sitemap", "all"],
|
|
471
|
+
description: "Where URLs come from: sitemap uses the site's sitemap, or the links on the start page when it has none; all uses every link discovered from the start URL, which can include other sites",
|
|
472
|
+
default: "sitemap",
|
|
473
|
+
},
|
|
474
|
+
task_id: {
|
|
475
|
+
type: "string",
|
|
476
|
+
minLength: 1,
|
|
477
|
+
maxLength: 200,
|
|
478
|
+
description: "task_id from an earlier deepcrawl result that was still running; collects that crawl for free instead of starting a new one, and the other arguments are ignored",
|
|
479
|
+
},
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
outputSchema: {
|
|
483
|
+
type: "object",
|
|
484
|
+
properties: {
|
|
485
|
+
task_id: {
|
|
486
|
+
type: "string",
|
|
487
|
+
description: "Crawl task id; pass it back as task_id while status is running",
|
|
488
|
+
},
|
|
489
|
+
status: {
|
|
490
|
+
type: "string",
|
|
491
|
+
enum: ["completed", "running"],
|
|
492
|
+
description: "completed when the crawl has finished; running when it needs more time and results is empty",
|
|
493
|
+
},
|
|
494
|
+
message: {
|
|
495
|
+
type: "string",
|
|
496
|
+
description: "Summary of the finished crawl, including why nothing was crawled",
|
|
497
|
+
},
|
|
498
|
+
results: {
|
|
499
|
+
type: "array",
|
|
500
|
+
description: "Crawled pages in discovery order; text holds each page's Markdown, shortened when metadata.truncated is true",
|
|
501
|
+
items: CITABLE_RESULT_SCHEMA,
|
|
502
|
+
},
|
|
503
|
+
stats: {
|
|
504
|
+
type: "object",
|
|
505
|
+
description: "Page counts: links discovered, links matching the path rules, matches left out by max_pages, and pages crawled or failed",
|
|
506
|
+
properties: {
|
|
507
|
+
discovered: { type: "integer" },
|
|
508
|
+
matched: { type: "integer" },
|
|
509
|
+
skipped: { type: "integer" },
|
|
510
|
+
crawled: { type: "integer" },
|
|
511
|
+
failed: { type: "integer" },
|
|
512
|
+
},
|
|
513
|
+
},
|
|
514
|
+
failed: {
|
|
515
|
+
type: "array",
|
|
516
|
+
description: "Pages that could not be crawled, with the reason",
|
|
517
|
+
items: {
|
|
518
|
+
type: "object",
|
|
519
|
+
properties: {
|
|
520
|
+
url: { type: "string" },
|
|
521
|
+
error: { type: "string" },
|
|
522
|
+
},
|
|
523
|
+
required: ["url", "error"],
|
|
524
|
+
},
|
|
525
|
+
},
|
|
526
|
+
result_url: {
|
|
527
|
+
type: "string",
|
|
528
|
+
format: "uri",
|
|
529
|
+
description: "JSON file with every crawled page in full",
|
|
530
|
+
},
|
|
531
|
+
truncated: {
|
|
532
|
+
type: "boolean",
|
|
533
|
+
description: "Whether page text was shortened or left out to bound the response",
|
|
534
|
+
},
|
|
535
|
+
note: {
|
|
536
|
+
type: "string",
|
|
537
|
+
description: "What to do next: call again with task_id, or where to find text that was cut",
|
|
538
|
+
},
|
|
539
|
+
},
|
|
540
|
+
required: ["task_id", "status", "results"],
|
|
541
|
+
},
|
|
542
|
+
};
|
|
430
543
|
export const RENDER_RESULTS_TOOL = {
|
|
431
544
|
name: "render_results",
|
|
432
545
|
title: "Render the results",
|
|
@@ -450,5 +563,6 @@ export const ALL_TOOLS = [
|
|
|
450
563
|
TRENDING_TOOL,
|
|
451
564
|
ASK_TOOL,
|
|
452
565
|
MAPS_TOOL,
|
|
566
|
+
DEEPCRAWL_TOOL,
|
|
453
567
|
RENDER_RESULTS_TOOL,
|
|
454
568
|
];
|
package/build/types.js
CHANGED
|
@@ -191,3 +191,38 @@ export function isValidMapsArgs(args) {
|
|
|
191
191
|
}
|
|
192
192
|
return true;
|
|
193
193
|
}
|
|
194
|
+
export const DEEPCRAWL_MAX_PAGES = 50;
|
|
195
|
+
function isValidPathRules(rules) {
|
|
196
|
+
return (Array.isArray(rules) &&
|
|
197
|
+
rules.length <= 20 &&
|
|
198
|
+
rules.every((rule) => typeof rule === 'string' && rule.length >= 1 && rule.length <= 200));
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Either `task_id` (check an earlier crawl) or `url` (start a new one) is
|
|
202
|
+
* required. With `task_id`, the crawl options are ignored.
|
|
203
|
+
*/
|
|
204
|
+
export function isValidDeepcrawlArgs(args) {
|
|
205
|
+
if (typeof args !== 'object' || args === null) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
const { url, task_id, type, max_pages, include_paths, exclude_paths } = args;
|
|
209
|
+
if (task_id !== undefined) {
|
|
210
|
+
return typeof task_id === 'string' && task_id.trim().length > 0 && task_id.length <= 200;
|
|
211
|
+
}
|
|
212
|
+
if (typeof url !== 'string' || url.trim().length === 0) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
if (type !== undefined && type !== 'sitemap' && type !== 'all') {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (max_pages !== undefined && (!Number.isInteger(max_pages) || max_pages < 1 || max_pages > DEEPCRAWL_MAX_PAGES)) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
if (include_paths !== undefined && !isValidPathRules(include_paths)) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
if (exclude_paths !== undefined && !isValidPathRules(exclude_paths)) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
228
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "search1api-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"mcpName": "io.github.superagents-lab/search1api",
|
|
5
5
|
"description": "A Model Context Protocol (MCP) server that provides search and crawl functionality using Search1API",
|
|
6
6
|
"private": false,
|
package/server.json
CHANGED
|
@@ -11,12 +11,12 @@
|
|
|
11
11
|
]
|
|
12
12
|
}
|
|
13
13
|
],
|
|
14
|
-
"description": "
|
|
14
|
+
"description": "Multi-engine web and platform search, news, Google Maps places, page reading, sitemaps, and trends.",
|
|
15
15
|
"repository": {
|
|
16
16
|
"url": "https://github.com/superagents-lab/search1api-mcp",
|
|
17
17
|
"source": "github"
|
|
18
18
|
},
|
|
19
|
-
"version": "0.
|
|
19
|
+
"version": "0.12.0",
|
|
20
20
|
"remotes": [
|
|
21
21
|
{
|
|
22
22
|
"type": "streamable-http",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
{
|
|
28
28
|
"registryType": "npm",
|
|
29
29
|
"identifier": "search1api-mcp",
|
|
30
|
-
"version": "0.
|
|
30
|
+
"version": "0.12.0",
|
|
31
31
|
"transport": {
|
|
32
32
|
"type": "stdio"
|
|
33
33
|
},
|