UNPKG

mcp-omnisearch

Version:

MCP server for integrating Omnisearch with LLMs

48 lines (47 loc) 2.46 kB
import { http_json } from '../../../common/http.js'; import { apply_search_operators, handle_provider_error, parse_search_operators, retry_with_backoff, sanitize_query, validate_api_key, } from '../../../common/utils.js'; import { config } from '../../../config/env.js'; export class TavilySearchProvider { constructor() { this.name = 'tavily'; this.description = 'Search the web using Tavily Search API. Best for factual queries requiring reliable sources and citations. Supports domain filtering through API parameters (include_domains/exclude_domains). Provides high-quality results for technical, scientific, and academic topics. Use when you need verified information with strong citation support.'; } async search(params) { const api_key = validate_api_key(config.search.tavily.api_key, this.name); // Parse search operators from the query const parsed_query = parse_search_operators(params.query); const search_params = apply_search_operators(parsed_query); const search_request = async () => { try { // Only use officially supported parameters const request_body = { query: sanitize_query(params.query), // Use original query without operators max_results: params.limit ?? 5, include_domains: params.include_domains ?? [], exclude_domains: params.exclude_domains ?? [], search_depth: 'basic', topic: 'general', }; const data = await http_json(this.name, `${config.search.tavily.base_url}/search`, { method: 'POST', headers: { Authorization: `Bearer ${api_key}`, 'Content-Type': 'application/json', }, body: JSON.stringify(request_body), }); return (data.results || []).map((result) => ({ title: result.title, url: result.url, snippet: result.content, score: result.score, source_provider: this.name, })); } catch (error) { handle_provider_error(error, this.name, 'fetch search results'); } }; return retry_with_backoff(search_request); } }