"""
Crow Builtin MCP Server
Combines all builtin tools into one MCP server:
- file_editor: View, create, edit files
- web_search: Search the web via SearXNG
- fetch: Fetch and parse web pages
This is the default MCP server for crow agents.
"""
import markdownify
import readabilipy.simple_json
from httpx import AsyncClient
from crow_cli.mcp.server.logger import logger
from crow_cli.mcp.server.app import mcp
# Create the combined MCP server
# mcp = FastMCP(name="crow-builtin")
DEFAULT_USER_AGENT = "CrowAgent/1.0"
async def fetch_url(url: str, user_agent: str = DEFAULT_USER_AGENT) -> tuple[str, str]:
"""Fetch URL and return (content, prefix)."""
async with AsyncClient() as client:
response = await client.get(
url,
follow_redirects=True,
headers={"User-Agent": user_agent},
timeout=30,
)
response.raise_for_status()
page_raw = response.text
content_type = response.headers.get("content-type", "")
is_html = (
"Failed to parse HTML", ""
else:
return page_raw, f"Content type: {content_type}\n"
@mcp.tool
async def web_fetch(
url: str,
max_length: int = 5000,
start_index: int = 0,
raw: bool = False,
) -> str:
"""Fetch a URL and extract content as markdown.
Args:
url: URL to fetch
max_length: Max characters to return (default 5000)
start_index: Start at this character (for pagination)
raw: Get raw HTML instead of markdown
Returns:
Page content as markdown (or raw HTML if raw=True)
"""
try:
content, prefix = await fetch_url(url)
if raw:
# Return raw HTML
total_len = len(content)
truncated = content[start_index : start_index + max_length]
if start_index >= total_len:
return "No more content available."
result = f"{prefix}Raw HTML from {url}:\n{truncated}"
if len(truncated) == max_length and start_index + max_length < total_len:
result += f"\n\nContent truncated. Call fetch with start_index={start_index + max_length}"
return result
total_len = len(content)
if start_index >= total_len:
return "No more content available."
truncated = content[start_index : start_index + max_length]
result = f"{prefix}Contents of {url}:\n{truncated}"
if len(truncated) == max_length and start_index + max_length < total_len:
result += f"\n\nContent truncated. Call fetch with start_index={start_index + max_length}"
return result
except Exception as e:
return f"Error fetching {url}: {e}"