zendesk_log_time
Log time spent on a Zendesk ticket by providing ticket ID and seconds. Adds seconds to the running total and returns logged amount and new total in both seconds and human-readable format.
Instructions
Log time spent on a Zendesk ticket. Adds seconds to the running total and records it as the last-update time. Returns logged amount and new total in seconds and human-readable format.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ticket_id | Yes | ||
| seconds | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The tool handler function `zendesk_log_time` that logs time spent on a Zendesk ticket. It is decorated with @mcp.tool() and delegates to _log_time_data(ticket_id, seconds).
@mcp.tool() def zendesk_log_time(ticket_id: int, seconds: int) -> str: """Log time spent on a Zendesk ticket. Adds seconds to the running total and records it as the last-update time. Returns logged amount and new total in seconds and human-readable format.""" return _log_time_data(ticket_id, seconds) - The internal helper function `_log_time_data` that implements the actual business logic: validates seconds > 0, fetches the ticket, adds seconds to the running total via custom fields, updates the ticket via the Zendesk API, and returns a JSON response.
def _log_time_data(ticket_id: int, seconds: int) -> str: if seconds <= 0: return f"seconds must be a positive integer, got {seconds}." try: client = get_client() ticket = client.tickets(id=ticket_id) current_total = int(_get_custom_field(ticket, _FIELD_TOTAL_TIME_SPENT) or 0) new_total = current_total + seconds update = Ticket(id=ticket_id) update.custom_fields = [ {"id": _FIELD_TOTAL_TIME_SPENT, "value": new_total}, {"id": _FIELD_TIME_SPENT_LAST_UPDATE, "value": seconds}, ] client.tickets.update(update) return json.dumps({ "ticket_id": ticket_id, "logged_sec": seconds, "logged_human": _format_duration(seconds), "new_total_sec": new_total, "new_total_human": _format_duration(new_total), }, indent=2) except ConfigError as e: return str(e) except Exception as e: if "RecordNotFound" in str(e) or "404" in str(e): return f"Ticket #{ticket_id} not found or not accessible with current credentials." return f"Zendesk API error: {e}" - src/zendesk_mcp/server.py:19-29 (registration)The tool registration: `register_time_tracking_tools` is imported from `zendesk_mcp.tools.time_tracking` and called with the `mcp` instance in the `main()` function.
from zendesk_mcp.tools.time_tracking import register_time_tracking_tools from zendesk_mcp.tools.git_zen import register_git_zen_tools register_ticket_tools(mcp) register_comments_tools(mcp) register_attachment_tools(mcp) register_gitlab_context_tools(mcp) register_write_comment_tools(mcp) register_update_ticket_tools(mcp) register_time_tracking_tools(mcp) register_git_zen_tools(mcp) - src/zendesk_mcp/tools/time_tracking.py:74-82 (registration)The `register_time_tracking_tools` function that uses the `@mcp.tool()` decorator to register both `zendesk_get_time_tracking` and `zendesk_log_time` as MCP tools.
def register_time_tracking_tools(mcp) -> None: @mcp.tool() def zendesk_get_time_tracking(ticket_id: int) -> str: """Get time tracking data for a Zendesk ticket. Returns total time spent and time spent on the last update, in both seconds and human-readable format (e.g. '2h 15m').""" return _get_time_tracking_data(ticket_id) @mcp.tool() def zendesk_log_time(ticket_id: int, seconds: int) -> str: """Log time spent on a Zendesk ticket. Adds seconds to the running total and records it as the last-update time. Returns logged amount and new total in seconds and human-readable format.""" - Helper function `_format_duration` that converts seconds to a human-readable format (e.g., '2h 15m' or '5m 30s'). Used by both the logging and retrieval functions.
def _format_duration(seconds: int) -> str: m, s = divmod(seconds, 60) h, m = divmod(m, 60) if h: return f"{h}h {m:02d}m" return f"{m}m {s:02d}s"