Skip to main content
Glama
nickclyde

DuckDuckGo MCP Server

search

Search DuckDuckGo to find web information and return formatted results for queries, supporting content fetching and parsing through the MCP server.

Instructions

Search DuckDuckGo and return formatted results.

Args: query: The search query string max_results: Maximum number of results to return (default: 10) ctx: MCP context for logging

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes
max_resultsNo

Implementation Reference

  • The primary handler function for the 'search' MCP tool. It is registered via the @mcp.tool() decorator and orchestrates the search by calling the DuckDuckGoSearcher and formatting results.
    @mcp.tool()
    async def search(query: str, ctx: Context, max_results: int = 10) -> str:
        """
        Search DuckDuckGo and return formatted results.
    
        Args:
            query: The search query string
            max_results: Maximum number of results to return (default: 10)
            ctx: MCP context for logging
        """
        try:
            results = await searcher.search(query, ctx, max_results)
            return searcher.format_results_for_llm(results)
        except Exception as e:
            traceback.print_exc(file=sys.stderr)
            return f"An error occurred while searching: {str(e)}"
  • Core helper method implementing the DuckDuckGo search logic: rate limiting, HTTP POST to DuckDuckGo HTML endpoint, HTML parsing with BeautifulSoup, result extraction and cleaning.
    async def search(
        self, query: str, ctx: Context, max_results: int = 10
    ) -> List[SearchResult]:
        try:
            # Apply rate limiting
            await self.rate_limiter.acquire()
    
            # Create form data for POST request
            data = {
                "q": query,
                "b": "",
                "kl": "",
            }
    
            await ctx.info(f"Searching DuckDuckGo for: {query}")
    
            async with httpx.AsyncClient() as client:
                response = await client.post(
                    self.BASE_URL, data=data, headers=self.HEADERS, timeout=30.0
                )
                response.raise_for_status()
    
            # Parse HTML response
            soup = BeautifulSoup(response.text, "html.parser")
            if not soup:
                await ctx.error("Failed to parse HTML response")
                return []
    
            results = []
            for result in soup.select(".result"):
                title_elem = result.select_one(".result__title")
                if not title_elem:
                    continue
    
                link_elem = title_elem.find("a")
                if not link_elem:
                    continue
    
                title = link_elem.get_text(strip=True)
                link = link_elem.get("href", "")
    
                # Skip ad results
                if "y.js" in link:
                    continue
    
                # Clean up DuckDuckGo redirect URLs
                if link.startswith("//duckduckgo.com/l/?uddg="):
                    link = urllib.parse.unquote(link.split("uddg=")[1].split("&")[0])
    
                snippet_elem = result.select_one(".result__snippet")
                snippet = snippet_elem.get_text(strip=True) if snippet_elem else ""
    
                results.append(
                    SearchResult(
                        title=title,
                        link=link,
                        snippet=snippet,
                        position=len(results) + 1,
                    )
                )
    
                if len(results) >= max_results:
                    break
    
            await ctx.info(f"Successfully found {len(results)} results")
            return results
    
        except httpx.TimeoutException:
            await ctx.error("Search request timed out")
            return []
        except httpx.HTTPError as e:
            await ctx.error(f"HTTP error occurred: {str(e)}")
            return []
        except Exception as e:
            await ctx.error(f"Unexpected error during search: {str(e)}")
            traceback.print_exc(file=sys.stderr)
            return []
  • Dataclass schema defining the structure of individual search results returned by the search implementation.
    @dataclass
    class SearchResult:
        title: str
        link: str
        snippet: str
        position: int
  • Helper function to format the list of SearchResult objects into a human-readable string optimized for LLM processing.
    def format_results_for_llm(self, results: List[SearchResult]) -> str:
        """Format results in a natural language style that's easier for LLMs to process"""
        if not results:
            return "No results were found for your search query. This could be due to DuckDuckGo's bot detection or the query returned no matches. Please try rephrasing your search or try again in a few minutes."
    
        output = []
        output.append(f"Found {len(results)} search results:\n")
    
        for result in results:
            output.append(f"{result.position}. {result.title}")
            output.append(f"   URL: {result.link}")
            output.append(f"   Summary: {result.snippet}")
            output.append("")  # Empty line between results
    
        return "\n".join(output)
  • Initialization of the FastMCP server instance named 'ddg-search' and the searcher/fetcher instances used by the tools.
    mcp = FastMCP("ddg-search")
    searcher = DuckDuckGoSearcher()
    fetcher = WebContentFetcher()
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool searches DuckDuckGo and returns formatted results, but lacks details on rate limits, authentication needs, error handling, or what 'formatted results' entail (e.g., structure, fields). This is a significant gap for a search tool with external dependencies.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded: the first sentence states the core purpose, followed by a parameter list. The parameter explanations are brief and relevant. However, the inclusion of 'ctx: MCP context for logging' is unnecessary clutter since it's not in the input schema.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of a search tool with external API dependencies, no annotations, no output schema, and low schema coverage, the description is incomplete. It lacks critical details like result format, error cases, rate limits, and differentiation from 'fetch_content', making it inadequate for reliable agent use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate. It adds meaning by explaining 'query' as 'The search query string' and 'max_results' as 'Maximum number of results to return (default: 10)', which clarifies purpose beyond the schema's basic types. However, it doesn't cover constraints like query length or max_results range, leaving gaps.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Search DuckDuckGo and return formatted results.' This specifies the action (search), resource (DuckDuckGo), and outcome (formatted results). However, it doesn't explicitly differentiate from the sibling tool 'fetch_content', which might be a related content retrieval function.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It mentions no prerequisites, exclusions, or comparisons to the sibling tool 'fetch_content'. The agent must infer usage context solely from the purpose statement.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/nickclyde/duckduckgo-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server