-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure-devops-server-mcp-server.py
More file actions
176 lines (143 loc) · 5.01 KB
/
Copy pathazure-devops-server-mcp-server.py
File metadata and controls
176 lines (143 loc) · 5.01 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
import base64
import requests
from mcp.server.fastmcp import FastMCP
# ========= Configuration Area =========
ORG_URL = "your azure devops server url" # such as http://192.168.1.101/DefaultCollection
PAT = "your PAT"
# Azure DevOps using Basic Auth,no matter what username is
def _auth_header(pat: str) -> dict:
token = base64.b64encode(f":{pat}".encode()).decode()
return {
"Authorization": f"Basic {token}",
"Content-Type": "application/json"
}
headers = _auth_header(PAT)
# ========= MCP Server =========
mcp = FastMCP("azure-devops-server")
@mcp.tool()
def list_projects() -> list[dict]:
"""
List all projects of Azure DevOps Server
"""
url = f"{ORG_URL}/_apis/projects?api-version=6.0"
resp = requests.get(url, headers=headers, timeout=10)
resp.raise_for_status()
data = resp.json()
return [
{
"id": p["id"],
"name": p["name"],
"state": p["state"]
}
for p in data.get("value", [])
]
@mcp.tool()
def get_recent_work_items(project: str, limit: int = 30) -> list[dict]:
"""
Retrieve the most recent changes of Work Items for the specified project (without limiting to any specific type)
Return a structure suitable for summary analysis by Copilot/LLM
"""
# 1️⃣ WIQL query(unlimited WorkItemType)
wiql = {
"query": f"""
SELECT
[System.Id],
[System.Title],
[System.State],
[System.AssignedTo],
[System.ChangedDate],
[System.WorkItemType]
FROM WorkItems
WHERE
[System.TeamProject] = '{project}'
ORDER BY [System.ChangedDate] DESC
"""
}
wiql_url = f"{ORG_URL}/{project}/_apis/wit/wiql?api-version=6.0"
resp = requests.post(wiql_url, headers=headers, json=wiql, timeout=10)
resp.raise_for_status()
work_items = resp.json().get("workItems", [])[:limit]
if not work_items:
return []
# 2️⃣ Batch retrieval of Work Item details
ids = ",".join(str(wi["id"]) for wi in work_items)
items_url = f"{ORG_URL}/_apis/wit/workitems?ids={ids}&api-version=6.0"
resp = requests.get(items_url, headers=headers, timeout=10)
resp.raise_for_status()
result = []
for item in resp.json().get("value", []):
fields = item["fields"]
result.append({
"id": item["id"],
"title": fields.get("System.Title"),
"state": fields.get("System.State"),
"assigned_to": (
fields.get("System.AssignedTo", {}).get("displayName")
if isinstance(fields.get("System.AssignedTo"), dict)
else fields.get("System.AssignedTo")
),
"changed_date": fields.get("System.ChangedDate"),
"work_item_type": fields.get("System.WorkItemType")
})
return result
@mcp.tool()
def get_recent_pipelines(project: str, limit: int = 30) -> dict:
"""
Obtain the most recent Pipeline (Build) execution record of the specified project,
Return the success rate along with the original data, for Copilot to analyze the reasons for failure
"""
url = (
f"{ORG_URL}/{project}/_apis/build/builds"
f"?$top={limit}&api-version=6.0"
)
resp = requests.get(url, headers=headers, timeout=10)
resp.raise_for_status()
builds = resp.json().get("value", [])
result = []
success = 0
failed = 0
for b in builds:
build_result = b.get("result") # succeeded / failed / canceled
if build_result == "succeeded":
success += 1
elif build_result == "failed":
failed += 1
result.append({
"id": b.get("id"),
"pipeline_name": b.get("definition", {}).get("name"),
"status": b.get("status"), # completed / inProgress
"result": build_result,
"reason": b.get("reason"), # manual / individualCI / schedule
"source_branch": b.get("sourceBranch"),
"requested_by": b.get("requestedFor", {}).get("displayName"),
"start_time": b.get("startTime"),
"finish_time": b.get("finishTime"),
"web_url": b.get("_links", {})
.get("web", {})
.get("href")
})
total = len(builds)
return {
"project": project,
"total": total,
"succeeded": success,
"failed": failed,
"success_rate": round(success / total * 100, 2) if total else 0,
"pipelines": result
}
if __name__ == "__main__":
mcp.run(transport="stdio")
# if __name__ == "__main__":
# # debug get_recent_work_items
# items = get_recent_work_items("WBTL_ADC", limit=10)
# for i in items:
# print(i)
# projects = list_projects()
# for p in projects:
# print(p)
# if __name__ == "__main__":
# # debug get_recent_pipelines
# data = get_recent_pipelines("WBTL_ADC", limit=10)
# print(data["success_rate"])
# for p in data["pipelines"]:
# print(p)