| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- """Definition des ressources MCP (operations de lecture)."""
- from __future__ import annotations
- from typing import Any
- import httpx
- from ..services.client_rest import ClientRest, HTTPMethod
- _GET_RESOURCE_WHITELIST = {
- "id",
- "alias",
- "cacheable",
- "class_key",
- "context_key",
- "content",
- "content_type",
- "description",
- "hidemenu",
- "introtext",
- "isfolder",
- "link_attributes",
- "longtitle",
- "menuindex",
- "menutitle",
- "pagetitle",
- "parent",
- "published",
- "template",
- "uri",
- "full_uri",
- "imxcache",
- "templateVars",
- }
- _GET_RESOURCES_WHITELIST = {
- "id",
- "alias",
- "class_key",
- "content",
- "description",
- "isfolder",
- "pagetitle",
- "parent",
- "published",
- "template",
- "uri",
- }
- def _filter_dict(data: dict[str, Any], whitelist: set[str]) -> dict[str, Any]:
- return {key: value for key, value in data.items() if key in whitelist}
- def _filter_list(items: list[Any], whitelist: set[str]) -> list[Any]:
- filtered: list[Any] = []
- for item in items:
- if isinstance(item, dict):
- filtered.append(_filter_dict(item, whitelist))
- else:
- filtered.append(item)
- return filtered
- def _unwrap_data(response: Any) -> Any:
- """Extrait le payload utile des réponses LWE {code,message,data}."""
- if isinstance(response, dict) and "data" in response:
- return response["data"]
- return response
- def register_element_resources(mcp_server: Any, client_rest: ClientRest) -> None:
- """Enregistre les ressources de lecture du serveur MCP."""
- @mcp_server.resource("modx://resource/{id}",
- mime_type="application/json",
- description="Récupérer une ressource MODX.",
- )
- def get_resource(id: str) -> dict[str, Any]:
- try:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint=f"/rest/tools/resource/{id}",
- params={"showUnpublished": 1},
- )
- except httpx.HTTPStatusError:
- # Certains serveurs REST LWE retournent 500 avec ce parametre.
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint=f"/rest/tools/resource/{id}",
- )
- data = _unwrap_data(response)
- if isinstance(data, dict):
- return _filter_dict(data, _GET_RESOURCE_WHITELIST)
- return {"data": response}
- @mcp_server.resource("modx://resources/{id}",
- mime_type="application/json",
- description="Récupérer une liste de ressources MODX enfants niveau 1 d'un parent donné.",
- )
- def get_resources(id: str) -> dict[str, Any]:
- try:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint=f"/rest/tools/getresources/{id}",
- params={"depth": -1, "includedTv": 0, "showUnpublished": 1},
- )
- except httpx.HTTPStatusError:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint=f"/rest/tools/getresources/{id}",
- )
- data = _unwrap_data(response)
- if isinstance(data, list):
- return {"items": _filter_list(data, _GET_RESOURCES_WHITELIST)}
- if isinstance(data, dict):
- if "items" in data and isinstance(data["items"], list):
- return {
- **data,
- "items": _filter_list(data["items"], _GET_RESOURCES_WHITELIST),
- }
- return _filter_dict(data, _GET_RESOURCES_WHITELIST)
- return {"data": response}
- @mcp_server.resource("modx://template/{id}",
- mime_type="application/json",
- description="Récupérer un template MODX et ses variables de modèle (TV) liées.",
- )
- def get_template(id: str) -> dict[str, Any]:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint=f"/rest/tools/template/{id}",
- )
- data = _unwrap_data(response)
- return data if isinstance(data, dict) else {"data": data}
- @mcp_server.resource("modx://templates",
- mime_type="application/json",
- description="Récupérer une liste de templates MODX.",
- )
- def get_templates() -> dict[str, Any]:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint="/rest/tools/templates",
- )
- data = _unwrap_data(response)
- return {"items": data} if isinstance(data, list) else {"data": data}
- @mcp_server.resource("modx://tv/{id}",
- mime_type="application/json",
- description="Récupérer une variable de modèle (TV) spécifique.",
- )
- def get_tv(id: str) -> dict[str, Any]:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint=f"/rest/tools/tv/{id}",
- )
- data = _unwrap_data(response)
- return data if isinstance(data, dict) else {"data": data}
- @mcp_server.resource("modx://category/{id}",
- mime_type="application/json",
- description="Récupérer une catégorie MODX.",
- )
- def get_category(id: str) -> dict[str, Any]:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint=f"/rest/tools/category/{id}",
- )
- data = _unwrap_data(response)
- return data if isinstance(data, dict) else {"data": data}
- @mcp_server.resource("modx://categories",
- mime_type="application/json",
- description="Récupérer une liste de catégories MODX.",
- )
- def get_categories() -> dict[str, Any]:
- try:
- response = client_rest.execute(
- method=HTTPMethod.GET,
- endpoint="/rest/tools/categories",
- )
- data = _unwrap_data(response)
- return {"items": data} if isinstance(data, list) else {"data": data}
- except httpx.HTTPStatusError:
- # Endpoint non expose sur certains serveurs LWE.
- return {
- "items": [],
- "warning": "Endpoint /rest/tools/categories indisponible sur ce serveur.",
- }
|