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.

83 lines (82 loc) 3.32 kB
import axios from 'axios'; import { SafeSearchLevel } from 'brave-search/dist/types.js'; import { z } from 'zod'; import { formatVideoResults } from '../utils.js'; import { BaseTool } from './BaseTool.js'; // end workaround const videoSearchInputSchema = z.object({ query: z.string().describe('The term to search the internet for videos of'), count: z.number().min(1).max(20).default(10).optional().describe('The number of results to return, minimum 1, maximum 20'), 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 BraveVideoSearchTool extends BaseTool { braveMcpServer; braveSearch; apiKey; name = 'brave_video_search'; description = 'Searches for videos using the Brave Search API. ' + 'Use this for video content, tutorials, or any media-related queries. ' + 'Returns a list of videos with titles, URLs, and descriptions. ' + 'Maximum 20 results per request.'; inputSchema = videoSearchInputSchema; baseUrl = 'https://api.search.brave.com/res/v1'; constructor(braveMcpServer, braveSearch, apiKey) { super(); this.braveMcpServer = braveMcpServer; this.braveSearch = braveSearch; this.apiKey = apiKey; } async executeCore(input) { const { query, count, freshness } = input; const videoSearchResults = await this.videoSearch(query, { count, safesearch: SafeSearchLevel.Strict, ...(freshness ? { freshness } : {}), }); if (!videoSearchResults.results || videoSearchResults.results.length === 0) { this.braveMcpServer.log(`No video results found for "${query}"`); const text = `No video results found for "${query}"`; return { content: [{ type: 'text', text }] }; } const text = formatVideoResults(videoSearchResults.results); return { content: [{ type: 'text', text }] }; } // workaround for https://github.com/erik-balfe/brave-search/pull/4 // not being merged yet into brave-search async videoSearch(query, options = {}) { const response = await axios.get(`${this.baseUrl}/videos/search?`, { params: { q: query, ...this.formatOptions(options), }, headers: this.getHeaders(), }); return response.data; } formatOptions(options) { return Object.entries(options).reduce((acc, [key, value]) => { if (value !== undefined) { acc[key] = value.toString(); } return acc; }, {}); } getHeaders() { return { 'Accept': 'application/json', 'Accept-Encoding': 'gzip', 'X-Subscription-Token': this.apiKey, }; } }