-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
203 lines (160 loc) · 6.25 KB
/
server.py
File metadata and controls
203 lines (160 loc) · 6.25 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
import json
from typing import Any, Dict
from fastmcp import FastMCP
from app.api import gitlab
server = FastMCP(name="gitlab-mcp", host="localhost", port=8000, stateless_http=True)
@server.tool("gitlab://user")
def get_user_resource():
"""
Tool: gitlab://user
Purpose:
Retrieves information about the currently authenticated GitLab user.
Output:
JSON object containing user details:
- username
- name
- email (if available)
- any other metadata provided by GitLab for the authenticated user
"""
user = gitlab.get_gitlab_user()
return {
"uri": "gitlab://user",
"name": "user",
"title": "GitLab Authenticated User",
"mimeType": "application/json",
"text": json.dumps(user),
}
@server.tool("gitlab://merge_request_changes")
def get_whole_mr_diff_by_id_and_path(mr_iid: str, encoded_path: str):
"""
Tool: gitlab://merge_request_changes
Purpose:
Retrieves the full diff and metadata for a GitLab Merge Request (MR) using
the MR IID and encoded project path.
Input:
- mr_iid (str): The Merge Request IID (e.g., "42")
- encoded_path (str): URL-encoded GitLab project path (e.g., "group%2Frepo")
Output:
JSON object containing:
- title
- author
- description
- state
- changes (per-file diffs)
"""
formatted = gitlab.get_whole_mr_diff(mr_iid=mr_iid, encoded_path=encoded_path)
formatted_dict = formatted.model_dump()
return {
"uri": f"gitlab://merge_request_changes/{encoded_path}/{mr_iid}",
"name": f"MR {mr_iid} changes",
"title": f"Merge Request {mr_iid} Diff",
"mimeType": "application/json",
"text": json.dumps(formatted_dict),
}
@server.tool("gitlab://merge_request_commits")
def get_per_commit_diff_by_id_and_path(mr_iid: str, encoded_path: str):
"""
Tool: gitlab://merge_request_commits
Purpose:
Retrieves per-commit diffs for a GitLab Merge Request
using encoded project path and MR IID.
Input:
- mr_iid (str): Merge Request IID
- encoded_path (str): URL-encoded project path (e.g. group%2Frepo)
Output:
JSON object containing per-commit diffs
"""
formatted = gitlab.get_per_commit_diffs(encoded_path=encoded_path, mr_iid=mr_iid)
formatted_dict = formatted.model_dump()
return {
"uri": f"gitlab://merge_request_commits/{encoded_path}/{mr_iid}",
"name": f"MR {mr_iid} commits",
"title": f"Merge Request {mr_iid} Commit Diffs",
"mimeType": "application/json",
"text": json.dumps(formatted_dict),
}
@server.tool("gitlab://merge_request_discussions")
def get_merge_request_discussions_by_id_and_path(mr_iid: str, encoded_path: str) -> Dict[str, Any]:
"""
Tool: gitlab://merge_request_discussions
Purpose:
Retrieves all discussions (threaded comments) for a GitLab Merge Request
using MR IID and encoded project path.
Input:
- mr_iid (str): Merge Request IID (e.g., "12")
- encoded_path (str): URL-encoded GitLab project path (e.g., "group%2Frepo")
Output:
MCP Resource object:
{
"uri": "gitlab://merge_request_discussions/<encoded_path>/<mr_iid>",
"name": "MR <iid> discussions",
"title": "Merge Request <iid> Discussions",
"mimeType": "application/json",
"text": "<JSON-encoded discussions data>"
}
"""
try:
discussions = gitlab.get_merge_request_discussions(mr_iid=mr_iid, encoded_path=encoded_path)
formatted_payload = {
"encoded_path": encoded_path,
"mr_iid": mr_iid,
"discussion_count": len(discussions) if isinstance(discussions, list) else None,
"discussions": discussions,
}
return {
"uri": f"gitlab://merge_request_discussions/{encoded_path}/{mr_iid}",
"name": f"MR {mr_iid} discussions",
"title": f"Merge Request {mr_iid} Discussions",
"mimeType": "application/json",
"text": json.dumps(formatted_payload, indent=2),
}
except Exception as e:
err_obj = {"error": True, "message": str(e), "mr_iid": mr_iid, "encoded_path": encoded_path}
return {
"uri": f"gitlab://merge_request_discussions/{encoded_path}/{mr_iid}",
"name": "MR discussions (error)",
"title": "Merge Request Discussions — Error",
"mimeType": "application/json",
"text": json.dumps(err_obj, indent=2),
}
@server.tool("gitlab://commit_diff")
def get_commit_diff_by_path_and_sha(encoded_path: str, sha: str) -> Dict[str, Any]:
"""
Tool: gitlab://commit_diff
Purpose:
Retrieves the diff for a specific commit in a GitLab project
using an encoded project path and commit SHA.
Input:
- encoded_path (str): URL-encoded GitLab project path (e.g., "group%2Frepo")
- sha (str): Commit SHA
Output:
Resource object MCP expects:
{
"uri": "gitlab://commit_diff/<encoded_path>/<sha>",
"name": "Commit <sha> diff",
"title": "Commit <sha> Diff",
"mimeType": "application/json",
"text": "<JSON-encoded commit diff data>"
}
"""
try:
commit_diff_response = gitlab.get_commit_diff(encoded_path=encoded_path, sha=sha)
commit_diff_dict = commit_diff_response.model_dump()
return {
"uri": f"gitlab://commit_diff/{encoded_path}/{sha}",
"name": f"Commit {sha} diff",
"title": f"Commit {sha} Diff",
"mimeType": "application/json",
"text": json.dumps(commit_diff_dict, indent=2),
}
except Exception as e:
err_obj = {"error": True, "message": str(e), "encoded_path": encoded_path, "sha": sha}
return {
"uri": f"gitlab://commit_diff/{encoded_path}/{sha}",
"name": "Commit diff (error)",
"title": "Commit Diff — Error",
"mimeType": "application/json",
"text": json.dumps(err_obj, indent=2),
}
if __name__ == "__main__":
server.run(transport="http", host="127.0.0.1", port=8000, stateless_http=True)