web_search
Search the web and get a plain-text block with abstract, source URL, and top related topics for well-known entities.
Instructions
Search the web via DuckDuckGo Instant Answer API.
Returns a plain-text block with the abstract, source URL, and top related topics. Best for well-known entities (companies, people, places). Returns 'no results' if the query has no instant answer.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:31-67 (handler)The web_search tool handler function. Uses DuckDuckGo Instant Answer API to search the web and returns a formatted plain-text result with heading, abstract, source URL, and related topics.
def web_search(query: str) -> str: """Search the web via DuckDuckGo Instant Answer API. Returns a plain-text block with the abstract, source URL, and top related topics. Best for well-known entities (companies, people, places). Returns 'no results' if the query has no instant answer. """ resp = httpx.get( "https://api.duckduckgo.com/", params={"q": query, "format": "json", "no_html": 1}, timeout=10.0, ) resp.raise_for_status() data = resp.json() heading = data.get("Heading") or "" abstract = data.get("AbstractText") or "" url = data.get("AbstractURL") or "" related = data.get("RelatedTopics") or [] if not (heading or abstract or related): return f"No instant answer for {query!r}. Try a more specific or well-known entity." lines: list[str] = [] if heading: lines.append(f"# {heading}") if abstract: lines.append(abstract) if url: lines.append(f"Source: {url}") if related: lines.append("\nRelated:") for item in related[:5]: text = item.get("Text") or "" if text: lines.append(f"- {text}") return "\n".join(lines) - server.py:30-30 (registration)The @mcp.tool decorator registers web_search as a tool with the FastMCP server instance.
@mcp.tool