generate_with_search
Generate text responses grounded in current Google Search results to provide up-to-date, cited information for user queries.
Instructions
Generate text with Google Search grounding for up-to-date, cited responses.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | The prompt to search and answer | |
| model | No | Gemini model to use | gemini-2.5-flash |
| temperature | No | Sampling temperature | |
| maxOutputTokens | No | Maximum output tokens |
Implementation Reference
- src/tools/generate-with-search.ts:7-42 (handler)Main implementation of generate_with_search tool. Contains the register function that defines the tool with its schema, annotations, and the async handler function that calls Google GenAI's generateContent with Google Search grounding.
export function register(server: McpServer, ai: GoogleGenAI): void { server.registerTool( 'generate_with_search', { title: 'Generate with Search', description: 'Generate text with Google Search grounding for up-to-date, cited responses.', inputSchema: { prompt: z.string().min(1).describe('The prompt to search and answer'), model: TextModel.default('gemini-2.5-flash').describe('Gemini model to use'), temperature: z.number().min(0).max(2).optional().describe('Sampling temperature'), maxOutputTokens: z.number().min(1).optional().describe('Maximum output tokens'), }, annotations: { readOnlyHint: true, destructiveHint: false, openWorldHint: true, }, }, async ({ prompt, model, temperature, maxOutputTokens }) => { try { const response = await ai.models.generateContent({ model, contents: prompt, config: { temperature, maxOutputTokens, tools: [{ googleSearch: {} }], }, }); return { content: [{ type: 'text' as const, text: response.text ?? '' }] }; } catch (error) { return formatToolError(error); } }, ); } - src/types.ts:3-10 (schema)TextModel schema definition used by generate_with_search tool's input validation. Defines the allowed Gemini model options for text generation.
export const TextModel = z.enum([ 'gemini-2.5-flash', 'gemini-2.5-pro', 'gemini-3-flash-preview', 'gemini-3-pro-preview', 'gemini-3.1-pro-preview', ]); export type TextModel = z.infer<typeof TextModel>; - src/index.ts:6-27 (registration)Registration of generate_with_search tool. Imports the register function and calls it to register the tool with the MCP server instance.
import { register as registerGenerateWithSearch } from './tools/generate-with-search.js'; import { register as registerCodeExecution } from './tools/code-execution.js'; import { register as registerGenerateImage } from './tools/generate-image.js'; import { register as registerEditImage, registerMulti as registerEditImageMulti } from './tools/edit-image.js'; const GEMINI_API_KEY = process.env.GEMINI_API_KEY; if (!GEMINI_API_KEY) { console.error('GEMINI_API_KEY environment variable is required'); process.exit(1); } const ai = createClient(GEMINI_API_KEY); const server = new McpServer( { name: 'gemini', version: '2.0.0' }, { capabilities: { logging: {} } }, ); // Register all 7 tools registerGenerateText(server, ai); registerChat(server, ai); registerGenerateWithSearch(server, ai); - src/utils/errors.ts:1-7 (helper)Utility function used by generate_with_search handler to format errors consistently. Called in the catch block to return error responses.
export function formatToolError(error: unknown) { const text = error instanceof Error ? error.message : String(error); return { content: [{ type: 'text' as const, text }], isError: true, }; }