Spaces:
Paused
Paused
Download gcal_server.py from OverbyteLabs/zeroclaw: direct link, hf CLI and curl.
- Browser
- Download file 15.3 kB
-
https://huggingface.co/spaces/OverbyteLabs/zeroclaw/resolve/main/gcal_server.py
- Command line
-
hf download hf://spaces/OverbyteLabs/zeroclaw/gcal_server.py
-
curl -L -o gcal_server.py https://huggingface.co/spaces/OverbyteLabs/zeroclaw/resolve/main/gcal_server.py
15.3 kB
| #!/usr/bin/env python3 | |
| """ | |
| Google Calendar + Tasks MCP Server for ZeroClaw | |
| Uses official Google libraries only. | |
| Deps: google-auth-oauthlib google-api-python-client mcp | |
| """ | |
| import asyncio | |
| import os | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from typing import Any | |
| from google.auth.transport.requests import Request | |
| from google.oauth2.credentials import Credentials | |
| from googleapiclient.discovery import build | |
| import mcp.types as types | |
| from mcp.server import Server | |
| from mcp.server.stdio import stdio_server | |
| SCOPES = [ | |
| "https://www.googleapis.com/auth/calendar", | |
| "https://www.googleapis.com/auth/tasks", | |
| ] | |
| TOKEN_FILE = Path(os.environ.get("GOOGLE_TOKEN_FILE", "/zeroclaw-data/.zeroclaw/google_token.json")) | |
| app = Server("google-workspace") | |
| def get_credentials(): | |
| if not TOKEN_FILE.exists(): | |
| raise RuntimeError( | |
| f"Not authenticated. Token file not found: {TOKEN_FILE}\n" | |
| "Copy the token file into the ZeroClaw volume and restart." | |
| ) | |
| creds = Credentials.from_authorized_user_file(str(TOKEN_FILE), SCOPES) | |
| if creds.expired and creds.refresh_token: | |
| creds.refresh(Request()) | |
| TOKEN_FILE.write_text(creds.to_json()) | |
| return creds | |
| def cal_service(): | |
| return build("calendar", "v3", credentials=get_credentials()) | |
| def tasks_service(): | |
| return build("tasks", "v1", credentials=get_credentials()) | |
| async def list_tools() -> list[types.Tool]: | |
| return [ | |
| # ΓöÇΓöÇ Calendar tools ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ | |
| types.Tool( | |
| name="list_calendar_events", | |
| description="List upcoming Google Calendar events", | |
| inputSchema={ | |
| "type": "object", | |
| "properties": { | |
| "max_results": {"type": "integer", "default": 10}, | |
| "calendar_id": {"type": "string", "default": "primary"}, | |
| "time_min": {"type": "string", "description": "ISO 8601 start filter (default: now)"}, | |
| "time_max": {"type": "string", "description": "ISO 8601 end filter"}, | |
| }, | |
| }, | |
| ), | |
| types.Tool( | |
| name="create_calendar_event", | |
| description="Create a new Google Calendar event", | |
| inputSchema={ | |
| "type": "object", | |
| "required": ["summary", "start_time", "end_time"], | |
| "properties": { | |
| "summary": {"type": "string"}, | |
| "start_time": {"type": "string", "description": "ISO 8601 e.g. 2026-04-22T14:00:00+01:00"}, | |
| "end_time": {"type": "string"}, | |
| "description": {"type": "string"}, | |
| "location": {"type": "string"}, | |
| "timezone": {"type": "string", "default": "UTC"}, | |
| "calendar_id": {"type": "string", "default": "primary"}, | |
| }, | |
| }, | |
| ), | |
| types.Tool( | |
| name="update_calendar_event", | |
| description="Update an existing Google Calendar event", | |
| inputSchema={ | |
| "type": "object", | |
| "required": ["event_id"], | |
| "properties": { | |
| "event_id": {"type": "string"}, | |
| "summary": {"type": "string"}, | |
| "start_time": {"type": "string"}, | |
| "end_time": {"type": "string"}, | |
| "description": {"type": "string"}, | |
| "location": {"type": "string"}, | |
| "calendar_id": {"type": "string", "default": "primary"}, | |
| }, | |
| }, | |
| ), | |
| types.Tool( | |
| name="delete_calendar_event", | |
| description="Delete a Google Calendar event by ID", | |
| inputSchema={ | |
| "type": "object", | |
| "required": ["event_id"], | |
| "properties": { | |
| "event_id": {"type": "string"}, | |
| "calendar_id": {"type": "string", "default": "primary"}, | |
| }, | |
| }, | |
| ), | |
| # ΓöÇΓöÇ Tasks tools ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ | |
| types.Tool( | |
| name="list_task_lists", | |
| description="List all Google Task lists", | |
| inputSchema={"type": "object", "properties": {}}, | |
| ), | |
| types.Tool( | |
| name="list_tasks", | |
| description="List tasks in a Google Task list", | |
| inputSchema={ | |
| "type": "object", | |
| "properties": { | |
| "tasklist_id": {"type": "string", "default": "@default", "description": "Task list ID (use list_task_lists to find IDs)"}, | |
| "show_completed": {"type": "boolean", "default": False}, | |
| "show_hidden": {"type": "boolean", "default": False}, | |
| "max_results": {"type": "integer", "default": 20}, | |
| }, | |
| }, | |
| ), | |
| types.Tool( | |
| name="create_task", | |
| description="Create a new task in a Google Task list", | |
| inputSchema={ | |
| "type": "object", | |
| "required": ["title"], | |
| "properties": { | |
| "title": {"type": "string"}, | |
| "notes": {"type": "string"}, | |
| "due": {"type": "string", "description": "RFC 3339 due date e.g. 2026-04-30T00:00:00Z"}, | |
| "tasklist_id": {"type": "string", "default": "@default"}, | |
| }, | |
| }, | |
| ), | |
| types.Tool( | |
| name="update_task", | |
| description="Update an existing task (title, notes, due date)", | |
| inputSchema={ | |
| "type": "object", | |
| "required": ["task_id"], | |
| "properties": { | |
| "task_id": {"type": "string"}, | |
| "title": {"type": "string"}, | |
| "notes": {"type": "string"}, | |
| "due": {"type": "string"}, | |
| "tasklist_id": {"type": "string", "default": "@default"}, | |
| }, | |
| }, | |
| ), | |
| types.Tool( | |
| name="complete_task", | |
| description="Mark a task as completed", | |
| inputSchema={ | |
| "type": "object", | |
| "required": ["task_id"], | |
| "properties": { | |
| "task_id": {"type": "string"}, | |
| "tasklist_id": {"type": "string", "default": "@default"}, | |
| }, | |
| }, | |
| ), | |
| types.Tool( | |
| name="delete_task", | |
| description="Delete a task by ID", | |
| inputSchema={ | |
| "type": "object", | |
| "required": ["task_id"], | |
| "properties": { | |
| "task_id": {"type": "string"}, | |
| "tasklist_id": {"type": "string", "default": "@default"}, | |
| }, | |
| }, | |
| ), | |
| ] | |
| async def call_tool(name: str, arguments: dict[str, Any]) -> list[types.TextContent]: | |
| try: | |
| get_credentials() | |
| except RuntimeError as e: | |
| return [types.TextContent(type="text", text=str(e))] | |
| # ΓöÇΓöÇ Calendar ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ | |
| if name == "list_calendar_events": | |
| svc = cal_service() | |
| cal_id = arguments.get("calendar_id", "primary") | |
| max_results = int(arguments.get("max_results", 10)) | |
| time_min = arguments.get("time_min", datetime.now(timezone.utc).isoformat()) | |
| params = dict(calendarId=cal_id, timeMin=time_min, maxResults=max_results, singleEvents=True, orderBy="startTime") | |
| if "time_max" in arguments: | |
| params["timeMax"] = arguments["time_max"] | |
| result = svc.events().list(**params).execute() | |
| events = result.get("items", []) | |
| if not events: | |
| return [types.TextContent(type="text", text="No upcoming events found.")] | |
| lines = [f"Upcoming {len(events)} event(s):"] | |
| for e in events: | |
| start = e["start"].get("dateTime", e["start"].get("date")) | |
| lines.append(f" [{e['id']}] {e.get('summary', '(no title)')} @ {start}") | |
| return [types.TextContent(type="text", text="\n".join(lines))] | |
| elif name == "create_calendar_event": | |
| svc = cal_service() | |
| cal_id = arguments.get("calendar_id", "primary") | |
| tz = arguments.get("timezone", "UTC") | |
| event = { | |
| "summary": arguments["summary"], | |
| "start": {"dateTime": arguments["start_time"], "timeZone": tz}, | |
| "end": {"dateTime": arguments["end_time"], "timeZone": tz}, | |
| } | |
| for opt in ("description", "location"): | |
| if opt in arguments: | |
| event[opt] = arguments[opt] | |
| created = svc.events().insert(calendarId=cal_id, body=event).execute() | |
| return [types.TextContent(type="text", text=f"Created event: [{created['id']}] {created['summary']}")] | |
| elif name == "update_calendar_event": | |
| svc = cal_service() | |
| cal_id = arguments.get("calendar_id", "primary") | |
| event = svc.events().get(calendarId=cal_id, eventId=arguments["event_id"]).execute() | |
| for field in ("summary", "description", "location"): | |
| if field in arguments: | |
| event[field] = arguments[field] | |
| if "start_time" in arguments: | |
| event["start"]["dateTime"] = arguments["start_time"] | |
| if "end_time" in arguments: | |
| event["end"]["dateTime"] = arguments["end_time"] | |
| updated = svc.events().update(calendarId=cal_id, eventId=arguments["event_id"], body=event).execute() | |
| return [types.TextContent(type="text", text=f"Updated event: [{updated['id']}] {updated['summary']}")] | |
| elif name == "delete_calendar_event": | |
| svc = cal_service() | |
| cal_id = arguments.get("calendar_id", "primary") | |
| svc.events().delete(calendarId=cal_id, eventId=arguments["event_id"]).execute() | |
| return [types.TextContent(type="text", text=f"Deleted event {arguments['event_id']}")] | |
| # ΓöÇΓöÇ Tasks ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ | |
| elif name == "list_task_lists": | |
| svc = tasks_service() | |
| result = svc.tasklists().list().execute() | |
| lists = result.get("items", []) | |
| if not lists: | |
| return [types.TextContent(type="text", text="No task lists found.")] | |
| lines = [f"Task lists ({len(lists)}):"] | |
| for tl in lists: | |
| lines.append(f" [{tl['id']}] {tl['title']}") | |
| return [types.TextContent(type="text", text="\n".join(lines))] | |
| elif name == "list_tasks": | |
| svc = tasks_service() | |
| tl_id = arguments.get("tasklist_id", "@default") | |
| params = dict( | |
| tasklist=tl_id, | |
| showCompleted=arguments.get("show_completed", False), | |
| showHidden=arguments.get("show_hidden", False), | |
| maxResults=int(arguments.get("max_results", 20)), | |
| ) | |
| result = svc.tasks().list(**params).execute() | |
| tasks = result.get("items", []) | |
| if not tasks: | |
| return [types.TextContent(type="text", text="No tasks found.")] | |
| lines = [f"Tasks ({len(tasks)}):"] | |
| for t in tasks: | |
| status = "Γ£ô" if t.get("status") == "completed" else "Γùï" | |
| due = f" (due {t['due'][:10]})" if t.get("due") else "" | |
| lines.append(f" {status} [{t['id']}] {t.get('title', '(no title)')}{due}") | |
| if t.get("notes"): | |
| lines.append(f" {t['notes']}") | |
| return [types.TextContent(type="text", text="\n".join(lines))] | |
| elif name == "create_task": | |
| svc = tasks_service() | |
| tl_id = arguments.get("tasklist_id", "@default") | |
| body: dict[str, Any] = {"title": arguments["title"], "status": "needsAction"} | |
| if "notes" in arguments: | |
| body["notes"] = arguments["notes"] | |
| if "due" in arguments: | |
| body["due"] = arguments["due"] | |
| created = svc.tasks().insert(tasklist=tl_id, body=body).execute() | |
| return [types.TextContent(type="text", text=f"Created task: [{created['id']}] {created['title']}")] | |
| elif name == "update_task": | |
| svc = tasks_service() | |
| tl_id = arguments.get("tasklist_id", "@default") | |
| task = svc.tasks().get(tasklist=tl_id, task=arguments["task_id"]).execute() | |
| for field in ("title", "notes", "due"): | |
| if field in arguments: | |
| task[field] = arguments[field] | |
| updated = svc.tasks().update(tasklist=tl_id, task=arguments["task_id"], body=task).execute() | |
| return [types.TextContent(type="text", text=f"Updated task: [{updated['id']}] {updated['title']}")] | |
| elif name == "complete_task": | |
| svc = tasks_service() | |
| tl_id = arguments.get("tasklist_id", "@default") | |
| task = svc.tasks().get(tasklist=tl_id, task=arguments["task_id"]).execute() | |
| task["status"] = "completed" | |
| updated = svc.tasks().update(tasklist=tl_id, task=arguments["task_id"], body=task).execute() | |
| return [types.TextContent(type="text", text=f"Completed task: [{updated['id']}] {updated['title']}")] | |
| elif name == "delete_task": | |
| svc = tasks_service() | |
| tl_id = arguments.get("tasklist_id", "@default") | |
| svc.tasks().delete(tasklist=tl_id, task=arguments["task_id"]).execute() | |
| return [types.TextContent(type="text", text=f"Deleted task {arguments['task_id']}")] | |
| return [types.TextContent(type="text", text=f"Unknown tool: {name}")] | |
| async def main(): | |
| async with stdio_server() as (read_stream, write_stream): | |
| await app.run(read_stream, write_stream, app.create_initialization_options()) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |