Skip to main content
Glama

google_search

Perform web searches using Google Custom Search API, retrieving specific results based on query, number of results, and starting index for enhanced data extraction.

Instructions

Search the web using Google Custom Search API. Requires google_search_config resource.

Input Schema

NameRequiredDescriptionDefault
numNoNumber of results to return (1-10, optional)
queryYesSearch query
startNoIndex of the first result to return (optional)

Input Schema (JSON Schema)

{ "properties": { "num": { "description": "Number of results to return (1-10, optional)", "type": "number" }, "query": { "description": "Search query", "type": "string" }, "start": { "description": "Index of the first result to return (optional)", "type": "number" } }, "required": [ "query" ], "type": "object" }

Implementation Reference

  • src/index.ts:201-224 (registration)
    Registration of the 'google_search' tool in the ListTools response, including name, description, and input schema definition.
    { name: 'google_search', description: 'Search the web using Google Custom Search API. Requires google_search_config resource.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query, including any operators like site:, filetype:, etc.' }, num: { type: 'number', description: 'Number of results to return (1-10, optional)' }, start: { type: 'number', description: 'Index of the first result to return (optional)' } }, required: ['query'], additionalProperties: false, description: 'Search the web using Google Custom Search API.' }, },
  • Input schema for the google_search tool defining parameters: query (required), num, start.
    inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query, including any operators like site:, filetype:, etc.' }, num: { type: 'number', description: 'Number of results to return (1-10, optional)' }, start: { type: 'number', description: 'Index of the first result to return (optional)' } }, required: ['query'], additionalProperties: false, description: 'Search the web using Google Custom Search API.' },
  • Handler implementation for 'google_search' tool: validates args, checks env vars for API key/CX, calls Google Custom Search API, formats results (title, link, snippet), handles errors.
    } else if (toolName === 'google_search') { // Validate google_search arguments const isValidGoogleSearchArgs = (a: any): boolean => typeof a === 'object' && a !== null && typeof a.query === 'string' && (a.num === undefined || (typeof a.num === 'number' && a.num >= 1 && a.num <= 10)) && (a.start === undefined || typeof a.start === 'number'); if (!isValidGoogleSearchArgs(args)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid google_search arguments' ); } // Accept advanced arguments; apiKey/cx from resource const { query, num, start } = args as { query: string; num?: number; start?: number; }; // Use config from resource const apiKey = process.env.APIKEY_GOOGLE_SEARCH; const cx = process.env.CX_GOOGLE_SEARCH; if (!apiKey || !cx) { throw new McpError(ErrorCode.InvalidParams, 'Google Search API key and cx not set. Please set APIKEY_GOOGLE_SEARCH and CX_GOOGLE_SEARCH in environment variable.'); } const url = new URL('https://www.googleapis.com/customsearch/v1'); url.searchParams.set('key', apiKey); url.searchParams.set('cx', cx); url.searchParams.set('q', query); if (num !== undefined) url.searchParams.set('num', String(num)); if (start !== undefined) url.searchParams.set('start', String(start)); try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 20000); // Apply timeout manually const response = await fetch(url.toString(), { method: 'GET', headers: { 'Content-Type': 'application/json' }, signal: controller.signal // Use abort signal for timeout }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); // Parse JSON directly let formatted; if (data && Array.isArray(data.items)) { formatted = data.items.map((item: any) => ({ title: item.title, link: item.link, snippet: item.snippet, })); } else { formatted = data; // Fallback to full data if items not found } return { content: [ { type: 'text', text: JSON.stringify(formatted, null, 2), }, ], }; } catch (error: any) { console.error('Error calling google_search:', error); return { content: [ { type: 'text', text: `Error calling google_search: ${error.message}`, }, ], isError: true, }; } } else if (toolName === 'smart_command') {
  • Helper implementation of Google Search logic reused in 'smart_command' tool for non-URL queries.
    const apiKey = process.env.APIKEY_GOOGLE_SEARCH; const cx = process.env.CX_GOOGLE_SEARCH; if (!apiKey || !cx) { return { content: [{ type: 'text', text: 'Google Search API key and cx not set. Please set APIKEY_GOOGLE_SEARCH and CX_GOOGLE_SEARCH in environment variable.' }], isError: true }; } const url = new URL('https://www.googleapis.com/customsearch/v1'); url.searchParams.set('key', apiKey); url.searchParams.set('cx', cx); url.searchParams.set('q', command); try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 20000); // Apply timeout manually const response = await fetch(url.toString(), { method: 'GET', headers: { 'Content-Type': 'application/json' }, signal: controller.signal // Use abort signal for timeout }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); // Parse JSON directly let formatted; if (data && Array.isArray(data.items)) { formatted = data.items.map((item: any) => ({ title: item.title, link: item.link, snippet: item.snippet, })); } else { formatted = data; // Fallback to full data if items not found } return { content: [ { type: 'text', text: JSON.stringify(formatted, null, 2), }, ], }; } catch (error: any) { console.error('Error during Google Search:', error); return { content: [{ type: 'text', text: 'Error during Google Search: ' + error.message }], isError: true }; }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rayss868/MCP-Web-Curl'

If you have feedback or need assistance with the MCP directory API, please join our Discord server