web_search
Search the web through a privacy-respecting metasearch engine, returning results in human-readable text or structured JSON format with filtering options.
Instructions
Perform a web search using SearxNG and return formatted results.
Results are returned in either text format (human-readable) or JSON format depending on the result_format parameter selected.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The search query string to look for on the web | |
| result_count | No | Maximum number of results to return | |
| categories | No | Categories to filter by (e.g., 'general', 'images', 'news', 'videos') | |
| language | No | Language code for results (e.g., 'all', 'en', 'ru', 'fr') | all |
| time_range | No | Time restriction for results | |
| result_format | No | Output format - 'text' for human-readable, 'json' for structured data | text |
Implementation Reference
- src/searxng_simple_mcp/server.py:67-122 (handler)The core handler function for the 'web_search' tool. Decorated with @mcp.tool() for automatic registration in FastMCP. Handles input parameters (defining the schema), performs the search via SearxNGClient, formats results in text or JSON, and includes error handling.@mcp.tool() async def web_search( query: str = _QUERY_FIELD, result_count: int = _RESULT_COUNT_FIELD, categories: list[str] | None = _CATEGORIES_FIELD, language: str | None = _LANGUAGE_FIELD, time_range: Literal["day", "week", "month", "year"] | None = _TIME_RANGE_FIELD, result_format: Literal["text", "json"] = _RESULT_FORMAT_FIELD, ctx: Context | None = None, ) -> str | list[SearxngResult]: """ Perform a web search using SearxNG and return formatted results. Results are returned in either text format (human-readable) or JSON format depending on the result_format parameter selected. """ try: # Inform about the search operation if ctx: await ctx.info("Starting web search...") # Validate inputs if result_count <= 0: raise ValueError("result_count must be greater than 0") # Perform the search using SearxNG response = await searxng_client.search( query=query, categories=categories, language=language, time_range=time_range, ) results = response.results[:result_count] # Format results according to requested format if result_format == "json": # Return JSON-formatted results return results else: # Return human-readable text formatted_results = [] for i, result in enumerate(results, 1): title = result.title url = result.url content = result.content[:200] + "..." formatted_results.append(f"{i}. [{title}]({url})\n {content}") return "\n\n".join(formatted_results) except Exception as e: logger.error(f"Error during web search: {e}") if ctx: await ctx.info(f"Error during search: {e}") raise