ollama_web_search
Search the web to augment AI models with current information, reducing inaccuracies by providing up-to-date data for queries.
Instructions
Perform a web search using Ollama's web search API. Augments models with latest information to reduce hallucinations. Requires OLLAMA_API_KEY environment variable.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The search query string | |
| max_results | No | Maximum number of results to return (1-10, default 5) | |
| format | No | json |
Implementation Reference
- src/tools/web-search.ts:13-59 (handler)Implements the core logic for performing web search via Ollama's API endpoint, including API key check, HTTP POST request with retry and timeout, error handling, and response formatting.export async function webSearch( ollama: Ollama, query: string, maxResults: number, format: ResponseFormat ): Promise<string> { // Web search requires direct API call as it's not in the SDK const apiKey = process.env.OLLAMA_API_KEY; if (!apiKey) { throw new Error( 'OLLAMA_API_KEY environment variable is required for web search' ); } return retryWithBackoff( async () => { const response = await fetchWithTimeout( 'https://ollama.com/api/web_search', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}`, }, body: JSON.stringify({ query, max_results: maxResults, }), }, WEB_API_TIMEOUT ); if (!response.ok) { const retryAfter = response.headers.get('retry-after') ?? undefined; throw new HttpError( `Web search failed: ${response.status} ${response.statusText}`, response.status, retryAfter ); } const data = await response.json(); return formatResponse(JSON.stringify(data), format); }, WEB_API_RETRY_CONFIG ); }
- src/schemas.ts:208-214 (schema)Zod schema used for input validation in the ollama_web_search tool handler.* Schema for ollama_web_search tool */ export const WebSearchInputSchema = z.object({ query: z.string().min(1), max_results: z.number().int().min(1).max(10).default(5), format: ResponseFormatSchema.default('json'), });
- src/tools/web-search.ts:61-89 (registration)Tool definition export that registers the ollama_web_search tool with MCP, including metadata, inline input schema, and handler that validates args and delegates to webSearch implementation.export const toolDefinition: ToolDefinition = { name: 'ollama_web_search', description: 'Perform a web search using Ollama\'s web search API. Augments models with latest information to reduce hallucinations. Requires OLLAMA_API_KEY environment variable.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The search query string', }, max_results: { type: 'number', description: 'Maximum number of results to return (1-10, default 5)', default: 5, }, format: { type: 'string', enum: ['json', 'markdown'], default: 'json', }, }, required: ['query'], }, handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = WebSearchInputSchema.parse(args); return webSearch(ollama, validated.query, validated.max_results, format); }, };