{
"event": "PreToolUse",
"tool_name": "Write",
"tool_input": {
"file_path": "\/var\/www\/mcp-servers\/mcp-docs\/tools\/docs_tools\/docs_writer.py",
"content": "\"\"\"Write operations for documentation management.\"\"\"\n\nimport json\nimport sys\nimport time\nfrom typing import Optional\n\nsys.path.insert(0, \"\/opt\/mcp-servers\/mcp-docs\")\nfrom domain.dokumentation import Dokumentation, DocStatus, LogEntry\nfrom infrastructure.docs_repository import get_repository\nfrom infrastructure.protokoll_logger import get_logger\n\nfrom .constants import (\n DEFAULT_SORT_ORDER,\n MS_PER_SECOND,\n ROOT_DEPTH,\n ROOT_PATH_PREFIX,\n STATUS_DRAFT,\n VALID_STATUSES,\n)\n\n\ndef register_writer_tools(mcp) -> None:\n \"\"\"\n Register write documentation tools with the MCP server.\n\n Args:\n mcp: The MCP server instance to register tools with\n \"\"\"\n\n @mcp.tool()\n def docs_create(\n title: str,\n slug: str,\n content: str = \"\",\n description: Optional[str] = None,\n parent_id: Optional[int] = None,\n status: str = STATUS_DRAFT,\n sort_order: int = DEFAULT_SORT_ORDER\n ) -> dict:\n \"\"\"\n Create a new document.\n\n Args:\n title: Document title (required)\n slug: URL slug (required)\n content: HTML content\n description: Short description\n parent_id: Parent document ID\n status: Status (draft, published, archived)\n sort_order: Sort order within parent\n\n Returns:\n Dict containing created document\n \"\"\"\n start_time = time.time()\n logger = get_logger()\n repo = get_repository()\n\n try:\n # Calculate path\n if parent_id is not None:\n parent = repo.find_by_id(parent_id)\n if not parent:\n return {\n \"success\": False,\n \"error\": f\"Parent with ID {parent_id} not found\"\n }\n path = f\"{parent.path}\/{slug}\"\n depth = parent.depth + 1\n else:\n path = f\"{ROOT_PATH_PREFIX}{slug}\"\n depth = ROOT_DEPTH\n\n # Check if path already exists\n existing = repo.find_by_path(path)\n if existing:\n return {\n \"success\": False,\n \"error\": f\"Path '{path}' already exists\"\n }\n\n doc = Dokumentation(\n parent_id=parent_id,\n slug=slug,\n path=path,\n title=title,\n description=description,\n content=content,\n status=DocStatus(status) if status in VALID_STATUSES else DocStatus.DRAFT,\n sort_order=sort_order,\n depth=depth\n )\n\n new_id = repo.create(doc)\n created_doc = repo.find_by_id(new_id)\n\n logger.log(LogEntry(\n tool_name=\"docs_create\",\n request=json.dumps({\n \"title\": title,\n \"slug\": slug,\n \"parent_id\": parent_id\n }),\n status=\"success\",\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n\n return {\n \"success\": True,\n \"doc\": created_doc.to_dict() if created_doc else None,\n \"message\": f\"Document '{title}' created with ID {new_id}\"\n }\n\n except Exception as e:\n logger.log(LogEntry(\n tool_name=\"docs_create\",\n request=json.dumps({\"title\": title, \"slug\": slug}),\n status=\"error\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n\n @mcp.tool()\n def docs_update(\n id: int,\n title: Optional[str] = None,\n content: Optional[str] = None,\n description: Optional[str] = None,\n status: Optional[str] = None\n ) -> dict:\n \"\"\"\n Update a document.\n\n Args:\n id: Document ID (required)\n title: New title\n content: New content\n description: New description\n status: New status (draft, published, archived)\n\n Returns:\n Dict containing updated document\n \"\"\"\n start_time = time.time()\n logger = get_logger()\n repo = get_repository()\n\n try:\n doc = repo.find_by_id(id)\n if not doc:\n return {\n \"success\": False,\n \"error\": f\"Document with ID {id} not found\"\n }\n\n updates = {}\n if title is not None:\n updates[\"title\"] = title\n if content is not None:\n updates[\"content\"] = content\n if description is not None:\n updates[\"description\"] = description\n if status is not None and status in VALID_STATUSES:\n updates[\"status\"] = status\n\n if not updates:\n return {\n \"success\": False,\n \"error\": \"No changes specified\"\n }\n\n success = repo.update(id, updates)\n if success:\n updated_doc = repo.find_by_id(id)\n\n logger.log(LogEntry(\n tool_name=\"docs_update\",\n request=json.dumps({\n \"id\": id,\n \"updates\": list(updates.keys())\n }),\n status=\"success\",\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n\n return {\n \"success\": True,\n \"doc\": updated_doc.to_dict() if updated_doc else None,\n \"message\": f\"Document #{id} updated\"\n }\n else:\n return {\"success\": False, \"error\": \"Update failed\"}\n\n except Exception as e:\n logger.log(LogEntry(\n tool_name=\"docs_update\",\n request=json.dumps({\"id\": id}),\n status=\"error\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n\n @mcp.tool()\n def docs_delete(id: int) -> dict:\n \"\"\"\n Delete a document.\n\n Args:\n id: Document ID (required)\n\n Returns:\n Dict containing confirmation\n \"\"\"\n start_time = time.time()\n logger = get_logger()\n repo = get_repository()\n\n try:\n doc = repo.find_by_id(id)\n if not doc:\n return {\n \"success\": False,\n \"error\": f\"Document with ID {id} not found\"\n }\n\n success = repo.delete(id)\n\n logger.log(LogEntry(\n tool_name=\"docs_delete\",\n request=json.dumps({\"id\": id, \"title\": doc.title}),\n status=\"success\" if success else \"error\",\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n\n return {\n \"success\": success,\n \"message\": (\n f\"Document '{doc.title}' (ID {id}) deleted\"\n if success\n else \"Deletion failed\"\n )\n }\n\n except ValueError as e:\n # Children exist\n logger.log(LogEntry(\n tool_name=\"docs_delete\",\n request=json.dumps({\"id\": id}),\n status=\"denied\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n\n except Exception as e:\n logger.log(LogEntry(\n tool_name=\"docs_delete\",\n request=json.dumps({\"id\": id}),\n status=\"error\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n"
}
}
{
"tool_response": {
"type": "create",
"filePath": "\/var\/www\/mcp-servers\/mcp-docs\/tools\/docs_tools\/docs_writer.py",
"content": "\"\"\"Write operations for documentation management.\"\"\"\n\nimport json\nimport sys\nimport time\nfrom typing import Optional\n\nsys.path.insert(0, \"\/opt\/mcp-servers\/mcp-docs\")\nfrom domain.dokumentation import Dokumentation, DocStatus, LogEntry\nfrom infrastructure.docs_repository import get_repository\nfrom infrastructure.protokoll_logger import get_logger\n\nfrom .constants import (\n DEFAULT_SORT_ORDER,\n MS_PER_SECOND,\n ROOT_DEPTH,\n ROOT_PATH_PREFIX,\n STATUS_DRAFT,\n VALID_STATUSES,\n)\n\n\ndef register_writer_tools(mcp) -> None:\n \"\"\"\n Register write documentation tools with the MCP server.\n\n Args:\n mcp: The MCP server instance to register tools with\n \"\"\"\n\n @mcp.tool()\n def docs_create(\n title: str,\n slug: str,\n content: str = \"\",\n description: Optional[str] = None,\n parent_id: Optional[int] = None,\n status: str = STATUS_DRAFT,\n sort_order: int = DEFAULT_SORT_ORDER\n ) -> dict:\n \"\"\"\n Create a new document.\n\n Args:\n title: Document title (required)\n slug: URL slug (required)\n content: HTML content\n description: Short description\n parent_id: Parent document ID\n status: Status (draft, published, archived)\n sort_order: Sort order within parent\n\n Returns:\n Dict containing created document\n \"\"\"\n start_time = time.time()\n logger = get_logger()\n repo = get_repository()\n\n try:\n # Calculate path\n if parent_id is not None:\n parent = repo.find_by_id(parent_id)\n if not parent:\n return {\n \"success\": False,\n \"error\": f\"Parent with ID {parent_id} not found\"\n }\n path = f\"{parent.path}\/{slug}\"\n depth = parent.depth + 1\n else:\n path = f\"{ROOT_PATH_PREFIX}{slug}\"\n depth = ROOT_DEPTH\n\n # Check if path already exists\n existing = repo.find_by_path(path)\n if existing:\n return {\n \"success\": False,\n \"error\": f\"Path '{path}' already exists\"\n }\n\n doc = Dokumentation(\n parent_id=parent_id,\n slug=slug,\n path=path,\n title=title,\n description=description,\n content=content,\n status=DocStatus(status) if status in VALID_STATUSES else DocStatus.DRAFT,\n sort_order=sort_order,\n depth=depth\n )\n\n new_id = repo.create(doc)\n created_doc = repo.find_by_id(new_id)\n\n logger.log(LogEntry(\n tool_name=\"docs_create\",\n request=json.dumps({\n \"title\": title,\n \"slug\": slug,\n \"parent_id\": parent_id\n }),\n status=\"success\",\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n\n return {\n \"success\": True,\n \"doc\": created_doc.to_dict() if created_doc else None,\n \"message\": f\"Document '{title}' created with ID {new_id}\"\n }\n\n except Exception as e:\n logger.log(LogEntry(\n tool_name=\"docs_create\",\n request=json.dumps({\"title\": title, \"slug\": slug}),\n status=\"error\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n\n @mcp.tool()\n def docs_update(\n id: int,\n title: Optional[str] = None,\n content: Optional[str] = None,\n description: Optional[str] = None,\n status: Optional[str] = None\n ) -> dict:\n \"\"\"\n Update a document.\n\n Args:\n id: Document ID (required)\n title: New title\n content: New content\n description: New description\n status: New status (draft, published, archived)\n\n Returns:\n Dict containing updated document\n \"\"\"\n start_time = time.time()\n logger = get_logger()\n repo = get_repository()\n\n try:\n doc = repo.find_by_id(id)\n if not doc:\n return {\n \"success\": False,\n \"error\": f\"Document with ID {id} not found\"\n }\n\n updates = {}\n if title is not None:\n updates[\"title\"] = title\n if content is not None:\n updates[\"content\"] = content\n if description is not None:\n updates[\"description\"] = description\n if status is not None and status in VALID_STATUSES:\n updates[\"status\"] = status\n\n if not updates:\n return {\n \"success\": False,\n \"error\": \"No changes specified\"\n }\n\n success = repo.update(id, updates)\n if success:\n updated_doc = repo.find_by_id(id)\n\n logger.log(LogEntry(\n tool_name=\"docs_update\",\n request=json.dumps({\n \"id\": id,\n \"updates\": list(updates.keys())\n }),\n status=\"success\",\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n\n return {\n \"success\": True,\n \"doc\": updated_doc.to_dict() if updated_doc else None,\n \"message\": f\"Document #{id} updated\"\n }\n else:\n return {\"success\": False, \"error\": \"Update failed\"}\n\n except Exception as e:\n logger.log(LogEntry(\n tool_name=\"docs_update\",\n request=json.dumps({\"id\": id}),\n status=\"error\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n\n @mcp.tool()\n def docs_delete(id: int) -> dict:\n \"\"\"\n Delete a document.\n\n Args:\n id: Document ID (required)\n\n Returns:\n Dict containing confirmation\n \"\"\"\n start_time = time.time()\n logger = get_logger()\n repo = get_repository()\n\n try:\n doc = repo.find_by_id(id)\n if not doc:\n return {\n \"success\": False,\n \"error\": f\"Document with ID {id} not found\"\n }\n\n success = repo.delete(id)\n\n logger.log(LogEntry(\n tool_name=\"docs_delete\",\n request=json.dumps({\"id\": id, \"title\": doc.title}),\n status=\"success\" if success else \"error\",\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n\n return {\n \"success\": success,\n \"message\": (\n f\"Document '{doc.title}' (ID {id}) deleted\"\n if success\n else \"Deletion failed\"\n )\n }\n\n except ValueError as e:\n # Children exist\n logger.log(LogEntry(\n tool_name=\"docs_delete\",\n request=json.dumps({\"id\": id}),\n status=\"denied\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n\n except Exception as e:\n logger.log(LogEntry(\n tool_name=\"docs_delete\",\n request=json.dumps({\"id\": id}),\n status=\"error\",\n error_message=str(e),\n duration_ms=int((time.time() - start_time) * MS_PER_SECOND)\n ))\n return {\"success\": False, \"error\": str(e)}\n",
"structuredPatch": [],
"originalFile": null
}
}