-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
executable file
·304 lines (259 loc) · 9.6 KB
/
mcp_server.py
File metadata and controls
executable file
·304 lines (259 loc) · 9.6 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
#!/usr/bin/env python3
"""
GitLab AI Agent MCP Server
MCP (Model Context Protocol) Server for Claude Code integration
"""
import json
import sys
import os
import logging
from pathlib import Path
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent))
from core.gitlab import GitLabClient
from core.state import StateManager
# MCP Server 配置
MCP_SERVER_NAME = "gitissue-ai-agent"
MCP_SERVER_VERSION = "0.1.0"
def setup_logging():
"""配置日志"""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def get_config():
"""加载配置"""
import yaml
config_path = Path(__file__).parent / "config" / "config.yaml"
with open(config_path, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
def handle_mcp_request(method: str, params: dict) -> dict:
"""
处理 MCP 请求
Args:
method: MCP 方法名
params: 参数
Returns:
响应数据
"""
logger = logging.getLogger(__name__)
if method == "tools/list":
# 列出可用工具
return {
"tools": [
{
"name": "gitlab_fetch_issues",
"description": "从 GitLab 获取分配给用户的 issues",
"inputSchema": {
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "GitLab 用户名"
},
"labels": {
"type": "array",
"items": {"type": "string"},
"description": "过滤标签"
}
},
"required": ["username"]
}
},
{
"name": "gitlab_analyze_issue",
"description": "分析单个 GitLab issue 并决定如何处理",
"inputSchema": {
"type": "object",
"properties": {
"issue_id": {
"type": "string",
"description": "Issue 完整引用 (如 'group/project#123')"
}
},
"required": ["issue_id"]
}
},
{
"name": "gitlab_fix_issue",
"description": "自动修复 GitLab issue(克隆代码、修改、测试、创建 MR)",
"inputSchema": {
"type": "object",
"properties": {
"issue_id": {
"type": "string",
"description": "Issue 完整引用"
},
"plan": {
"type": "string",
"description": "修复计划"
}
},
"required": ["issue_id", "plan"]
}
},
{
"name": "gitlab_comment",
"description": "在 GitLab issue 上添加评论",
"inputSchema": {
"type": "object",
"properties": {
"issue_id": {
"type": "string",
"description": "Issue 完整引用"
},
"comment": {
"type": "string",
"description": "评论内容"
}
},
"required": ["issue_id", "comment"]
}
}
]
}
elif method == "tools/call":
# 执行工具
tool_name = params.get("name")
arguments = params.get("arguments", {})
config = get_config()
gitlab = GitLabClient(
url=config['gitlab']['url'],
token=config['gitlab']['access_token']
)
if tool_name == "gitlab_fetch_issues":
username = arguments.get("username")
labels = arguments.get("labels")
issues = gitlab.get_assigned_issues(username, labels)
return {
"content": [
{
"type": "text",
"text": json.dumps({
"count": len(issues),
"issues": [
{
"id": issue['references']['full'],
"title": issue['title'],
"author": issue['author']['username'],
"labels": issue.get('labels', []),
"url": issue['web_url']
}
for issue in issues
]
}, indent=2, ensure_ascii=False)
}
]
}
elif tool_name == "gitlab_analyze_issue":
issue_id = arguments.get("issue_id")
# 解析 issue_id (format: "group/project#123")
parts = issue_id.split('#')
project_path = parts[0]
issue_iid = int(parts[1])
# 获取项目信息
project_info = gitlab.get_project_info(project_path)
# 获取 issue 详情
issue = gitlab.get_issue_by_id(str(project_info['id']), issue_iid)
# 返回详细信息供 Claude 分析
return {
"content": [
{
"type": "text",
"text": json.dumps({
"issue": {
"id": issue['iid'],
"title": issue['title'],
"description": issue.get('description', ''),
"author": issue['author']['username'],
"labels": issue.get('labels', []),
"url": issue['web_url']
},
"project": {
"name": project_info['name'],
"path": project_info['path_with_namespace'],
"url": project_info['http_url_to_repo'],
"default_branch": project_info.get('default_branch', 'main')
},
"message": "请分析此 issue 并决定如何处理"
}, indent=2, ensure_ascii=False)
}
]
}
elif tool_name == "gitlab_comment":
issue_id = arguments.get("issue_id")
comment = arguments.get("comment")
parts = issue_id.split('#')
project_path = parts[0]
issue_iid = int(parts[1])
project_info = gitlab.get_project_info(project_path)
result = gitlab.add_comment(str(project_info['id']), issue_iid, comment)
return {
"content": [
{
"type": "text",
"text": f"✅ 评论已发送到 {issue_id}"
}
]
}
elif tool_name == "gitlab_fix_issue":
issue_id = arguments.get("issue_id")
plan = arguments.get("plan")
return {
"content": [
{
"type": "text",
"text": f"""🔧 准备修复 {issue_id}
修复计划:
{plan}
接下来我将:
1. 克隆代码仓库
2. 创建修复分支
3. 实施代码修改
4. 运行测试
5. 提交并推送
6. 创建 Merge Request
7. 在 issue 中评论
开始执行...
"""
}
]
}
else:
return {"error": f"Unknown method: {method}"}
def main():
"""MCP Server 主函数"""
setup_logging()
logger = logging.getLogger(__name__)
logger.info(f"Starting {MCP_SERVER_NAME} MCP Server v{MCP_SERVER_VERSION}")
# MCP 使用 stdio 通信
# 读取 JSON-RPC 请求,返回 JSON-RPC 响应
for line in sys.stdin:
try:
request = json.loads(line)
method = request.get("method")
params = request.get("params", {})
request_id = request.get("id")
logger.info(f"Received request: {method}")
result = handle_mcp_request(method, params)
response = {
"jsonrpc": "2.0",
"id": request_id,
"result": result
}
print(json.dumps(response))
sys.stdout.flush()
except Exception as e:
logger.error(f"Error handling request: {e}", exc_info=True)
error_response = {
"jsonrpc": "2.0",
"id": request.get("id") if 'request' in locals() else None,
"error": {
"code": -32603,
"message": str(e)
}
}
print(json.dumps(error_response))
sys.stdout.flush()
if __name__ == "__main__":
main()