fix: address PR review feedback (round 3)

Bugs:
- assign_tag/remove_tag now call _unwrap_ocs to surface OCS-level errors
- trash_page changed to idempotentHint=False (trashing twice errors)
- WebDAV path parts stripped of slashes to prevent double-slash paths

Robustness:
- _unwrap_ocs uses ocs.get("data", {}) instead of ocs["data"]
- Unit test added for missing data key in OCS envelope

Minor:
- MCP error codes use -1 (project convention) instead of HTTP status codes
- update_collective docstring notes that emoji is required
- CollectiveTag.color validated as hex format via field_validator

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-03-25 13:25:09 +01:00
co-authored by Claude Opus 4.6
parent 44a27bd9e9
commit f3caad122d
4 changed files with 45 additions and 13 deletions
+5 -3
View File
@@ -40,7 +40,7 @@ class CollectivesClient(BaseNextcloudClient):
if status_code >= 400:
message = meta.get("message", "OCS error")
raise OCSError(status_code, message)
return ocs["data"]
return ocs.get("data", {})
# Collectives
@@ -215,19 +215,21 @@ class CollectivesClient(BaseNextcloudClient):
async def assign_tag(self, collective_id: int, page_id: int, tag_id: int) -> None:
"""Assign a tag to a page."""
await self._make_request(
response = await self._make_request(
"PUT",
f"{API_BASE}/collectives/{collective_id}/pages/{page_id}/tags/{tag_id}",
headers=self._get_ocs_headers(),
)
self._unwrap_ocs(response.json())
async def remove_tag(self, collective_id: int, page_id: int, tag_id: int) -> None:
"""Remove a tag from a page."""
await self._make_request(
response = await self._make_request(
"DELETE",
f"{API_BASE}/collectives/{collective_id}/pages/{page_id}/tags/{tag_id}",
headers=self._get_ocs_headers(),
)
self._unwrap_ocs(response.json())
# Trash
+11 -2
View File
@@ -1,6 +1,8 @@
"""Pydantic models for Nextcloud Collectives app."""
from pydantic import BaseModel, Field
import re
from pydantic import BaseModel, Field, field_validator
from .base import BaseResponse, StatusResponse
@@ -53,7 +55,14 @@ class CollectiveTag(BaseModel):
id: int = Field(description="Tag ID")
collectiveId: int = Field(description="Parent collective ID")
name: str = Field(description="Tag name")
color: str = Field(description="Hex color code")
color: str = Field(description="Hex color code (e.g. 'FF0000')")
@field_validator("color")
@classmethod
def validate_hex_color(cls, v: str) -> str:
if not re.fullmatch(r"[0-9A-Fa-f]{3,8}", v):
raise ValueError(f"Invalid hex color: {v!r}")
return v
# Response Models
+8 -6
View File
@@ -34,8 +34,8 @@ logger = logging.getLogger(__name__)
def _handle_collectives_error(e: OCSError | HTTPStatusError) -> McpError:
"""Convert OCS or HTTP errors to McpError."""
if isinstance(e, OCSError):
return McpError(ErrorData(code=e.status_code, message=e.message))
return McpError(ErrorData(code=e.response.status_code, message=str(e)))
return McpError(ErrorData(code=-1, message=e.message))
return McpError(ErrorData(code=-1, message=str(e)))
def configure_collectives_tools(mcp: FastMCP):
@@ -120,7 +120,7 @@ def configure_collectives_tools(mcp: FastMCP):
if page.filePath:
parts.append(page.filePath)
parts.append(page.fileName)
webdav_path = "/".join(parts)
webdav_path = "/".join(p.strip("/") for p in parts)
try:
file_bytes, _ = await client.webdav.read_file(webdav_path)
content = file_bytes.decode("utf-8")
@@ -243,11 +243,13 @@ def configure_collectives_tools(mcp: FastMCP):
async def collectives_update_collective(
ctx: Context, collective_id: int, emoji: str | None = None
) -> CollectiveOperationResponse:
"""Update a Nextcloud Collective (emoji)
"""Update a Nextcloud Collective (emoji).
At least one field must be provided.
Args:
collective_id: ID of the collective
emoji: New emoji for the collective
emoji: New emoji for the collective (required)
"""
client = await get_client(ctx)
try:
@@ -340,7 +342,7 @@ def configure_collectives_tools(mcp: FastMCP):
@mcp.tool(
title="Trash Collective Page",
annotations=ToolAnnotations(
destructiveHint=True, idempotentHint=True, openWorldHint=True
destructiveHint=True, idempotentHint=False, openWorldHint=True
),
)
@require_scopes("collectives:write")