-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
302 lines (266 loc) · 7.9 KB
/
Copy pathmcp_server.py
File metadata and controls
302 lines (266 loc) · 7.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
import os
import sys
from pathlib import Path
import django
from asgiref.sync import sync_to_async
from dotenv import load_dotenv
from mcp.server.fastmcp import FastMCP
REPO_ROOT = Path(__file__).resolve().parent.parent
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
load_dotenv(REPO_ROOT / ".env")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "team_task_manager.settings")
django.setup()
from core.agent import ( # noqa: E402
close_task_for_agent,
create_project_for_agent,
create_task_for_agent,
execute_agent_batch_request,
execute_agent_file_request,
list_members_for_agent,
list_projects_for_agent,
list_tasks_for_agent,
list_workspaces_for_agent,
update_task_for_agent,
)
from core.exceptions import DomainError # noqa: E402
server = FastMCP(
name="TTM Agent Tools",
instructions=(
"Use these tools to manage Team Task Manager workspaces, projects, and tasks "
"through Django domain services instead of browser clicks or raw API calls."
),
)
def resolve_actor_ref(actor_ref: str | None) -> str:
if actor_ref:
return actor_ref
default_actor = os.environ.get("TTM_AGENT_DEFAULT_ACTOR", "").strip()
if default_actor:
return default_actor
raise ValueError(
"No actor was provided. Set TTM_AGENT_DEFAULT_ACTOR in the plugin config or pass actor_ref."
)
def _domain_error(exc: DomainError) -> ValueError:
return ValueError(str(exc))
async def run_agent_sync(callable_obj, /, **kwargs):
try:
return await sync_to_async(callable_obj, thread_sensitive=True)(**kwargs)
except DomainError as exc:
raise _domain_error(exc) from exc
@server.tool(
name="ttm_get_context",
description=(
"Show the repo root and default actor used by the Team Task Manager "
"automation tools."
),
)
def ttm_get_context() -> dict:
return {
"repo_root": str(REPO_ROOT),
"default_actor": os.environ.get("TTM_AGENT_DEFAULT_ACTOR"),
}
@server.tool(
name="ttm_list_workspaces",
description="List workspaces the actor can access.",
)
async def ttm_list_workspaces(actor_ref: str | None = None) -> list[dict]:
return await run_agent_sync(
list_workspaces_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
)
@server.tool(
name="ttm_list_projects",
description="List projects in a workspace the actor can access.",
)
async def ttm_list_projects(workspace_ref: str, actor_ref: str | None = None) -> list[dict]:
return await run_agent_sync(
list_projects_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
workspace_ref=workspace_ref,
)
@server.tool(
name="ttm_list_members",
description="List members in a workspace so Codex can choose valid assignees and admins.",
)
async def ttm_list_members(workspace_ref: str, actor_ref: str | None = None) -> list[dict]:
return await run_agent_sync(
list_members_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
workspace_ref=workspace_ref,
)
@server.tool(
name="ttm_list_tasks",
description="List tasks in a project, including assignee, status, and priority.",
)
async def ttm_list_tasks(
workspace_ref: str,
project_ref: str,
actor_ref: str | None = None,
) -> list[dict]:
return await run_agent_sync(
list_tasks_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
workspace_ref=workspace_ref,
project_ref=project_ref,
)
@server.tool(
name="ttm_create_project",
description="Create a project inside a workspace using the project's Django services.",
)
async def ttm_create_project(
workspace_ref: str,
name: str,
description: str = "",
actor_ref: str | None = None,
) -> dict:
project = await run_agent_sync(
create_project_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
workspace_ref=workspace_ref,
name=name,
description=description,
)
return {
"workspace": project.workspace.slug,
"project": project.slug,
"name": project.name,
}
@server.tool(
name="ttm_create_task",
description=(
"Create a task in a project. Use list_members first when you need a "
"valid assignee."
),
)
async def ttm_create_task(
workspace_ref: str,
project_ref: str,
title: str,
description: str = "",
priority: str | None = None,
due_date: str | None = None,
assignee_ref: str | None = None,
status: str | None = None,
actor_ref: str | None = None,
) -> dict:
task = await run_agent_sync(
create_task_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
workspace_ref=workspace_ref,
project_ref=project_ref,
title=title,
description=description,
priority=priority,
due_date=due_date,
assignee_ref=assignee_ref,
status=status,
)
return {
"workspace": task.project.workspace.slug,
"project": task.project.slug,
"task": task.slug,
"title": task.title,
"status": task.status,
"priority": task.priority,
"assignee": task.assignee.username if task.assignee else None,
}
@server.tool(
name="ttm_update_task",
description=(
"Update an existing task by slug or title. Use assignee_ref='' to "
"clear the assignee."
),
)
async def ttm_update_task(
workspace_ref: str,
project_ref: str,
task_ref: str,
title: str | None = None,
description: str | None = None,
priority: str | None = None,
due_date: str | None = None,
assignee_ref: str | None = None,
status: str | None = None,
actor_ref: str | None = None,
) -> dict:
task = await run_agent_sync(
update_task_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
workspace_ref=workspace_ref,
project_ref=project_ref,
task_ref=task_ref,
title=title,
description=description,
priority=priority,
due_date=due_date,
assignee_ref=assignee_ref,
status=status,
)
return {
"workspace": task.project.workspace.slug,
"project": task.project.slug,
"task": task.slug,
"title": task.title,
"status": task.status,
"priority": task.priority,
"assignee": task.assignee.username if task.assignee else None,
}
@server.tool(
name="ttm_close_task",
description="Mark a task as done.",
)
async def ttm_close_task(
workspace_ref: str,
project_ref: str,
task_ref: str,
actor_ref: str | None = None,
) -> dict:
task = await run_agent_sync(
close_task_for_agent,
actor_ref=resolve_actor_ref(actor_ref),
workspace_ref=workspace_ref,
project_ref=project_ref,
task_ref=task_ref,
)
return {
"workspace": task.project.workspace.slug,
"project": task.project.slug,
"task": task.slug,
"title": task.title,
"status": task.status,
}
@server.tool(
name="ttm_apply_request",
description=(
"Apply a structured, batch, markdown, or bilingual request directly "
"through the TTM agent layer."
),
)
async def ttm_apply_request(
request_text: str,
preview: bool = False,
actor_ref: str | None = None,
) -> list[dict]:
return await run_agent_sync(
execute_agent_batch_request,
actor_ref=resolve_actor_ref(actor_ref),
request_text=request_text,
preview=preview,
)
@server.tool(
name="ttm_apply_file",
description="Apply a local brief file through the TTM agent layer.",
)
async def ttm_apply_file(
file_path: str,
preview: bool = False,
actor_ref: str | None = None,
) -> list[dict]:
return await run_agent_sync(
execute_agent_file_request,
actor_ref=resolve_actor_ref(actor_ref),
file_path=file_path,
preview=preview,
)
if __name__ == "__main__":
server.run()