web_search
Search the web for information to enhance LLM interactions within the continue.dev environment by providing relevant data and resources.
Instructions
Search the web for information
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes |
Implementation Reference
- src/mcps/server.py:67-67 (registration)Registers the web_search tool with the MCP server using the @tool decorator.@self.mcp.tool(name="web_search", description="Search the web for information")
- src/mcps/server.py:68-92 (handler)The handler function for the web_search tool. It performs some root listing logging and delegates the search to perplexity_search.do_search.async def web_search(query: str) -> str: """ Performs a web search using the provided query. Find the most relevant pages and return summary result. Args: query: The search query. Returns: The summary of the most relevant search results. """ try: session: ServerSession = self.mcp.get_context().session if session.check_client_capability(ClientCapabilities(roots=RootsCapability())) : result = await session.list_roots() logger.info(f"Result: {result}") for root in result.roots: logger.info(f"Root: {root.name} , location: {root.uri}") else: logger.info("Client does not support roots capability") # Try to get the roots from the environment variable ROOT root_value = os.getenv("ROOT") logger.info(f"ROOT environment variable: {root_value}") except Exception as e: logger.error(f"Error listing roots: {e}") return await perplexity_search.do_search(query, self.config)
- Core helper function that performs the actual web search using Perplexity AI API.async def do_search(query: str, config: ServerConfig) -> str: """ Performs a search and returns the results. Args: query: The search query. Returns: The search query string back. """ url = "https://api.perplexity.ai/chat/completions" headers = { "Authorization": f"Bearer {config.perplexity_api_key}", "Content-Type": "application/json" } payload = { "model": "sonar", "messages": [ {"role": "system", "content": "Be precise and concise."}, {"role": "user", "content": query} ], "max_tokens": 1000, "temperature": 0.01, "top_p": 0.9, "return_related_questions": False, "web_search_options": { "search_context_size": "medium" } } async with httpx.AsyncClient() as client: response = await client.post(url, json=payload, headers=headers) response.raise_for_status() return format_response_with_citations(response.json())
- Helper function to format the Perplexity API response with citations in markdown.def format_response_with_citations(response: dict) -> str: """ Formats the response from Perplexity.ai to include citations as a markdown list. Args: response: The JSON response from Perplexity.ai. Returns: A formatted string with the content and citations. """ content = response.get("choices", [{}])[0].get("message", {}).get("content", "No content available") citations = response.get("citations", []) if citations: citations_md = "\n".join([f"- {url}" for url in citations]) return f"{content}\n\n### Citations\n{citations_md}" return content