Skip to main content
Glama
Quqing-future

mcp-bing-search

README.md
# mcp-bing-search

A local **MCP server** for web search that works from **mainland China without any API key**.

- Primary engine: **Bing** (`cn.bing.com`)
- Automatic fallback for fresh Chinese trending topics: **Qihoo 360** → **Sogou**
- Resolves search-engine redirect links to real URLs

It was built to replace public searxng instances (e.g. `baresearch.org`), which are
unreachable from mainland China and return nothing for Chinese long-tail queries.

> **Not affiliated with Microsoft's official `mcp-server-bing-search`** (which
> requires an API key) — this is a no-key, China-friendly alternative.

## Why this exists

| Problem | This project's answer |
|---|---|
| Public searxng instances unreachable from mainland China | Direct request to `cn.bing.com`, which is reachable without a proxy |
| No API key / no billing | Scrapes the search result page (HTML parsing), no key needed |
| Bing indexes Chinese campus/trending topics poorly | `fallback` automatically retries with 360 / Sogou |
| Baidu blocks automated requests with captchas | Baidu is intentionally **not** used |

## Features

- `web_search(query, max_results, fallback)` — Bing first; when results are irrelevant
  (common for very fresh Chinese topics), automatically retries 360 → Sogou.
- `cn_search(query, max_results)` — search Chinese engines directly (360 → Sogou).
- Redirect-link resolution — 360/Sogou's encrypted jump links (`so.com/link?m=...`)
  are resolved to the final real URL (HTTP redirects and JS redirects supported).
- Self-contained single file; only depends on `mcp`, `httpx`, `lxml`.

## Install

### Option A: pip (recommended)

```bash
pip install mcp-bing-search
```

### Option B: from source

```bash
git clone https://github.com/Quqing-future/mcp-bing-search.git
cd mcp-bing-search
pip install .
# or run directly without installing:
python mcp_bing_search.py
```

## Configure with Reasonix

Add to your global `config.toml` (all projects) or project `reasonix.toml`:

```toml
[[plugins]]
name    = "bing-search"
command = "python"                      # or the absolute path to python.exe
args    = ["-m", "mcp_bing_search"]     # if installed via pip
# args = ["/path/to/mcp_bing_search.py"] # if running from source
```

Or use the standard `.mcp.json`:

```json
{
  "mcpServers": {
    "bing-search": {
      "command": "python",
      "args": ["-m", "mcp_bing_search"]
    }
  }
}
```

Changes take effect on the **next session**. Tools appear as
`mcp__bing-search__web_search` and `mcp__bing-search__cn_search`.

### Configure with other MCP clients

Any MCP client works — Claude Desktop, Cursor, VS Code, etc. Point it at:

```bash
python -m mcp_bing_search
# or
python /path/to/mcp_bing_search.py
```

## Tools

### `web_search(query: str, max_results: int = 8, fallback: bool = True) -> str`

Search the web. Bing first; if `fallback` is enabled and Bing returns no relevant
results (no title/url contains a query token), retries with Qihoo 360, then Sogou.

### `cn_search(query: str, max_results: int = 8) -> str`

Search Chinese engines directly (360 → Sogou). Use for fresh Chinese trending
topics, campus/weibo news, or anything Bing indexes poorly.

## How it works

```
MCP client (Reasonix / Claude Desktop / ...)
   ↕  stdio + JSON-RPC
mcp_bing_search.py  (MCP server)
   ↕  HTTPS (plain HTTP GET, browser User-Agent)
cn.bing.com  →  www.so.com  →  www.sogou.com
```

1. **Request** — `httpx` GETs `https://cn.bing.com/search?q=...` with a desktop
   browser User-Agent.
2. **Parse** — `lxml` XPath extracts `title / url / snippet` from `li.b_algo` nodes.
3. **Relevance check** — if no result title/url contains any query token (e.g.
   "本地大模型", "部署教程"), the result set is considered irrelevant.
4. **Fallback** — on empty/irrelevant Bing results, tries 360 → Sogou and uses the
   first engine with relevant results.
5. **Link cleanup** — redirect links (`so.com/link?m=...`, `/link?url=...`) are
   resolved once more to their real destination (Location header or
   `window.location.replace(...)`).

## Known limitations

- Scraping depends on the engines' HTML structure; a redesign may break parsing.
- No key = no SLA; very frequent calls may be rate-limited.
- Bing's coverage of fresh Chinese social-media topics is weak — use `fallback`
  or `cn_search` for those.
- Baidu is excluded on purpose: it aggressively captchas automated requests.
- **ToS disclaimer**: this project works by parsing public search-result pages.
  It is provided for personal / educational use and does **not** claim to comply
  with the terms of service of Bing, Qihoo 360, or Sogou. Scraping may violate
  those terms; automated access may be throttled or blocked. Use at your own
  risk, and consider the official Search APIs for production use.

## License

MIT © Quqing-future