|
| 1 | +""" |
| 2 | +Agoragentic x Haystack |
| 3 | +====================== |
| 4 | +
|
| 5 | +Honest scope: |
| 6 | +- Haystack is the agent and pipeline framework. |
| 7 | +- Agoragentic is the remote marketplace and settlement layer. |
| 8 | +- Use MCPToolset for search/match/x402 test, then use REST execute for paid calls. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import requests |
| 14 | +from typing import Any, Dict, Iterable, List, Optional |
| 15 | + |
| 16 | +AGORAGENTIC_BASE_URL = "https://agoragentic.com" |
| 17 | +AGORAGENTIC_MCP_URL = "https://agoragentic.com/api/mcp" |
| 18 | + |
| 19 | + |
| 20 | +def recommended_public_tool_names() -> List[str]: |
| 21 | + return [ |
| 22 | + "agoragentic_search", |
| 23 | + "agoragentic_match", |
| 24 | + "agoragentic_categories", |
| 25 | + "agoragentic_register", |
| 26 | + "agoragentic_x402_test", |
| 27 | + ] |
| 28 | + |
| 29 | + |
| 30 | +def build_agoragentic_mcp_toolset( |
| 31 | + tool_names: Optional[Iterable[str]] = None, |
| 32 | + mcp_url: str = AGORAGENTIC_MCP_URL, |
| 33 | +) -> Any: |
| 34 | + """ |
| 35 | + Build a Haystack MCPToolset over Agoragentic's remote MCP transport. |
| 36 | +
|
| 37 | + Keep this toolset narrow. Large MCP tool surfaces tend to degrade model tool selection. |
| 38 | + """ |
| 39 | + from haystack_integrations.tools.mcp import MCPToolset, StreamableHttpServerInfo |
| 40 | + |
| 41 | + server_info = StreamableHttpServerInfo(url=mcp_url) |
| 42 | + selected_names = list(tool_names) if tool_names else recommended_public_tool_names() |
| 43 | + return MCPToolset(server_info=server_info, tool_names=selected_names) |
| 44 | + |
| 45 | + |
| 46 | +def build_execute_request( |
| 47 | + api_key: str, |
| 48 | + task: str, |
| 49 | + input_data: Optional[Dict[str, Any]] = None, |
| 50 | + constraints: Optional[Dict[str, Any]] = None, |
| 51 | + base_url: str = AGORAGENTIC_BASE_URL, |
| 52 | +) -> Dict[str, Any]: |
| 53 | + return { |
| 54 | + "url": f"{base_url}/api/execute", |
| 55 | + "method": "POST", |
| 56 | + "headers": { |
| 57 | + "Authorization": f"Bearer {api_key}", |
| 58 | + "Content-Type": "application/json", |
| 59 | + }, |
| 60 | + "json": { |
| 61 | + "task": task, |
| 62 | + "input": input_data or {}, |
| 63 | + "constraints": constraints or {}, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | +def match( |
| 69 | + api_key: str, |
| 70 | + task: str, |
| 71 | + constraints: Optional[Dict[str, Any]] = None, |
| 72 | + base_url: str = AGORAGENTIC_BASE_URL, |
| 73 | +) -> Dict[str, Any]: |
| 74 | + response = requests.get( |
| 75 | + f"{base_url}/api/execute/match", |
| 76 | + params={"task": task, **(constraints or {})}, |
| 77 | + headers={"Authorization": f"Bearer {api_key}"}, |
| 78 | + timeout=20, |
| 79 | + ) |
| 80 | + return response.json() |
| 81 | + |
| 82 | + |
| 83 | +def execute( |
| 84 | + api_key: str, |
| 85 | + task: str, |
| 86 | + input_data: Optional[Dict[str, Any]] = None, |
| 87 | + constraints: Optional[Dict[str, Any]] = None, |
| 88 | + base_url: str = AGORAGENTIC_BASE_URL, |
| 89 | +) -> Dict[str, Any]: |
| 90 | + request = build_execute_request( |
| 91 | + api_key=api_key, |
| 92 | + task=task, |
| 93 | + input_data=input_data, |
| 94 | + constraints=constraints, |
| 95 | + base_url=base_url, |
| 96 | + ) |
| 97 | + response = requests.post( |
| 98 | + request["url"], |
| 99 | + json=request["json"], |
| 100 | + headers=request["headers"], |
| 101 | + timeout=60, |
| 102 | + ) |
| 103 | + return response.json() |
0 commit comments