devlake-mcp 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of devlake-mcp might be problematic. Click here for more details.

Files changed (33) hide show
  1. devlake_mcp-0.3.1/LICENSE +21 -0
  2. devlake_mcp-0.3.1/PKG-INFO +417 -0
  3. devlake_mcp-0.3.1/README.md +380 -0
  4. devlake_mcp-0.3.1/pyproject.toml +117 -0
  5. devlake_mcp-0.3.1/setup.cfg +4 -0
  6. devlake_mcp-0.3.1/src/devlake_mcp/__init__.py +7 -0
  7. devlake_mcp-0.3.1/src/devlake_mcp/__main__.py +10 -0
  8. devlake_mcp-0.3.1/src/devlake_mcp/cli.py +576 -0
  9. devlake_mcp-0.3.1/src/devlake_mcp/client.py +414 -0
  10. devlake_mcp-0.3.1/src/devlake_mcp/config.py +204 -0
  11. devlake_mcp-0.3.1/src/devlake_mcp/constants.py +157 -0
  12. devlake_mcp-0.3.1/src/devlake_mcp/generation_manager.py +231 -0
  13. devlake_mcp-0.3.1/src/devlake_mcp/git_utils.py +489 -0
  14. devlake_mcp-0.3.1/src/devlake_mcp/hooks/__init__.py +49 -0
  15. devlake_mcp-0.3.1/src/devlake_mcp/hooks/hook_utils.py +192 -0
  16. devlake_mcp-0.3.1/src/devlake_mcp/hooks/post_tool_use.py +325 -0
  17. devlake_mcp-0.3.1/src/devlake_mcp/hooks/pre_tool_use.py +110 -0
  18. devlake_mcp-0.3.1/src/devlake_mcp/hooks/record_session.py +183 -0
  19. devlake_mcp-0.3.1/src/devlake_mcp/hooks/session_start.py +74 -0
  20. devlake_mcp-0.3.1/src/devlake_mcp/hooks/stop.py +271 -0
  21. devlake_mcp-0.3.1/src/devlake_mcp/hooks/transcript_utils.py +547 -0
  22. devlake_mcp-0.3.1/src/devlake_mcp/hooks/user_prompt_submit.py +197 -0
  23. devlake_mcp-0.3.1/src/devlake_mcp/logging_config.py +102 -0
  24. devlake_mcp-0.3.1/src/devlake_mcp/retry_queue.py +556 -0
  25. devlake_mcp-0.3.1/src/devlake_mcp/server.py +618 -0
  26. devlake_mcp-0.3.1/src/devlake_mcp/session_manager.py +367 -0
  27. devlake_mcp-0.3.1/src/devlake_mcp/utils.py +195 -0
  28. devlake_mcp-0.3.1/src/devlake_mcp.egg-info/PKG-INFO +417 -0
  29. devlake_mcp-0.3.1/src/devlake_mcp.egg-info/SOURCES.txt +31 -0
  30. devlake_mcp-0.3.1/src/devlake_mcp.egg-info/dependency_links.txt +1 -0
  31. devlake_mcp-0.3.1/src/devlake_mcp.egg-info/entry_points.txt +3 -0
  32. devlake_mcp-0.3.1/src/devlake_mcp.egg-info/requires.txt +14 -0
  33. devlake_mcp-0.3.1/src/devlake_mcp.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 DevLake MCP Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,417 @@
