helpers.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from typing import Any, Dict, List, Optional
  2. import requests
  3. from .config import GRAPH, MCP_URL
  4. class GardenLayer:
  5. """Domain helpers that orchestrate MCP calls for your garden project."""
  6. def __init__(self, mcp_url: str = MCP_URL):
  7. self.mcp_url = mcp_url
  8. def _call(self, tool: str, payload: Dict[str, Any]) -> Any:
  9. response = requests.post(self.mcp_url, json={"tool": tool, "input": payload}, timeout=10)
  10. response.raise_for_status()
  11. body = response.json()
  12. if body.get("status") != "ok":
  13. raise RuntimeError(body.get("detail", "MCP tool failed"))
  14. return body["result"]
  15. def traverse(self, subject_uri: str, property_uri: str, direction: str = "outgoing", limit: int = 20) -> Any:
  16. return self._call("traverse_property", {
  17. "subject_uri": subject_uri,
  18. "property_uri": property_uri,
  19. "direction": direction,
  20. "limit": limit,
  21. })
  22. def describe_subject(self, subject_uri: str, limit: int = 10) -> Any:
  23. return self._call("describe_subject", {
  24. "subject_uri": subject_uri,
  25. "limit": limit,
  26. })
  27. def path_traverse(
  28. self,
  29. subject_uri: str,
  30. property_path: List[str],
  31. direction: str = "outgoing",
  32. limit: int = 10,
  33. ) -> Any:
  34. return self._call("path_traverse", {
  35. "subject_uri": subject_uri,
  36. "property_path": property_path,
  37. "direction": direction,
  38. "limit": limit,
  39. })
  40. def property_usage_statistics(self, property_uri: str, examples_limit: int = 5) -> Any:
  41. return self._call("property_usage_statistics", {
  42. "property_uri": property_uri,
  43. "examples_limit": examples_limit,
  44. })
  45. def batch_insert(self, ttl: str, graph: Optional[str] = None) -> Any:
  46. payload = {"ttl": ttl}
  47. if graph:
  48. payload["graph"] = graph
  49. return self._call("batch_insert", payload)
  50. def add_seedling(
  51. self,
  52. plant_uri: str,
  53. seed_product_uri: str,
  54. cycle_uri: str,
  55. label: str,
  56. strain_uri: str = "http://world.eu.org/example1#Strain_lucky_experimental_line",
  57. graph: str = GRAPH,
  58. ) -> None:
  59. triples = [
  60. ("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://world.eu.org/cannabis-breeding#IndividualPlant", "uri"),
  61. ("http://www.w3.org/2000/01/rdf-schema#label", label, "literal"),
  62. ("http://world.eu.org/cannabis-breeding#inCycle", cycle_uri, "uri"),
  63. ("http://world.eu.org/cannabis-breeding#grownFromSeedProduct", seed_product_uri, "uri"),
  64. ("http://world.eu.org/cannabis-breeding#belongsToStrain", strain_uri, "uri"),
  65. ]
  66. for predicate, obj, obj_type in triples:
  67. self._call("insert_triple", {
  68. "subject": plant_uri,
  69. "predicate": predicate,
  70. "object": obj,
  71. "object_type": obj_type,
  72. "graph": graph,
  73. })