web_search
Search the web for information using Grok MCP to find answers, verify facts, and gather data from online sources.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| model | No | grok-4-1-fast | |
| allowed_domains | No | ||
| excluded_domains | No | ||
| enable_image_understanding | No | ||
| include_inline_citations | No | ||
| max_turns | No |
Implementation Reference
- src/server.py:172-219 (handler)Main handler function for web_search tool. Validates domain parameters (allowed_domains/excluded_domains cannot both be specified, max 5 each), creates chat client with xai_sdk's web_search tool, executes search and returns formatted results with citations if available.@mcp.tool() async def web_search( prompt: str, model: str = "grok-4-1-fast", allowed_domains: Optional[List[str]] = None, excluded_domains: Optional[List[str]] = None, enable_image_understanding: bool = False, include_inline_citations: bool = False, max_turns: Optional[int] = None ): if allowed_domains and excluded_domains: raise ValueError("Cannot specify both allowed_domains and excluded_domains") if allowed_domains and len(allowed_domains) > 5: raise ValueError("allowed_domains max 5") if excluded_domains and len(excluded_domains) > 5: raise ValueError("excluded_domains max 5") client = Client(api_key=XAI_API_KEY) tool_params = build_params( allowed_domains=allowed_domains, excluded_domains=excluded_domains, enable_image_understanding=enable_image_understanding, ) include_options = [] if include_inline_citations: include_options.append("inline_citations") chat_params = {"model": model, "tools": [xai_web_search(**tool_params)]} if include_options: chat_params["include"] = include_options if max_turns: chat_params["max_turns"] = max_turns chat = client.chat.create(**chat_params) chat.append(user(prompt)) response = chat.sample() client.close() result = [response.content] if response.citations: result.append("\n\n**Sources:**") for url in response.citations: result.append(f"- {url}") return "\n".join(result)
- src/server.py:7-7 (registration)Imports the web_search tool from xai_sdk.tools as xai_web_search, which is the underlying SDK implementation used by the MCP tool.from xai_sdk.tools import web_search as xai_web_search, x_search as xai_x_search, code_execution
- src/server.py:172-172 (registration)Tool registration via @mcp.tool() decorator that registers the web_search function as an MCP tool.@mcp.tool()
- src/utils.py:40-45 (helper)Helper function build_params that filters out None values from keyword arguments, used to construct tool parameters for web_search.def build_params(**kwargs): result = {} for key, value in kwargs.items(): if value: result[key] = value return result