Class WebSearchTool
A built-in tool for performing web searches using various search providers.
Enables agents to search the web for information using DuckDuckGo, Brave Search, SearXNG, Tavily, Serper, or custom search providers.
Production note: the default DuckDuckGo provider scrapes the public no-JavaScript HTML endpoint and is rate-limited aggressively by anti-bot protections. It is suitable for local development and ad-hoc queries only. For production agent workloads, use Brave, Tavily, or Serper (all offer free tiers and require an API key).
public sealed class WebSearchTool : IBuiltInTool, ITool, IToolMetadata, IDisposable
- Inheritance
-
WebSearchTool
- Implements
- Inherited Members
Examples
Example: Basic usage with DuckDuckGo (no API key)
// DuckDuckGo - no API key required
var webSearch = new WebSearchTool();
registry.Register(webSearch);
// Agent can now search the web
Example: Using Brave Search with API key
var webSearch = new WebSearchTool(new WebSearchTool.Options
{
SearchProvider = WebSearchTool.Provider.Brave,
ApiKey = "your-brave-api-key"
});
registry.Register(webSearch);
Example: Using self-hosted SearXNG
var webSearch = new WebSearchTool(new WebSearchTool.Options
{
SearchProvider = WebSearchTool.Provider.SearXNG,
BaseUrl = "https://your-searxng-instance.com"
});
registry.Register(webSearch);
Example: Using Tavily for AI-optimized search
var webSearch = new WebSearchTool(new WebSearchTool.Options
{
SearchProvider = WebSearchTool.Provider.Tavily,
ApiKey = Environment.GetEnvironmentVariable("TAVILY_API_KEY")
});
registry.Register(webSearch);
Remarks
Provider Overview:
| Provider | Features |
|---|---|
| DuckDuckGo | Free, no API key required. HTML scraping based. Suitable for local development only; returns blocking errors after a small number of queries from any single IP. |
| Brave | Free tier available (2000 queries/month). High quality results. Requires API key. |
| SearXNG | Self-hosted meta-search engine. Privacy-focused. Aggregates multiple sources. |
| Tavily | AI-optimized search API. Excellent for RAG applications. Requires API key. |
| Serper | Google search results via API. High accuracy. Requires API key. |
| Custom | Bring your own search endpoint. Flexible integration. |
Security Considerations:
This tool makes real network requests. Consider security implications
when enabling it, especially with untrusted input.
Constructors
- WebSearchTool()
Initializes a new instance with default settings (DuckDuckGo provider, no API key required).
- WebSearchTool(Options)
Initializes a new instance with custom options.
- WebSearchTool(Provider, string)
Initializes a new instance with the specified provider and API key.
- WebSearchTool(HttpClient, Options)
Initializes a new instance with an existing HttpClient.
Properties
- Description
Gets a concise description of what the tool does.
- InputSchema
Gets the JSON Schema defining the expected input arguments.
- Name
Gets the stable, unique identifier for this tool.
- SearchProvider
Gets the current search provider.
Methods
- InvokeAsync(string, CancellationToken)
Executes the tool with the specified JSON arguments.
Events
- RawContentReceived
Occurs when raw content is received from a search provider.
This event allows interception of the raw HTML or JSON response before parsing. Useful for logging, caching, or custom processing of full page content.
- ResultProcessing
Occurs before a result is processed and added to the response.
Allows inspection or modification of individual results before they are included. Set Cancel to true to exclude a result.