pdd-skills 3.0.0

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.
Files changed (261) hide show
  1. package/README.md +1478 -0
  2. package/bin/pdd.js +354 -0
  3. package/config/bpmn-rules.yaml +166 -0
  4. package/config/checkstyle.xml +105 -0
  5. package/config/eslint.config.js +48 -0
  6. package/config/pmd.xml +91 -0
  7. package/config/prd-rules.yaml +113 -0
  8. package/config/ruff.toml +45 -0
  9. package/config/sqlfluff.cfg +82 -0
  10. package/hooks/hook-executor.js +332 -0
  11. package/index.js +43 -0
  12. package/lib/api-routes.js +750 -0
  13. package/lib/api-server.js +408 -0
  14. package/lib/cache/cache-config.js +209 -0
  15. package/lib/cache/system-cache.js +852 -0
  16. package/lib/config-manager.js +373 -0
  17. package/lib/generate.js +528 -0
  18. package/lib/grpc/grpc-routes.js +1134 -0
  19. package/lib/grpc/grpc-server.js +912 -0
  20. package/lib/grpc/proto-definitions.js +1033 -0
  21. package/lib/init.js +172 -0
  22. package/lib/iteration/auto-fixer.js +1025 -0
  23. package/lib/iteration/auto-reviewer.js +923 -0
  24. package/lib/iteration/controller.js +577 -0
  25. package/lib/list.js +130 -0
  26. package/lib/mcp-server.js +548 -0
  27. package/lib/openclaw/api-integration.js +535 -0
  28. package/lib/openclaw/cli-integration.js +567 -0
  29. package/lib/openclaw/data-sync.js +845 -0
  30. package/lib/openclaw/openclaw-adapter.js +783 -0
  31. package/lib/plugin/example-plugins/code-stats/index.js +332 -0
  32. package/lib/plugin/example-plugins/code-stats/plugin.json +1 -0
  33. package/lib/plugin/example-plugins/custom-linter/index.js +472 -0
  34. package/lib/plugin/example-plugins/custom-linter/plugin.json +1 -0
  35. package/lib/plugin/example-plugins/hello-world/index.js +86 -0
  36. package/lib/plugin/example-plugins/hello-world/plugin.json +1 -0
  37. package/lib/plugin/plugin-manager.js +655 -0
  38. package/lib/plugin/plugin-sdk.js +565 -0
  39. package/lib/plugin/sandbox.js +627 -0
  40. package/lib/quality/rules/maintainability.js +418 -0
  41. package/lib/quality/rules/performance.js +498 -0
  42. package/lib/quality/rules/readability.js +441 -0
  43. package/lib/quality/rules/robustness.js +504 -0
  44. package/lib/quality/rules/security.js +444 -0
  45. package/lib/quality/scorer.js +576 -0
  46. package/lib/report.js +669 -0
  47. package/lib/sdk-base.js +301 -0
  48. package/lib/sdk-js.js +446 -0
  49. package/lib/sdk-python/README.md +546 -0
  50. package/lib/sdk-python/examples/basic_usage.py +450 -0
  51. package/lib/sdk-python/pdd_sdk/__init__.py +180 -0
  52. package/lib/sdk-python/pdd_sdk/client.py +1170 -0
  53. package/lib/sdk-python/pdd_sdk/events.py +423 -0
  54. package/lib/sdk-python/pdd_sdk/exceptions.py +158 -0
  55. package/lib/sdk-python/pdd_sdk/models.py +518 -0
  56. package/lib/sdk-python/pdd_sdk/utils.py +759 -0
  57. package/lib/token/budget-alert.js +367 -0
  58. package/lib/token/budget-manager.js +485 -0
  59. package/lib/update.js +54 -0
  60. package/lib/utils/logger.js +88 -0
  61. package/lib/verify.js +741 -0
  62. package/lib/version.js +52 -0
  63. package/lib/vm/README.md +102 -0
  64. package/lib/vm/dashboard/api-routes.js +669 -0
  65. package/lib/vm/dashboard/server.js +391 -0
  66. package/lib/vm/dashboard/sse.js +358 -0
  67. package/lib/vm/dashboard/static/css/dashboard.css +1378 -0
  68. package/lib/vm/dashboard/static/index.html +118 -0
  69. package/lib/vm/dashboard/static/js/app.js +949 -0
  70. package/lib/vm/dashboard/static/js/charts.js +913 -0
  71. package/lib/vm/dashboard/static/js/kanban-view.js +1053 -0
  72. package/lib/vm/dashboard/static/js/pipeline-view.js +463 -0
  73. package/lib/vm/dashboard/static/js/quality-view.js +598 -0
  74. package/lib/vm/dashboard/static/js/system-view.js +1021 -0
  75. package/lib/vm/data-provider.js +1191 -0
  76. package/lib/vm/event-bus.js +402 -0
  77. package/lib/vm/hooks/extract-hook.js +307 -0
  78. package/lib/vm/hooks/generate-hook.js +374 -0
  79. package/lib/vm/hooks/hook-interface.js +458 -0
  80. package/lib/vm/hooks/report-hook.js +331 -0
  81. package/lib/vm/hooks/verify-hook.js +454 -0
  82. package/lib/vm/models.js +1003 -0
  83. package/lib/vm/reconciler.js +855 -0
  84. package/lib/vm/scanner.js +988 -0
  85. package/lib/vm/state-schema.js +955 -0
  86. package/lib/vm/state-store.js +733 -0
  87. package/lib/vm/tui/components/card.js +339 -0
  88. package/lib/vm/tui/components/progress-bar.js +368 -0
  89. package/lib/vm/tui/components/sparkline.js +327 -0
  90. package/lib/vm/tui/components/status-light.js +294 -0
  91. package/lib/vm/tui/components/table.js +370 -0
  92. package/lib/vm/tui/input.js +335 -0
  93. package/lib/vm/tui/renderer.js +548 -0
  94. package/lib/vm/tui/screens/kanban-screen.js +397 -0
  95. package/lib/vm/tui/screens/overview-screen.js +357 -0
  96. package/lib/vm/tui/screens/quality-screen.js +336 -0
  97. package/lib/vm/tui/screens/system-screen.js +379 -0
  98. package/lib/vm/tui/tui.js +805 -0
  99. package/package.json +1 -0
  100. package/scripts/cso-analyzer.js +198 -0
  101. package/scripts/eval-runner.js +359 -0
  102. package/scripts/i18n-checker.js +109 -0
  103. package/scripts/linter/activiti-linter.js +272 -0
  104. package/scripts/linter/prd-linter.js +162 -0
  105. package/scripts/linter/report-generator.js +207 -0
  106. package/scripts/linter/run-linters.js +285 -0
  107. package/scripts/linter/sql-linter.js +166 -0
  108. package/scripts/token-analyzer.js +162 -0
  109. package/scripts/vm-test.js +180 -0
  110. package/skills/core/official-doc-writer/LICENSE +21 -0
  111. package/skills/core/official-doc-writer/README.md +232 -0
  112. package/skills/core/official-doc-writer/SKILL.md +475 -0
  113. package/skills/core/official-doc-writer/_meta.json +1 -0
  114. package/skills/core/official-doc-writer/document_generator.py +580 -0
  115. package/skills/core/official-doc-writer/evals/default-evals.json +1 -0
  116. package/skills/core/official-doc-writer/examples.md +150 -0
  117. package/skills/core/official-doc-writer/fonts/FONTS_LIST.md +45 -0
  118. package/skills/core/official-doc-writer/fonts/README.md +141 -0
  119. package/skills/core/official-doc-writer/fonts/SIMFANG.TTF +0 -0
  120. package/skills/core/official-doc-writer/fonts/SIMHEI.TTF +0 -0
  121. package/skills/core/official-doc-writer/fonts/SIMKAI.TTF +0 -0
  122. package/skills/core/official-doc-writer/fonts/SIMSUN.TTC +0 -0
  123. package/skills/core/official-doc-writer/fonts//346/226/271/346/255/243/345/260/217/346/240/207/345/256/213GBK.TTF +0 -0
  124. package/skills/core/official-doc-writer/references/GBT_9704-2012_/345/205/232/346/224/277/346/234/272/345/205/263/345/205/254/346/226/207/346/240/274/345/274/217.md +422 -0
  125. package/skills/core/official-doc-writer/scripts/__pycache__/generate_official_doc.cpython-313.pyc +0 -0
  126. package/skills/core/official-doc-writer/scripts/dialog_manager.py +564 -0
  127. package/skills/core/official-doc-writer/scripts/generate_official_doc.py +252 -0
  128. package/skills/core/official-doc-writer/scripts/install_fonts.py +390 -0
  129. package/skills/core/official-doc-writer/scripts/smart_prompts.py +363 -0
  130. package/skills/core/pdd-ba/SKILL.md +305 -0
  131. package/skills/core/pdd-ba/_meta.json +1 -0
  132. package/skills/core/pdd-ba/evals/default-evals.json +1 -0
  133. package/skills/core/pdd-code-reviewer/SKILL.md +378 -0
  134. package/skills/core/pdd-code-reviewer/_meta.json +1 -0
  135. package/skills/core/pdd-code-reviewer/evals/default-evals.json +1 -0
  136. package/skills/core/pdd-doc-change/SKILL.md +350 -0
  137. package/skills/core/pdd-doc-change/_meta.json +1 -0
  138. package/skills/core/pdd-doc-change/evals/default-evals.json +1 -0
  139. package/skills/core/pdd-doc-gardener/SKILL.md +248 -0
  140. package/skills/core/pdd-doc-gardener/_meta.json +1 -0
  141. package/skills/core/pdd-doc-gardener/evals/default-evals.json +1 -0
  142. package/skills/core/pdd-entropy-reduction/SKILL.md +360 -0
  143. package/skills/core/pdd-entropy-reduction/_meta.json +1 -0
  144. package/skills/core/pdd-entropy-reduction/evals/default-evals.json +1 -0
  145. package/skills/core/pdd-entropy-reduction/references/entropy-report-template.md +287 -0
  146. package/skills/core/pdd-entropy-reduction/references/golden-principles.md +573 -0
  147. package/skills/core/pdd-entropy-reduction/scripts/entropy_scan.py +712 -0
  148. package/skills/core/pdd-extract-features/SKILL.md +320 -0
  149. package/skills/core/pdd-extract-features/_meta.json +1 -0
  150. package/skills/core/pdd-extract-features/evals/default-evals.json +1 -0
  151. package/skills/core/pdd-generate-spec/SKILL.md +418 -0
  152. package/skills/core/pdd-generate-spec/_meta.json +1 -0
  153. package/skills/core/pdd-generate-spec/evals/default-evals.json +1 -0
  154. package/skills/core/pdd-implement-feature/SKILL.md +332 -0
  155. package/skills/core/pdd-implement-feature/_meta.json +1 -0
  156. package/skills/core/pdd-implement-feature/evals/default-evals.json +1 -0
  157. package/skills/core/pdd-main/SKILL.md +540 -0
  158. package/skills/core/pdd-main/_meta.json +1 -0
  159. package/skills/core/pdd-main/evals/default-evals.json +1 -0
  160. package/skills/core/pdd-main/evals/evals.json +215 -0
  161. package/skills/core/pdd-verify-feature/SKILL.md +474 -0
  162. package/skills/core/pdd-verify-feature/_meta.json +1 -0
  163. package/skills/core/pdd-verify-feature/evals/default-evals.json +1 -0
  164. package/skills/core/pdd-vm/evals/default-evals.json +1 -0
  165. package/skills/core/traffic-accident-assessor/LICENSE +29 -0
  166. package/skills/core/traffic-accident-assessor/SKILL.md +439 -0
  167. package/skills/core/traffic-accident-assessor/evals/evals.json +1 -0
  168. package/skills/core/traffic-accident-assessor/references/accident-types.md +369 -0
  169. package/skills/core/traffic-accident-assessor/references/liability-rules.md +287 -0
  170. package/skills/core/traffic-accident-assessor/references/traffic-laws.md +226 -0
  171. package/skills/core/traffic-accident-assessor/references//351/253/230/345/260/224/345/244/253/350/257/264/346/230/216/344/271/246.pdf +32576 -106
  172. package/skills/core/traffic-accident-assessor/scripts/generate_official_statement.py +588 -0
  173. package/skills/core/traffic-accident-assessor/scripts/generate_report.py +495 -0
  174. package/skills/core/traffic-accident-assessor/scripts/generate_statement.py +528 -0
  175. package/skills/core/traffic-accident-assessor.zip +0 -0
  176. package/skills/entropy/expert-arch-enforcer/SKILL.md +292 -0
  177. package/skills/entropy/expert-arch-enforcer/_meta.json +1 -0
  178. package/skills/entropy/expert-arch-enforcer/evals/default-evals.json +1 -0
  179. package/skills/entropy/expert-auto-refactor/SKILL.md +327 -0
  180. package/skills/entropy/expert-auto-refactor/_meta.json +1 -0
  181. package/skills/entropy/expert-auto-refactor/evals/default-evals.json +1 -0
  182. package/skills/entropy/expert-code-quality/SKILL.md +468 -0
  183. package/skills/entropy/expert-code-quality/_meta.json +1 -0
  184. package/skills/entropy/expert-code-quality/evals/default-evals.json +1 -0
  185. package/skills/entropy/expert-code-quality/evals/evals.json +109 -0
  186. package/skills/entropy/expert-code-quality/references/code-smells.md +605 -0
  187. package/skills/entropy/expert-code-quality/references/design-patterns.md +1111 -0
  188. package/skills/entropy/expert-code-quality/references/refactoring-catalog.md +1281 -0
  189. package/skills/entropy/expert-code-quality/references/solid-principles.md +524 -0
  190. package/skills/entropy/expert-entropy-auditor/SKILL.md +276 -0
  191. package/skills/entropy/expert-entropy-auditor/_meta.json +1 -0
  192. package/skills/entropy/expert-entropy-auditor/evals/default-evals.json +1 -0
  193. package/skills/expert/expert-activiti/SKILL.md +497 -0
  194. package/skills/expert/expert-activiti/_meta.json +1 -0
  195. package/skills/expert/expert-mysql/SKILL.md +832 -0
  196. package/skills/expert/expert-mysql/_meta.json +1 -0
  197. package/skills/expert/expert-performance/SKILL.md +379 -0
  198. package/skills/expert/expert-performance/_meta.json +1 -0
  199. package/skills/expert/expert-performance/evals/default-evals.json +1 -0
  200. package/skills/expert/expert-ruoyi/SKILL.md +472 -0
  201. package/skills/expert/expert-ruoyi/_meta.json +1 -0
  202. package/skills/expert/expert-security/SKILL.md +1341 -0
  203. package/skills/expert/expert-security/_meta.json +1 -0
  204. package/skills/expert/expert-security/evals/default-evals.json +1 -0
  205. package/skills/expert/software-architect/SKILL.md +350 -0
  206. package/skills/expert/software-architect/_meta.json +1 -0
  207. package/skills/expert/software-engineer/SKILL.md +437 -0
  208. package/skills/expert/software-engineer/_meta.json +1 -0
  209. package/skills/expert/software-engineer/architecture.md +130 -0
  210. package/skills/expert/software-engineer/patterns.md +151 -0
  211. package/skills/expert/software-engineer/testing.md +135 -0
  212. package/skills/expert/system-architect/SKILL.md +628 -0
  213. package/skills/expert/system-architect/_meta.json +1 -0
  214. package/skills/expert/system-architect/assets/templates/ARCHITECTURE.md +25 -0
  215. package/skills/expert/system-architect/assets/templates/README.md +44 -0
  216. package/skills/expert/system-architect/references/js-ts-standards.md +18 -0
  217. package/skills/expert/system-architect/references/python-standards.md +19 -0
  218. package/skills/expert/system-architect/references/scaffolding.md +61 -0
  219. package/skills/expert/system-architect/references/security-checklist.md +21 -0
  220. package/skills/openspec/openspec-apply-change/SKILL.md +156 -0
  221. package/skills/openspec/openspec-apply-change/_meta.json +1 -0
  222. package/skills/openspec/openspec-archive-change/SKILL.md +114 -0
  223. package/skills/openspec/openspec-archive-change/_meta.json +1 -0
  224. package/skills/openspec/openspec-bulk-archive-change/SKILL.md +246 -0
  225. package/skills/openspec/openspec-bulk-archive-change/_meta.json +1 -0
  226. package/skills/openspec/openspec-continue-change/SKILL.md +118 -0
  227. package/skills/openspec/openspec-continue-change/_meta.json +1 -0
  228. package/skills/openspec/openspec-explore/SKILL.md +288 -0
  229. package/skills/openspec/openspec-explore/_meta.json +1 -0
  230. package/skills/openspec/openspec-ff-change/SKILL.md +101 -0
  231. package/skills/openspec/openspec-ff-change/_meta.json +1 -0
  232. package/skills/openspec/openspec-new-change/SKILL.md +74 -0
  233. package/skills/openspec/openspec-new-change/_meta.json +1 -0
  234. package/skills/openspec/openspec-onboard/SKILL.md +554 -0
  235. package/skills/openspec/openspec-onboard/_meta.json +1 -0
  236. package/skills/openspec/openspec-sync-specs/SKILL.md +138 -0
  237. package/skills/openspec/openspec-sync-specs/_meta.json +1 -0
  238. package/skills/openspec/openspec-verify-change/SKILL.md +168 -0
  239. package/skills/openspec/openspec-verify-change/_meta.json +1 -0
  240. package/skills/pr/pdd-multi-review/SKILL.md +534 -0
  241. package/skills/pr/pdd-multi-review/_meta.json +1 -0
  242. package/skills/pr/pdd-pr-batch/SKILL.md +303 -0
  243. package/skills/pr/pdd-pr-batch/_meta.json +1 -0
  244. package/skills/pr/pdd-pr-create/SKILL.md +344 -0
  245. package/skills/pr/pdd-pr-create/_meta.json +1 -0
  246. package/skills/pr/pdd-pr-merge/SKILL.md +286 -0
  247. package/skills/pr/pdd-pr-merge/_meta.json +1 -0
  248. package/skills/pr/pdd-pr-review/SKILL.md +217 -0
  249. package/skills/pr/pdd-pr-review/_meta.json +1 -0
  250. package/skills/pr/pdd-task-manager/SKILL.md +636 -0
  251. package/skills/pr/pdd-task-manager/_meta.json +1 -0
  252. package/skills/pr/pdd-template-engine/SKILL.md +306 -0
  253. package/skills/pr/pdd-template-engine/_meta.json +1 -0
  254. package/templates/behavior-shaping/iron-law-template.md +87 -0
  255. package/templates/behavior-shaping/rationalization-template.md +62 -0
  256. package/templates/behavior-shaping/red-flags-template.md +70 -0
  257. package/templates/bilingual-template.md +139 -0
  258. package/templates/config/default.yaml +47 -0
  259. package/templates/project/default/README.md +31 -0
  260. package/templates/project/frontend/README.md +46 -0
  261. package/templates/project/java/README.md +48 -0
@@ -0,0 +1,548 @@
1
+ // lib/mcp-server.js - PDD MCP Server
2
+ // 实现MCP协议的资源定义和工具定义,让AI工具可以调用PDD能力
3
+
4
+ import http from 'node:http';
5
+ import { URL } from 'node:url';
6
+ import { log } from './utils/logger.js';
7
+
8
+ /**
9
+ * PDD资源定义
10
+ * 定义了MCP协议中暴露的PDD核心资源
11
+ */
12
+ const PDD_RESOURCES = {
13
+ 'pdd://specs': {
14
+ name: 'Development Specs',
15
+ description: 'All development specifications generated from feature matrices'
16
+ },
17
+ 'pdd://features': {
18
+ name: 'Feature Matrix',
19
+ description: 'Extracted feature matrices from PRD documents'
20
+ },
21
+ 'pdd://reviews': {
22
+ name: 'Code Reviews',
23
+ description: 'Code review reports and quality assessments'
24
+ },
25
+ 'pdd://status': {
26
+ name: 'Project Status',
27
+ description: 'Current project status and progress tracking'
28
+ }
29
+ };
30
+
31
+ /**
32
+ * PDD工具定义
33
+ * 定义了MCP协议中暴露的PDD核心工具
34
+ */
35
+ const PDD_TOOLS = [
36
+ {
37
+ name: 'pdd_generate_spec',
38
+ description: 'Generate development spec from feature matrix',
39
+ inputSchema: {
40
+ type: 'object',
41
+ properties: {
42
+ featureId: {
43
+ type: 'string',
44
+ description: 'Feature matrix identifier'
45
+ },
46
+ outputPath: {
47
+ type: 'string',
48
+ description: 'Output path for the generated spec'
49
+ }
50
+ },
51
+ required: ['featureId']
52
+ }
53
+ },
54
+ {
55
+ name: 'pdd_implement_feature',
56
+ description: 'Implement feature from dev spec',
57
+ inputSchema: {
58
+ type: 'object',
59
+ properties: {
60
+ specPath: {
61
+ type: 'string',
62
+ description: 'Path to the development spec'
63
+ },
64
+ targetDir: {
65
+ type: 'string',
66
+ description: 'Target directory for implementation'
67
+ }
68
+ },
69
+ required: ['specPath']
70
+ }
71
+ },
72
+ {
73
+ name: 'pdd_verify_feature',
74
+ description: 'Verify feature against acceptance criteria',
75
+ inputSchema: {
76
+ type: 'object',
77
+ properties: {
78
+ specPath: {
79
+ type: 'string',
80
+ description: 'Path to the development spec'
81
+ },
82
+ dimensions: {
83
+ type: 'array',
84
+ items: { type: 'string' },
85
+ description: 'Verification dimensions (completeness, correctness, consistency)'
86
+ }
87
+ },
88
+ required: ['specPath']
89
+ }
90
+ },
91
+ {
92
+ name: 'pdd_code_review',
93
+ description: 'Review code against development specs',
94
+ inputSchema: {
95
+ type: 'object',
96
+ properties: {
97
+ targetPath: {
98
+ type: 'string',
99
+ description: 'Path to review (file or directory)'
100
+ },
101
+ specPath: {
102
+ type: 'string',
103
+ description: 'Path to the development spec for comparison'
104
+ }
105
+ },
106
+ required: ['targetPath']
107
+ }
108
+ },
109
+ {
110
+ name: 'pdd_analyze_business',
111
+ description: 'Analyze business requirements using 5W1H methodology',
112
+ inputSchema: {
113
+ type: 'object',
114
+ properties: {
115
+ prdPath: {
116
+ type: 'string',
117
+ description: 'Path to PRD document'
118
+ },
119
+ outputFormat: {
120
+ type: 'string',
121
+ enum: ['json', 'markdown'],
122
+ description: 'Output format for analysis results'
123
+ }
124
+ },
125
+ required: ['prdPath']
126
+ }
127
+ },
128
+ {
129
+ name: 'pdd_extract_features',
130
+ description: 'Extract features from PRD documents',
131
+ inputSchema: {
132
+ type: 'object',
133
+ properties: {
134
+ prdPath: {
135
+ type: 'string',
136
+ description: 'Path to PRD document'
137
+ },
138
+ outputDir: {
139
+ type: 'string',
140
+ description: 'Output directory for extracted features'
141
+ }
142
+ },
143
+ required: ['prdPath']
144
+ }
145
+ }
146
+ ];
147
+
148
+ /**
149
+ * PDD MCP Server 类
150
+ * 实现Model Context Protocol服务端,提供资源和工具访问能力
151
+ */
152
+ export class PDDMCPServer {
153
+ constructor(options = {}) {
154
+ this.port = options.port || 9090;
155
+ this.host = options.host || 'localhost';
156
+ this.server = null;
157
+ this.toolHandlers = new Map();
158
+ this.resourceHandlers = new Map();
159
+ this.initialized = false;
160
+
161
+ // 注册默认的工具处理器
162
+ this._registerDefaultHandlers();
163
+ }
164
+
165
+ /**
166
+ * 注册默认的工具和资源处理器
167
+ * @private
168
+ */
169
+ _registerDefaultHandlers() {
170
+ // 工具处理器路由表
171
+ this.toolHandlers.set('pdd_generate_spec', async (args) => {
172
+ return {
173
+ content: [{
174
+ type: 'text',
175
+ text: `Generate spec for feature: ${args.featureId}`
176
+ }],
177
+ isError: false
178
+ };
179
+ });
180
+
181
+ this.toolHandlers.set('pdd_implement_feature', async (args) => {
182
+ return {
183
+ content: [{
184
+ type: 'text',
185
+ text: `Implement feature from spec: ${args.specPath}`
186
+ }],
187
+ isError: false
188
+ };
189
+ });
190
+
191
+ this.toolHandlers.set('pdd_verify_feature', async (args) => {
192
+ return {
193
+ content: [{
194
+ type: 'text',
195
+ text: `Verify feature at: ${args.specPath}`
196
+ }],
197
+ isError: false
198
+ };
199
+ });
200
+
201
+ this.toolHandlers.set('pdd_code_review', async (args) => {
202
+ return {
203
+ content: [{
204
+ type: 'text',
205
+ text: `Review code at: ${args.targetPath}`
206
+ }],
207
+ isError: false
208
+ };
209
+ });
210
+
211
+ this.toolHandlers.set('pdd_analyze_business', async (args) => {
212
+ return {
213
+ content: [{
214
+ type: 'text',
215
+ text: `Analyze business requirements from: ${args.prdPath}`
216
+ }],
217
+ isError: false
218
+ };
219
+ });
220
+
221
+ this.toolHandlers.set('pdd_extract_features', async (args) => {
222
+ return {
223
+ content: [{
224
+ type: 'text',
225
+ text: `Extract features from: ${args.prdPath}`
226
+ }],
227
+ isError: false
228
+ };
229
+ });
230
+ }
231
+
232
+ /**
233
+ * 初始化MCP服务器
234
+ */
235
+ async initialize() {
236
+ if (this.initialized) {
237
+ return;
238
+ }
239
+
240
+ log('info', 'Initializing PDD MCP Server...');
241
+ this.initialized = true;
242
+ log('success', 'PDD MCP Server initialized successfully');
243
+ }
244
+
245
+ /**
246
+ * 列出所有可用的资源
247
+ * @returns {Object} 资源列表
248
+ */
249
+ async listResources() {
250
+ if (!this.initialized) {
251
+ throw new Error('Server not initialized. Call initialize() first.');
252
+ }
253
+
254
+ return Object.entries(PDD_RESOURCES).map(([uri, info]) => ({
255
+ uri,
256
+ name: info.name,
257
+ description: info.description,
258
+ mimeType: 'application/json'
259
+ }));
260
+ }
261
+
262
+ /**
263
+ * 列出所有可用的工具
264
+ * @returns {Array} 工具列表
265
+ */
266
+ async listTools() {
267
+ if (!this.initialized) {
268
+ throw new Error('Server not initialized. Call initialize() first.');
269
+ }
270
+
271
+ return PDD_TOOLS.map(tool => ({
272
+ name: tool.name,
273
+ description: tool.description,
274
+ inputSchema: tool.inputSchema
275
+ }));
276
+ }
277
+
278
+ /**
279
+ * 调用指定工具
280
+ * @param {string} name - 工具名称
281
+ * @param {Object} args - 工具参数
282
+ * @returns {Object} 工具执行结果
283
+ */
284
+ async callTool(name, args) {
285
+ if (!this.initialized) {
286
+ throw new Error('Server not initialized. Call initialize() first.');
287
+ }
288
+
289
+ const handler = this.toolHandlers.get(name);
290
+ if (!handler) {
291
+ return {
292
+ content: [{
293
+ type: 'text',
294
+ text: `Unknown tool: ${name}`
295
+ }],
296
+ isError: true
297
+ };
298
+ }
299
+
300
+ try {
301
+ log('info', `Calling tool: ${name}`);
302
+ const result = await handler(args);
303
+ return result;
304
+ } catch (error) {
305
+ log('error', `Tool execution failed: ${name}`, error.message);
306
+ return {
307
+ content: [{
308
+ type: 'text',
309
+ text: `Error executing ${name}: ${error.message}`
310
+ }],
311
+ isError: true
312
+ };
313
+ }
314
+ }
315
+
316
+ /**
317
+ * 读取指定的资源
318
+ * @param {string} uri - 资源URI
319
+ * @returns {Object} 资源内容
320
+ */
321
+ async readResource(uri) {
322
+ if (!this.initialized) {
323
+ throw new Error('Server not initialized. Call initialize() first.');
324
+ }
325
+
326
+ const resourceInfo = PDD_RESOURCES[uri];
327
+ if (!resourceInfo) {
328
+ throw new Error(`Resource not found: ${uri}`);
329
+ }
330
+
331
+ // 这里可以扩展为从文件系统或数据库读取实际数据
332
+ const handler = this.resourceHandlers.get(uri);
333
+ if (handler) {
334
+ return await handler(uri);
335
+ }
336
+
337
+ return {
338
+ contents: [{
339
+ uri,
340
+ mimeType: 'application/json',
341
+ text: JSON.stringify({
342
+ uri,
343
+ name: resourceInfo.name,
344
+ description: resourceInfo.description,
345
+ timestamp: new Date().toISOString()
346
+ }, null, 2)
347
+ }]
348
+ };
349
+ }
350
+
351
+ /**
352
+ * 注册自定义工具处理器
353
+ * @param {string} name - 工具名称
354
+ * @param {Function} handler - 处理函数
355
+ */
356
+ registerToolHandler(name, handler) {
357
+ this.toolHandlers.set(name, handler);
358
+ log('info', `Registered custom tool handler: ${name}`);
359
+ }
360
+
361
+ /**
362
+ * 注册自定义资源处理器
363
+ * @param {string} uri - 资源URI
364
+ * @param {Function} handler - 处理函数
365
+ */
366
+ registerResourceHandler(uri, handler) {
367
+ this.resourceHandlers.set(uri, handler);
368
+ log('info', `Registered custom resource handler: ${uri}`);
369
+ }
370
+
371
+ /**
372
+ * 处理HTTP请求
373
+ * @private
374
+ */
375
+ _handleRequest(req, res) {
376
+ const url = new URL(req.url, `http://${this.host}:${this.port}`);
377
+
378
+ // 设置CORS头
379
+ res.setHeader('Access-Control-Allow-Origin', '*');
380
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
381
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
382
+
383
+ if (req.method === 'OPTIONS') {
384
+ res.writeHead(200);
385
+ res.end();
386
+ return;
387
+ }
388
+
389
+ // MCP协议端点路由
390
+ if (url.pathname === '/mcp' && req.method === 'POST') {
391
+ let body = '';
392
+ req.on('data', chunk => body += chunk);
393
+ req.on('end', async () => {
394
+ try {
395
+ const request = JSON.parse(body);
396
+ const response = await this._processRequest(request);
397
+ res.writeHead(200, { 'Content-Type': 'application/json' });
398
+ res.end(JSON.stringify(response));
399
+ } catch (error) {
400
+ res.writeHead(400, { 'Content-Type': 'application/json' });
401
+ res.end(JSON.stringify({
402
+ error: error.message,
403
+ code: 'INVALID_REQUEST'
404
+ }));
405
+ }
406
+ });
407
+ } else if (url.pathname === '/health' && req.method === 'GET') {
408
+ res.writeHead(200, { 'Content-Type': 'application/json' });
409
+ res.end(JSON.stringify({ status: 'ok', timestamp: new Date().toISOString() }));
410
+ } else {
411
+ res.writeHead(404, { 'Content-Type': 'application/json' });
412
+ res.end(JSON.stringify({ error: 'Not found' }));
413
+ }
414
+ }
415
+
416
+ /**
417
+ * 处理MCP请求
418
+ * @private
419
+ */
420
+ async _processRequest(request) {
421
+ const { method, params } = request;
422
+
423
+ switch (method) {
424
+ case 'initialize':
425
+ await this.initialize();
426
+ return {
427
+ jsonrpc: '2.0',
428
+ id: request.id,
429
+ result: {
430
+ protocolVersion: '2024-11-05',
431
+ capabilities: {
432
+ tools: {},
433
+ resources: {}
434
+ },
435
+ serverInfo: {
436
+ name: 'pdd-mcp-server',
437
+ version: '1.0.0'
438
+ }
439
+ }
440
+ };
441
+
442
+ case 'tools/list':
443
+ const tools = await this.listTools();
444
+ return {
445
+ jsonrpc: '2.0',
446
+ id: request.id,
447
+ result: { tools }
448
+ };
449
+
450
+ case 'tools/call':
451
+ const toolResult = await this.callTool(params.name, params.arguments);
452
+ return {
453
+ jsonrpc: '2.0',
454
+ id: request.id,
455
+ result: toolResult
456
+ };
457
+
458
+ case 'resources/list':
459
+ const resources = await this.listResources();
460
+ return {
461
+ jsonrpc: '2.0',
462
+ id: request.id,
463
+ result: { resources }
464
+ };
465
+
466
+ case 'resources/read':
467
+ const resourceResult = await this.readResource(params.uri);
468
+ return {
469
+ jsonrpc: '2.0',
470
+ id: request.id,
471
+ result: resourceResult
472
+ };
473
+
474
+ default:
475
+ return {
476
+ jsonrpc: '2.0',
477
+ id: request.id,
478
+ error: {
479
+ code: -32601,
480
+ message: `Method not found: ${method}`
481
+ }
482
+ };
483
+ }
484
+ }
485
+
486
+ /**
487
+ * 启动MCP服务器
488
+ * @param {number} port - 监听端口(可选)
489
+ */
490
+ start(port) {
491
+ if (port) this.port = port;
492
+
493
+ this.server = http.createServer(this._handleRequest.bind(this));
494
+
495
+ return new Promise((resolve, reject) => {
496
+ this.server.listen(this.port, this.host, () => {
497
+ log('success', `PDD MCP Server running at http://${this.host}:${this.port}`);
498
+ log('info', `Available endpoints:`);
499
+ log(`info`, ` - POST /mcp - MCP protocol endpoint`);
500
+ log(`info`, ` - GET /health - Health check endpoint`);
501
+ resolve(this);
502
+ });
503
+
504
+ this.server.on('error', (error) => {
505
+ log('error', 'Failed to start MCP Server', error.message);
506
+ reject(error);
507
+ });
508
+ });
509
+ }
510
+
511
+ /**
512
+ * 停止MCP服务器
513
+ */
514
+ stop() {
515
+ return new Promise((resolve, reject) => {
516
+ if (!this.server) {
517
+ resolve();
518
+ return;
519
+ }
520
+
521
+ this.server.close((error) => {
522
+ if (error) {
523
+ reject(error);
524
+ } else {
525
+ log('info', 'PDD MCP Server stopped');
526
+ this.server = null;
527
+ resolve();
528
+ }
529
+ });
530
+ });
531
+ }
532
+ }
533
+
534
+ /**
535
+ * 导出常量供外部使用
536
+ */
537
+ export { PDD_RESOURCES, PDD_TOOLS };
538
+
539
+ /**
540
+ * 创建并启动MCP服务器的便捷函数
541
+ * @param {Object} options - 配置选项
542
+ * @returns {Promise<PDDMCPServer>} 服务器实例
543
+ */
544
+ export async function createMCPServer(options = {}) {
545
+ const server = new PDDMCPServer(options);
546
+ await server.initialize();
547
+ return server;
548
+ }