get_resource.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. -----------------------------------------------------------------------------------------
  3. INTERSEL - 4 cité d'Hauteville - 75010 PARIS
  4. RCS PARIS 488 379 660 - NAF 721Z
  5. File : tools/resource/get_resource.py
  6. Object : GetResource tool for the MCP server
  7. -----------------------------------------------------------------------------------------
  8. Modifications :
  9. - 2026/01/27 - Damien Lugagne-Delpon - V1.0.0 - Creation
  10. -----------------------------------------------------------------------------------------
  11. @copyright : Intersel 2026
  12. @author : Damien Lugagne-Delpon - damien.lugagne-delpon@livinweb.fr
  13. @version : 1.0.0 (to be updated to the current_version of the developed software if file is touched)
  14. -----------------------------------------------------------------------------------------
  15. """
  16. import logging
  17. from typing import Dict, Any
  18. import asyncio
  19. from mcp_server.tools.tool_class import Tool
  20. from mcp_server.api.modx_client import get_modx_client
  21. from mcp_server.config import settings
  22. from mcp_server.core.errors import (
  23. ResourceNotFoundError,
  24. MODXAPIError,
  25. InternalError,
  26. )
  27. logger = logging.getLogger(__name__)
  28. class GetResourceTool(Tool):
  29. """Outil pour lire une ressource MODX."""
  30. def __init__(self):
  31. """Initialiser l'outil GetResource."""
  32. super().__init__(
  33. name="get_resource",
  34. description="Retrieve a resource from MODX by its ID.",
  35. remarks="The resource must be available in the context with read permission. "
  36. "Returns full resource data including TVs and metadata.",
  37. parameters={
  38. "id": {
  39. "required":True,
  40. "type":"integer",
  41. "description":"ID of the resource to retrieve"
  42. },
  43. },
  44. handler=self.handler
  45. )
  46. async def handler(self, **kwargs) -> Dict[str, Any]:
  47. """
  48. Lire une ressource MODX.
  49. Returns:
  50. Dict avec structure {success, code, data, message}
  51. """
  52. try:
  53. # Extraire les paramètres
  54. resource_id = kwargs.get("id")
  55. # Validation basique
  56. if not isinstance(resource_id, int) or resource_id <= 0:
  57. return {
  58. "success": False,
  59. "code": 400,
  60. "data": [],
  61. "message": "id must be a positive integer"
  62. }
  63. # Appeler l'API MODX
  64. logger.info(f"Retrieving resource: ID {resource_id}")
  65. client = get_modx_client()
  66. endpoint = settings.MODX_ENDPOINTS["resource"]["read"].format(id=resource_id)
  67. response = client.get(endpoint)
  68. return {
  69. "success": response.get("success", False),
  70. "code": response.get("code", 500),
  71. "data": response.get("data", []),
  72. "message": response.get("message", "Resource retrieval completed")
  73. }
  74. except MODXAPIError as e:
  75. logger.error(f"MODX API error: {e.message}")
  76. return {
  77. "success": False,
  78. "code": e.code,
  79. "data": [],
  80. "message": e.message
  81. }
  82. except Exception as e:
  83. logger.error(f"Unexpected error in get_resource: {str(e)}")
  84. return {
  85. "success": False,
  86. "code": 500,
  87. "data": [],
  88. "message": f"Internal error: {str(e)}"
  89. }
  90. # Instance globale de l'outil
  91. get_resource_tool = GetResourceTool()
  92. # """Handler function to get the resource."""
  93. # pass