| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- """
- -----------------------------------------------------------------------------------------
- INTERSEL - 4 cité d'Hauteville - 75010 PARIS
- RCS PARIS 488 379 660 - NAF 721Z
- File : tools/resource/get_resource.py
- Object : GetResource tool for the MCP server
- -----------------------------------------------------------------------------------------
- Modifications :
- - 2026/01/27 - Damien Lugagne-Delpon - V1.0.0 - Creation
- -----------------------------------------------------------------------------------------
- @copyright : Intersel 2026
- @author : Damien Lugagne-Delpon - damien.lugagne-delpon@livinweb.fr
- @version : 1.0.0 (to be updated to the current_version of the developed software if file is touched)
- -----------------------------------------------------------------------------------------
- """
- import logging
- from typing import Dict, Any
- import asyncio
- from mcp_server.tools.tool_class import Tool
- from mcp_server.api.modx_client import get_modx_client
- from mcp_server.config import settings
- from mcp_server.core.errors import (
- ResourceNotFoundError,
- MODXAPIError,
- InternalError,
- )
- logger = logging.getLogger(__name__)
- class GetResourceTool(Tool):
- """Outil pour lire une ressource MODX."""
- def __init__(self):
- """Initialiser l'outil GetResource."""
- super().__init__(
- name="get_resource",
- description="Retrieve a resource from MODX by its ID.",
- remarks="The resource must be available in the context with read permission. "
- "Returns full resource data including TVs and metadata.",
- parameters={
- "id": {
- "required":True,
- "type":"integer",
- "description":"ID of the resource to retrieve"
- },
- },
- handler=self.handler
- )
- async def handler(self, **kwargs) -> Dict[str, Any]:
- """
- Lire une ressource MODX.
-
- Returns:
- Dict avec structure {success, code, data, message}
- """
- try:
- # Extraire les paramètres
- resource_id = kwargs.get("id")
- # Validation basique
- if not isinstance(resource_id, int) or resource_id <= 0:
- return {
- "success": False,
- "code": 400,
- "data": [],
- "message": "id must be a positive integer"
- }
- # Appeler l'API MODX
- logger.info(f"Retrieving resource: ID {resource_id}")
- client = get_modx_client()
- endpoint = settings.MODX_ENDPOINTS["resource"]["read"].format(id=resource_id)
- response = client.get(endpoint)
- return {
- "success": response.get("success", False),
- "code": response.get("code", 500),
- "data": response.get("data", []),
- "message": response.get("message", "Resource retrieval completed")
- }
- except MODXAPIError as e:
- logger.error(f"MODX API error: {e.message}")
- return {
- "success": False,
- "code": e.code,
- "data": [],
- "message": e.message
- }
- except Exception as e:
- logger.error(f"Unexpected error in get_resource: {str(e)}")
- return {
- "success": False,
- "code": 500,
- "data": [],
- "message": f"Internal error: {str(e)}"
- }
- # Instance globale de l'outil
- get_resource_tool = GetResourceTool()
- # """Handler function to get the resource."""
- # pass
|