web_search
Access real-time web information to verify facts or retrieve up-to-date data on any topic. Customize results with domain filtering and control the number of outputs for precise searches.
Instructions
Search the web for real-time information about any topic. Use this tool when you need up-to-date information that might not be available in your training data, or when you need to verify current facts.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| allowedDomains | No | Only include results from these domains | |
| blockedDomains | No | Never include results from these domains | |
| maxResults | No | Maximum number of search results to return (default: 5) | |
| query | Yes | The search query to look up on the web |
Input Schema (JSON Schema)
{
"properties": {
"allowedDomains": {
"description": "Only include results from these domains",
"items": {
"type": "string"
},
"type": "array"
},
"blockedDomains": {
"description": "Never include results from these domains",
"items": {
"type": "string"
},
"type": "array"
},
"maxResults": {
"description": "Maximum number of search results to return (default: 5)",
"type": "number"
},
"query": {
"description": "The search query to look up on the web",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:95-171 (handler)Handler for CallToolRequestSchema that implements the web_search tool by calling Anthropic Claude API with web search tool configuration, processes the results, and returns formatted search results.server.setRequestHandler(CallToolRequestSchema, async (request: any) => { try { const { name, arguments: args } = request.params; if (name !== "web_search") { throw new Error(`Unknown tool: ${name}`); } if (!args || typeof args !== 'object') { throw new Error("No arguments provided"); } const { query, maxResults = 5, allowedDomains, blockedDomains } = args as any; if (!query || typeof query !== 'string') { throw new Error("Invalid query parameter"); } // Prepare the web search tool configuration const webSearchTool = { type: "web_search_20250305", name: "web_search", max_uses: maxResults }; // Add domain filtering if provided if (allowedDomains && Array.isArray(allowedDomains) && allowedDomains.length > 0) { (webSearchTool as any).allowed_domains = allowedDomains; } if (blockedDomains && Array.isArray(blockedDomains) && blockedDomains.length > 0) { (webSearchTool as any).blocked_domains = blockedDomains; } // Create a Claude message with the web search const response = await anthropic.messages.create({ model: "claude-3-7-sonnet-latest", max_tokens: 1024, messages: [ { role: "user", content: query } ], // @ts-ignore - Ignoring TypeScript error since Claude API does support this tools: [webSearchTool] }); // Extract and format the search results let results = ""; for (const item of response.content) { if (item.type === 'text') { results += item.text + "\n\n"; } else if (item.type === 'web_search_tool_result' && 'content' in item && Array.isArray(item.content)) { results += "### Search Results\n\n"; for (const result of item.content) { if (result.type === 'web_search_result') { results += `- [${result.title}](${result.url})\n`; results += ` Last updated: ${result.page_age || 'Unknown'}\n\n`; } } } } // Return the formatted results return { content: [{ type: "text", text: results.trim() }] }; } catch (error) { console.error('Error performing web search:', error); return { content: [{ type: "text", text: `Error performing web search: ${(error as Error).message}` }], isError: true }; } });
- src/index.ts:41-72 (schema)Tool schema definition including name, description, and input schema for the web_search tool.const WEB_SEARCH_TOOL: Tool = { name: "web_search", description: "Search the web for real-time information about any topic. Use this tool when you need up-to-date information that might not be available in your training data, or when you need to verify current facts.", inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query to look up on the web" }, maxResults: { type: "number", description: "Maximum number of search results to return (default: 5)" }, allowedDomains: { type: "array", items: { type: "string" }, description: "Only include results from these domains" }, blockedDomains: { type: "array", items: { type: "string" }, description: "Never include results from these domains" } }, required: ["query"] } };
- src/index.ts:88-92 (registration)Registration of the web_search tool in the ListToolsRequestSchema handler.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [WEB_SEARCH_TOOL] }; });