web_search
Search the web to retrieve up-to-date information on any topic, including news, blogs, companies, and events.
Instructions
Search the web for information. Use this tool when you need to search the web for information. You can use this tool to search for news, blogs, or all types of information. You can also use this tool to search for information about a specific company or product. You can also use this tool to search for information about a specific person. You can also use this tool to search for information about a specific product. You can also use this tool to search for information about a specific company. You can also use this tool to search for information about a specific event. You can also use this tool to search for information about a specific location. You can also use this tool to search for information about a specific thing. If you request search with 1 result number and failed, retry with bigger results number.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The search query to look up | |
| numResults | No | Number of results to return (default: 5) | |
| language | No | Language code for search results (e.g., 'en') | |
| region | No | Region code for search results (e.g., 'us') | |
| excludeDomains | No | Domains to exclude from results | |
| includeDomains | No | Only include these domains in results | |
| excludeTerms | No | Terms to exclude from results | |
| resultType | No | Type of results to return |
Implementation Reference
- src/index.ts:52-172 (registration)Registers the 'web_search' tool on the MCP server using server.tool() with tool name 'web_search', description, Zod schema for input params, and the async handler callback.
server.tool( "web_search", "Search the web for information.\n" + "Use this tool when you need to search the web for information.\n" + "You can use this tool to search for news, blogs, or all types of information.\n" + "You can also use this tool to search for information about a specific company or product.\n" + "You can also use this tool to search for information about a specific person.\n" + "You can also use this tool to search for information about a specific product.\n" + "You can also use this tool to search for information about a specific company.\n" + "You can also use this tool to search for information about a specific event.\n" + "You can also use this tool to search for information about a specific location.\n" + "You can also use this tool to search for information about a specific thing.\n" + "If you request search with 1 result number and failed, retry with bigger results number.", { query: z.string().describe("The search query to look up"), numResults: z .number() .optional() .describe( `Number of results to return (default: ${MAX_SEARCH_RESULT})` ), language: z .string() .optional() .describe("Language code for search results (e.g., 'en')"), region: z .string() .optional() .describe("Region code for search results (e.g., 'us')"), excludeDomains: z .array(z.string()) .optional() .describe("Domains to exclude from results"), includeDomains: z .array(z.string()) .optional() .describe("Only include these domains in results"), excludeTerms: z .array(z.string()) .optional() .describe("Terms to exclude from results"), resultType: z .enum(["all", "news", "blogs"]) .optional() .describe("Type of results to return"), }, async (params) => { try { console.error(`Performing web search for: ${params.query}`); // Prepare request payload for crawler API const requestPayload: CrawlRequest = { query: params.query, numResults: params.numResults ?? MAX_SEARCH_RESULT, language: params.language, region: params.region, filters: { excludeDomains: params.excludeDomains, includeDomains: params.includeDomains, excludeTerms: params.excludeTerms, resultType: params.resultType as "all" | "news" | "blogs", }, }; // Call the crawler API console.error(`Sending request to ${API_URL}/crawl`); const response = await axios.post<CrawlResponse>( `${API_URL}/crawl`, requestPayload ); // Format the response for the MCP client const results = response.data.results.map((result) => ({ title: result.title, snippet: result.excerpt, text: result.text, url: result.url, siteName: result.siteName || "", byline: result.byline || "", })); return { content: [ { type: "text", text: JSON.stringify( { query: response.data.query, results: results, }, null, 2 ), }, ], }; } catch (error) { console.error("Error performing web search:", error); if (axios.isAxiosError(error)) { const errorMessage = error.response?.data?.error || error.message; return { content: [{ type: "text", text: `Error: ${errorMessage}` }], isError: true, }; } return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : "Unknown error" }`, }, ], isError: true, }; } } ); - src/index.ts:65-97 (schema)Zod schema defining input parameters for web_search: query (string, required), numResults (optional number), language (optional string), region (optional string), excludeDomains (optional string array), includeDomains (optional string array), excludeTerms (optional string array), resultType (optional enum: 'all'|'news'|'blogs').
{ query: z.string().describe("The search query to look up"), numResults: z .number() .optional() .describe( `Number of results to return (default: ${MAX_SEARCH_RESULT})` ), language: z .string() .optional() .describe("Language code for search results (e.g., 'en')"), region: z .string() .optional() .describe("Region code for search results (e.g., 'us')"), excludeDomains: z .array(z.string()) .optional() .describe("Domains to exclude from results"), includeDomains: z .array(z.string()) .optional() .describe("Only include these domains in results"), excludeTerms: z .array(z.string()) .optional() .describe("Terms to exclude from results"), resultType: z .enum(["all", "news", "blogs"]) .optional() .describe("Type of results to return"), }, - src/index.ts:98-172 (handler)Async handler function that executes the web search logic: builds a CrawlRequest payload from params, calls an external crawler API via axios.post(), formats the response results (title, snippet, text, url, siteName, byline), and returns the formatted content as JSON string. Includes error handling for Axios errors and general exceptions.
async (params) => { try { console.error(`Performing web search for: ${params.query}`); // Prepare request payload for crawler API const requestPayload: CrawlRequest = { query: params.query, numResults: params.numResults ?? MAX_SEARCH_RESULT, language: params.language, region: params.region, filters: { excludeDomains: params.excludeDomains, includeDomains: params.includeDomains, excludeTerms: params.excludeTerms, resultType: params.resultType as "all" | "news" | "blogs", }, }; // Call the crawler API console.error(`Sending request to ${API_URL}/crawl`); const response = await axios.post<CrawlResponse>( `${API_URL}/crawl`, requestPayload ); // Format the response for the MCP client const results = response.data.results.map((result) => ({ title: result.title, snippet: result.excerpt, text: result.text, url: result.url, siteName: result.siteName || "", byline: result.byline || "", })); return { content: [ { type: "text", text: JSON.stringify( { query: response.data.query, results: results, }, null, 2 ), }, ], }; } catch (error) { console.error("Error performing web search:", error); if (axios.isAxiosError(error)) { const errorMessage = error.response?.data?.error || error.message; return { content: [{ type: "text", text: `Error: ${errorMessage}` }], isError: true, }; } return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : "Unknown error" }`, }, ], isError: true, }; } } ); - src/index.ts:8-10 (helper)Configuration constants: API_URL (from env, default http://localhost:3001) and MAX_SEARCH_RESULT (from env, default 5) used by the web_search tool.
// Configuration const API_URL = process.env.API_URL || "http://localhost:3001"; const MAX_SEARCH_RESULT = parseInt(process.env.MAX_SEARCH_RESULT || "5", 10); - src/index.ts:13-41 (helper)TypeScript interfaces (CrawlRequest, CrawlResult, CrawlResponse) defining the data structures used by the web_search tool when communicating with the backend crawler API.
interface CrawlRequest { query: string; numResults?: number; language?: string; region?: string; filters?: { excludeDomains?: string[]; includeDomains?: string[]; excludeTerms?: string[]; resultType?: "all" | "news" | "blogs"; }; } interface CrawlResult { url: string; title: string; excerpt: string; text?: string; html?: string; siteName?: string; byline?: string; error?: string | null; } interface CrawlResponse { query: string; results: CrawlResult[]; error: string | null; }