UNPKG

@andrewlwn77/google-trends-mcp

Version:

MCP server for Google Trends API integration - Access trending topics, questions, and search data

204 lines 7.06 kB
#!/usr/bin/env node import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js'; import axios, { AxiosError } from 'axios'; import * as dotenv from 'dotenv'; dotenv.config(); const API_KEY = process.env.GOOGLE_TRENDS_API_KEY || '44a4da87d0msh5a2369bfd18fae9p1b5245jsne0f4b09f2132'; if (!API_KEY) { console.error('Error: GOOGLE_TRENDS_API_KEY environment variable is required'); console.error('Please set it in your .env file or environment'); console.error('Get your API key from: https://rapidapi.com/google-trends-scraper1/api/google-trends-scraper1'); process.exit(1); } const API_HOST = 'google-trends-scraper1.p.rapidapi.com'; class GoogleTrendsMcpServer { server; constructor() { this.server = new Server({ name: 'google-trends-mcp', version: '1.0.0', }, { capabilities: { tools: {}, }, }); this.setupHandlers(); } setupHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: this.getTools(), })); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { switch (name) { case 'google_trends_status': return await this.handleStatus(args); case 'google_trends_questions': return await this.handleQuestions(args); case 'google_trends_trending': return await this.handleTrending(args); default: throw new Error(`Unknown tool: ${name}`); } } catch (error) { const apiError = this.handleError(error); return { content: [ { type: 'text', text: `Error: ${apiError.message}`, }, ], isError: true, }; } }); } getTools() { return [ { name: 'google_trends_status', description: 'Check the status of the Google Trends API', inputSchema: { type: 'object', properties: { language: { type: 'string', description: 'Language code (e.g., "en")', default: 'en', }, country: { type: 'string', description: 'Country code (e.g., "US")', default: 'US', }, }, required: [], }, }, { name: 'google_trends_questions', description: 'Get trending questions from Google Trends', inputSchema: { type: 'object', properties: { language: { type: 'string', description: 'Language code (e.g., "en")', default: 'en', }, country: { type: 'string', description: 'Country code (e.g., "US")', default: 'US', }, }, required: [], }, }, { name: 'google_trends_trending', description: 'Get currently trending topics from Google Trends', inputSchema: { type: 'object', properties: { language: { type: 'string', description: 'Language code (e.g., "en")', default: 'en', }, country: { type: 'string', description: 'Country code (e.g., "US")', default: 'US', }, }, required: [], }, }, ]; } async makeApiRequest(endpoint, params) { const response = await axios.get(`https://${API_HOST}/${endpoint}`, { params, headers: { 'x-rapidapi-key': API_KEY, 'x-rapidapi-host': API_HOST, }, }); return response.data; } async handleStatus(_params) { const data = await this.makeApiRequest('status'); const response = { status: data || { status: 'unknown' }, }; return { content: [ { type: 'text', text: JSON.stringify(response, null, 2), }, ], }; } async handleQuestions(_params) { const data = await this.makeApiRequest('questions'); const response = { questions: data.questions || data || [], }; return { content: [ { type: 'text', text: JSON.stringify(response, null, 2), }, ], }; } async handleTrending(_params) { const data = await this.makeApiRequest('trending'); const response = { trending: data.trending || data || [], }; return { content: [ { type: 'text', text: JSON.stringify(response, null, 2), }, ], }; } handleError(error) { if (error instanceof AxiosError) { return { error: 'API Error', message: error.response?.data?.message || error.message, statusCode: error.response?.status, }; } if (error instanceof Error) { return { error: 'Server Error', message: error.message, }; } return { error: 'Unknown Error', message: 'An unexpected error occurred', }; } async run() { const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('Google Trends MCP server running on stdio'); } } const server = new GoogleTrendsMcpServer(); server.run().catch(console.error); //# sourceMappingURL=index.js.map