Protokoll #28327

ID28327
Zeitstempel2025-12-28 13:12:46.089057
Clientroot
IP145.224.72.140
Modellclaude-sonnet-4-20250514
Statuscompleted
Tokens4,207 (Input: 1,234, Output: 2,973)
Dauer425 ms
Request-Zeit2025-12-28 13:12:46.089057
Response-Zeit2025-12-28 13:12:46.514020

Request

{
    "event": "PreToolUse",
    "tool_name": "Write",
    "tool_input": {
        "file_path": "\/var\/www\/mcp-servers\/mcp_db\/tools\/describe_tool.py",
        "content": "\"\"\"Describe Tool - Zeigt Tabellen-Struktur an.\"\"\"\n\nimport re\nimport time\nfrom datetime import datetime\n\nfrom constants import LOG_ENTRY_MAX_LENGTH, LOG_QUERY_MAX_LENGTH, MS_PER_SECOND\n\nfrom config import Config\nfrom infrastructure.db_connection import DatabaseConnection\nfrom shared.domain import LogEntry\nfrom shared.infrastructure import get_logger\n\n\ndef register_describe_tool(mcp) -> None:\n    \"\"\"Registriert db_describe Tool.\"\"\"\n    logger = get_logger(\"mcp-db\", Config)\n\n    @mcp.tool()\n    def db_describe(\n        table: str,\n        database: str = \"ki_dev\",\n        show_create: bool = False,\n    ) -> dict:\n        \"\"\"\n        Zeigt Tabellen-Struktur an.\n\n        Args:\n            table: Tabellenname\n            database: Zieldatenbank (ki_dev oder ki_content)\n            show_create: True = SHOW CREATE TABLE, False = DESCRIBE\n\n        Returns:\n            Dict mit columns oder create_statement\n        \"\"\"\n        start = time.time()\n\n        # Validierung: Tabellenname (SQL-Injection-Schutz)\n        if not re.match(r\"^[a-zA-Z0-9_]+$\", table):\n            return {\n                \"status\": \"denied\",\n                \"error\": \"Invalid table name. Only alphanumeric and underscore allowed.\",\n            }\n\n        # Validierung: Datenbank\n        if database not in Config.ALLOWED_DATABASES:\n            return {\n                \"status\": \"denied\",\n                \"error\": f\"Database '{database}' not allowed.\",\n            }\n\n        try:\n            with DatabaseConnection.get_connection(database) as conn:\n                cursor = conn.cursor(dictionary=True, buffered=True)\n\n                if show_create:\n                    cursor.execute(f\"SHOW CREATE TABLE `{table}`\")\n                    row = cursor.fetchone()\n                    cursor.close()\n\n                    duration = int((time.time() - start) * MS_PER_SECOND)\n\n                    # Log\n                    try:\n                        logger.log(\n                            LogEntry(\n                                timestamp=datetime.now(),\n                                client_name=\"mcp-db\",\n                                tool_name=\"db_describe\",\n                                request=f\"SHOW CREATE TABLE {table}\",\n                                status=\"success\",\n                                duration_ms=duration,\n                            )\n                        )\n                    except Exception:\n                        pass\n\n                    return {\n                        \"status\": \"success\",\n                        \"table\": table,\n                        \"create_statement\": row.get(\"Create Table\", \"\") if row else \"\",\n                        \"execution_ms\": duration,\n                    }\n                else:\n                    cursor.execute(f\"DESCRIBE `{table}`\")\n                    columns = cursor.fetchall()\n                    cursor.close()\n\n                    duration = int((time.time() - start) * MS_PER_SECOND)\n\n                    # Log\n                    try:\n                        logger.log(\n                            LogEntry(\n                                timestamp=datetime.now(),\n                                client_name=\"mcp-db\",\n                                tool_name=\"db_describe\",\n                                request=f\"DESCRIBE {table}\",\n                                status=\"success\",\n                                duration_ms=duration,\n                            )\n                        )\n                    except Exception:\n                        pass\n\n                    return {\n                        \"status\": \"success\",\n                        \"table\": table,\n                        \"columns\": columns,\n                        \"execution_ms\": duration,\n                    }\n\n        except Exception as e:\n            duration = int((time.time() - start) * MS_PER_SECOND)\n\n            try:\n                logger.log(\n                    LogEntry(\n                        timestamp=datetime.now(),\n                        client_name=\"mcp-db\",\n                        tool_name=\"db_describe\",\n                        request=f\"DESCRIBE {table}\",\n                        status=\"error\",\n                        duration_ms=duration,\n                        error_message=str(e)[:LOG_ENTRY_MAX_LENGTH],\n                    )\n                )\n            except Exception:\n                pass\n\n            return {\n                \"status\": \"error\",\n                \"error\": str(e)[:LOG_QUERY_MAX_LENGTH],\n                \"execution_ms\": duration,\n            }\n"
    }
}

