@astratra/web-search 1.2.0 → 1.2.2
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.fr.md +6 -4
- package/README.md +5 -4
- package/package.json +8 -12
- package/src/address.js +27 -1
- package/src/browser/deploy.js +4 -2
- package/src/browser/egressProxy.js +6 -2
- package/src/browser/engine.js +2 -1
- package/src/browser/guard.js +5 -2
- package/src/index.d.ts +7 -1
- package/src/mcp.js +8 -19
- package/src/mcpStdio.js +2 -2
- package/src/tools.js +4 -1
package/README.fr.md
CHANGED
|
@@ -160,8 +160,8 @@ toolDefinitions('gemini'); // [{ functionDeclarations: [...] }], schéma ne
|
|
|
160
160
|
- **`runAgentLoop` de `@astratra/ai`** : `registryTools(ws, { roles })`
|
|
161
161
|
inscrit `type: 'read'`, `risk: 'LOW'`, `external: true` (la requête sort de
|
|
162
162
|
l'application : le masque des données personnelles s'y applique).
|
|
163
|
-
- **MCP** : `createMcpServer(ws)` de `@astratra/web-search/mcp` (
|
|
164
|
-
`@
|
|
163
|
+
- **MCP** : `createMcpServer(ws)` de `@astratra/web-search/mcp` (protocole de
|
|
164
|
+
`@astratra/mcp`, sans dépendance) sert les deux mêmes outils, marqués en lecture
|
|
165
165
|
seule. Un serveur stdio prêt à l'emploi lit ses moteurs dans l'environnement :
|
|
166
166
|
|
|
167
167
|
```bash
|
|
@@ -315,8 +315,10 @@ Chaque protection tient si une autre cède :
|
|
|
315
315
|
WebSocket, images, `fetch` des scripts et DNS rebinding sont couverts.
|
|
316
316
|
WebRTC est coupé.
|
|
317
317
|
- **Pare-feu système, obligatoire** : les règles nftables refusent à
|
|
318
|
-
l'utilisateur `navigateur` toute sortie vers une adresse non publique
|
|
319
|
-
|
|
318
|
+
l'utilisateur `navigateur` toute sortie vers une adresse non publique ou
|
|
319
|
+
vers une adresse de la machine elle-même (son adresse publique comprise),
|
|
320
|
+
sauf le résolveur DNS et le relais. Le relais refuse aussi les adresses de
|
|
321
|
+
la machine.
|
|
320
322
|
- **Contrôle au démarrage** : le service tente de joindre la boucle locale,
|
|
321
323
|
une adresse privée de la machine et `169.254.169.254` ; si l'une répond, il
|
|
322
324
|
refuse de démarrer (`isolation_failed`). `/health` donne le résultat.
|
package/README.md
CHANGED
|
@@ -155,8 +155,8 @@ toolDefinitions('gemini'); // [{ functionDeclarations: [...] }], schema cle
|
|
|
155
155
|
- **`runAgentLoop` of `@astratra/ai`**: `registryTools(ws, { roles })` registers
|
|
156
156
|
`type: 'read'`, `risk: 'LOW'`, `external: true` (the query leaves the
|
|
157
157
|
application: the masker applies to it).
|
|
158
|
-
- **MCP**: `createMcpServer(ws)` from `@astratra/web-search/mcp` (
|
|
159
|
-
`@
|
|
158
|
+
- **MCP**: `createMcpServer(ws)` from `@astratra/web-search/mcp` (protocol from
|
|
159
|
+
`@astratra/mcp`, no dependency) serves the same two tools, marked read-only. A
|
|
160
160
|
ready-made stdio server reads its engines from the environment:
|
|
161
161
|
|
|
162
162
|
```bash
|
|
@@ -303,8 +303,9 @@ Each protection holds if another one gives way:
|
|
|
303
303
|
WebSockets, images, script `fetch` calls and DNS rebinding are covered.
|
|
304
304
|
WebRTC is off.
|
|
305
305
|
- **System firewall, mandatory**: the nftables rules refuse the `navigateur`
|
|
306
|
-
user any outgoing connection to a non-public address
|
|
307
|
-
|
|
306
|
+
user any outgoing connection to a non-public address or to an address of the
|
|
307
|
+
machine itself (its public address included), except the DNS resolver and
|
|
308
|
+
the relay. The relay refuses the machine's addresses too.
|
|
308
309
|
- **Startup check**: the service tries to reach loopback, a private address of
|
|
309
310
|
the machine and `169.254.169.254`; if one answers, it refuses to start
|
|
310
311
|
(`isolation_failed`). `/health` reports the result.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astratra/web-search",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Web search for AI agents: several engines (Serper, Brave, Tavily, Exa, or a gateway of your own) with automatic fallback, rank fusion without content mixing (one entry = one page = one site), verbatim passages with their position, SSRF-safe page reading, content framed as data, cache, quotas, cost report, tool definitions and an MCP server, and an integrated read-only browser (shared, isolated Chromium service) for JavaScript pages, site links and site search.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -15,10 +15,6 @@
|
|
|
15
15
|
"astratra-web-search-mcp": "src/mcpStdio.js",
|
|
16
16
|
"astratra-web-browser": "src/browser/cli.js"
|
|
17
17
|
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"test": "jest",
|
|
20
|
-
"eval:live": "node scripts/evalLive.js"
|
|
21
|
-
},
|
|
22
18
|
"jest": {
|
|
23
19
|
"testMatch": [
|
|
24
20
|
"**/__tests__/**/*.test.js"
|
|
@@ -47,22 +43,18 @@
|
|
|
47
43
|
"access": "public"
|
|
48
44
|
},
|
|
49
45
|
"dependencies": {
|
|
50
|
-
"@astratra/ai": "^1.15.0 || ^2.0.0"
|
|
46
|
+
"@astratra/ai": "^1.15.0 || ^2.0.0",
|
|
47
|
+
"@astratra/mcp": "^1.0.0"
|
|
51
48
|
},
|
|
52
49
|
"peerDependencies": {
|
|
53
|
-
"@modelcontextprotocol/sdk": ">=1.20.0",
|
|
54
50
|
"playwright": ">=1.48"
|
|
55
51
|
},
|
|
56
52
|
"peerDependenciesMeta": {
|
|
57
|
-
"@modelcontextprotocol/sdk": {
|
|
58
|
-
"optional": true
|
|
59
|
-
},
|
|
60
53
|
"playwright": {
|
|
61
54
|
"optional": true
|
|
62
55
|
}
|
|
63
56
|
},
|
|
64
57
|
"devDependencies": {
|
|
65
|
-
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
66
58
|
"jest": "30.4.2",
|
|
67
59
|
"playwright": "^1.64.1"
|
|
68
60
|
},
|
|
@@ -76,5 +68,9 @@
|
|
|
76
68
|
"default": "./src/mcp.js"
|
|
77
69
|
},
|
|
78
70
|
"./package.json": "./package.json"
|
|
71
|
+
},
|
|
72
|
+
"scripts": {
|
|
73
|
+
"test": "jest",
|
|
74
|
+
"eval:live": "node scripts/evalLive.js"
|
|
79
75
|
}
|
|
80
|
-
}
|
|
76
|
+
}
|
package/src/address.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
const net = require('net');
|
|
11
|
+
const os = require('os');
|
|
11
12
|
|
|
12
13
|
/* Les mêmes listes servent au pare-feu du navigateur (createBrowserFirewall). */
|
|
13
14
|
const BLOCKED_IPV4 = Object.freeze([
|
|
@@ -73,4 +74,29 @@ function isBlockedHostname(hostname) {
|
|
|
73
74
|
|| host.endsWith('.in-addr.arpa') || host.endsWith('.ip6.arpa');
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
/** Une adresse sous une forme comparable : minuscules, sans zone, IPv4 tirée d'une IPv4 mappée (::ffff:a.b.c.d). */
|
|
78
|
+
function comparable(address) {
|
|
79
|
+
const text = String(address ?? '').toLowerCase().replace(/^\[|\]$/g, '').replace(/%.*$/, '');
|
|
80
|
+
const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(text);
|
|
81
|
+
if (mapped) return mapped[1];
|
|
82
|
+
const groups = net.isIPv6(text) ? groupsOf(text) : null;
|
|
83
|
+
if (groups && groups.slice(0, 5).every((g) => g === 0) && groups[5] === 0xffff) {
|
|
84
|
+
return `${groups[6] >> 8}.${groups[6] & 0xff}.${groups[7] >> 8}.${groups[7] & 0xff}`;
|
|
85
|
+
}
|
|
86
|
+
return groups ? groups.map((g) => g.toString(16)).join(':') : text;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Vrai pour une adresse de cette machine (ses interfaces), même publique :
|
|
91
|
+
* un service interne qui écoute sur toutes les adresses, caché de l'extérieur
|
|
92
|
+
* par un pare-feu d'entrée, se joint aussi par l'adresse publique de la machine.
|
|
93
|
+
* @param {string} address
|
|
94
|
+
* @param {string[]} [own] les adresses de la machine (défaut : os.networkInterfaces()).
|
|
95
|
+
*/
|
|
96
|
+
function isOwnAddress(address, own) {
|
|
97
|
+
const list = own || Object.values(os.networkInterfaces()).flat().filter(Boolean).map((entry) => entry.address);
|
|
98
|
+
const wanted = comparable(address);
|
|
99
|
+
return list.some((entry) => comparable(entry) === wanted);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = { isPublicAddress, isBlockedHostname, isOwnAddress, BLOCKED_IPV4, BLOCKED_IPV6 };
|
package/src/browser/deploy.js
CHANGED
|
@@ -236,8 +236,9 @@ function nftAddress(prefix, bits) {
|
|
|
236
236
|
|
|
237
237
|
/**
|
|
238
238
|
* Les règles nftables (à charger par `nft -f`) : l'utilisateur du service ne
|
|
239
|
-
* peut sortir que vers des adresses publiques
|
|
240
|
-
*
|
|
239
|
+
* peut sortir que vers des adresses publiques qui ne sont pas celles de la
|
|
240
|
+
* machine (son adresse publique comprise), son résolveur DNS et son relais de
|
|
241
|
+
* sortie local. Toute autre adresse est jetée en sortie (la
|
|
241
242
|
* connexion n'aboutit jamais), ce que vérifie le contrôle au démarrage.
|
|
242
243
|
* @param {object} [options]
|
|
243
244
|
* @param {string} [options.user] Défaut "navigateur".
|
|
@@ -272,6 +273,7 @@ function createBrowserFirewall(options = {}) {
|
|
|
272
273
|
' ct state established,related accept',
|
|
273
274
|
` ip daddr 127.0.0.1 tcp dport ${proxyPort} accept comment "browser egress relay"`,
|
|
274
275
|
...dnsLines,
|
|
276
|
+
' fib daddr type local drop comment "addresses of this machine, its public address included"',
|
|
275
277
|
` ip daddr { ${BLOCKED_IPV4.map(([prefix, bits]) => nftAddress(prefix, bits)).join(', ')} } drop comment "non-public IPv4"`,
|
|
276
278
|
' ip6 daddr != 2000::/3 drop comment "IPv6 outside global unicast"',
|
|
277
279
|
` ip6 daddr { ${BLOCKED_IPV6.filter(([prefix]) => /^[23]/.test(prefix)).map(([prefix, bits]) => nftAddress(prefix, bits)).join(', ')} } drop comment "reserved IPv6 inside 2000::/3"`,
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* - le port doit être permis ;
|
|
8
8
|
* - le nom est refusé sans résolution s'il est interne, puis résolu ICI ; si
|
|
9
9
|
* UNE de ses adresses n'est pas publique, la connexion est refusée ;
|
|
10
|
+
* - les adresses de la machine elle-même sont refusées, même publiques ;
|
|
10
11
|
* - la connexion part vers l'adresse vérifiée elle-même : un DNS qui
|
|
11
12
|
* changerait de réponse (DNS rebinding) n'y gagne rien.
|
|
12
13
|
*
|
|
@@ -23,7 +24,7 @@ const http = require('http');
|
|
|
23
24
|
const net = require('net');
|
|
24
25
|
const dns = require('dns');
|
|
25
26
|
const crypto = require('crypto');
|
|
26
|
-
const { isPublicAddress, isBlockedHostname } = require('../address');
|
|
27
|
+
const { isPublicAddress, isBlockedHostname, isOwnAddress } = require('../address');
|
|
27
28
|
|
|
28
29
|
function lookupAll(resolve, hostname) {
|
|
29
30
|
return new Promise((resolvePromise, reject) => {
|
|
@@ -54,6 +55,7 @@ function splitTarget(value) {
|
|
|
54
55
|
* @param {string} [options.host] Défaut 127.0.0.1.
|
|
55
56
|
* @param {number} [options.idleMs] Défaut 30 s.
|
|
56
57
|
* @param {Function} [options.onRefused] ({ hostname, port, reason }) => void.
|
|
58
|
+
* @param {Function} [options.ownAddresses] () => string[] : les adresses de la machine (défaut : ses interfaces).
|
|
57
59
|
*/
|
|
58
60
|
async function startEgressProxy(options = {}) {
|
|
59
61
|
const resolve = options.resolve || dns.lookup;
|
|
@@ -78,7 +80,8 @@ async function startEgressProxy(options = {}) {
|
|
|
78
80
|
if (!ports.has(port)) return { reason: 'port' };
|
|
79
81
|
const host = hostname.replace(/^\[|\]$/g, '');
|
|
80
82
|
if (isBlockedHostname(host)) return { reason: 'address' };
|
|
81
|
-
|
|
83
|
+
const own = typeof options.ownAddresses === 'function' ? options.ownAddresses() : undefined;
|
|
84
|
+
if (net.isIP(host)) return isOwnAddress(host, own) ? { reason: 'own_address' } : { address: host };
|
|
82
85
|
let addresses;
|
|
83
86
|
try {
|
|
84
87
|
addresses = await lookupAll(resolve, host);
|
|
@@ -86,6 +89,7 @@ async function startEgressProxy(options = {}) {
|
|
|
86
89
|
return { reason: 'dns' };
|
|
87
90
|
}
|
|
88
91
|
if (!addresses.length || addresses.some((address) => !isPublicAddress(address))) return { reason: 'address' };
|
|
92
|
+
if (addresses.some((address) => isOwnAddress(address, own))) return { reason: 'own_address' };
|
|
89
93
|
return { address: addresses[0] };
|
|
90
94
|
}
|
|
91
95
|
|
package/src/browser/engine.js
CHANGED
|
@@ -178,7 +178,7 @@ function defaultFetchRobots({ userAgent, resolve }) {
|
|
|
178
178
|
function createBrowserEngine(options = {}) {
|
|
179
179
|
const settings = { ...DEFAULTS, ...Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined)) };
|
|
180
180
|
const userAgent = options.userAgent || browserUserAgent({ infoUrl: options.infoUrl || DEFAULT_INFO_URL });
|
|
181
|
-
const guard = createRequestGuard({ resolve: options.resolve, ports: options.ports, allowHttp: options.allowHttp });
|
|
181
|
+
const guard = createRequestGuard({ resolve: options.resolve, ports: options.ports, allowHttp: options.allowHttp, ownAddresses: options.ownAddresses });
|
|
182
182
|
const robots = options.robots === false ? null : (options.robotsCache || createRobotsCache({
|
|
183
183
|
token: ROBOTS_AGENT_TOKEN,
|
|
184
184
|
fetchRobots: options.fetchRobots || defaultFetchRobots({ userAgent, resolve: options.resolve })
|
|
@@ -211,6 +211,7 @@ function createBrowserEngine(options = {}) {
|
|
|
211
211
|
connect: options.connect,
|
|
212
212
|
ports: options.ports,
|
|
213
213
|
allowHttp: options.allowHttp,
|
|
214
|
+
ownAddresses: options.ownAddresses,
|
|
214
215
|
port: options.proxyPort ?? 0
|
|
215
216
|
});
|
|
216
217
|
}
|
package/src/browser/guard.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
const dns = require('dns');
|
|
19
19
|
const net = require('net');
|
|
20
|
-
const { isPublicAddress, isBlockedHostname } = require('../address');
|
|
20
|
+
const { isPublicAddress, isBlockedHostname, isOwnAddress } = require('../address');
|
|
21
21
|
|
|
22
22
|
const SECURE = new Set(['https:', 'wss:']);
|
|
23
23
|
const PLAIN = new Set(['http:', 'ws:']);
|
|
@@ -38,6 +38,7 @@ function lookupAll(resolve, hostname) {
|
|
|
38
38
|
* @param {number[]} [options.ports] Défaut [443] (et 80 avec allowHttp).
|
|
39
39
|
* @param {boolean} [options.allowHttp] Défaut false.
|
|
40
40
|
* @param {number} [options.dnsTtlMs] Défaut 60 s.
|
|
41
|
+
* @param {Function} [options.ownAddresses] () => string[] : les adresses de la machine (défaut : ses interfaces).
|
|
41
42
|
*/
|
|
42
43
|
function createRequestGuard(options = {}) {
|
|
43
44
|
const resolve = options.resolve || dns.lookup;
|
|
@@ -75,7 +76,8 @@ function createRequestGuard(options = {}) {
|
|
|
75
76
|
if (!ports.has(port)) return { ok: false, reason: 'port' };
|
|
76
77
|
const hostname = url.hostname.replace(/^\[|\]$/g, '').toLowerCase().replace(/\.$/, '');
|
|
77
78
|
if (isBlockedHostname(hostname)) return { ok: false, reason: 'address' };
|
|
78
|
-
|
|
79
|
+
const own = typeof options.ownAddresses === 'function' ? options.ownAddresses() : undefined;
|
|
80
|
+
if (net.isIP(hostname)) return isOwnAddress(hostname, own) ? { ok: false, reason: 'own_address' } : { ok: true, url };
|
|
79
81
|
let addresses;
|
|
80
82
|
try {
|
|
81
83
|
addresses = await addressesOf(hostname);
|
|
@@ -83,6 +85,7 @@ function createRequestGuard(options = {}) {
|
|
|
83
85
|
return { ok: false, reason: 'dns' };
|
|
84
86
|
}
|
|
85
87
|
if (!addresses.length || addresses.some((address) => !isPublicAddress(address))) return { ok: false, reason: 'address' };
|
|
88
|
+
if (addresses.some((address) => isOwnAddress(address, own))) return { ok: false, reason: 'own_address' };
|
|
86
89
|
return { ok: true, url };
|
|
87
90
|
}
|
|
88
91
|
|
package/src/index.d.ts
CHANGED
|
@@ -327,6 +327,8 @@ export function rankEntries(entries: FusedEntry[], request: SearchRequest, optio
|
|
|
327
327
|
|
|
328
328
|
export function isPublicAddress(address: string): boolean;
|
|
329
329
|
export function isBlockedHostname(hostname: string): boolean;
|
|
330
|
+
/** Vrai pour une adresse de cette machine (ses interfaces), même publique. */
|
|
331
|
+
export function isOwnAddress(address: string, own?: string[]): boolean;
|
|
330
332
|
|
|
331
333
|
export const SAFE_FETCH_DEFAULTS: Readonly<{ timeoutMs: number; maxBytes: number; maxRedirects: number; ports: number[]; userAgent: string }>;
|
|
332
334
|
export function safeFetch(url: string, options?: ReadingOptions & { lang?: string; signal?: AbortSignal; request?: (...args: any[]) => any; resolve?: (...args: any[]) => any }): Promise<{
|
|
@@ -475,6 +477,8 @@ export interface BrowserEngineOptions {
|
|
|
475
477
|
allowHttp?: boolean;
|
|
476
478
|
resolve?: (...args: any[]) => any;
|
|
477
479
|
proxyPort?: number;
|
|
480
|
+
/** Les adresses de la machine, refusées même publiques (défaut : ses interfaces). */
|
|
481
|
+
ownAddresses?: () => string[];
|
|
478
482
|
maxContexts?: number;
|
|
479
483
|
queueMs?: number;
|
|
480
484
|
maxQueue?: number;
|
|
@@ -523,7 +527,7 @@ export function createBrowserService(options: {
|
|
|
523
527
|
isolation(): IsolationReport | null;
|
|
524
528
|
};
|
|
525
529
|
|
|
526
|
-
export function startEgressProxy(options?: { resolve?: (...args: any[]) => any; connect?: (...args: any[]) => any; ports?: number[]; allowHttp?: boolean; port?: number; host?: string; idleMs?: number; onRefused?: (event: { hostname: string; port: number; reason: string }) => void }): Promise<{
|
|
530
|
+
export function startEgressProxy(options?: { resolve?: (...args: any[]) => any; connect?: (...args: any[]) => any; ports?: number[]; allowHttp?: boolean; port?: number; host?: string; idleMs?: number; ownAddresses?: () => string[]; onRefused?: (event: { hostname: string; port: number; reason: string }) => void }): Promise<{
|
|
527
531
|
port: number; server: string; username: string; password: string; stats(): { tunnels: number; refused: number; open: number }; close(): Promise<void>;
|
|
528
532
|
}>;
|
|
529
533
|
|
|
@@ -558,6 +562,8 @@ export const SIGNATURE_DIRECTORY_TYPE: string;
|
|
|
558
562
|
export interface BrowserDeployOptions {
|
|
559
563
|
port?: number;
|
|
560
564
|
proxyPort?: number;
|
|
565
|
+
/** Les adresses de la machine, refusées même publiques (défaut : ses interfaces). */
|
|
566
|
+
ownAddresses?: () => string[];
|
|
561
567
|
maxContexts?: number;
|
|
562
568
|
queueMs?: number;
|
|
563
569
|
infoUrl?: string;
|
package/src/mcp.js
CHANGED
|
@@ -4,38 +4,27 @@
|
|
|
4
4
|
* navigateur est branché) et le même texte cadré. Une capture d'écran revient
|
|
5
5
|
* en contenu image, seulement avec `screenshots: true`.
|
|
6
6
|
*
|
|
7
|
-
* Le
|
|
8
|
-
*
|
|
9
|
-
* schémas JSON tels quels (sans zod).
|
|
7
|
+
* Le protocole vient de @astratra/mcp (sans dépendance) ; un transport du
|
|
8
|
+
* SDK officiel se branche aussi tel quel.
|
|
10
9
|
*
|
|
11
10
|
* const { createMcpServer } = require('@astratra/web-search/mcp');
|
|
12
11
|
* const server = createMcpServer(ws);
|
|
13
|
-
* await server.connect(transport); //
|
|
12
|
+
* await server.connect(transport); // createStdioServerTransport(), createStreamableHttpHandler…
|
|
14
13
|
*
|
|
15
14
|
* Une erreur d'outil revient au client en `isError: true` avec son code et son
|
|
16
15
|
* message (jamais une clé), pas en panne du serveur.
|
|
17
16
|
*/
|
|
18
17
|
|
|
18
|
+
const { createMcpServer: createProtocolServer } = require('@astratra/mcp');
|
|
19
19
|
const { toolDefinitions, runTool } = require('./tools');
|
|
20
20
|
|
|
21
|
-
function loadSdk() {
|
|
22
|
-
try {
|
|
23
|
-
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
|
|
24
|
-
const { ListToolsRequestSchema, CallToolRequestSchema } = require('@modelcontextprotocol/sdk/types.js');
|
|
25
|
-
return { Server, ListToolsRequestSchema, CallToolRequestSchema };
|
|
26
|
-
} catch (error) {
|
|
27
|
-
throw new Error('createMcpServer needs @modelcontextprotocol/sdk: npm install @modelcontextprotocol/sdk', { cause: error });
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
21
|
/**
|
|
32
22
|
* @param {object} ws createWebSearch(…)
|
|
33
23
|
* @param {{ name?: string, version?: string, defaults?: object, maxChars?: number, browser?: boolean, screenshots?: boolean }} [options]
|
|
34
24
|
*/
|
|
35
25
|
function createMcpServer(ws, options = {}) {
|
|
36
26
|
if (!ws || typeof ws.search !== 'function' || typeof ws.read !== 'function') throw new Error('createMcpServer needs a web search from createWebSearch().');
|
|
37
|
-
const
|
|
38
|
-
const server = new Server(
|
|
27
|
+
const server = createProtocolServer(
|
|
39
28
|
{ name: options.name || 'astratra-web-search', version: options.version || require('../package.json').version },
|
|
40
29
|
{
|
|
41
30
|
capabilities: { tools: {} },
|
|
@@ -49,9 +38,9 @@ function createMcpServer(ws, options = {}) {
|
|
|
49
38
|
inputSchema: parameters,
|
|
50
39
|
annotations: { readOnlyHint: true, openWorldHint: true, destructiveHint: false }
|
|
51
40
|
}));
|
|
52
|
-
server.setRequestHandler(
|
|
53
|
-
server.setRequestHandler(
|
|
54
|
-
const { name, arguments: args } = request.params;
|
|
41
|
+
server.setRequestHandler('tools/list', async () => ({ tools }));
|
|
42
|
+
server.setRequestHandler('tools/call', async (request, extra) => {
|
|
43
|
+
const { name, arguments: args } = request.params || {};
|
|
55
44
|
try {
|
|
56
45
|
if ((name === 'browse_page' || name === 'search_site') && !offered.browser) throw Object.assign(new Error(`unknown tool ${name}`), { code: 'WEB_SEARCH_BAD_REQUEST' });
|
|
57
46
|
const { text, images } = await runTool(ws, name, args || {}, { signal: extra?.signal, defaults: options.defaults, maxChars: options.maxChars, screenshots: offered.screenshots });
|
package/src/mcpStdio.js
CHANGED
|
@@ -43,8 +43,8 @@ async function main() {
|
|
|
43
43
|
}
|
|
44
44
|
const browser = browserFromEnv(process.env);
|
|
45
45
|
const ws = createWebSearch({ providers, mode: process.env.WEB_SEARCH_MODE === 'fallback' ? 'fallback' : 'fuse', ...(browser ? { browser } : {}) });
|
|
46
|
-
const {
|
|
47
|
-
await createMcpServer(ws, { screenshots: process.env.WEB_BROWSER_SCREENSHOTS === '1' }).connect(
|
|
46
|
+
const { createStdioServerTransport } = require('@astratra/mcp');
|
|
47
|
+
await createMcpServer(ws, { screenshots: process.env.WEB_BROWSER_SCREENSHOTS === '1' }).connect(createStdioServerTransport());
|
|
48
48
|
process.stderr.write(`web-search: MCP server ready (${ws.providerIds.join(', ')}${browser ? ', browser' : ''}).\n`);
|
|
49
49
|
}
|
|
50
50
|
|
package/src/tools.js
CHANGED
|
@@ -229,6 +229,9 @@ function registryTools(ws, { roles, defaults, maxChars = 10_000, browser } = {})
|
|
|
229
229
|
}));
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
+
// Des noms seuls et des « ...require() » dans l'objet exporté : Node les lit sans exécuter le fichier, pour les
|
|
233
|
+
// imports nommés d'un module ES.
|
|
234
|
+
const TOOL_SCHEMAS = { web_search: SEARCH_SCHEMA, read_page: READ_SCHEMA, browse_page: BROWSE_SCHEMA, search_site: SITE_SEARCH_SCHEMA };
|
|
232
235
|
module.exports = {
|
|
233
236
|
toolDefinitions,
|
|
234
237
|
nativeTools,
|
|
@@ -236,5 +239,5 @@ module.exports = {
|
|
|
236
239
|
runTool,
|
|
237
240
|
searchArgs,
|
|
238
241
|
toolList,
|
|
239
|
-
TOOL_SCHEMAS
|
|
242
|
+
TOOL_SCHEMAS
|
|
240
243
|
};
|