forked from oharan2/prowject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
82 lines (64 loc) · 2.37 KB
/
mcp_server.py
File metadata and controls
82 lines (64 loc) · 2.37 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
import os
from typing import Any
from dateutil.parser import parse as parse_date
import httpx
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("mcp-server")
PROW_URL = "https://prow.ci.openshift.org"
async def make_request(
url: str, method: str = "GET", data: dict[str, Any] = None
) -> dict[str, Any] | None:
api_key = os.environ.get("API_KEY")
if api_key:
headers = {
"Authorization": f"Bearer {api_key}",
"Accept": "application/json",
}
else:
headers = {}
async with httpx.AsyncClient() as client:
if method.upper() == "GET":
response = await client.request(method, url, headers=headers, params=data)
else:
response = await client.request(method, url, headers=headers, json=data)
response.raise_for_status()
return response.json()
@mcp.tool()
async def get_latest_job_run(job_name: str):
"""Get the latest job run information from Prow for a specific job name.
Args:
job_name: The name of the Prow job to query
Returns:
Dictionary containing job information including ID, state, start time, completion time, and URL
"""
url = f"{PROW_URL}/prowjobs.js"
try:
response = await make_request(url)
if not response:
return {"error": "No response from Prow API"}
prowjobs = response.get("items", [])
# Filter by job name
matching_jobs = [
job for job in prowjobs
if job.get("spec", {}).get("job") == job_name and "startTime" in job.get("status", {})
]
if not matching_jobs:
return {"error": f"No matching job found for: {job_name}"}
# Sort by startTime descending
matching_jobs.sort(
key=lambda job: parse_date(job["status"]["startTime"]),
reverse=True
)
latest = matching_jobs[0]
status = latest.get("status", {})
return {
"job_id": latest["metadata"]["name"],
"state": status.get("state"),
"start": status.get("startTime"),
"completion": status.get("completionTime"),
"url": status.get("url"),
}
except Exception as e:
return {"error": f"Failed to fetch job info: {str(e)}"}
if __name__ == "__main__":
mcp.run(transport=os.environ.get("MCP_TRANSPORT", "stdio"))