UNPKG

mcp-brave-search

Version:

Brave Search MCP Server - Web and News Search via stdio

120 lines โ€ข 4.64 kB
/** * Manual test for MCP server tool calls * This simulates how Claude Desktop would call the MCP tools */ import { BraveSearchAPI } from '../build/brave-api.js'; async function simulateToolCall(toolName, args) { console.log(`\n๐Ÿ“ž Calling tool: ${toolName}`); console.log('Arguments:', JSON.stringify(args, null, 2)); const apiKey = process.env.BRAVE_API_KEY; if (!apiKey) { throw new Error('BRAVE_API_KEY environment variable is required'); } const braveAPI = new BraveSearchAPI({ apiKey }); try { if (toolName === 'brave_web_search') { const params = args; if (!params.q || params.q.trim().length === 0) { throw new Error('Search query "q" is required and cannot be empty'); } const results = await braveAPI.webSearch(params); const webResults = results.web?.results || []; const formattedResults = webResults.map((result, index) => ({ position: index + 1, title: result.title, url: result.url, description: result.description || 'No description available', age: result.age, })); const response = { query: results.query.original, total_results: webResults.length, results: formattedResults, }; console.log('\nโœ… Tool call successful!'); console.log('Response:', JSON.stringify(response, null, 2)); return response; } if (toolName === 'brave_news_search') { const params = args; if (!params.q || params.q.trim().length === 0) { throw new Error('Search query "q" is required and cannot be empty'); } const results = await braveAPI.newsSearch(params); const formattedResults = results.results.map((result, index) => ({ position: index + 1, title: result.title, url: result.url, description: result.description || 'No description available', age: result.age, source: result.meta_url?.hostname, })); const response = { query: results.query.original, total_results: results.results.length, results: formattedResults, }; console.log('\nโœ… Tool call successful!'); console.log('Response:', JSON.stringify(response, null, 2)); return response; } throw new Error(`Unknown tool: ${toolName}`); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); console.error('\nโŒ Tool call failed:', errorMessage); throw error; } } async function runMCPTests() { console.log('๐Ÿงช Testing MCP Tool Calls...\n'); try { // Test 1: brave_web_search console.log('\n--- Test 1: Web Search Tool ---'); await simulateToolCall('brave_web_search', { q: 'MCP protocol examples', count: 3, }); // Test 2: brave_web_search with filters console.log('\n--- Test 2: Web Search with Filters ---'); await simulateToolCall('brave_web_search', { q: 'TypeScript async programming', count: 5, country: 'US', search_lang: 'en', freshness: 'pm', }); // Test 3: brave_news_search console.log('\n--- Test 3: News Search Tool ---'); await simulateToolCall('brave_news_search', { q: 'artificial intelligence', count: 5, freshness: 'pd', }); // Test 4: brave_news_search with country filter console.log('\n--- Test 4: News Search with Country Filter ---'); await simulateToolCall('brave_news_search', { q: 'technology innovation', count: 5, country: 'US', }); // Test 5: Error handling - empty query console.log('\n--- Test 5: Error Handling (Empty Query) ---'); try { await simulateToolCall('brave_web_search', { q: '', count: 5, }); } catch (error) { console.log('โœ“ Empty query error handled correctly'); } console.log('\n\n๐ŸŽ‰ All MCP tool tests completed!\n'); } catch (error) { console.error('\n๐Ÿ’ฅ MCP tests failed:', error); process.exit(1); } } runMCPTests(); //# sourceMappingURL=test-mcp.js.map