ソースを参照

update tool getPage allow agent to curl a page from livinweb url

Khanh Nguyen 5 ヶ月 前
親
コミット
11848fb56e
4 ファイル変更、118 行追加、3 行削除
  1. 50 0
      src/server_mcp/tools.py
  2. 3 0
      src/services/client_rest.py
  3. 6 0
      tests/conftest.py
  4. 59 3
      tests/test_tools.py

+ 50 - 0
src/server_mcp/tools.py

@@ -8,8 +8,11 @@ from pydantic import Field
 
 from mcp.types import ToolAnnotations
 
+import httpx
+
 from ..services.client_rest import ClientRest, HTTPMethod
 
+_READ_ONLY = ToolAnnotations(readOnlyHint=True)
 _WRITE_ONLY = ToolAnnotations(readOnlyHint=False, destructiveHint=False)
 _WRITE_DESTRUCTIVE = ToolAnnotations(readOnlyHint=False, destructiveHint=True)
 _WRITE_IDEMPOTENT = ToolAnnotations(
@@ -121,10 +124,28 @@ CategoryNameOpt = Annotated[str | None, Field(default=None, description="name of
 CategoryId = Annotated[int, Field(description="id of the category")]
 CategoryParent = Annotated[int | None, Field(default=None, description="parent id of the category (default: 0)")]
 
+# --- getPage ---
+
+PageUrl = Annotated[str, Field(description="url of the page")]
+PageKeyword = Annotated[str | None, Field(default=None, description="keyword to search in the page")]
+PageOffsetTop = Annotated[int, Field(default=0, description="number of characters before the keyword to return in the response")]
+PageStart = Annotated[
+    int, 
+    Field(default=0, description="start of the number of characters to search from")]
+PageLimit = Annotated[int, Field(default=100, description="limit of the number of characters to return in the response")]
 
 def _omit_none(**kwargs: Any) -> dict[str, Any]:
     return {key: value for key, value in kwargs.items() if value is not None}
 
+def _search_keyword(html: str, keyword: PageKeyword, offsetTop: PageOffsetTop, start: PageStart, limit: PageLimit) -> str:
+    if keyword is not None:
+        index = html.find(keyword, start)
+        if index != -1:
+            start = index - offsetTop
+            end = index + len(keyword) + limit
+            return html[start:end]
+    end = min(len(html), start + limit)
+    return html[start:end]
 
 def register_tools(mcp_server: Any, client_rest: ClientRest) -> None:
     """Enregistre les outils d'ecriture du serveur MCP."""
@@ -453,3 +474,32 @@ def register_tools(mcp_server: Any, client_rest: ClientRest) -> None:
             endpoint="/rest/tools/category",
             json=_omit_none(id=id, name=name, parent=parent),
         )
+
+    @mcp_server.tool(
+        description=(
+            "Récupérer une page MODX à partir de son url. "
+            "Chercher une section dans la page en fonction de mot clef."
+        ),
+        annotations=_READ_ONLY,
+    )
+    def getPage(
+        url: PageUrl,   
+        keyword: PageKeyword = None,
+        offsetTop: PageOffsetTop = 0,
+        start: PageStart = 0,
+        limit: PageLimit = 100,
+    ) -> str:
+        try:
+            html = client_rest.execute(
+                method=HTTPMethod.GET,
+                endpoint=f"/{url}",
+            )
+        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)

+ 3 - 0
src/services/client_rest.py

@@ -6,6 +6,7 @@ from enum import StrEnum
 from typing import Any
 
 import httpx
+from json import JSONDecodeError
 
 from src.config.initialization import Settings, get_settings
 
@@ -58,6 +59,8 @@ class ClientRest:
             return response.json()
         except httpx.TimeoutException as e:
             raise TimeoutError(f"Timeout lors de l'exécution de la requête: {e}") from e
+        except JSONDecodeError as e:
+            return response.text
 
     def _build_url(self, endpoint: str) -> str:
         endpoint = endpoint if endpoint.startswith("/") else f"/{endpoint}"

+ 6 - 0
tests/conftest.py

@@ -49,6 +49,12 @@ async def call_tool(session: ClientSession, tool_name: str, params: dict[str, An
     return json.loads(result.content[0].text)
 
 
+async def call_tool_text(session: ClientSession, tool_name: str, params: dict[str, Any]) -> str:
+    """Appelle un outil MCP dont le retour est une chaîne brute (ex. getPage)."""
+    result = await session.call_tool(tool_name, params)
+    return result.content[0].text
+
+
 @pytest.fixture
 def require_integration_env() -> None:
     """Ignore le test si l'environnement d'integration live n'est pas active."""

+ 59 - 3
tests/test_tools.py

@@ -1,11 +1,12 @@
 from __future__ import annotations
 
+import httpx
 import pytest
 
 from unittest.mock import MagicMock
 
 from mcp.client.session import ClientSession
-from conftest import call_tool
+from conftest import call_tool, call_tool_text
 from src.services.client_rest import HTTPMethod
 
 @pytest.mark.anyio
@@ -15,7 +16,7 @@ class TestToolsMcpIntegration:
         tools_name = {tool.name for tool in result.tools}
         assert tools_name == {"createResource", "updateResource", "deleteResource",
                               "createTemplate", "updateTemplate", "createTv", 
-                              "updateTv", "createCategory", "updateCategory"}
+                              "updateTv", "createCategory", "updateCategory", "getPage"}
     
     async def test_create_resource(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
         client_rest.execute.return_value = {
@@ -199,5 +200,60 @@ class TestToolsMcpIntegration:
                 "id": 1,
             },
         }
+    
+    async def test_get_page_returns_html_slice(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
+        html = "0123456789" * 20
+        client_rest.execute.return_value = html
+
+        result = await call_tool_text(mcp_session, "getPage", {"url": "accueil/test", "limit": 50})
+
+        assert result == html[:50]
+        client_rest.execute.assert_called_once_with(
+            method=HTTPMethod.GET,
+            endpoint="/accueil/test",
+        )
 
-    
+    async def test_get_page_with_keyword(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
+        html = "aaaKEYWORDbbb"
+        client_rest.execute.return_value = html
+
+        result = await call_tool_text(
+            mcp_session,
+            "getPage",
+            {"url": "accueil/test", "keyword": "KEYWORD", "offsetTop": 2, "limit": 5},
+        )
+
+        assert result == html[1:15]
+        client_rest.execute.assert_called_once_with(
+            method=HTTPMethod.GET,
+            endpoint="/accueil/test",
+        )
+
+    async def test_get_page_retries_on_301(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
+        request = httpx.Request("GET", "https://example.com/accueil/test")
+        response = httpx.Response(301, request=request)
+        redirect_error = httpx.HTTPStatusError("Moved", request=request, response=response)
+        html = "<html><body>redirected</body></html>"
+        client_rest.execute.side_effect = [redirect_error, html]
+
+        result = await call_tool_text(mcp_session, "getPage", {"url": "accueil/test"})
+
+        assert result == html[:100]
+        assert client_rest.execute.call_count == 2
+        client_rest.execute.assert_any_call(method=HTTPMethod.GET, endpoint="/accueil/test")
+        client_rest.execute.assert_any_call(method=HTTPMethod.GET, endpoint="/accueil/test/")
+
+    async def test_get_page_propagates_http_error(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
+        request = httpx.Request("GET", "https://example.com/missing")
+        response = httpx.Response(404, request=request)
+        not_found_error = httpx.HTTPStatusError("Not Found", request=request, response=response)
+        client_rest.execute.side_effect = not_found_error
+
+        result = await mcp_session.call_tool("getPage", {"url": "missing"})
+
+        assert result.isError is True
+        assert "Not Found" in result.content[0].text
+        client_rest.execute.assert_called_once_with(
+            method=HTTPMethod.GET,
+            endpoint="/missing",
+        )