forked from taylorwilsdon/google_workspace_mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks_tools.py
More file actions
925 lines (769 loc) · 33.9 KB
/
tasks_tools.py
File metadata and controls
925 lines (769 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
"""
Google Tasks MCP Tools
This module provides MCP tools for interacting with Google Tasks API.
"""
import asyncio
import logging
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional
from googleapiclient.errors import HttpError # type: ignore
from mcp import Resource
from auth.service_decorator import require_google_service
from core.server import server
from core.utils import handle_http_errors
logger = logging.getLogger(__name__)
LIST_TASKS_MAX_RESULTS_DEFAULT = 20
LIST_TASKS_MAX_RESULTS_MAX = 10_000
LIST_TASKS_MAX_POSITION = "99999999999999999999"
class StructuredTask:
def __init__(self, task: Dict[str, str], is_placeholder_parent: bool) -> None:
self.id = task["id"]
self.title = task.get("title", None)
self.status = task.get("status", None)
self.due = task.get("due", None)
self.notes = task.get("notes", None)
self.updated = task.get("updated", None)
self.completed = task.get("completed", None)
self.is_placeholder_parent = is_placeholder_parent
self.subtasks: List["StructuredTask"] = []
def add_subtask(self, subtask: "StructuredTask") -> None:
self.subtasks.append(subtask)
def __repr__(self) -> str:
return f"StructuredTask(title={self.title}, {len(self.subtasks)} subtasks)"
def _adjust_due_max_for_tasks_api(due_max: str) -> str:
"""
Compensate for the Google Tasks API treating dueMax as an exclusive bound.
The API stores due dates at day resolution and compares using < dueMax, so to
include tasks due on the requested date we bump the bound by one day.
"""
try:
parsed = datetime.fromisoformat(due_max.replace("Z", "+00:00"))
except ValueError:
logger.warning("[list_tasks] Unable to parse due_max '%s'; sending unmodified value", due_max)
return due_max
if parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=timezone.utc)
adjusted = parsed + timedelta(days=1)
if adjusted.tzinfo == timezone.utc:
return adjusted.isoformat().replace("+00:00", "Z")
return adjusted.isoformat()
@server.tool() # type: ignore
@require_google_service("tasks", "tasks_read") # type: ignore
@handle_http_errors("list_task_lists", service_type="tasks") # type: ignore
async def list_task_lists(
service: Resource,
user_google_email: str,
max_results: int = 1000,
page_token: Optional[str] = None
) -> str:
"""
List all task lists for the user.
Args:
user_google_email (str): The user's Google email address. Required.
max_results (int): Maximum number of task lists to return (default: 1000, max: 1000).
page_token (Optional[str]): Token for pagination.
Returns:
str: List of task lists with their IDs, titles, and details.
"""
logger.info(f"[list_task_lists] Invoked. Email: '{user_google_email}'")
try:
params: Dict[str, Any] = {}
if max_results is not None:
params["maxResults"] = max_results
if page_token:
params["pageToken"] = page_token
result = await asyncio.to_thread(
service.tasklists().list(**params).execute
)
task_lists = result.get("items", [])
next_page_token = result.get("nextPageToken")
if not task_lists:
return f"No task lists found for {user_google_email}."
response = f"Task Lists for {user_google_email}:\n"
for task_list in task_lists:
response += f"- {task_list['title']} (ID: {task_list['id']})\n"
response += f" Updated: {task_list.get('updated', 'N/A')}\n"
if next_page_token:
response += f"\nNext page token: {next_page_token}"
logger.info(f"Found {len(task_lists)} task lists for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks_read") # type: ignore
@handle_http_errors("get_task_list", service_type="tasks") # type: ignore
async def get_task_list(
service: Resource,
user_google_email: str,
task_list_id: str
) -> str:
"""
Get details of a specific task list.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list to retrieve.
Returns:
str: Task list details including title, ID, and last updated time.
"""
logger.info(f"[get_task_list] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}")
try:
task_list = await asyncio.to_thread(
service.tasklists().get(tasklist=task_list_id).execute
)
response = f"""Task List Details for {user_google_email}:
- Title: {task_list['title']}
- ID: {task_list['id']}
- Updated: {task_list.get('updated', 'N/A')}
- Self Link: {task_list.get('selfLink', 'N/A')}"""
logger.info(f"Retrieved task list '{task_list['title']}' for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("create_task_list", service_type="tasks") # type: ignore
async def create_task_list(
service: Resource,
user_google_email: str,
title: str
) -> str:
"""
Create a new task list.
Args:
user_google_email (str): The user's Google email address. Required.
title (str): The title of the new task list.
Returns:
str: Confirmation message with the new task list ID and details.
"""
logger.info(f"[create_task_list] Invoked. Email: '{user_google_email}', Title: '{title}'")
try:
body = {
"title": title
}
result = await asyncio.to_thread(
service.tasklists().insert(body=body).execute
)
response = f"""Task List Created for {user_google_email}:
- Title: {result['title']}
- ID: {result['id']}
- Created: {result.get('updated', 'N/A')}
- Self Link: {result.get('selfLink', 'N/A')}"""
logger.info(f"Created task list '{title}' with ID {result['id']} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("update_task_list", service_type="tasks") # type: ignore
async def update_task_list(
service: Resource,
user_google_email: str,
task_list_id: str,
title: str
) -> str:
"""
Update an existing task list.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list to update.
title (str): The new title for the task list.
Returns:
str: Confirmation message with updated task list details.
"""
logger.info(f"[update_task_list] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, New Title: '{title}'")
try:
body = {
"id": task_list_id,
"title": title
}
result = await asyncio.to_thread(
service.tasklists().update(tasklist=task_list_id, body=body).execute
)
response = f"""Task List Updated for {user_google_email}:
- Title: {result['title']}
- ID: {result['id']}
- Updated: {result.get('updated', 'N/A')}"""
logger.info(f"Updated task list {task_list_id} with new title '{title}' for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("delete_task_list", service_type="tasks") # type: ignore
async def delete_task_list(
service: Resource,
user_google_email: str,
task_list_id: str
) -> str:
"""
Delete a task list. Note: This will also delete all tasks in the list.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list to delete.
Returns:
str: Confirmation message.
"""
logger.info(f"[delete_task_list] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}")
try:
await asyncio.to_thread(
service.tasklists().delete(tasklist=task_list_id).execute
)
response = f"Task list {task_list_id} has been deleted for {user_google_email}. All tasks in this list have also been deleted."
logger.info(f"Deleted task list {task_list_id} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks_read") # type: ignore
@handle_http_errors("list_tasks", service_type="tasks") # type: ignore
async def list_tasks(
service: Resource,
user_google_email: str,
task_list_id: str,
max_results: int = LIST_TASKS_MAX_RESULTS_DEFAULT,
page_token: Optional[str] = None,
show_completed: bool = True,
show_deleted: bool = False,
show_hidden: bool = False,
show_assigned: bool = False,
completed_max: Optional[str] = None,
completed_min: Optional[str] = None,
due_max: Optional[str] = None,
due_min: Optional[str] = None,
updated_min: Optional[str] = None,
) -> str:
"""
List all tasks in a specific task list.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list to retrieve tasks from.
max_results (int): Maximum number of tasks to return. (default: 20, max: 10000).
page_token (Optional[str]): Token for pagination.
show_completed (bool): Whether to include completed tasks (default: True). Note that show_hidden must also be true to show tasks completed in first party clients, such as the web UI and Google's mobile apps.
show_deleted (bool): Whether to include deleted tasks (default: False).
show_hidden (bool): Whether to include hidden tasks (default: False).
show_assigned (bool): Whether to include assigned tasks (default: False).
completed_max (Optional[str]): Upper bound for completion date (RFC 3339 timestamp).
completed_min (Optional[str]): Lower bound for completion date (RFC 3339 timestamp).
due_max (Optional[str]): Upper bound for due date (RFC 3339 timestamp).
due_min (Optional[str]): Lower bound for due date (RFC 3339 timestamp).
updated_min (Optional[str]): Lower bound for last modification time (RFC 3339 timestamp).
Returns:
str: List of tasks with their details.
"""
logger.info(f"[list_tasks] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}")
try:
params: Dict[str, Any] = {"tasklist": task_list_id}
if max_results is not None:
params["maxResults"] = max_results
if page_token:
params["pageToken"] = page_token
if show_completed is not None:
params["showCompleted"] = show_completed
if show_deleted is not None:
params["showDeleted"] = show_deleted
if show_hidden is not None:
params["showHidden"] = show_hidden
if show_assigned is not None:
params["showAssigned"] = show_assigned
if completed_max:
params["completedMax"] = completed_max
if completed_min:
params["completedMin"] = completed_min
if due_max:
adjusted_due_max = _adjust_due_max_for_tasks_api(due_max)
if adjusted_due_max != due_max:
logger.info(
"[list_tasks] Adjusted due_max from '%s' to '%s' to include due date boundary",
due_max,
adjusted_due_max,
)
params["dueMax"] = adjusted_due_max
if due_min:
params["dueMin"] = due_min
if updated_min:
params["updatedMin"] = updated_min
result = await asyncio.to_thread(
service.tasks().list(**params).execute
)
tasks = result.get("items", [])
next_page_token = result.get("nextPageToken")
# In order to return a sorted and organized list of tasks all at once, we support retrieving more than a single
# page from the Google tasks API.
results_remaining = (
min(max_results, LIST_TASKS_MAX_RESULTS_MAX) if max_results else LIST_TASKS_MAX_RESULTS_DEFAULT
)
results_remaining -= len(tasks)
while results_remaining > 0 and next_page_token:
params["pageToken"] = next_page_token
params["maxResults"] = str(results_remaining)
result = await asyncio.to_thread(
service.tasks().list(**params).execute
)
more_tasks = result.get("items", [])
next_page_token = result.get("nextPageToken")
if len(more_tasks) == 0:
# For some unexpected reason, no more tasks were returned. Break to avoid an infinite loop.
break
tasks.extend(more_tasks)
results_remaining -= len(more_tasks)
if not tasks:
return f"No tasks found in task list {task_list_id} for {user_google_email}."
structured_tasks = get_structured_tasks(tasks)
response = f"Tasks in list {task_list_id} for {user_google_email}:\n"
response += serialize_tasks(structured_tasks, 0)
if next_page_token:
response += f"Next page token: {next_page_token}\n"
logger.info(f"Found {len(tasks)} tasks in list {task_list_id} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
def get_structured_tasks(tasks: List[Dict[str, str]]) -> List[StructuredTask]:
"""
Convert a flat list of task dictionaries into StructuredTask objects based on parent-child relationships sorted by position.
Args:
tasks: List of task dictionaries.
Returns:
list: Sorted list of top-level StructuredTask objects with nested subtasks.
"""
tasks_by_id = {
task["id"]: StructuredTask(task, is_placeholder_parent=False) for task in tasks
}
positions_by_id = {
task["id"]: int(task["position"]) for task in tasks if "position" in task
}
# Placeholder virtual root as parent for top-level tasks
root_task = StructuredTask(
{"id": "root", "title": "Root"}, is_placeholder_parent=False
)
for task in tasks:
structured_task = tasks_by_id[task["id"]]
parent_id = task.get("parent")
parent = None
if not parent_id:
# Task without parent: parent to the virtual root
parent = root_task
elif parent_id in tasks_by_id:
# Subtask: parent to its actual parent
parent = tasks_by_id[parent_id]
else:
# Orphaned subtask: create placeholder parent
# Due to paging or filtering, a subtask may have a parent that is not present in the list of tasks.
# We will create placeholder StructuredTask objects for these missing parents to maintain the hierarchy.
parent = StructuredTask({"id": parent_id}, is_placeholder_parent=True)
tasks_by_id[parent_id] = parent
root_task.add_subtask(parent)
parent.add_subtask(structured_task)
sort_structured_tasks(root_task, positions_by_id)
return root_task.subtasks
def sort_structured_tasks(
root_task: StructuredTask, positions_by_id: Dict[str, int]
) -> None:
"""
Recursively sort--in place--StructuredTask objects and their subtasks based on position.
Args:
root_task: The root StructuredTask object.
positions_by_id: Dictionary mapping task IDs to their positions.
"""
def get_position(task: StructuredTask) -> int | float:
# Tasks without position go to the end (infinity)
result = positions_by_id.get(task.id, float("inf"))
return result
root_task.subtasks.sort(key=get_position)
for subtask in root_task.subtasks:
sort_structured_tasks(subtask, positions_by_id)
def serialize_tasks(structured_tasks: List[StructuredTask], subtask_level: int) -> str:
"""
Serialize a list of StructuredTask objects into a formatted string with indentation for subtasks.
Args:
structured_tasks (list): List of StructuredTask objects.
subtask_level (int): Current level of indentation for subtasks.
Returns:
str: Formatted string representation of the tasks.
"""
response = ""
placeholder_parent_count = 0
placeholder_parent_title = "Unknown parent"
for task in structured_tasks:
indent = " " * subtask_level
bullet = "-" if subtask_level == 0 else "*"
if task.title is not None:
title = task.title
elif task.is_placeholder_parent:
title = placeholder_parent_title
placeholder_parent_count += 1
else:
title = "Untitled"
response += f"{indent}{bullet} {title} (ID: {task.id})\n"
response += f"{indent} Status: {task.status or 'N/A'}\n"
response += f"{indent} Due: {task.due}\n" if task.due else ""
if task.notes:
response += f"{indent} Notes: {task.notes[:100]}{'...' if len(task.notes) > 100 else ''}\n"
response += f"{indent} Completed: {task.completed}\n" if task.completed else ""
response += f"{indent} Updated: {task.updated or 'N/A'}\n"
response += "\n"
response += serialize_tasks(task.subtasks, subtask_level + 1)
if placeholder_parent_count > 0:
# Placeholder parents should only appear at the top level
assert subtask_level == 0
response += f"""
{placeholder_parent_count} tasks with title {placeholder_parent_title} are included as placeholders.
These placeholders contain subtasks whose parents were not present in the task list.
This can occur due to pagination. Callers can often avoid this problem if max_results is large enough to contain all tasks (subtasks and their parents) without paging.
This can also occur due to filtering that excludes parent tasks while including their subtasks or due to deleted or hidden parent tasks.
"""
return response
@server.tool() # type: ignore
@require_google_service("tasks", "tasks_read") # type: ignore
@handle_http_errors("get_task", service_type="tasks") # type: ignore
async def get_task(
service: Resource,
user_google_email: str,
task_list_id: str,
task_id: str
) -> str:
"""
Get details of a specific task.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list containing the task.
task_id (str): The ID of the task to retrieve.
Returns:
str: Task details including title, notes, status, due date, etc.
"""
logger.info(f"[get_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}")
try:
task = await asyncio.to_thread(
service.tasks().get(tasklist=task_list_id, task=task_id).execute
)
response = f"""Task Details for {user_google_email}:
- Title: {task.get('title', 'Untitled')}
- ID: {task['id']}
- Status: {task.get('status', 'N/A')}
- Updated: {task.get('updated', 'N/A')}"""
if task.get('due'):
response += f"\n- Due Date: {task['due']}"
if task.get('completed'):
response += f"\n- Completed: {task['completed']}"
if task.get('notes'):
response += f"\n- Notes: {task['notes']}"
if task.get('parent'):
response += f"\n- Parent Task ID: {task['parent']}"
if task.get('position'):
response += f"\n- Position: {task['position']}"
if task.get('selfLink'):
response += f"\n- Self Link: {task['selfLink']}"
if task.get('webViewLink'):
response += f"\n- Web View Link: {task['webViewLink']}"
logger.info(f"Retrieved task '{task.get('title', 'Untitled')}' for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("create_task", service_type="tasks") # type: ignore
async def create_task(
service: Resource,
user_google_email: str,
task_list_id: str,
title: str,
notes: Optional[str] = None,
due: Optional[str] = None,
parent: Optional[str] = None,
previous: Optional[str] = None
) -> str:
"""
Create a new task in a task list.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list to create the task in.
title (str): The title of the task.
notes (Optional[str]): Notes/description for the task.
due (Optional[str]): Due date in RFC 3339 format (e.g., "2024-12-31T23:59:59Z").
parent (Optional[str]): Parent task ID (for subtasks).
previous (Optional[str]): Previous sibling task ID (for positioning).
Returns:
str: Confirmation message with the new task ID and details.
"""
logger.info(f"[create_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Title: '{title}'")
try:
body = {
"title": title
}
if notes:
body["notes"] = notes
if due:
body["due"] = due
params = {"tasklist": task_list_id, "body": body}
if parent:
params["parent"] = parent
if previous:
params["previous"] = previous
result = await asyncio.to_thread(
service.tasks().insert(**params).execute
)
response = f"""Task Created for {user_google_email}:
- Title: {result['title']}
- ID: {result['id']}
- Status: {result.get('status', 'N/A')}
- Updated: {result.get('updated', 'N/A')}"""
if result.get('due'):
response += f"\n- Due Date: {result['due']}"
if result.get('notes'):
response += f"\n- Notes: {result['notes']}"
if result.get('webViewLink'):
response += f"\n- Web View Link: {result['webViewLink']}"
logger.info(f"Created task '{title}' with ID {result['id']} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("update_task", service_type="tasks") # type: ignore
async def update_task(
service: Resource,
user_google_email: str,
task_list_id: str,
task_id: str,
title: Optional[str] = None,
notes: Optional[str] = None,
status: Optional[str] = None,
due: Optional[str] = None
) -> str:
"""
Update an existing task.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list containing the task.
task_id (str): The ID of the task to update.
title (Optional[str]): New title for the task.
notes (Optional[str]): New notes/description for the task.
status (Optional[str]): New status ("needsAction" or "completed").
due (Optional[str]): New due date in RFC 3339 format.
Returns:
str: Confirmation message with updated task details.
"""
logger.info(f"[update_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}")
try:
# First get the current task to build the update body
current_task = await asyncio.to_thread(
service.tasks().get(tasklist=task_list_id, task=task_id).execute
)
body = {
"id": task_id,
"title": title if title is not None else current_task.get("title", ""),
"status": status if status is not None else current_task.get("status", "needsAction")
}
if notes is not None:
body["notes"] = notes
elif current_task.get("notes"):
body["notes"] = current_task["notes"]
if due is not None:
body["due"] = due
elif current_task.get("due"):
body["due"] = current_task["due"]
result = await asyncio.to_thread(
service.tasks().update(tasklist=task_list_id, task=task_id, body=body).execute
)
response = f"""Task Updated for {user_google_email}:
- Title: {result['title']}
- ID: {result['id']}
- Status: {result.get('status', 'N/A')}
- Updated: {result.get('updated', 'N/A')}"""
if result.get('due'):
response += f"\n- Due Date: {result['due']}"
if result.get('notes'):
response += f"\n- Notes: {result['notes']}"
if result.get('completed'):
response += f"\n- Completed: {result['completed']}"
logger.info(f"Updated task {task_id} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("delete_task", service_type="tasks") # type: ignore
async def delete_task(
service: Resource,
user_google_email: str,
task_list_id: str,
task_id: str
) -> str:
"""
Delete a task from a task list.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list containing the task.
task_id (str): The ID of the task to delete.
Returns:
str: Confirmation message.
"""
logger.info(f"[delete_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}")
try:
await asyncio.to_thread(
service.tasks().delete(tasklist=task_list_id, task=task_id).execute
)
response = f"Task {task_id} has been deleted from task list {task_list_id} for {user_google_email}."
logger.info(f"Deleted task {task_id} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("move_task", service_type="tasks") # type: ignore
async def move_task(
service: Resource,
user_google_email: str,
task_list_id: str,
task_id: str,
parent: Optional[str] = None,
previous: Optional[str] = None,
destination_task_list: Optional[str] = None
) -> str:
"""
Move a task to a different position or parent within the same list, or to a different list.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the current task list containing the task.
task_id (str): The ID of the task to move.
parent (Optional[str]): New parent task ID (for making it a subtask).
previous (Optional[str]): Previous sibling task ID (for positioning).
destination_task_list (Optional[str]): Destination task list ID (for moving between lists).
Returns:
str: Confirmation message with updated task details.
"""
logger.info(f"[move_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}")
try:
params = {
"tasklist": task_list_id,
"task": task_id
}
if parent:
params["parent"] = parent
if previous:
params["previous"] = previous
if destination_task_list:
params["destinationTasklist"] = destination_task_list
result = await asyncio.to_thread(
service.tasks().move(**params).execute
)
response = f"""Task Moved for {user_google_email}:
- Title: {result['title']}
- ID: {result['id']}
- Status: {result.get('status', 'N/A')}
- Updated: {result.get('updated', 'N/A')}"""
if result.get('parent'):
response += f"\n- Parent Task ID: {result['parent']}"
if result.get('position'):
response += f"\n- Position: {result['position']}"
move_details = []
if destination_task_list:
move_details.append(f"moved to task list {destination_task_list}")
if parent:
move_details.append(f"made a subtask of {parent}")
if previous:
move_details.append(f"positioned after {previous}")
if move_details:
response += f"\n- Move Details: {', '.join(move_details)}"
logger.info(f"Moved task {task_id} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)
@server.tool() # type: ignore
@require_google_service("tasks", "tasks") # type: ignore
@handle_http_errors("clear_completed_tasks", service_type="tasks") # type: ignore
async def clear_completed_tasks(
service: Resource,
user_google_email: str,
task_list_id: str
) -> str:
"""
Clear all completed tasks from a task list. The tasks will be marked as hidden.
Args:
user_google_email (str): The user's Google email address. Required.
task_list_id (str): The ID of the task list to clear completed tasks from.
Returns:
str: Confirmation message.
"""
logger.info(f"[clear_completed_tasks] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}")
try:
await asyncio.to_thread(
service.tasks().clear(tasklist=task_list_id).execute
)
response = f"All completed tasks have been cleared from task list {task_list_id} for {user_google_email}. The tasks are now hidden and won't appear in default task list views."
logger.info(f"Cleared completed tasks from list {task_list_id} for {user_google_email}")
return response
except HttpError as error:
message = f"API error: {error}. You might need to re-authenticate. LLM: Try 'start_google_auth' with the user's email ({user_google_email}) and service_name='Google Tasks'."
logger.error(message, exc_info=True)
raise Exception(message)
except Exception as e:
message = f"Unexpected error: {e}."
logger.exception(message)
raise Exception(message)