1
+ Metadata-Version: 2.4
2
+ Name: devlake-mcp
3
+ Version: 0.3.1
4
+ Summary: Model Context Protocol server for DevLake
5
+ Author-email: wangzhong <wangzhong@g7.com.cn>
6
+ License: MIT
7
+ Project-URL: Homepage, https://git.chinawayltd.com/engineering-efficiency/devlake-mcp
8
+ Project-URL: Repository, https://git.chinawayltd.com/engineering-efficiency/devlake-mcp
9
+ Project-URL: Issues, https://git.chinawayltd.com/engineering-efficiency/devlake-mcp
10
+ Keywords: mcp,devlake,model-context-protocol,ai,llm
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: fastmcp>=0.5.0
24
+ Requires-Dist: requests>=2.31.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
27
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
28
+ Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
29
+ Requires-Dist: pytest-mock>=3.12.0; extra == "dev"
30
+ Requires-Dist: pytest-timeout>=2.2.0; extra == "dev"
31
+ Requires-Dist: pytest-xdist>=3.5.0; extra == "dev"
32
+ Requires-Dist: responses>=0.24.0; extra == "dev"
33
+ Requires-Dist: freezegun>=1.4.0; extra == "dev"
34
+ Requires-Dist: black>=23.0.0; extra == "dev"
35
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
36
+ Dynamic: license-file
37
+
38
+ # DevLake MCP Server
39
+
40
+ 一个基于 [FastMCP](https://gofastmcp.com) 框架的 DevLake MCP 服务器,允许 AI 助手(如 Claude)与 DevLake 进行交互。
41
+
42
+
43
+ ## 安装
44
+
45
+ ### 方式 1: 使用 pipx(推荐)
46
+
47
+ pipx 会在隔离环境中安装 Python 应用程序:
48
+
49
+ ```bash
50
+ # 安装 pipx(如果还没有安装)
51
+ # macOS
52
+ brew install pipx
53
+
54
+ # Linux
55
+ python3 -m pip install --user pipx
56
+ python3 -m pipx ensurepath
57
+
58
+ # 使用 pipx 安装 devlake-mcp
59
+ pipx install devlake-mcp
60
+ ```
61
+
62
+ ### 方式 2: 从源码安装
63
+
64
+ ```bash
65
+ # 克隆仓库
66
+ git clone https://github.com/yourusername/devlake-mcp.git
67
+ cd devlake-mcp
68
+
69
+ # 使用 pip 安装(开发模式)
70
+ pip install -e .
71
+
72
+ # 或使用 uv(推荐用于开发)
73
+ uv pip install -e .
74
+ ```
75
+
76
+
77
+ ## 使用方法
78
+
79
+ ### CLI 命令
80
+
81
+ DevLake MCP 提供了多个命令行工具:
82
+
83
+ ```bash
84
+ # 启动 MCP 服务器(用于 Claude Desktop 集成)
85
+ devlake-mcp
86
+
87
+ # 初始化 Claude Code hooks 配置
88
+ devlake-mcp init [--force]
89
+
90
+ # 初始化 Cursor hooks 配置
91
+ devlake-mcp init-cursor [--force]
92
+
93
+ # 手动触发失败重试
94
+ devlake-mcp retry
95
+
96
+ # 查看失败队列状态
97
+ devlake-mcp queue-status
98
+
99
+ # 清理过期的失败记录
100
+ devlake-mcp queue-clean
101
+
102
+ # 显示版本信息
103
+ devlake-mcp --version
104
+
105
+ # 显示帮助信息
106
+ devlake-mcp --help
107
+ ```
108
+
109
+ ### 与 Claude Desktop 集成
110
+
111
+ ```bash
112
+ claude mcp add devlake-mcp devlake-mcp
113
+ ```
114
+
115
+ ### Cursor Hooks 支持
116
+
117
+ DevLake MCP 现在支持 Cursor IDE!通过 Cursor hooks,可以自动采集 AI 编程数据,与 Claude Code 完全兼容。
118
+
119
+ **快速安装**:
120
+
121
+ ```bash
122
+ # 使用 CLI 命令安装(推荐)
123
+ devlake-mcp init-cursor
124
+
125
+ # 强制覆盖已有配置
126
+ devlake-mcp init-cursor --force
127
+ ```
128
+
129
+ **支持的 Hooks**:
130
+ - ✅ **beforeSubmitPrompt**:用户提交 prompt 前触发,记录用户输入
131
+ - ✅ **beforeReadFile**:读取文件前触发,记录文件访问
132
+ - ✅ **beforeShellExecution**:执行 Shell 命令前触发,记录命令信息
133
+ - ✅ **afterShellExecution**:Shell 命令执行后触发,检测文件变更
134
+ - ✅ **afterFileEdit**:文件编辑后触发,上传文件变更
135
+ - ✅ **afterAgentResponse**:AI 回复后触发,记录对话内容
136
+ - ✅ **stop**:会话结束时触发,上传会话统计
137
+
138
+ **功能特性**:
139
+ - ✅ **原生 Cursor 集成**:直接使用 Cursor 提供的 `conversation_id` 作为 session_id,无需额外状态管理
140
+ - ✅ **变更关联追踪**:通过 `generation_id` 关联同一次 AI 生成的多个文件变更
141
+ - ✅ **精确工作目录**:使用 Cursor 的 `workspace_roots` 字段准确定位项目根目录
142
+ - ✅ **文件变更记录**:自动记录 before/after diff,支持文件编辑和 Shell 命令
143
+ - ✅ **Shell 命令支持**:智能检测 vim/nano/echo>/cp/mv 等文件操作命令
144
+ - ✅ **与 Claude Code 完全兼容**:相同的数据格式和 API 接口
145
+
146
+ **技术亮点**:
147
+ - **简化的 Session 管理**:由 Cursor 管理对话生命周期,无需 30 分钟超时逻辑
148
+ - **Hook 事件追踪**:记录每次变更的 `hook_event_name`(如 afterFileEdit、afterShellExecution)
149
+ - **异步上传**:所有 API 调用异步执行,不阻塞 Cursor Agent 工作流
150
+ - **失败重试队列**:上传失败自动加入重试队列,确保数据不丢失
151
+
152
+ **详细文档**:查看 [CURSOR_HOOKS.md](CURSOR_HOOKS.md) 了解完整配置和使用指南。
153
+
154
+ ### Claude Code Hooks 支持
155
+
156
+ DevLake MCP 支持 Claude Code 的 Hooks 机制,通过配置 `.claude/settings.json` 自动采集 AI 编程数据。
157
+
158
+ **已实现的 Hooks**:
159
+ - ✅ **SessionStart**:会话启动时记录会话信息
160
+ - ✅ **SessionEnd**:会话结束时上传会话数据
161
+ - ✅ **UserPromptSubmit**:用户提交 prompt 时记录用户输入
162
+ - ✅ **PreToolUse**:工具使用前记录文件状态
163
+ - ✅ **PostToolUse**:工具使用后上传文件变更
164
+ - ✅ **Stop**:AI 循环停止时更新会话统计
165
+
166
+ **UserPromptSubmit Hook**(新增):
167
+ - **触发时机**:用户点击发送按钮后、发起后端请求之前
168
+ - **功能**:
169
+ - 检查并创建 session(如果是首次)
170
+ - 记录用户的 prompt 输入
171
+ - 上传到 DevLake API
172
+ - **自动管理**:
173
+ - 使用 Claude Code 提供的 `session_id` 作为会话标识
174
+ - 自动检测会话切换
175
+ - 异步执行,不阻塞用户操作
176
+
177
+ **配置位置**:`.claude/settings.json`
178
+
179
+ ```json
180
+ {
181
+ "hooks": {
182
+ "UserPromptSubmit": [
183
+ {
184
+ "hooks": [
185
+ {
186
+ "type": "command",
187
+ "command": "python3 -m devlake_mcp.hooks.user_prompt_submit",
188
+ "timeout": 5
189
+ }
190
+ ]
191
+ }
192
+ ]
193
+ }
194
+ }
195
+ ```
196
+
197
+ **技术特点**:
198
+ - **静默失败**:Hook 执行失败不影响 Claude Code 正常工作
199
+ - **失败重试**:上传失败自动保存到重试队列
200
+ - **会话管理**:自动管理会话状态,检测会话切换
201
+
202
+
203
+ ## 环境变量配置
204
+
205
+ 在使用前,需要配置 DevLake API 连接信息。
206
+
207
+ ### 方式 1: 使用 .env 文件(推荐)
208
+
209
+ ```bash
210
+ # 复制示例配置文件
211
+ cp .env.example .env
212
+
213
+ # 编辑配置文件,填写实际值
214
+ vim .env
215
+ ```
216
+
217
+ `.env` 文件内容:
218
+ ```bash
219
+ # DevLake API 地址(必需)
220
+ DEVLAKE_BASE_URL=http://devlake.test.chinawayltd.com
221
+
222
+ # API 超时时间(秒,默认 30)
223
+ DEVLAKE_TIMEOUT=30
224
+
225
+ # API 认证 Token(可选)
226
+ # DEVLAKE_API_TOKEN=your-token-here
227
+
228
+ # 失败重试配置(可选)
229
+ DEVLAKE_RETRY_ENABLED=true # 启用失败重试(默认 true)
230
+ DEVLAKE_RETRY_MAX_ATTEMPTS=5 # 最大重试次数(默认 5)
231
+ DEVLAKE_RETRY_CLEANUP_DAYS=7 # 失败记录保留天数(默认 7)
232
+ DEVLAKE_RETRY_CHECK_ON_HOOK=true # Hook 执行时检查重试(默认 true)
233
+ ```
234
+
235
+ ### 方式 2: 直接设置环境变量
236
+
237
+ ```bash
238
+ export DEVLAKE_BASE_URL="http://devlake.test.chinawayltd.com"
239
+ export DEVLAKE_TIMEOUT=30
240
+ ```
241
+
242
+ ### Git 配置(必需)
243
+
244
+ 工具会自动从 Git 配置读取用户信息,请确保已配置:
245
+
246
+ ```bash
247
+ # 配置 Git 用户信息
248
+ git config user.name "Your Name"
249
+ git config user.email "your.email@example.com"
250
+
251
+ # 配置仓库远程地址
252
+ git remote add origin <repository-url>
253
+ ```
254
+
255
+
256
+ ## 失败重试机制
257
+
258
+ DevLake MCP 内置了智能的失败重试队列系统,确保数据不会因为网络问题或临时故障而丢失。
259
+
260
+ ### 工作原理
261
+
262
+ 当 API 调用失败时(如网络超时、服务器错误),失败的记录会自动保存到本地队列,并按照指数退避策略自动重试:
263
+
264
+ **重试时间表**:
265
+ - 第 1 次重试:1 分钟后
266
+ - 第 2 次重试:5 分钟后
267
+ - 第 3 次重试:15 分钟后
268
+ - 第 4 次重试:60 分钟后
269
+ - 第 5 次重试:4 小时后
270
+
271
+ **队列类型**:
272
+ - `session` - 会话记录
273
+ - `prompt` - 用户输入记录
274
+ - `prompt_update` - Prompt 更新记录
275
+ - `file_change` - 文件变更记录
276
+
277
+ ### 管理失败队列
278
+
279
+ ```bash
280
+ # 查看失败队列状态和统计信息
281
+ devlake-mcp queue-status
282
+
283
+ # 手动触发重试(不等待自动重试时间)
284
+ devlake-mcp retry
285
+
286
+ # 清理过期的失败记录(默认保留 7 天)
287
+ devlake-mcp queue-clean
288
+ ```
289
+
290
+ ### 配置选项
291
+
292
+ 通过环境变量配置重试行为(参见"环境变量配置"章节):
293
+
294
+ - `DEVLAKE_RETRY_ENABLED` - 启用/禁用重试(默认 `true`)
295
+ - `DEVLAKE_RETRY_MAX_ATTEMPTS` - 最大重试次数(默认 `5`)
296
+ - `DEVLAKE_RETRY_CLEANUP_DAYS` - 失败记录保留天数(默认 `7`)
297
+ - `DEVLAKE_RETRY_CHECK_ON_HOOK` - Hook 执行时检查重试(默认 `true`)
298
+
299
+ ### 自动重试
300
+
301
+ 当 `DEVLAKE_RETRY_CHECK_ON_HOOK=true` 时(默认),每次 Hook 执行时会自动检查并重试失败的记录(每次最多重试 3 条,避免阻塞)。
302
+
303
+ 如需禁用自动重试,可设置:
304
+ ```bash
305
+ export DEVLAKE_RETRY_CHECK_ON_HOOK=false
306
+ ```
307
+
308
+ 然后使用 `devlake-mcp retry` 手动触发重试。
309
+
310
+
311
+ ## 可用工具
312
+
313
+ 当前服务器提供 3 个核心工具,用于记录 AI 编程会话和文件变更:
314
+
315
+ ### `record_session`
316
+ 记录 AI 会话的元数据和统计信息。
317
+
318
+ **参数**:
319
+ - `session_id` (string, 可选): 会话 ID,不提供则自动生成 UUID
320
+ - `metadata` (dict, 可选): 会话元数据,支持字段:
321
+ - `user_intent`: 用户意图描述
322
+ - `model`: 模型名称(如 "claude-sonnet-4-5")
323
+ - `ide`: IDE 类型(如 "cursor", "claude-code")
324
+ - `project_path`: 项目路径
325
+
326
+ **返回**:
327
+ ```json
328
+ {
329
+ "success": true,
330
+ "session_id": "uuid-xxx",
331
+ "timestamp": "2025-01-07T10:00:00Z",
332
+ "git_info": {
333
+ "git_repo_path": "yourorg/devlake",
334
+ "git_branch": "main",
335
+ "git_author": "Your Name"
336
+ }
337
+ }
338
+ ```
339
+
340
+ **示例**:
341
+ ```
342
+ 调用 record_session 工具,metadata 设置为 {"ide": "cursor", "model": "claude-sonnet-4-5"}
343
+ ```
344
+
345
+ ---
346
+
347
+ ### `before_edit_file`
348
+ 在文件变更前调用,记录文件的当前状态。
349
+
350
+ **参数**:
351
+ - `session_id` (string, 必需): 会话唯一标识
352
+ - `file_paths` (list[string], 必需): 即将变更的文件绝对路径列表
353
+
354
+ **返回**:
355
+ ```json
356
+ {
357
+ "success": true,
358
+ "session_id": "session-123",
359
+ "files_snapshot": {
360
+ "/path/to/file.py": {
361
+ "exists": true,
362
+ "line_count": 100,
363
+ "size": 2048
364
+ }
365
+ }
366
+ }
367
+ ```
368
+
369
+ **示例**:
370
+ ```
371
+ 调用 before_edit_file 工具,session_id 为 "session-123",file_paths 为 ["/path/to/file.py"]
372
+ ```
373
+
374
+ ---
375
+
376
+ ### `after_edit_file`
377
+ 在文件变更后调用,上传变更数据到 DevLake API。
378
+
379
+ **参数**:
380
+ - `session_id` (string, 必需): 会话唯一标识(与 before_edit_file 一致)
381
+ - `file_paths` (list[string], 必需): 已变更的文件绝对路径列表
382
+
383
+ **返回**:
384
+ ```json
385
+ {
386
+ "success": true,
387
+ "session_id": "session-123",
388
+ "uploaded_count": 1,
389
+ "changes": [
390
+ {
391
+ "file_path": "src/main.py",
392
+ "change_type": "edit",
393
+ "file_type": "py"
394
+ }
395
+ ]
396
+ }
397
+ ```
398
+
399
+ **工作流程**:
400
+ ```
401
+ 1. before_edit_file() - 记录文件变更前状态
402
+ 2. [执行文件变更操作]
403
+ 3. after_edit_file() - 对比差异并上传
404
+ ```
405
+
406
+ **示例**:
407
+ ```
408
+ 调用 after_edit_file 工具,session_id 为 "session-123",file_paths 为 ["/path/to/file.py"]
409
+ ```
410
+
411
+ ## 相关资源
412
+
413
+ - [Model Context Protocol 官方文档](https://modelcontextprotocol.io)
414
+ - [FastMCP 官方文档](https://gofastmcp.com)
415
+ - [FastMCP GitHub](https://github.com/jlowin/fastmcp)
416
+ - [Claude Desktop](https://claude.ai/download)
417
+ - [Cursor 编辑器](https://cursor.sh)