web_search
Search the web using the SearXNG API to gather diverse information, news, articles, and online content. Filter results by language, time range, and safesearch for precise query outcomes.
Instructions
Performs a web search using the SearXNG API, ideal for general queries, news, articles, and online content. Use this for broad information gathering, recent events, or when you need diverse web sources.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | Number of results per page (default: 10) | |
| language | No | Language code for search results (e.g., 'en', 'fr', 'de'). Default is instance-dependent. | |
| pageno | No | Search page number (starts at 1) | |
| query | Yes | The search query. This is the main input for the web search | |
| safesearch | No | Safe search filter level (0: None, 1: Moderate, 2: Strict) (default: 0) | |
| time_range | No | Time range of search (day, month, year) |
Implementation Reference
- index.ts:125-178 (handler)Core handler function that executes the web_search tool by querying the SearXNG API, processing parameters, fetching JSON results, and formatting them into a text response.async function performWebSearch( query: string, pageno: number = 1, count: number = 10, time_range?: string, language: string = "all", safesearch?: string ) { const searxngUrl = process.env.SEARXNG_URL || "http://localhost:8080"; const url = new URL(`${searxngUrl}/search`); url.searchParams.set("q", query); url.searchParams.set("format", "json"); url.searchParams.set("pageno", pageno.toString()); url.searchParams.set("count", count.toString()); if ( time_range !== undefined && ["day", "month", "year"].includes(time_range) ) { url.searchParams.set("time_range", time_range); } if (language && language !== "all") { url.searchParams.set("language", language); } if (safesearch !== undefined && ["0", "1", "2"].includes(safesearch)) { url.searchParams.set("safesearch", safesearch); } const response = await fetch(url.toString(), { method: "GET", }); if (!response.ok) { throw new Error( `SearXNG API error: ${response.status} ${ response.statusText }\n${await response.text()}` ); } const data = (await response.json()) as SearXNGWeb; const results = (data.results || []).map((result) => ({ title: result.title || "", content: result.content || "", url: result.url || "", })); return results .map((r) => `Title: ${r.title}\nDescription: ${r.content}\nURL: ${r.url}`) .join("\n\n"); }
- index.ts:17-59 (schema)Tool definition including name, description, and detailed inputSchema for the web_search tool.const WEB_SEARCH_TOOL: Tool = { name: "web_search", description: "Performs a web search using the SearXNG API, ideal for general queries, news, articles, and online content. " + "Use this for broad information gathering, recent events, or when you need diverse web sources.", inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query. This is the main input for the web search", }, pageno: { type: "number", description: "Search page number (starts at 1)", default: 1, }, count: { type: "number", description: "Number of results per page (default: 10)", default: 10, }, time_range: { type: "string", description: "Time range of search (day, month, year)", enum: ["day", "month", "year"], }, language: { type: "string", description: "Language code for search results (e.g., 'en', 'fr', 'de'). Default is instance-dependent.", }, safesearch: { type: "string", description: "Safe search filter level (0: None, 1: Moderate, 2: Strict) (default: 0)", enum: ["0", "1", "2"], }, }, required: ["query"], }, };
- index.ts:88-91 (registration)Registers the web_search tool in the MCP server capabilities, referencing its description and schema.web_search: { description: WEB_SEARCH_TOOL.description, schema: WEB_SEARCH_TOOL.inputSchema, },
- index.ts:109-123 (helper)Type guard function to validate arguments for the web_search tool.function isSearXNGWebSearchArgs(args: unknown): args is { query: string; pageno?: number; count?: number; time_range?: string; language?: string; safesearch?: string; } { return ( typeof args === "object" && args !== null && "query" in args && typeof (args as { query: string }).query === "string" ); }
- index.ts:258-282 (registration)Dispatch logic in the CallToolRequestSchema handler that validates arguments and invokes the performWebSearch function for web_search.if (name === "web_search") { if (!isSearXNGWebSearchArgs(args)) { throw new Error("Invalid arguments for web_search"); } const { query, pageno = 1, count = 10, time_range, language = "all", safesearch, } = args; const results = await performWebSearch( query, pageno, count, time_range, language, safesearch ); return { content: [{ type: "text", text: results }], isError: false, }; }