Back to use cases

    Use case / AI AGENTS / Python 3 (a saved prompt is also available) / Published

    Build a Wikipedia search API primer with Desearch MCP

    Use Desearch as a Wikipedia API for topic primers. Search articles with MCP, clean titles and links, and add an optional web cross-check with a Python CLI.

    A topic search flowing through Desearch MCP to a primer with article links

    Use Desearch as a Wikipedia API for a topic primer with article links. This Python example searches through Desearch MCP and cleans the returned pages before formatting a summary. This page shows a stored sample; there is no hosted demo.

    The problem

    An assistant needs sources when it explains a new topic. Search results need checks too. A page title can differ from its linked article, and a result can lead to a discussion page. Developers need a reading list that keeps its links and explains what was excluded.

    What the app does

    • Searches Wikipedia through the Desearch MCP ai-search tool.
    • Drops non-article namespaces and results whose normalized title differs from the URL slug.
    • Removes duplicate links and marks non-English pages.
    • Replaces numbered citations with links and removes citations to excluded pages.
    • Checks summary numbers against returned snippets and highlights, then re-links or omits sentences when needed.
    • Lists up to six cleaned articles and optionally adds up to three web reading links.

    How it works

    1. Read the key. The CLI reads ~/.desearch-api-key, or the file supplied with --key-file. It sends the key in the Authorization header to https://mcp.desearch.ai/mcp.
    2. Search the topic. ai-search receives tools: ["wikipedia"] and result_type: "LINKS_WITH_FINAL_SUMMARY".
    3. Clean the results. clean_pages() checks namespaces, compares normalized titles with URL slugs, deduplicates links and adds language labels.
    4. Format the primer. The script removes links to excluded pages and sentences cited only to those pages. It checks numbers against returned snippets and highlights. Notes explain exclusions.
    5. Cross-check if requested. --web runs a separate web-search call. Those links appear in their own reading list.

    The namespace list uses English names. A translated namespace can pass that check. These filters do not verify every claim, so open the linked articles before relying on the summary.

    The code

    These argument builders are verbatim from wikipedia_primer.py:

    PYTHON
    def build_args(topic):
        if not topic or not topic.strip():
            raise ValueError("topic is empty")
        t = topic.strip()
        return {"prompt": f"{t}: what it is, how it works, key facts, criticism, and related topics",
                "tools": ["wikipedia"], "result_type": "LINKS_WITH_FINAL_SUMMARY"}
    
    
    def web_args(topic):
        return {"query": f"{topic.strip()} explained"}
    

    The same file opens the MCP session and calls the tools. The key argument comes from read_key() using the configured key file.

    PYTHON
    async def run(topic, key, cross_check):
        from mcp import ClientSession
        from mcp.client.streamable_http import streamable_http_client
        from mcp.shared._httpx_utils import create_mcp_http_client
    
        async with create_mcp_http_client(headers={"Authorization": key}) as hc, \
                streamable_http_client(MCP_URL, http_client=hc) as streams:
            async with ClientSession(streams[0], streams[1]) as s:
                await s.initialize()
                res = await s.call_tool("ai-search", build_args(topic), read_timeout_seconds=180)
                wiki = parse_json([c.model_dump() for c in res.content])
                web = None
                if cross_check:
                    res = await s.call_tool("web-search", web_args(topic), read_timeout_seconds=180)
                    web = parse_json([c.model_dump() for c in res.content]) or {}
                return wiki, web
    

    Endpoints

    • https://mcp.desearch.ai/mcp
    • MCP tool ai-search (tools: wikipedia)
    • MCP tool web-search

    Example output

    This article-list excerpt comes verbatim from samples/live-run-2026-10-19.md, a stored run for the topic "carbon credits" with the web cross-check enabled. The file includes the summary, reading links and exclusion notes.

    ### Wikipedia pages
    - Verified Carbon Standard: https://en.wikipedia.org/wiki/Verified_Carbon_Standard
    - Personal carbon credits: https://en.wikipedia.org/wiki/Personal_Carbon_Credits
    - Comercio de emisiones de carbono (es Wikipedia): https://es.wikipedia.org/wiki/Comercio_de_emisiones_de_carbono
    - Carbon emission trading: https://en.wikipedia.org/wiki/Carbon_emission_trading
    - Compensación de carbono (es Wikipedia): https://es.wikipedia.org/wiki/Compensaci%C3%B3n_de_carbono
    - Personal carbon trading: https://en.wikipedia.org/wiki/Personal_carbon_trading

    Run it yourself

    Open the example folder and follow its README to create a virtual environment and install requirements.txt. Put your key from the console in a file only you can read at ~/.desearch-api-key, or pass its path with --key-file.

    Run the command from the saved sample:

    BASH
    python wikipedia_primer.py "carbon credits" --web

    Omit --web for Wikipedia only. The folder also includes saved-prompt.md for an MCP-compatible assistant and offline tests in tests/.

    What it costs

    On 2026-10-19, the stored run recorded $0.004 for ai-search and $0.001 for the separate web-search, using each response's cost_usd field. The combined API cost was $0.005. Tool times were 9.3 seconds and 2.4 seconds respectively. These are measurements from that run, not a speed guarantee. See pricing for current rates. Assistant usage is separate.

    Ideas to extend it

    • Save the cleaned title, link and language fields as a reading-list file.
    • Add translated namespace checks for the languages you use.
    • Keep dated primers so readers can compare source lists over time.

    Use with AI agents

    Point an MCP client at the hosted Desearch server. Replace the placeholder with a key from the console. Do not commit the key.

    JSON
    {
      "mcpServers": {
        "desearch": {
          "url": "https://mcp.desearch.ai/mcp",
          "headers": {
            "Authorization": "Bearer <key>"
          }
        }
      }
    }

    Prompt you can paste for this example:

    Use Desearch ai-search with tools set to wikipedia and result_type LINKS_WITH_FINAL_SUMMARY for the topic I supply. Keep article links whose titles match their URL slugs, remove duplicates and mark non-English pages. Remove citations to excluded pages and omit sentences cited only to those pages. Check summary numbers against returned snippets and highlights. Write a short primer with source links and notes about exclusions. If I request a web cross-check, call web-search separately and list those reading links separately. Do not invent facts or sources. Ask me to read the linked pages before relying on the primer.

    Build your own example.

    Give your agents access to web and social search. Get an API key to start building, or talk to us about your integration.

    Read docs