|
|
@@ -2,7 +2,9 @@
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
+import json
|
|
|
from typing import Annotated, Any
|
|
|
+from urllib.parse import urlparse
|
|
|
|
|
|
from pydantic import Field
|
|
|
|
|
|
@@ -73,7 +75,7 @@ TemplateName = Annotated[str, Field(description="name of the template")]
|
|
|
TemplateNameOpt = Annotated[str | None, Field(default=None, description="name of the template")]
|
|
|
TemplateIdMandatory = Annotated[int, Field(description="id of the template")]
|
|
|
TemplateDescription = Annotated[str | None, Field(default=None, description="description of the template")]
|
|
|
-Category = Annotated[str | None, Field(default=None, description="category the element belongs in")]
|
|
|
+Category = Annotated[int | None, Field(default=None, description="category id the element belongs in")]
|
|
|
IsStatic = Annotated[
|
|
|
bool | None,
|
|
|
Field(default=None, description="if true, the element source is stored in an external file (default: false)")
|
|
|
@@ -83,13 +85,17 @@ StaticFile = Annotated[
|
|
|
Field(default=None, description="if isStatic true, path of the external file where the element source is stored")
|
|
|
]
|
|
|
TemplateContent = Annotated[str | None, Field(default=None, description="template code (HTML)")]
|
|
|
-LockTemplate = Annotated[
|
|
|
- bool | None,
|
|
|
- Field(default=None, description="lock template for editing; requires edit_locked attribute (default: false)")
|
|
|
+Locked = Annotated[
|
|
|
+ bool | None,
|
|
|
+ Field(default=None, description="lock template for editing (default: false)"),
|
|
|
+]
|
|
|
+TemplateProperties = Annotated[
|
|
|
+ dict[str, Any] | list[Any] | str | None,
|
|
|
+ Field(default=None, description="template properties (JSON)"),
|
|
|
]
|
|
|
TemplateVarsAccess = Annotated[
|
|
|
dict[str, Any] | list[Any] | str | None,
|
|
|
- Field(default=None, description="JSON list that indicates the access of template vars (default: {})")
|
|
|
+ Field(default=None, description="JSON list of linked TVs: [{id, access, rank}] or [tvId, ...]")
|
|
|
]
|
|
|
|
|
|
# --- createTv / updateTv ---
|
|
|
@@ -99,22 +105,36 @@ TvNameOpt = Annotated[str | None, Field(default=None, description="name of the T
|
|
|
TvId = Annotated[int, Field(description="id of the TV")]
|
|
|
TvDescription = Annotated[str | None, Field(default=None, description="description of the template variable")]
|
|
|
TvCaption = Annotated[str | None, Field(default=None, description="text displayed beside the TV when editing it in a Resource")]
|
|
|
-TvCategory = Annotated[str | None, Field(default=None, description="category the TV belongs in")]
|
|
|
-TvType = Annotated[str | None, Field(default=None, description="the type of TV")]
|
|
|
+TvCategory = Annotated[int | None, Field(default=None, description="category id the TV belongs in")]
|
|
|
+TvType = Annotated[str, Field(description="the type of TV (e.g. text, textarea, listbox)")]
|
|
|
+TvTypeOpt = Annotated[str | None, Field(default=None, description="the type of TV (e.g. text, textarea, listbox)")]
|
|
|
TvElements = Annotated[
|
|
|
str | None,
|
|
|
- Field(default=None, description="list of options for TV; only use if the type of TV is listbox"),
|
|
|
+ Field(default=None, description="option elements for list-type TVs"),
|
|
|
]
|
|
|
-TvDefaultText = Annotated[
|
|
|
- str | None,
|
|
|
- Field(default=None, description="default option for TV; only use if the type of TV is listbox"),
|
|
|
+TvDefaultText = Annotated[str | None, Field(default=None, description="default value")]
|
|
|
+TvRank = Annotated[int | None, Field(default=None, description="TV rank/order")]
|
|
|
+TvDisplay = Annotated[str | None, Field(default=None, description="output widget type")]
|
|
|
+TvProperties = Annotated[
|
|
|
+ dict[str, Any] | list[Any] | str | None,
|
|
|
+ Field(default=None, description="TV properties (JSON)"),
|
|
|
+]
|
|
|
+TvInputProperties = Annotated[
|
|
|
+ dict[str, Any] | list[Any] | str | None,
|
|
|
+ Field(default=None, description="input widget properties (JSON)"),
|
|
|
+]
|
|
|
+TvOutputProperties = Annotated[
|
|
|
+ dict[str, Any] | list[Any] | str | None,
|
|
|
+ Field(default=None, description="output widget properties (JSON)"),
|
|
|
]
|
|
|
-TvInputPropeties = Annotated[
|
|
|
- str | None, Field(default=None, description="JSON list of the input properties of the TV")
|
|
|
+TvEditorType = Annotated[int | None, Field(default=None, description="editor type id")]
|
|
|
+LockTemplate = Annotated[
|
|
|
+ bool | None,
|
|
|
+ Field(default=None, description="lock TV for editing (maps to locked, default: false)"),
|
|
|
]
|
|
|
TvTemplates = Annotated[
|
|
|
dict[str, Any] | list[Any] | str | None,
|
|
|
- Field(default=None, description="JSON list that indicates the access of templates (default: {})")
|
|
|
+ Field(default=None, description="JSON list of linked templates: [{id, access, rank}] or [templateId, ...]")
|
|
|
]
|
|
|
|
|
|
# --- createCategory / updateCategory ---
|
|
|
@@ -123,6 +143,7 @@ CategoryName = Annotated[str, Field(description="name of the category")]
|
|
|
CategoryNameOpt = Annotated[str | None, Field(default=None, description="name of the category")]
|
|
|
CategoryId = Annotated[int, Field(description="id of the category")]
|
|
|
CategoryParent = Annotated[int | None, Field(default=None, description="parent id of the category (default: 0)")]
|
|
|
+CategoryRank = Annotated[int | None, Field(default=None, description="category rank/order")]
|
|
|
|
|
|
# --- getPage ---
|
|
|
|
|
|
@@ -141,6 +162,25 @@ PageLimit = Annotated[
|
|
|
def _omit_none(**kwargs: Any) -> dict[str, Any]:
|
|
|
return {key: value for key, value in kwargs.items() if value is not None}
|
|
|
|
|
|
+
|
|
|
+def _response_to_text(response: Any) -> str:
|
|
|
+ """Convertit la reponse ClientRest en texte exploitable par getPage."""
|
|
|
+ if isinstance(response, dict):
|
|
|
+ if "text" in response:
|
|
|
+ return response["text"]
|
|
|
+ return json.dumps(response, ensure_ascii=False)
|
|
|
+ return str(response)
|
|
|
+
|
|
|
+
|
|
|
+def _redirect_endpoint(location: str) -> str:
|
|
|
+ """Extrait le chemin relatif depuis un en-tete Location."""
|
|
|
+ parsed = urlparse(location)
|
|
|
+ endpoint = parsed.path or "/"
|
|
|
+ if parsed.query:
|
|
|
+ endpoint = f"{endpoint}?{parsed.query}"
|
|
|
+ return endpoint
|
|
|
+
|
|
|
+
|
|
|
def _search_keyword(html: str, keyword: PageKeyword, offsetTop: PageOffsetTop, start: PageStart, limit: PageLimit) -> str:
|
|
|
if limit == 0:
|
|
|
limit = len(html)
|
|
|
@@ -316,7 +356,8 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
isStatic: IsStatic = None,
|
|
|
staticFile: StaticFile = None,
|
|
|
content: TemplateContent = None,
|
|
|
- lockTemplate: LockTemplate = None,
|
|
|
+ locked: Locked = None,
|
|
|
+ properties: TemplateProperties = None,
|
|
|
templateVars: TemplateVarsAccess = None,
|
|
|
) -> dict[str, Any]:
|
|
|
return client_rest.execute(
|
|
|
@@ -329,7 +370,8 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
isStatic=isStatic,
|
|
|
staticFile=staticFile,
|
|
|
content=content,
|
|
|
- lockTemplate=lockTemplate,
|
|
|
+ locked=locked,
|
|
|
+ properties=properties,
|
|
|
templateVars=templateVars,
|
|
|
),
|
|
|
)
|
|
|
@@ -350,7 +392,8 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
isStatic: IsStatic = None,
|
|
|
staticFile: StaticFile = None,
|
|
|
content: TemplateContent = None,
|
|
|
- lockTemplate: LockTemplate = None,
|
|
|
+ locked: Locked = None,
|
|
|
+ properties: TemplateProperties = None,
|
|
|
templateVars: TemplateVarsAccess = None,
|
|
|
) -> dict[str, Any]:
|
|
|
return client_rest.execute(
|
|
|
@@ -364,7 +407,8 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
isStatic=isStatic,
|
|
|
staticFile=staticFile,
|
|
|
content=content,
|
|
|
- lockTemplate=lockTemplate,
|
|
|
+ locked=locked,
|
|
|
+ properties=properties,
|
|
|
templateVars=templateVars,
|
|
|
),
|
|
|
)
|
|
|
@@ -375,16 +419,21 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
)
|
|
|
def createTv(
|
|
|
name: TvName,
|
|
|
+ type: TvType,
|
|
|
description: TvDescription = None,
|
|
|
caption: TvCaption = None,
|
|
|
category: TvCategory = None,
|
|
|
- type: TvType = None,
|
|
|
elements: TvElements = None,
|
|
|
+ rank: TvRank = None,
|
|
|
+ display: TvDisplay = None,
|
|
|
defaultText: TvDefaultText = None,
|
|
|
+ properties: TvProperties = None,
|
|
|
+ inputProperties: TvInputProperties = None,
|
|
|
+ outputProperties: TvOutputProperties = None,
|
|
|
lockTemplate: LockTemplate = None,
|
|
|
- inputPropeties: TvInputPropeties = None,
|
|
|
isStatic: IsStatic = None,
|
|
|
staticFile: StaticFile = None,
|
|
|
+ editorType: TvEditorType = None,
|
|
|
templates: TvTemplates = None,
|
|
|
) -> dict[str, Any]:
|
|
|
return client_rest.execute(
|
|
|
@@ -392,16 +441,21 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
endpoint="/rest/tools/tv",
|
|
|
json=_omit_none(
|
|
|
name=name,
|
|
|
+ type=type,
|
|
|
description=description,
|
|
|
caption=caption,
|
|
|
category=category,
|
|
|
- type=type,
|
|
|
elements=elements,
|
|
|
+ rank=rank,
|
|
|
+ display=display,
|
|
|
defaultText=defaultText,
|
|
|
+ properties=properties,
|
|
|
+ inputProperties=inputProperties,
|
|
|
+ outputProperties=outputProperties,
|
|
|
lockTemplate=lockTemplate,
|
|
|
- inputPropeties=inputPropeties,
|
|
|
isStatic=isStatic,
|
|
|
staticFile=staticFile,
|
|
|
+ editorType=editorType,
|
|
|
templates=templates,
|
|
|
),
|
|
|
)
|
|
|
@@ -417,16 +471,21 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
def updateTv(
|
|
|
id: TvId,
|
|
|
name: TvNameOpt = None,
|
|
|
+ type: TvTypeOpt = None,
|
|
|
description: TvDescription = None,
|
|
|
caption: TvCaption = None,
|
|
|
category: TvCategory = None,
|
|
|
- type: TvType = None,
|
|
|
elements: TvElements = None,
|
|
|
+ rank: TvRank = None,
|
|
|
+ display: TvDisplay = None,
|
|
|
defaultText: TvDefaultText = None,
|
|
|
+ properties: TvProperties = None,
|
|
|
+ inputProperties: TvInputProperties = None,
|
|
|
+ outputProperties: TvOutputProperties = None,
|
|
|
lockTemplate: LockTemplate = None,
|
|
|
- inputPropeties: TvInputPropeties = None,
|
|
|
isStatic: IsStatic = None,
|
|
|
staticFile: StaticFile = None,
|
|
|
+ editorType: TvEditorType = None,
|
|
|
templates: TvTemplates = None,
|
|
|
) -> dict[str, Any]:
|
|
|
return client_rest.execute(
|
|
|
@@ -435,16 +494,21 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
json=_omit_none(
|
|
|
id=id,
|
|
|
name=name,
|
|
|
+ type=type,
|
|
|
description=description,
|
|
|
caption=caption,
|
|
|
category=category,
|
|
|
- type=type,
|
|
|
elements=elements,
|
|
|
+ rank=rank,
|
|
|
+ display=display,
|
|
|
defaultText=defaultText,
|
|
|
+ properties=properties,
|
|
|
+ inputProperties=inputProperties,
|
|
|
+ outputProperties=outputProperties,
|
|
|
lockTemplate=lockTemplate,
|
|
|
- inputPropeties=inputPropeties,
|
|
|
isStatic=isStatic,
|
|
|
staticFile=staticFile,
|
|
|
+ editorType=editorType,
|
|
|
templates=templates,
|
|
|
),
|
|
|
)
|
|
|
@@ -456,11 +520,12 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
def createCategory(
|
|
|
name: CategoryName,
|
|
|
parent: CategoryParent = None,
|
|
|
+ rank: CategoryRank = None,
|
|
|
) -> dict[str, Any]:
|
|
|
return client_rest.execute(
|
|
|
method=HTTPMethod.POST,
|
|
|
endpoint="/rest/tools/category",
|
|
|
- json=_omit_none(name=name, parent=parent),
|
|
|
+ json=_omit_none(name=name, parent=parent, rank=rank),
|
|
|
)
|
|
|
|
|
|
@mcp_server.tool(
|
|
|
@@ -475,11 +540,12 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
id: CategoryId,
|
|
|
name: CategoryNameOpt = None,
|
|
|
parent: CategoryParent = None,
|
|
|
+ rank: CategoryRank = None,
|
|
|
) -> dict[str, Any]:
|
|
|
return client_rest.execute(
|
|
|
method=HTTPMethod.PUT,
|
|
|
endpoint="/rest/tools/category",
|
|
|
- json=_omit_none(id=id, name=name, parent=parent),
|
|
|
+ json=_omit_none(id=id, name=name, parent=parent, rank=rank),
|
|
|
)
|
|
|
|
|
|
@mcp_server.tool(
|
|
|
@@ -497,16 +563,20 @@ def register_element_tools(mcp_server: Any, client_rest: ClientRest) -> None:
|
|
|
limit: PageLimit = 1000,
|
|
|
) -> str:
|
|
|
try:
|
|
|
- html = client_rest.execute(
|
|
|
+ response = client_rest.execute(
|
|
|
method=HTTPMethod.GET,
|
|
|
- endpoint=f"/{url}",
|
|
|
+ endpoint=f"/{url}" if url else "/",
|
|
|
)
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
- if e.response.status_code == 301:
|
|
|
- html = client_rest.execute(
|
|
|
- method=HTTPMethod.GET,
|
|
|
- endpoint=f"/{url}/",
|
|
|
- )
|
|
|
- else:
|
|
|
- raise e
|
|
|
- return _search_keyword(html, keyword, offsetTop, start, limit)
|
|
|
+ if e.response.status_code not in (301, 302, 303, 307, 308):
|
|
|
+ raise
|
|
|
+ location = e.response.headers.get("location")
|
|
|
+ if not location:
|
|
|
+ raise
|
|
|
+ response = client_rest.execute(
|
|
|
+ method=HTTPMethod.GET,
|
|
|
+ endpoint=_redirect_endpoint(location),
|
|
|
+ )
|
|
|
+ return _search_keyword(
|
|
|
+ _response_to_text(response), keyword, offsetTop, start, limit
|
|
|
+ )
|