UNPKG

markov-exa-mcp-server

Version:

A Model Context Protocol server with Exa for web search, academic paper search, and Twitter/X.com search. Provides real-time web searches with configurable tool selection, allowing users to enable or disable specific search capabilities. Supports customiz

84 lines (83 loc) 3.87 kB
import { z } from "zod"; import axios from "axios"; import { API_CONFIG } from "./config.js"; import { createRequestLogger } from "../utils/logger.js"; export function registerWikipediaSearchTool(server, config) { server.tool("wikipedia_search_exa", "Search Wikipedia articles using Exa AI - finds comprehensive, factual information from Wikipedia entries. Ideal for research, fact-checking, and getting authoritative information on various topics.", { query: z.string().describe("Wikipedia search query (topic, person, place, concept, etc.)"), numResults: z.number().optional().describe("Number of Wikipedia articles to return (default: 5)") }, async ({ query, numResults }) => { const requestId = `wikipedia_search_exa-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`; const logger = createRequestLogger(requestId, 'wikipedia_search_exa'); logger.start(query); try { // Create a fresh axios instance for each request const axiosInstance = axios.create({ baseURL: API_CONFIG.BASE_URL, headers: { 'accept': 'application/json', 'content-type': 'application/json', 'x-api-key': config?.exaApiKey || process.env.EXA_API_KEY || '' }, timeout: 25000 }); const searchRequest = { query: `${query} Wikipedia`, type: "neural", numResults: numResults || API_CONFIG.DEFAULT_NUM_RESULTS, contents: { text: { maxCharacters: API_CONFIG.DEFAULT_MAX_CHARACTERS }, livecrawl: 'preferred' }, includeDomains: ["en.wikipedia.org", "en.m.wikipedia.org"] }; logger.log("Sending request to Exa API for Wikipedia search"); const response = await axiosInstance.post(API_CONFIG.ENDPOINTS.SEARCH, searchRequest, { timeout: 25000 }); logger.log("Received response from Exa API"); if (!response.data || !response.data.results) { logger.log("Warning: Empty or invalid response from Exa API"); return { content: [{ type: "text", text: "No Wikipedia articles found. Please try a different query." }] }; } logger.log(`Found ${response.data.results.length} Wikipedia articles`); const result = { content: [{ type: "text", text: JSON.stringify(response.data, null, 2) }] }; logger.complete(); return result; } catch (error) { logger.error(error); if (axios.isAxiosError(error)) { // Handle Axios errors specifically const statusCode = error.response?.status || 'unknown'; const errorMessage = error.response?.data?.message || error.message; logger.log(`Axios error (${statusCode}): ${errorMessage}`); return { content: [{ type: "text", text: `Wikipedia search error (${statusCode}): ${errorMessage}` }], isError: true, }; } // Handle generic errors return { content: [{ type: "text", text: `Wikipedia search error: ${error instanceof Error ? error.message : String(error)}` }], isError: true, }; } }); }