bing_web_search
Perform a web search via Bing API to retrieve relevant websites and general information by specifying query, result count, offset, and market code.
Instructions
Performs a web search using the Bing Search API for general information and websites.
Args:
query: Search query (required)
count: Number of results (1-50, default 10)
offset: Pagination offset (default 0)
market: Market code like en-US, en-GB, etc.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | ||
| market | No | en-US | |
| offset | No | ||
| query | Yes |
Input Schema (JSON Schema)
{
"properties": {
"count": {
"default": 10,
"title": "Count",
"type": "integer"
},
"market": {
"default": "en-US",
"title": "Market",
"type": "string"
},
"offset": {
"default": 0,
"title": "Offset",
"type": "integer"
},
"query": {
"title": "Query",
"type": "string"
}
},
"required": [
"query"
],
"title": "bing_web_searchArguments",
"type": "object"
}
Implementation Reference
- mcp_server_bing/server.py:94-159 (handler)The core handler function for the 'bing_web_search' tool. It is registered via the @server.tool() decorator. The function signature with type annotations defines the input schema. It performs the Bing web search API call, applies rate limiting, handles errors, and formats search results as a string.@server.tool() async def bing_web_search( query: str, count: int = 10, offset: int = 0, market: str = "en-US" ) -> str: """Performs a web search using the Bing Search API for general information and websites. Args: query: Search query (required) count: Number of results (1-50, default 10) offset: Pagination offset (default 0) market: Market code like en-US, en-GB, etc. """ # Get API key from environment api_key = os.environ.get("BING_API_KEY", "") if not api_key: return ( "Error: Bing API key is not configured. Please set the " "BING_API_KEY environment variable." ) try: check_rate_limit() headers = { "User-Agent": USER_AGENT, "Ocp-Apim-Subscription-Key": api_key, "Accept": "application/json", } params = { "q": query, "count": min(count, 50), # Bing limits to 50 results max "offset": offset, "mkt": market, "responseFilter": "Webpages", } search_url = f"{BING_API_URL}v7.0/search" async with httpx.AsyncClient() as client: response = await client.get( search_url, headers=headers, params=params, timeout=10.0 ) response.raise_for_status() data = response.json() if "webPages" not in data: return "No results found." results = [] for result in data["webPages"]["value"]: results.append( f"Title: {result['name']}\n" f"URL: {result['url']}\n" f"Description: {result['snippet']}" ) return "\n\n".join(results) except httpx.HTTPError as e: return f"Error communicating with Bing API: {str(e)}" except Exception as e: return f"Unexpected error: {str(e)}"
- mcp_server_bing/server.py:77-92 (helper)Helper function for rate limiting, called within bing_web_search to enforce API usage limits.def check_rate_limit(): """Check if we're within rate limits""" now = time.time() if now - request_count["last_reset"] > 1: request_count["second"] = 0 request_count["last_reset"] = now if ( request_count["second"] >= RATE_LIMIT["per_second"] or request_count["month"] >= RATE_LIMIT["per_month"] ): raise Exception("Rate limit exceeded") request_count["second"] += 1 request_count["month"] += 1
- mcp_server_bing/server.py:94-94 (registration)The @server.tool() decorator registers the bing_web_search function as an MCP tool.@server.tool()