Forward a request to the NLWeb MCP endpoint
(function_name: str, arguments: dict[str, Any], server_url: str, endpoint: str)
| 21 | DEFAULT_ENDPOINT = "/mcp" |
| 22 | |
| 23 | async def forward_to_nlweb(function_name: str, arguments: dict[str, Any], server_url: str, endpoint: str) -> dict[str, Any]: |
| 24 | """Forward a request to the NLWeb MCP endpoint""" |
| 25 | nlweb_mcp_url = f"{server_url}{endpoint}" |
| 26 | try: |
| 27 | # Format the request in MCP format |
| 28 | payload = { |
| 29 | "function_call": { |
| 30 | "name": function_name, |
| 31 | "arguments": json.dumps(arguments) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | # Print some debug info to stderr (won't interfere with stdio protocol) |
| 36 | print(f"Forwarding to {nlweb_mcp_url}: {function_name}", file=sys.stderr) |
| 37 | |
| 38 | async with httpx.AsyncClient() as client: |
| 39 | response = await client.post( |
| 40 | nlweb_mcp_url, |
| 41 | json=payload, |
| 42 | headers={"Content-Type": "application/json"}, |
| 43 | timeout=30 |
| 44 | ) |
| 45 | |
| 46 | if response.status_code != 200: |
| 47 | print(f"Error from server: {response.status_code} - {response.text}", file=sys.stderr) |
| 48 | return { |
| 49 | "error": f"Server error: {response.status_code} - {response.text}" |
| 50 | } |
| 51 | |
| 52 | result = response.json() |
| 53 | return result |
| 54 | |
| 55 | except Exception as e: |
| 56 | print(f"Request failed: {e!s}", file=sys.stderr) |
| 57 | return { |
| 58 | "error": f"Request failed: {e!s}" |
| 59 | } |
| 60 | |
| 61 | async def serve(server_url: str = DEFAULT_SERVER_URL, endpoint: str = DEFAULT_ENDPOINT) -> None: |
| 62 | """ |
no outgoing calls
no test coverage detected