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
|
|
@ -1,5 +1,6 @@
|
|||
"""Pipeline stages and tracing. The orchestrator owns sequencing + degradation."""
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
|
|
@ -111,3 +112,44 @@ async def scrape_url(client: httpx.AsyncClient, url: str, cache: Cache | None =
|
|||
if cache and result["content"] and not result["thin"]:
|
||||
await cache.set_json(cache_key, result, config.EXTRACT_CACHE_TTL)
|
||||
return {**result, "trace": trace.as_dict()}
|
||||
|
||||
|
||||
async def search_contents(
|
||||
client: httpx.AsyncClient, query: str, max_results: int, cache: Cache | None = None
|
||||
) -> dict:
|
||||
"""Tavily-equivalent flow: search, then render+extract the results in parallel.
|
||||
|
||||
Per-URL failures degrade to a result without content instead of failing the
|
||||
whole request.
|
||||
"""
|
||||
trace = Trace()
|
||||
results = await search(client, query, max_results, trace)
|
||||
|
||||
start = time.monotonic()
|
||||
scraped = await asyncio.gather(
|
||||
*(scrape_url(client, r["url"], cache) for r in results), return_exceptions=True
|
||||
)
|
||||
trace.record("contents", (time.monotonic() - start) * 1000)
|
||||
|
||||
out = []
|
||||
failed = []
|
||||
for meta, page in zip(results, scraped):
|
||||
entry = dict(meta)
|
||||
if isinstance(page, StageError):
|
||||
entry.update({"content": None, "error": page.detail})
|
||||
failed.append(meta["url"])
|
||||
elif isinstance(page, BaseException):
|
||||
raise page
|
||||
else:
|
||||
entry.update(
|
||||
{
|
||||
"content": page["content"],
|
||||
"title": page.get("title") or meta["title"],
|
||||
"metadata": page.get("metadata", {}),
|
||||
"final_url": page.get("final_url"),
|
||||
}
|
||||
)
|
||||
out.append(entry)
|
||||
if failed:
|
||||
trace.degraded.append("contents")
|
||||
return {"results": out, "failed_urls": failed, "trace": trace.as_dict()}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue