M4: Tavily parity + MCP — /search/contents and MCP web_search/fetch_url
- pipeline.search_contents: search then render+extract all results in parallel; per-URL failures degrade to content:null + failed_urls instead of failing the request - REST: POST /search/contents (Tavily-like full flow with trace) - MCP server (streamable HTTP, stateless) mounted at /mcp on the orchestrator; tools web_search (with optional full content) and fetch_url share the exact pipeline + caches as REST Verified: /search/contents end-to-end with parallel scrapes; MCP tool listing and both tool calls via the official Python MCP client. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
53f0d50e7a
commit
a70a6b5f5f
3 changed files with 118 additions and 1 deletions
54
services/orchestrator/app/mcp_server.py
Normal file
54
services/orchestrator/app/mcp_server.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
"""MCP server: the primary interface. Tools call the same pipeline as REST.
|
||||
|
||||
Mounted into the FastAPI app at /mcp (streamable HTTP, stateless).
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
from . import pipeline
|
||||
|
||||
# Populated by main.py's lifespan with the shared httpx client + cache.
|
||||
runtime: dict = {"client": None, "cache": None}
|
||||
|
||||
mcp = FastMCP(
|
||||
"local-web-search",
|
||||
instructions=(
|
||||
"Private, self-hosted web search and page fetching. web_search returns "
|
||||
"ranked open-web results (optionally with full page content); fetch_url "
|
||||
"returns the cleaned text of a single page. Content is mechanically "
|
||||
"extracted (boilerplate stripped); rank/filter relevance yourself."
|
||||
),
|
||||
streamable_http_path="/",
|
||||
stateless_http=True,
|
||||
)
|
||||
|
||||
|
||||
def _compact(trace: dict) -> dict:
|
||||
return {"total_ms": round(sum(s["ms"] for s in trace["stages"]), 1), "degraded": trace["degraded"]}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def web_search(query: str, max_results: int = 5, include_content: bool = True) -> str:
|
||||
"""Search the web. Returns ranked results as JSON: url, title, snippet, and
|
||||
(when include_content is true) the cleaned full text of each page."""
|
||||
client, cache = runtime["client"], runtime["cache"]
|
||||
if include_content:
|
||||
data = await pipeline.search_contents(client, query, max_results, cache)
|
||||
results, trace = data["results"], data["trace"]
|
||||
else:
|
||||
trace_obj = pipeline.Trace()
|
||||
results = await pipeline.search(client, query, max_results, trace_obj)
|
||||
trace = trace_obj.as_dict()
|
||||
return json.dumps({"results": results, "meta": _compact(trace)})
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def fetch_url(url: str) -> str:
|
||||
"""Fetch one URL (with JS rendering) and return its cleaned text content,
|
||||
title, and metadata as JSON."""
|
||||
client, cache = runtime["client"], runtime["cache"]
|
||||
result = await pipeline.scrape_url(client, url, cache)
|
||||
trace = result.pop("trace")
|
||||
return json.dumps({**result, "meta": _compact(trace)})
|
||||
Loading…
Add table
Add a link
Reference in a new issue