zendesk_log_time
Log time spent on a Zendesk ticket by adding seconds to the running total and returning the 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 @mcp.tool() decorated function that implements the 'zendesk_log_time' tool. It delegates to _log_time_data.
@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) - Core business logic: reads current total from custom field, adds seconds, updates ticket's custom fields (total time spent and last update time), returns JSON result.
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:39-39 (registration)Registration call in server.py's main() that wires up the time tracking tools (including zendesk_log_time) into the MCP server.
register_time_tracking_tools(mcp) - src/zendesk_mcp/tools/time_tracking.py:74-83 (registration)Registration function that uses @mcp.tool() decorator to register both time tracking tools with FastMCP.
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.""" return _log_time_data(ticket_id, seconds) - Helper that formats seconds into human-readable duration (e.g., '2h 15m' or '5m 30s').
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"