-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasana_mcp_server.py
More file actions
504 lines (411 loc) · 16.4 KB
/
Copy pathasana_mcp_server.py
File metadata and controls
504 lines (411 loc) · 16.4 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
from __future__ import annotations
import os
import re
from dataclasses import dataclass
from datetime import date, datetime, timedelta
from pathlib import Path
from typing import Any
import requests
try:
from dotenv import load_dotenv
except ImportError: # pragma: no cover - dependency is declared for runtime use.
load_dotenv = None
try:
from fastmcp import FastMCP
except ImportError: # pragma: no cover - lets pure logic tests run before install.
FastMCP = None
DEFAULT_ASANA_API_BASE = "https://app.asana.com/api/1.0"
DEFAULT_IMPLEMENTED_SECTION_NAME = "Implemented"
TASK_OPT_FIELDS = "gid,name,due_on,completed,memberships.section.name"
LAST_ACTIVE_TASKS: list["TaskSummary"] = []
@dataclass(frozen=True)
class AsanaConfig:
token: str
workspace_gid: str
project_gid: str
api_base: str = DEFAULT_ASANA_API_BASE
implemented_section_name: str = DEFAULT_IMPLEMENTED_SECTION_NAME
@dataclass(frozen=True)
class TaskSummary:
list_number: int
gid: str
title: str
due_on: str | None = None
def to_dict(self) -> dict[str, Any]:
return {
"row_id": f"{self.list_number:03}",
"asana_task_id": self.gid,
"task_title": self.title,
"due_date": self.due_on or "",
}
class AsanaConfigError(RuntimeError):
pass
class AsanaApiError(RuntimeError):
pass
def load_environment() -> None:
if load_dotenv is not None:
load_dotenv()
return
env_path = Path(".env")
if not env_path.exists():
return
for raw_line in env_path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
os.environ.setdefault(key.strip(), value.strip().strip('"').strip("'"))
def load_config() -> AsanaConfig:
load_environment()
missing = [
name
for name in ("ASANA_TOKEN", "ASANA_WORKSPACE_GID", "ASANA_PROJECT_GID")
if not os.environ.get(name, "").strip()
]
if missing:
raise AsanaConfigError(f"Missing required environment variables: {', '.join(missing)}")
return AsanaConfig(
token=os.environ["ASANA_TOKEN"].strip(),
workspace_gid=os.environ["ASANA_WORKSPACE_GID"].strip(),
project_gid=os.environ["ASANA_PROJECT_GID"].strip(),
api_base=os.environ.get("ASANA_API_BASE", DEFAULT_ASANA_API_BASE).strip().rstrip("/"),
implemented_section_name=os.environ.get(
"ASANA_IMPLEMENTED_SECTION_NAME", DEFAULT_IMPLEMENTED_SECTION_NAME
).strip(),
)
class AsanaClient:
def __init__(self, config: AsanaConfig, session: requests.Session | None = None) -> None:
self.config = config
self.session = session or requests.Session()
self.session.headers.update(
{
"Authorization": f"Bearer {config.token}",
"Accept": "application/json",
"Content-Type": "application/json",
}
)
def _request(self, method: str, path: str, **kwargs: Any) -> dict[str, Any]:
url = f"{self.config.api_base}{path}"
try:
response = self.session.request(method, url, timeout=20, **kwargs)
except requests.RequestException as exc:
raise AsanaApiError(f"network error: {exc}") from exc
if response.ok:
return response.json()
reason = response.text.strip()
try:
payload = response.json()
errors = payload.get("errors") or []
if errors:
reason = "; ".join(str(error.get("message", error)) for error in errors)
except ValueError:
pass
raise AsanaApiError(f"{response.status_code} {response.reason}: {reason}")
def create_task(self, description: str, due_on: date) -> dict[str, Any]:
payload = {
"data": {
"workspace": self.config.workspace_gid,
"projects": [self.config.project_gid],
"name": description,
"due_on": due_on.isoformat(),
}
}
return self._request("POST", "/tasks", json=payload).get("data", {})
def list_project_tasks(self) -> list[dict[str, Any]]:
params = {"opt_fields": TASK_OPT_FIELDS, "limit": 100}
tasks: list[dict[str, Any]] = []
path = f"/projects/{self.config.project_gid}/tasks"
while True:
response = self._request("GET", path, params=params)
tasks.extend(response.get("data", []))
next_page = response.get("next_page") or {}
offset = next_page.get("offset")
if not offset:
return tasks
params["offset"] = offset
def complete_task(self, task_gid: str) -> dict[str, Any]:
return self._request("PUT", f"/tasks/{task_gid}", json={"data": {"completed": True}}).get(
"data", {}
)
def modify_task(
self,
task_gid: str,
description: str | None = None,
due_on: date | None = None,
) -> dict[str, Any]:
data: dict[str, Any] = {}
if description is not None:
data["name"] = description
if due_on is not None:
data["due_on"] = due_on.isoformat()
if not data:
raise ValueError("Provide a new description, a new due date, or both.")
return self._request("PUT", f"/tasks/{task_gid}", json={"data": data}).get("data", {})
def parse_due_date(value: str, today: date | None = None) -> date:
base = today or date.today()
normalized = " ".join(value.strip().lower().split())
if normalized == "today":
return base
if normalized == "tomorrow":
return base + timedelta(days=1)
if normalized in {"next week", "a week from now", "one week from now"}:
return base + timedelta(days=7)
days_match = re.fullmatch(r"(\d+)\s+days?\s+from\s+now", normalized)
if days_match:
return base + timedelta(days=int(days_match.group(1)))
for fmt in (
"%m/%d/%Y",
"%m/%d/%y",
"%Y-%m-%d",
"%B %d, %Y",
"%B %d %Y",
"%b %d, %Y",
"%b %d %Y",
):
try:
return datetime.strptime(value.strip(), fmt).date()
except ValueError:
continue
raise ValueError(
"Use today, tomorrow, 3 days from now, next week, MM/DD/YYYY, or YYYY-MM-DD."
)
def is_active_task(task: dict[str, Any], implemented_section_name: str) -> bool:
if task.get("completed"):
return False
for membership in task.get("memberships", []) or []:
section = membership.get("section") or {}
if section.get("name", "").casefold() == implemented_section_name.casefold():
return False
return True
def summarize_active_tasks(
tasks: list[dict[str, Any]], implemented_section_name: str
) -> list[TaskSummary]:
active_tasks = [task for task in tasks if is_active_task(task, implemented_section_name)]
return [
TaskSummary(index, task.get("gid", ""), task.get("name", ""), task.get("due_on"))
for index, task in enumerate(active_tasks, start=1)
]
def format_task_table(tasks: list[TaskSummary]) -> str:
lines = [
"================================================",
" Active Asana tasks",
"================================================",
"Row ID | Asana Task id | Task title | Due Date",
]
if not tasks:
lines.append("No active Asana tasks found.")
return "\n".join(lines)
for task in tasks:
lines.append(f"{task.list_number:03} | {task.gid} | {task.title} | {task.due_on or ''}")
return "\n".join(lines)
def ordinal_to_number(text: str) -> int | None:
words = {
"first": 1,
"second": 2,
"third": 3,
"fourth": 4,
"fifth": 5,
"sixth": 6,
"seventh": 7,
"eighth": 8,
"ninth": 9,
"tenth": 10,
}
for word, number in words.items():
if re.search(rf"\b{word}\b", text):
return number
match = re.search(r"\b(\d+)(?:st|nd|rd|th)\b", text)
if match:
return int(match.group(1))
return None
def resolve_task_reference(task_ref: str, tasks: list[TaskSummary]) -> tuple[list[TaskSummary], str | None]:
text = task_ref.strip().lower()
if not text:
return [], "Task reference is empty."
top_match = re.search(r"\btop\s+(\d+)\b", text)
if top_match:
count = int(top_match.group(1))
return tasks[:count], None
ordinal = ordinal_to_number(text)
if ordinal is not None:
matches = [task for task in tasks if task.list_number == ordinal]
return matches, None if matches else f"No active task found for list number {ordinal:03}."
number_match = re.fullmatch(r"0*(\d+)", text)
if number_match:
number = int(number_match.group(1))
matches = [
task for task in tasks if task.list_number == number or task.gid == task_ref.strip()
]
return matches, None if matches else f"No active task found for {task_ref}."
gid_matches = [task for task in tasks if task.gid == task_ref.strip()]
if gid_matches:
return gid_matches, None
exact_title = [task for task in tasks if task.title.casefold() == task_ref.strip().casefold()]
if exact_title:
if len(exact_title) > 1:
return exact_title, "Multiple tasks matched that title. Use a Row ID or task id."
return exact_title, None
partial_title = [task for task in tasks if text in task.title.casefold()]
if len(partial_title) > 1:
return partial_title, "Multiple tasks matched that title. Use a Row ID or task id."
if partial_title:
return partial_title, None
return [], f"No active task matched {task_ref!r}."
def get_client() -> AsanaClient:
return AsanaClient(load_config())
def create_asana_task(description: str, due_date: str) -> dict[str, Any]:
"""Create an Asana task in the configured workspace and project."""
description = description.strip()
if not description:
return {"success": False, "message": "Task description is required."}
try:
config = load_config()
due_on = parse_due_date(due_date)
task = AsanaClient(config).create_task(description, due_on)
except (AsanaConfigError, AsanaApiError, ValueError) as exc:
return {"success": False, "message": f"failed to submit task: {exc}"}
task_gid = task.get("gid", "")
return {
"success": True,
"task_gid": task_gid,
"workspace_gid": config.workspace_gid,
"project_gid": config.project_gid,
"due_on": due_on.isoformat(),
"message": f"successfully submitted task {task_gid}",
}
def list_asana_tasks() -> dict[str, Any]:
"""List active Asana tasks in the configured project."""
global LAST_ACTIVE_TASKS
try:
config = load_config()
active_tasks = summarize_active_tasks(
AsanaClient(config).list_project_tasks(), config.implemented_section_name
)
except (AsanaConfigError, AsanaApiError) as exc:
return {"success": False, "message": f"failed to list tasks: {exc}", "tasks": []}
LAST_ACTIVE_TASKS = active_tasks
return {
"success": True,
"count": len(active_tasks),
"tasks": [task.to_dict() for task in active_tasks],
"table": format_task_table(active_tasks),
}
def close_asana_task(task_ref: str) -> dict[str, Any]:
"""Close one or more Asana tasks by marking them complete."""
global LAST_ACTIVE_TASKS
try:
config = load_config()
client = AsanaClient(config)
active_tasks = LAST_ACTIVE_TASKS or summarize_active_tasks(
client.list_project_tasks(), config.implemented_section_name
)
targets, resolution_error = resolve_task_reference(task_ref, active_tasks)
if resolution_error:
return {
"success": False,
"message": resolution_error,
"matches": [task.to_dict() for task in targets],
}
if not targets:
return {"success": False, "message": f"No active task matched {task_ref!r}."}
results = []
for task in targets:
try:
client.complete_task(task.gid)
results.append(
{
"success": True,
"asana_task_id": task.gid,
"task_title": task.title,
"message": f"closed task {task.gid}",
}
)
except AsanaApiError as exc:
results.append(
{
"success": False,
"asana_task_id": task.gid,
"task_title": task.title,
"message": f"failed to close task {task.gid}: {exc}",
}
)
except (AsanaConfigError, AsanaApiError) as exc:
return {"success": False, "message": f"failed to close task: {exc}"}
successful_gids = {result["asana_task_id"] for result in results if result["success"]}
LAST_ACTIVE_TASKS = [task for task in active_tasks if task.gid not in successful_gids]
return {
"success": all(result["success"] for result in results),
"closed_count": len(successful_gids),
"results": results,
}
def modify_asana_task(
task_ref: str,
description: str | None = None,
due_date: str | None = None,
) -> dict[str, Any]:
"""Modify an Asana task title/description and/or due date."""
global LAST_ACTIVE_TASKS
new_description = description.strip() if description is not None else None
if new_description == "":
return {"success": False, "message": "Description cannot be blank."}
try:
due_on = parse_due_date(due_date) if due_date and due_date.strip() else None
if new_description is None and due_on is None:
return {"success": False, "message": "Provide a description, a due_date, or both."}
config = load_config()
client = AsanaClient(config)
active_tasks = LAST_ACTIVE_TASKS or summarize_active_tasks(
client.list_project_tasks(), config.implemented_section_name
)
targets, resolution_error = resolve_task_reference(task_ref, active_tasks)
if resolution_error:
return {
"success": False,
"message": resolution_error,
"matches": [task.to_dict() for task in targets],
}
if not targets:
return {"success": False, "message": f"No active task matched {task_ref!r}."}
if len(targets) > 1:
return {
"success": False,
"message": "Multiple tasks matched. Use a single Row ID or task id.",
"matches": [task.to_dict() for task in targets],
}
target = targets[0]
task = client.modify_task(target.gid, description=new_description, due_on=due_on)
except (AsanaConfigError, AsanaApiError, ValueError) as exc:
return {"success": False, "message": f"failed to modify task: {exc}"}
updated_title = task.get("name") or new_description or target.title
updated_due_on = task.get("due_on") or (due_on.isoformat() if due_on else None)
updated_summary = TaskSummary(target.list_number, target.gid, updated_title, updated_due_on)
LAST_ACTIVE_TASKS = [
updated_summary if cached_task.gid == target.gid else cached_task
for cached_task in active_tasks
]
return {
"success": True,
"asana_task_id": target.gid,
"task_title": updated_title,
"due_on": updated_due_on,
"message": f"modified task {target.gid}",
}
def build_server() -> Any:
if FastMCP is None:
raise RuntimeError("fastmcp is required to run this MCP server. Install with `uv sync`.")
server = FastMCP(
name="AsanaProjectServer",
instructions=(
"Use these tools to create, list, modify, and close tasks in the configured Asana project."
),
)
server.tool()(create_asana_task)
server.tool()(list_asana_tasks)
server.tool()(close_asana_task)
server.tool()(modify_asana_task)
return server
mcp = build_server() if FastMCP is not None else None
if __name__ == "__main__":
if mcp is None:
raise RuntimeError("fastmcp is required to run this MCP server. Install with `uv sync`.")
mcp.run()