save_answer_query_websearch
Retrieves answers to natural language queries using Google Search, processes them with Vertex AI models, and saves the results to a specified file path for easy access and storage.
Instructions
Answers a natural language query using Google Search results and saves the answer to a file. Uses the configured Vertex AI model (gemini-2.5-pro-exp-03-25). Requires 'query' and 'output_path'.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| output_path | Yes | The relative path where the generated answer should be saved. | |
| query | Yes | The natural language question to answer using web search. |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"output_path": {
"description": "The relative path where the generated answer should be saved.",
"type": "string"
},
"query": {
"description": "The natural language question to answer using web search.",
"type": "string"
}
},
"required": [
"query",
"output_path"
],
"type": "object"
}
Implementation Reference
- src/index.ts:380-402 (handler)Handler execution logic for the save_answer_query_websearch tool: parses arguments, invokes the tool's buildPrompt to generate AI prompt, calls the generative AI with web search enabled, saves the generated content to the specified output_path, and returns a success message.} else if (toolName === "save_answer_query_websearch") { const parsedArgs = SaveAnswerQueryWebsearchArgsSchema.parse(args); const { output_path } = parsedArgs; const config = getAIConfig(); const { systemInstructionText, userQueryText, useWebSearch, enableFunctionCalling } = toolDefinition.buildPrompt(args, config.modelId); const initialContents = buildInitialContent(systemInstructionText, userQueryText) as CombinedContent[]; const toolsForApi = getToolsForApi(enableFunctionCalling, useWebSearch); const generatedContent = await callGenerativeAI( initialContents, toolsForApi ); const validOutputPath = validateWorkspacePath(output_path); await fs.mkdir(path.dirname(validOutputPath), { recursive: true }); await fs.writeFile(validOutputPath, generatedContent, "utf-8"); return { content: [{ type: "text", text: `Successfully generated websearch answer and saved to ${output_path}` }], };
- Core tool definition including the buildPrompt function that constructs the detailed system and user prompts for answering queries via web search, tailored for comprehensive, cited, structured responses.export const saveAnswerQueryWebsearchTool: ToolDefinition = { name: "save_answer_query_websearch", description: `Answers a natural language query using Google Search results and saves the answer to a file. Uses the configured Vertex AI model (${modelIdPlaceholder}). Requires 'query' and 'output_path'.`, inputSchema: SaveAnswerQueryWebsearchJsonSchema as any, buildPrompt: (args: any, modelId: string) => { const parsed = SaveAnswerQueryWebsearchArgsSchema.safeParse(args); if (!parsed.success) { throw new McpError(ErrorCode.InvalidParams, `Invalid arguments for save_answer_query_websearch: ${parsed.error.errors.map(e => `${e.path.join('.')}: ${e.message}`).join(', ')}`); } const { query } = parsed.data; // output_path used in handler // --- Use Prompt Logic from answer_query_websearch.ts --- const base = `You are an AI assistant designed to answer questions accurately using provided search results. You are an EXPERT at synthesizing information from diverse sources into comprehensive, well-structured responses.`; const ground = ` Base your answer *only* on Google Search results relevant to "${query}". Synthesize information from search results into a coherent, comprehensive response that directly addresses the query. If search results are insufficient or irrelevant, explicitly state which aspects you cannot answer based on available information. Never add information not present in search results. When search results conflict, acknowledge the contradictions and explain different perspectives.`; const structure = ` Structure your response with clear organization: 1. Begin with a concise executive summary of 2-3 sentences that directly answers the main question. 2. For complex topics, use appropriate headings and subheadings to organize different aspects of the answer. 3. Present information from newest to oldest when dealing with evolving topics or current events. 4. Where appropriate, use numbered or bulleted lists to present steps, features, or comparative points. 5. For controversial topics, present multiple perspectives fairly with supporting evidence from search results. 6. Include a "Sources and Limitations" section at the end that notes the reliability of sources and any information gaps.`; const citation = ` Citation requirements: 1. Cite specific sources within your answer using [Source X] format. 2. Prioritize information from reliable, authoritative sources over random websites or forums. 3. For statistics, quotes, or specific claims, attribute the specific source. 4. Evaluate source credibility and recency - prefer official, recent sources for time-sensitive topics. 5. When search results indicate information might be outdated, explicitly note this limitation.`; const format = ` Format your answer in clean, readable Markdown: 1. Use proper headings (##, ###) for major sections. 2. Use **bold** for emphasis of key points. 3. Use \`code formatting\` for technical terms, commands, or code snippets when relevant. 4. Create tables for comparing multiple items or options. 5. Use blockquotes (>) for direct quotations from sources.`; const systemInstructionText = base + ground + structure + citation + format; const userQueryText = `I need a comprehensive answer to this question: "${query}" In your answer: 1. Thoroughly search for and evaluate ALL relevant information from search results 2. Synthesize information from multiple sources into a coherent, well-structured response 3. Present differing viewpoints fairly when sources disagree 4. Include appropriate citations to specific sources 5. Note any limitations in the available information 6. Organize your response logically with clear headings and sections 7. Use appropriate formatting to enhance readability Please provide your COMPLETE response addressing all aspects of my question.`; return { systemInstructionText: systemInstructionText, userQueryText: userQueryText, useWebSearch: true, // Always true for this tool enableFunctionCalling: false }; } };
- Zod schema defining input arguments: query (string) and output_path (string).export const SaveAnswerQueryWebsearchArgsSchema = z.object({ query: z.string().describe("The natural language question to answer using web search."), output_path: z.string().describe("The relative path where the generated answer should be saved.") });
- src/tools/index.ts:64-65 (registration)Registration of the saveAnswerQueryWebsearchTool in the allTools array for export and toolMap creation.saveAnswerQueryDirectTool, saveAnswerQueryWebsearchTool,