From de42c67f9f8e23cd60582fed38c86d08f763d86a Mon Sep 17 00:00:00 2001 From: Fanyang Meng Date: Tue, 11 Feb 2025 23:56:58 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20deleting=20users?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +- src/ghost_mcp/tools/users.py | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e22700..5429aa4 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,10 @@ GHOST_API_URL=your_ghost_api_url GHOST_STAFF_API_KEY=your_staff_api_key npx @mod ### Users Management - `list_roles`: List all available roles - `create_invite`: Create a new user invitation email and role_id -- `update_user`: Update an existing user's information **(Please note: Ghost has not implemented the ability to update user roles via the API even though they include it in the API documentation)** - `list_users`: List all users with detailed role information - `read_user`: Get comprehensive details of a specific user +- `update_user`: Update an existing user's information **(Please note: Ghost has not implemented the ability to update user roles via the API even though they include it in the API documentation)** +- `delete_user`: Delete a specific user ### Members Management - `list_members`: List members with subscription and newsletter details diff --git a/src/ghost_mcp/tools/users.py b/src/ghost_mcp/tools/users.py index 25b2425..f878e3e 100644 --- a/src/ghost_mcp/tools/users.py +++ b/src/ghost_mcp/tools/users.py @@ -72,6 +72,70 @@ ID: {user.get('id', 'Unknown')} ctx.error(f"Failed to list users: {str(e)}") return str(e) +async def delete_user( + user_id: str, + ctx: Context = None +) -> str: + """Delete a user from Ghost. + + Args: + user_id: ID of the user to delete (required) + ctx: Optional context for logging + + Returns: + Success message if deletion was successful + + Raises: + GhostError: If the Ghost API request fails or if attempting to delete the Owner + ValueError: If user_id is not provided + """ + if not user_id: + raise ValueError("user_id is required") + + if ctx: + ctx.info(f"Attempting to delete user with ID: {user_id}") + + try: + # First get the user to check if they are the Owner + if ctx: + ctx.debug("Getting user details to check role") + headers = await get_auth_headers(STAFF_API_KEY) + + user_data = await make_ghost_request( + f"users/{user_id}/", + headers, + ctx + ) + + user = user_data.get("users", [{}])[0] + roles = [role.get('name') for role in user.get('roles', [])] + + if 'Owner' in roles: + error_msg = "Cannot delete the Owner user" + if ctx: + ctx.error(error_msg) + raise GhostError(error_msg) + + # Proceed with deletion + if ctx: + ctx.debug(f"Making API request to delete user {user_id}") + response = await make_ghost_request( + f"users/{user_id}/", + headers, + ctx, + http_method="DELETE" + ) + + return f""" +Successfully deleted user: +Name: {user.get('name', 'Unknown')} +Email: {user.get('email', 'Unknown')} +""" + except Exception as e: + if ctx: + ctx.error(f"Failed to delete user: {str(e)}") + raise + async def update_user( user_id: str, name: str = None,