Spaces:
Runtime error
Runtime error
Create client_example.py
Browse files- client_example.py +86 -0
client_example.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Helper script to verify the SAP MCP server is reachable and working.
|
| 2 |
+
|
| 3 |
+
Usage examples::
|
| 4 |
+
|
| 5 |
+
python client_example.py
|
| 6 |
+
python client_example.py --base-url https://your-space.hf.space --top 5
|
| 7 |
+
|
| 8 |
+
You can also set the environment variable ``MCP_SERVER_URL`` instead of
|
| 9 |
+
passing ``--base-url``.
|
| 10 |
+
"""
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
import argparse
|
| 14 |
+
import asyncio
|
| 15 |
+
import json
|
| 16 |
+
import os
|
| 17 |
+
from typing import Any
|
| 18 |
+
|
| 19 |
+
import httpx
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _normalise_base_url(url: str) -> str:
|
| 23 |
+
"""Ensure the base URL does not end with a slash."""
|
| 24 |
+
return url[:-1] if url.endswith("/") else url
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
async def _get_json(client: httpx.AsyncClient, url: str) -> Any:
|
| 28 |
+
response = await client.get(url)
|
| 29 |
+
response.raise_for_status()
|
| 30 |
+
return response.json()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
async def _post_json(client: httpx.AsyncClient, url: str, payload: dict[str, Any]) -> Any:
|
| 34 |
+
response = await client.post(url, json=payload)
|
| 35 |
+
response.raise_for_status()
|
| 36 |
+
return response.json()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
async def main(base_url: str, top: int) -> None:
|
| 40 |
+
base_url = _normalise_base_url(base_url)
|
| 41 |
+
print(f"Checking MCP server at {base_url} ...\n")
|
| 42 |
+
|
| 43 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 44 |
+
# 1. Health check
|
| 45 |
+
health = await _get_json(client, f"{base_url}/")
|
| 46 |
+
print("Health endpoint (/):")
|
| 47 |
+
print(json.dumps(health, indent=2), "\n")
|
| 48 |
+
|
| 49 |
+
# 2. Discover tools
|
| 50 |
+
tools_payload = await _get_json(client, f"{base_url}/mcp/tools")
|
| 51 |
+
print("Discovered tools:")
|
| 52 |
+
print(json.dumps(tools_payload, indent=2), "\n")
|
| 53 |
+
|
| 54 |
+
if not tools_payload.get("tools"):
|
| 55 |
+
print("No tools were returned. Check the server logs and ensure the deployment includes app.py.")
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
+
# 3. Call the purchase requisition tool
|
| 59 |
+
tool_endpoint = f"{base_url}/mcp/tools/list_purchase_requisitions"
|
| 60 |
+
print(f"Calling list_purchase_requisitions (top={top}) ...")
|
| 61 |
+
result = await _post_json(client, tool_endpoint, {"top": top})
|
| 62 |
+
|
| 63 |
+
print("\nTool response:")
|
| 64 |
+
print(json.dumps(result, indent=2))
|
| 65 |
+
print("\nDone.")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
parser = argparse.ArgumentParser(description="Quick MCP tool smoke test")
|
| 70 |
+
parser.add_argument(
|
| 71 |
+
"--base-url",
|
| 72 |
+
default=os.environ.get("MCP_SERVER_URL", "http://localhost:8000"),
|
| 73 |
+
help="Root URL of the MCP server (default: http://localhost:8000 or MCP_SERVER_URL env)",
|
| 74 |
+
)
|
| 75 |
+
parser.add_argument(
|
| 76 |
+
"--top",
|
| 77 |
+
type=int,
|
| 78 |
+
default=5,
|
| 79 |
+
help="Number of purchase requisitions to request (default: 5)",
|
| 80 |
+
)
|
| 81 |
+
args = parser.parse_args()
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
asyncio.run(main(args.base_url, args.top))
|
| 85 |
+
except httpx.HTTPError as exc: # pragma: no cover - best effort CLI feedback
|
| 86 |
+
raise SystemExit(f"Request failed: {exc}") from exc
|