search_web
Search the web and pay per query with Lightning Network or Arbitrum ETH, eliminating API key requirements for AI agents.
Instructions
Search the web. Pay with Lightning (payment_hash) or Arbitrum ETH (tx_hash).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| payment_hash | No | ||
| tx_hash | No | ||
| max_results | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:110-123 (handler)The main search_web tool handler. It validates payment (Lightning via payment_hash or Arbitrum ETH via tx_hash) and returns web search results. Decorated with @mcp.tool() for MCP registration.
@mcp.tool() def search_web(query: str, payment_hash: str = "", tx_hash: str = "", max_results: int = 5) -> str: """Search the web. Pay with Lightning (payment_hash) or Arbitrum ETH (tx_hash).""" if payment_hash: if not check_invoice(payment_hash): return "Lightning payment not settled. Call get_invoice first." elif tx_hash: ok, pid = arb_pay.verify_tx(tx_hash, "search") if not ok: return "Arbitrum payment not found or already used. Call get_arbitrum_invoice first." arb_pay.mark_used(pid) else: return "Provide payment_hash (Lightning) or tx_hash (Arbitrum)." return do_search(query, max_results) - server.py:75-94 (helper)Helper function get_invoice that creates a Lightning invoice for the search_web tool. It calculates karma-based discounts and returns payment request details that the user must pay before calling search_web.
@mcp.tool() def get_invoice(agent_id: str = "") -> str: """Get a Lightning invoice to pay before searching. agent_id: your identity in Giskard Marks (optional). High karma = lower price. Tiers: no mark=10 sats | karma 1-20=7 sats | 21-50=5 sats | 50+=3 sats.""" agent_id = sanitize_agent_id(agent_id) price, karma = karma_discount(agent_id, SEARCH_PRICE_SATS) invoice = create_invoice(price, "Giskard Search") discount_note = "" if agent_id and price < SEARCH_PRICE_SATS: discount_note = f"\nKarma discount applied ({karma} karma): {SEARCH_PRICE_SATS} → {price} sats." return ( f"Pay {price} sats to search.{discount_note}\n\n" f"payment_request: {invoice['payment_request']}\n" f"payment_hash: {invoice['payment_hash']}\n\n" f"After paying, call search_web or search_news with the payment_hash." ) - server.py:57-62 (helper)Core search helper function that performs the actual DuckDuckGo web search using the DDGS library. Called by search_web after payment verification.
def do_search(query: str, max_results: int = 5) -> str: with DDGS() as ddgs: results = list(ddgs.text(query, max_results=max_results)) if not results: return "No results found." return "\n---\n".join(f"**{r['title']}**\n{r['href']}\n{r['body']}" for r in results)