Response

{
    "tool_response": {
        "type": "update",
        "filePath": "\/var\/www\/mcp-servers\/mcp_db\/tools\/describe_tool.py",
        "content": "\"\"\"Describe Tool - Zeigt Tabellen-Struktur an.\"\"\"\n\nimport re\nimport time\nfrom datetime import datetime\n\nfrom constants import LOG_ENTRY_MAX_LENGTH, LOG_QUERY_MAX_LENGTH, MS_PER_SECOND\n\nfrom config import Config\nfrom infrastructure.db_connection import DatabaseConnection\nfrom shared.domain import LogEntry\nfrom shared.infrastructure import get_logger\n\n\ndef register_describe_tool(mcp) -> None:\n    \"\"\"Registriert db_describe Tool.\"\"\"\n    logger = get_logger(\"mcp-db\", Config)\n\n    @mcp.tool()\n    def db_describe(\n        table: str,\n        database: str = \"ki_dev\",\n        show_create: bool = False,\n    ) -> dict:\n        \"\"\"\n        Zeigt Tabellen-Struktur an.\n\n        Args:\n            table: Tabellenname\n            database: Zieldatenbank (ki_dev oder ki_content)\n            show_create: True = SHOW CREATE TABLE, False = DESCRIBE\n\n        Returns:\n            Dict mit columns oder create_statement\n        \"\"\"\n        start = time.time()\n\n        # Validierung: Tabellenname (SQL-Injection-Schutz)\n        if not re.match(r\"^[a-zA-Z0-9_]+$\", table):\n            return {\n                \"status\": \"denied\",\n                \"error\": \"Invalid table name. Only alphanumeric and underscore allowed.\",\n            }\n\n        # Validierung: Datenbank\n        if database not in Config.ALLOWED_DATABASES:\n            return {\n                \"status\": \"denied\",\n                \"error\": f\"Database '{database}' not allowed.\",\n            }\n\n        try:\n            with DatabaseConnection.get_connection(database) as conn:\n                cursor = conn.cursor(dictionary=True, buffered=True)\n\n                if show_create:\n                    cursor.execute(f\"SHOW CREATE TABLE `{table}`\")\n                    row = cursor.fetchone()\n                    cursor.close()\n\n                    duration = int((time.time() - start) * MS_PER_SECOND)\n\n                    # Log\n                    try:\n                        logger.log(\n                            LogEntry(\n                                timestamp=datetime.now(),\n                                client_name=\"mcp-db\",\n                                tool_name=\"db_describe\",\n                                request=f\"SHOW CREATE TABLE {table}\",\n                                status=\"success\",\n                                duration_ms=duration,\n                            )\n                        )\n                    except Exception:\n                        pass\n\n                    return {\n                        \"status\": \"success\",\n                        \"table\": table,\n                        \"create_statement\": row.get(\"Create Table\", \"\") if row else \"\",\n                        \"execution_ms\": duration,\n                    }\n                else:\n                    cursor.execute(f\"DESCRIBE `{table}`\")\n                    columns = cursor.fetchall()\n                    cursor.close()\n\n                    duration = int((time.time() - start) * MS_PER_SECOND)\n\n                    # Log\n                    try:\n                        logger.log(\n                            LogEntry(\n                                timestamp=datetime.now(),\n                                client_name=\"mcp-db\",\n                                tool_name=\"db_describe\",\n                                request=f\"DESCRIBE {table}\",\n                                status=\"success\",\n                                duration_ms=duration,\n                            )\n                        )\n                    except Exception:\n                        pass\n\n                    return {\n                        \"status\": \"success\",\n                        \"table\": table,\n                        \"columns\": columns,\n                        \"execution_ms\": duration,\n                    }\n\n        except Exception as e:\n            duration = int((time.time() - start) * MS_PER_SECOND)\n\n            try:\n                logger.log(\n                    LogEntry(\n                        timestamp=datetime.now(),\n                        client_name=\"mcp-db\",\n                        tool_name=\"db_describe\",\n                        request=f\"DESCRIBE {table}\",\n                        status=\"error\",\n                        duration_ms=duration,\n                        error_message=str(e)[:LOG_ENTRY_MAX_LENGTH],\n                    )\n                )\n            except Exception:\n                pass\n\n            return {\n                \"status\": \"error\",\n                \"error\": str(e)[:LOG_QUERY_MAX_LENGTH],\n                \"execution_ms\": duration,\n            }\n",
        "structuredPatch": [
            {
                "oldStart": 1,
                "oldLines": 22,
                "newStart": 1,
                "newLines": 20,
                "lines": [
                    " \"\"\"Describe Tool - Zeigt Tabellen-Struktur an.\"\"\"",
                    "+",
                    " import re",
                    "-import sys",
                    " import time",
                    "+from datetime import datetime",
                    " ",
                    "-sys.path.insert(0, \"\/var\/www\/mcp-servers\/mcp_db\")",
                    "-sys.path.insert(0, \"\/var\/www\/mcp-servers\/shared\")",
                    "-",
                    " from constants import LOG_ENTRY_MAX_LENGTH, LOG_QUERY_MAX_LENGTH, MS_PER_SECOND",
                    " ",
                    " from config import Config",
                    "-from domain.log_contract import LogEntry",
                    " from infrastructure.db_connection import DatabaseConnection",
                    "-from infrastructure.protokoll_logger import ProtokollLogger",
                    "+from shared.domain import LogEntry",
                    "+from shared.infrastructure import get_logger",
                    " ",
                    " ",
                    " def register_describe_tool(mcp) -> None:",
                    "     \"\"\"Registriert db_describe Tool.\"\"\"",
                    "-    logger = ProtokollLogger()",
                    "+    logger = get_logger(\"mcp-db\", Config)",
                    " ",
                    "     @mcp.tool()",
                    "     def db_describe("
                ]
            },
            {
                "oldStart": 66,
                "oldLines": 6,
                "newStart": 64,
                "newLines": 9,
                "lines": [
                    "                     try:",
                    "                         logger.log(",
                    "                             LogEntry(",
                    "+                                timestamp=datetime.now(),",
                    "+                                client_name=\"mcp-db\",",
                    "+                                tool_name=\"db_describe\",",
                    "                                 request=f\"SHOW CREATE TABLE {table}\",",
                    "                                 status=\"success\",",
                    "                                 duration_ms=duration,"
                ]
            },
            {
                "oldStart": 91,
                "oldLines": 6,
                "newStart": 92,
                "newLines": 9,
                "lines": [
                    "                     try:",
                    "                         logger.log(",
                    "                             LogEntry(",
                    "+                                timestamp=datetime.now(),",
                    "+                                client_name=\"mcp-db\",",
                    "+                                tool_name=\"db_describe\",",
                    "                                 request=f\"DESCRIBE {table}\",",
                    "                                 status=\"success\",",
                    "                                 duration_ms=duration,"
                ]
            },
            {
                "oldStart": 112,
                "oldLines": 6,
                "newStart": 116,
                "newLines": 9,
                "lines": [
                    "             try:",
                    "                 logger.log(",
                    "                     LogEntry(",
                    "+                        timestamp=datetime.now(),",
                    "+                        client_name=\"mcp-db\",",
                    "+                        tool_name=\"db_describe\",",
                    "                         request=f\"DESCRIBE {table}\",",
                    "                         status=\"error\",",
                    "                         duration_ms=duration,"
                ]
            }
        ],
        "originalFile": "\"\"\"Describe Tool - Zeigt Tabellen-Struktur an.\"\"\"\nimport re\nimport sys\nimport time\n\nsys.path.insert(0, \"\/var\/www\/mcp-servers\/mcp_db\")\nsys.path.insert(0, \"\/var\/www\/mcp-servers\/shared\")\n\nfrom constants import LOG_ENTRY_MAX_LENGTH, LOG_QUERY_MAX_LENGTH, MS_PER_SECOND\n\nfrom config import Config\nfrom domain.log_contract import LogEntry\nfrom infrastructure.db_connection import DatabaseConnection\nfrom infrastructure.protokoll_logger import ProtokollLogger\n\n\ndef register_describe_tool(mcp) -> None:\n    \"\"\"Registriert db_describe Tool.\"\"\"\n    logger = ProtokollLogger()\n\n    @mcp.tool()\n    def db_describe(\n        table: str,\n        database: str = \"ki_dev\",\n        show_create: bool = False,\n    ) -> dict:\n        \"\"\"\n        Zeigt Tabellen-Struktur an.\n\n        Args:\n            table: Tabellenname\n            database: Zieldatenbank (ki_dev oder ki_content)\n            show_create: True = SHOW CREATE TABLE, False = DESCRIBE\n\n        Returns:\n            Dict mit columns oder create_statement\n        \"\"\"\n        start = time.time()\n\n        # Validierung: Tabellenname (SQL-Injection-Schutz)\n        if not re.match(r\"^[a-zA-Z0-9_]+$\", table):\n            return {\n                \"status\": \"denied\",\n                \"error\": \"Invalid table name. Only alphanumeric and underscore allowed.\",\n            }\n\n        # Validierung: Datenbank\n        if database not in Config.ALLOWED_DATABASES:\n            return {\n                \"status\": \"denied\",\n                \"error\": f\"Database '{database}' not allowed.\",\n            }\n\n        try:\n            with DatabaseConnection.get_connection(database) as conn:\n                cursor = conn.cursor(dictionary=True, buffered=True)\n\n                if show_create:\n                    cursor.execute(f\"SHOW CREATE TABLE `{table}`\")\n                    row = cursor.fetchone()\n                    cursor.close()\n\n                    duration = int((time.time() - start) * MS_PER_SECOND)\n\n                    # Log\n                    try:\n                        logger.log(\n                            LogEntry(\n                                request=f\"SHOW CREATE TABLE {table}\",\n                                status=\"success\",\n                                duration_ms=duration,\n                            )\n                        )\n                    except Exception:\n                        pass\n\n                    return {\n                        \"status\": \"success\",\n                        \"table\": table,\n                        \"create_statement\": row.get(\"Create Table\", \"\") if row else \"\",\n                        \"execution_ms\": duration,\n                    }\n                else:\n                    cursor.execute(f\"DESCRIBE `{table}`\")\n                    columns = cursor.fetchall()\n                    cursor.close()\n\n                    duration = int((time.time() - start) * MS_PER_SECOND)\n\n                    # Log\n                    try:\n                        logger.log(\n                            LogEntry(\n                                request=f\"DESCRIBE {table}\",\n                                status=\"success\",\n                                duration_ms=duration,\n                            )\n                        )\n                    except Exception:\n                        pass\n\n                    return {\n                        \"status\": \"success\",\n                        \"table\": table,\n                        \"columns\": columns,\n                        \"execution_ms\": duration,\n                    }\n\n        except Exception as e:\n            duration = int((time.time() - start) * MS_PER_SECOND)\n\n            try:\n                logger.log(\n                    LogEntry(\n                        request=f\"DESCRIBE {table}\",\n                        status=\"error\",\n                        duration_ms=duration,\n                        error_message=str(e)[:LOG_ENTRY_MAX_LENGTH],\n                    )\n                )\n            except Exception:\n                pass\n\n            return {\n                \"status\": \"error\",\n                \"error\": str(e)[:LOG_QUERY_MAX_LENGTH],\n                \"execution_ms\": duration,\n            }\n"
    }
}
← Vorheriger Zur Liste Nächster →