-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgha_mcp_server.py
More file actions
464 lines (386 loc) · 14.5 KB
/
Copy pathgha_mcp_server.py
File metadata and controls
464 lines (386 loc) · 14.5 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
import json
import os
import time
from datetime import datetime, timezone
from typing import Any
import boto3
import httpx
from botocore.exceptions import BotoCoreError, ClientError
from dotenv import load_dotenv
from mcp.server.fastmcp import FastMCP
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
import uvicorn
load_dotenv()
def bootstrap_aws_region() -> str:
return os.getenv("AWS_REGION", "ap-northeast-1")
def parse_remote_runtime_config(raw_payload: str, *, source: str) -> dict[str, Any]:
try:
payload = json.loads(raw_payload)
except json.JSONDecodeError as exc:
raise RuntimeError(f"{source} must contain a valid JSON object.") from exc
if not isinstance(payload, dict):
raise RuntimeError(f"{source} must contain a JSON object at the top level.")
return payload
def apply_remote_runtime_config(payload: dict[str, Any]) -> None:
for key, value in payload.items():
if value is None:
continue
if isinstance(value, bool):
normalized = "true" if value else "false"
elif isinstance(value, (str, int, float)):
normalized = str(value)
elif isinstance(value, list):
normalized = ",".join(str(item) for item in value)
else:
raise RuntimeError(
f"Unsupported value type for runtime config key '{key}': {type(value).__name__}."
)
os.environ[key] = normalized
def load_runtime_config_from_secrets_manager(secret_id: str) -> None:
client = boto3.client("secretsmanager", region_name=bootstrap_aws_region())
response = client.get_secret_value(SecretId=secret_id)
secret_string = response.get("SecretString")
if not secret_string:
raise RuntimeError("Secrets Manager runtime config must be stored as SecretString.")
apply_remote_runtime_config(
parse_remote_runtime_config(secret_string, source=f"Secrets Manager secret '{secret_id}'")
)
def load_runtime_config_from_ssm(parameter_name: str) -> None:
client = boto3.client("ssm", region_name=bootstrap_aws_region())
response = client.get_parameter(Name=parameter_name, WithDecryption=True)
parameter_value = response["Parameter"]["Value"]
apply_remote_runtime_config(
parse_remote_runtime_config(parameter_value, source=f"SSM parameter '{parameter_name}'")
)
def bootstrap_runtime_config() -> str:
secret_id = os.getenv("RUNTIME_CONFIG_SECRET_ID", "").strip()
parameter_name = os.getenv("RUNTIME_CONFIG_SSM_PARAMETER_NAME", "").strip()
if secret_id and parameter_name:
raise RuntimeError(
"Configure only one of RUNTIME_CONFIG_SECRET_ID or RUNTIME_CONFIG_SSM_PARAMETER_NAME."
)
if secret_id:
load_runtime_config_from_secrets_manager(secret_id)
return "secretsmanager"
if parameter_name:
load_runtime_config_from_ssm(parameter_name)
return "ssm"
return "environment"
RUNTIME_CONFIG_SOURCE = bootstrap_runtime_config()
AWS_REGION = bootstrap_aws_region()
HOST = os.getenv("HOST", "0.0.0.0")
PORT = int(os.getenv("PORT", "8000"))
GITHUB_PAT_SECRET_ID = os.getenv("GITHUB_PAT_SECRET_ID", "").strip()
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY", "").strip()
ALLOWED_REPOSITORIES = [
r.strip()
for r in os.getenv("ALLOWED_REPOSITORIES", "").split(",")
if r.strip()
]
LOG_TAIL_MAX_LINES = int(os.getenv("LOG_TAIL_MAX_LINES", "500"))
GITHUB_API_BASE = "https://api.github.com"
_github_pat_cache: str | None = None
def get_github_pat() -> str:
global _github_pat_cache
if _github_pat_cache:
return _github_pat_cache
pat = os.getenv("GITHUB_PAT", "").strip()
if pat:
_github_pat_cache = pat
return pat
if not GITHUB_PAT_SECRET_ID:
raise RuntimeError("GITHUB_PAT or GITHUB_PAT_SECRET_ID must be configured.")
client = boto3.client("secretsmanager", region_name=AWS_REGION)
response = client.get_secret_value(SecretId=GITHUB_PAT_SECRET_ID)
pat = response["SecretString"].strip()
_github_pat_cache = pat
return pat
def effective_repository() -> str:
if GITHUB_REPOSITORY:
return GITHUB_REPOSITORY
if ALLOWED_REPOSITORIES:
return ALLOWED_REPOSITORIES[0]
raise RuntimeError("GITHUB_REPOSITORY must be configured.")
def effective_allowed_repositories() -> list[str]:
if ALLOWED_REPOSITORIES:
return ALLOWED_REPOSITORIES
repo = effective_repository()
return [repo] if repo else []
def github_get(path: str, params: dict[str, Any] | None = None) -> dict[str, Any] | str:
pat = get_github_pat()
headers = {
"Authorization": f"Bearer {pat}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
with httpx.Client(timeout=30.0) as client:
response = client.get(f"{GITHUB_API_BASE}{path}", headers=headers, params=params)
response.raise_for_status()
content_type = response.headers.get("content-type", "")
if "application/json" in content_type:
return response.json()
return response.text
mcp = FastMCP(
"github-actions-mcp",
host=HOST,
port=PORT,
streamable_http_path="/mcp",
stateless_http=True,
)
def now_utc() -> datetime:
return datetime.now(timezone.utc)
def error_response(message: str, *, error_type: str = "Error", **extra: Any) -> dict[str, Any]:
payload = {
"ok": False,
"error_type": error_type,
"message": message,
"region": AWS_REGION,
}
payload.update(extra)
return payload
async def healthz(_request) -> JSONResponse:
return JSONResponse(
{
"ok": True,
"service": "github-actions-mcp",
"region": AWS_REGION,
"repository": GITHUB_REPOSITORY or None,
"allowed_repositories": effective_allowed_repositories(),
"runtime_config_source": RUNTIME_CONFIG_SOURCE,
}
)
def build_app() -> Starlette:
base_app = mcp.streamable_http_app()
mcp_route = next(route for route in base_app.routes if getattr(route, "path", None) == "/mcp")
app = Starlette(
debug=base_app.debug,
routes=[
Route("/mcp", endpoint=mcp_route.endpoint),
Route("/mcp/", endpoint=mcp_route.endpoint),
Route("/healthz", endpoint=healthz, methods=["GET"]),
],
middleware=base_app.user_middleware,
lifespan=base_app.router.lifespan_context,
)
app.router.redirect_slashes = False
return app
@mcp.tool()
def list_workflows() -> dict[str, Any]:
"""List all workflow definitions in the repository."""
repo = effective_repository()
try:
data = github_get(f"/repos/{repo}/actions/workflows")
workflows = [
{
"id": w["id"],
"name": w["name"],
"path": w["path"],
"state": w["state"],
}
for w in data.get("workflows", [])
]
return {
"ok": True,
"repository": repo,
"workflows": workflows,
"queried_at": now_utc().isoformat(),
}
except httpx.HTTPStatusError as exc:
return error_response(
f"GitHub API error: {exc.response.status_code}",
error_type="GitHubAPIError",
details=exc.response.text[:500],
)
except Exception as exc:
return error_response(str(exc))
@mcp.tool()
def list_workflow_runs(
status: str = "",
branch: str = "",
limit: int = 10,
) -> dict[str, Any]:
"""List recent workflow runs with optional filters.
Args:
status: Filter by status ("completed", "in_progress", "queued", "success", "failure").
branch: Filter by branch name.
limit: Number of runs to return (default: 10, max: 30).
"""
repo = effective_repository()
limit = max(1, min(limit, 30))
params: dict[str, Any] = {"per_page": limit}
if status:
params["status"] = status
if branch:
params["branch"] = branch
try:
data = github_get(f"/repos/{repo}/actions/runs", params=params)
runs = []
for r in data.get("workflow_runs", [])[:limit]:
created = r.get("created_at", "")
updated = r.get("updated_at", "")
duration = None
if created and updated:
try:
t0 = datetime.fromisoformat(created.replace("Z", "+00:00"))
t1 = datetime.fromisoformat(updated.replace("Z", "+00:00"))
duration = int((t1 - t0).total_seconds())
except (ValueError, TypeError):
pass
runs.append({
"id": r["id"],
"name": r.get("name", ""),
"status": r.get("status", ""),
"conclusion": r.get("conclusion"),
"branch": r.get("head_branch", ""),
"commit_sha": r.get("head_sha", "")[:7],
"commit_message": (r.get("head_commit") or {}).get("message", "")[:120],
"actor": (r.get("actor") or {}).get("login", ""),
"created_at": created,
"updated_at": updated,
"run_duration_seconds": duration,
"url": r.get("html_url", ""),
})
return {
"ok": True,
"repository": repo,
"total_count": data.get("total_count", 0),
"runs": runs,
"queried_at": now_utc().isoformat(),
}
except httpx.HTTPStatusError as exc:
return error_response(
f"GitHub API error: {exc.response.status_code}",
error_type="GitHubAPIError",
details=exc.response.text[:500],
)
except Exception as exc:
return error_response(str(exc))
@mcp.tool()
def get_workflow_run(run_id: int) -> dict[str, Any]:
"""Get details of a specific workflow run.
Args:
run_id: The workflow run ID.
"""
repo = effective_repository()
try:
r = github_get(f"/repos/{repo}/actions/runs/{run_id}")
return {
"ok": True,
"run": {
"id": r["id"],
"name": r.get("name", ""),
"status": r.get("status", ""),
"conclusion": r.get("conclusion"),
"branch": r.get("head_branch", ""),
"event": r.get("event", ""),
"commit_sha": r.get("head_sha", ""),
"commit_message": (r.get("head_commit") or {}).get("message", ""),
"actor": (r.get("actor") or {}).get("login", ""),
"triggering_actor": (r.get("triggering_actor") or {}).get("login", ""),
"created_at": r.get("created_at", ""),
"updated_at": r.get("updated_at", ""),
"run_attempt": r.get("run_attempt", 1),
"url": r.get("html_url", ""),
},
"queried_at": now_utc().isoformat(),
}
except httpx.HTTPStatusError as exc:
return error_response(
f"GitHub API error: {exc.response.status_code}",
error_type="GitHubAPIError",
details=exc.response.text[:500],
)
except Exception as exc:
return error_response(str(exc))
@mcp.tool()
def get_workflow_run_jobs(run_id: int) -> dict[str, Any]:
"""Get all jobs and their step statuses for a workflow run.
Args:
run_id: The workflow run ID.
"""
repo = effective_repository()
try:
data = github_get(f"/repos/{repo}/actions/runs/{run_id}/jobs")
jobs = []
for j in data.get("jobs", []):
steps = [
{
"number": s.get("number"),
"name": s.get("name", ""),
"status": s.get("status", ""),
"conclusion": s.get("conclusion"),
}
for s in j.get("steps", [])
]
jobs.append({
"id": j["id"],
"name": j.get("name", ""),
"status": j.get("status", ""),
"conclusion": j.get("conclusion"),
"started_at": j.get("started_at", ""),
"completed_at": j.get("completed_at", ""),
"steps": steps,
})
return {
"ok": True,
"run_id": run_id,
"jobs": jobs,
"queried_at": now_utc().isoformat(),
}
except httpx.HTTPStatusError as exc:
return error_response(
f"GitHub API error: {exc.response.status_code}",
error_type="GitHubAPIError",
details=exc.response.text[:500],
)
except Exception as exc:
return error_response(str(exc))
@mcp.tool()
def get_job_logs(job_id: int, tail_lines: int = 100) -> dict[str, Any]:
"""Get logs for a specific job. Returns the last N lines of the log output.
Args:
job_id: The job ID (from get_workflow_run_jobs).
tail_lines: Number of lines from the end of the log to return (default: 100, max: 500).
"""
repo = effective_repository()
tail_lines = max(1, min(tail_lines, LOG_TAIL_MAX_LINES))
try:
pat = get_github_pat()
headers = {
"Authorization": f"Bearer {pat}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
with httpx.Client(timeout=30.0, follow_redirects=True) as client:
response = client.get(
f"{GITHUB_API_BASE}/repos/{repo}/actions/jobs/{job_id}/logs",
headers=headers,
)
response.raise_for_status()
log_text = response.text
lines = log_text.splitlines()
total_lines = len(lines)
truncated = total_lines > tail_lines
if truncated:
lines = lines[-tail_lines:]
return {
"ok": True,
"job_id": job_id,
"total_lines": total_lines,
"returned_lines": len(lines),
"truncated": truncated,
"logs": "\n".join(lines),
"queried_at": now_utc().isoformat(),
}
except httpx.HTTPStatusError as exc:
return error_response(
f"GitHub API error: {exc.response.status_code}",
error_type="GitHubAPIError",
details=exc.response.text[:500],
)
except Exception as exc:
return error_response(str(exc))
if __name__ == "__main__":
uvicorn.run(build_app(), host=HOST, port=PORT, log_level="info")