UNPKG

brave-search-mcp

Version:

MCP Server that uses Brave Search API to search for images, general web search, video, news and points of interest.

51 lines (50 loc) 2.41 kB
import { SafeSearchLevel } from 'brave-search/dist/types.js'; import { z } from 'zod'; import { BaseTool } from './BaseTool.js'; const webSearchInputSchema = z.object({ query: z.string().describe('The term to search the internet for'), count: z.number().min(1).max(20).default(10).optional().describe('The number of results to return, minimum 1, maximum 20'), offset: z.number().min(0).default(0).optional().describe('The offset for pagination, minimum 0'), freshness: z.union([ z.enum(['pd', 'pw', 'pm', 'py']), z.string().regex(/^\d{4}-\d{2}-\d{2}to\d{4}-\d{2}-\d{2}$/, 'Date range must be in format YYYY-MM-DDtoYYYY-MM-DD') ]) .optional() .describe(`Filters search results by when they were discovered. The following values are supported: - pd: Discovered within the last 24 hours. - pw: Discovered within the last 7 Days. - pm: Discovered within the last 31 Days. - py: Discovered within the last 365 Days. - YYYY-MM-DDtoYYYY-MM-DD: Custom date range (e.g., 2022-04-01to2022-07-30)`), }); export class BraveWebSearchTool extends BaseTool { braveMcpServer; braveSearch; name = 'brave_web_search'; description = 'Performs a web search using the Brave Search API, ideal for general queries, and online content. ' + 'Use this for broad information gathering, recent events, or when you need diverse web sources. ' + 'Maximum 20 results per request '; inputSchema = webSearchInputSchema; constructor(braveMcpServer, braveSearch) { super(); this.braveMcpServer = braveMcpServer; this.braveSearch = braveSearch; } async executeCore(input) { const { query, count, offset, freshness } = input; const results = await this.braveSearch.webSearch(query, { count, offset, safesearch: SafeSearchLevel.Strict, ...(freshness ? { freshness } : {}), }); if (!results.web || results.web?.results.length === 0) { this.braveMcpServer.log(`No results found for "${query}"`); const text = `No results found for "${query}"`; return { content: [{ type: 'text', text }] }; } const text = results.web.results.map(result => `Title: ${result.title}\nURL: ${result.url}\nDescription: ${result.description}`).join('\n\n'); return { content: [{ type: 'text', text }] }; } }