duckduckgo_web_search
Perform web searches using DuckDuckGo to gather diverse information, news, articles, and online content with support for filtering, region-specific results, and SafeSearch controls.
Instructions
Performs a web search using the DuckDuckGo, ideal for general queries, news, articles, and online content. Use this for broad information gathering, recent events, or when you need diverse web sources. Supports content filtering and region-specific searches. Maximum 20 results per request.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | Number of results (1-20, default 10) | |
| query | Yes | Search query (max 400 chars) | |
| safeSearch | No | SafeSearch level (strict, moderate, off) | moderate |
Implementation Reference
- src/index.ts:154-199 (handler)Core handler function that performs the DuckDuckGo web search. Calls DDG.search, handles rate limiting via checkRateLimit, processes results, and returns formatted Markdown output.async function performWebSearch( query: string, count: number = CONFIG.search.defaultResults, safeSearch: "strict" | "moderate" | "off" = CONFIG.search.defaultSafeSearch ): Promise<string> { console.error( `[DEBUG] Performing search - Query: "${query}", Count: ${count}, SafeSearch: ${safeSearch}` ); try { checkRateLimit(); const safeSearchMap = { strict: DDG.SafeSearchType.STRICT, moderate: DDG.SafeSearchType.MODERATE, off: DDG.SafeSearchType.OFF, }; const searchResults = await DDG.search(query, { safeSearch: safeSearchMap[safeSearch], }); if (searchResults.noResults) { console.error(`[INFO] No results found for query: "${query}"`); return `# DuckDuckGo 搜索结果\n没有找到与 "${query}" 相关的结果。`; } const results: SearchResult[] = searchResults.results .slice(0, count) .map((result: DDG.SearchResult) => ({ title: result.title, description: result.description || result.title, url: result.url, })); console.error( `[INFO] Found ${results.length} results for query: "${query}"` ); // 格式化结果 return formatSearchResults(query, results); } catch (error) { console.error(`[ERROR] Search failed - Query: "${query}"`, error); throw error; } }
- src/index.ts:56-80 (schema)JSON Schema definition for the tool's input parameters, including query, count, and safeSearch with constraints and defaults.inputSchema: { type: "object", properties: { query: { type: "string", description: `Search query (max ${CONFIG.search.maxQueryLength} chars)`, maxLength: CONFIG.search.maxQueryLength, }, count: { type: "number", description: `Number of results (1-${CONFIG.search.maxResults}, default ${CONFIG.search.defaultResults})`, minimum: 1, maximum: CONFIG.search.maxResults, default: CONFIG.search.defaultResults, }, safeSearch: { type: "string", description: "SafeSearch level (strict, moderate, off)", enum: ["strict", "moderate", "off"], default: CONFIG.search.defaultSafeSearch, }, }, required: ["query"], }, };
- src/index.ts:225-227 (registration)Registers the tool by including it in the list returned for ListTools requests.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [WEB_SEARCH_TOOL], }));
- src/index.ts:243-259 (handler)Tool dispatcher case in the CallToolRequest handler that validates arguments and invokes the performWebSearch function.case "duckduckgo_web_search": { if (!isDuckDuckGoWebSearchArgs(args)) { throw new Error("Invalid arguments for duckduckgo_web_search"); } const { query, count = CONFIG.search.defaultResults, safeSearch = CONFIG.search.defaultSafeSearch, } = args; const results = await performWebSearch(query, count, safeSearch); return { content: [{ type: "text", text: results }], isError: false, }; }
- src/index.ts:204-222 (helper)Helper function to format search results into a structured Markdown response.function formatSearchResults(query: string, results: SearchResult[]): string { const formattedResults = results .map((r: SearchResult) => { return `### ${r.title} ${r.description} 🔗 [阅读更多](${r.url}) `; }) .join("\n\n"); return `# DuckDuckGo 搜索结果 ${query} 的搜索结果(${results.length}件) --- ${formattedResults} `; }