-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
292 lines (255 loc) · 8.12 KB
/
Copy pathindex.js
File metadata and controls
292 lines (255 loc) · 8.12 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
#!/usr/bin/env node
import "dotenv/config";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
ListPromptsRequestSchema,
GetPromptRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { fileURLToPath } from "url";
import { exec } from "./exec.js";
import { normalizeRequestArgs, shellEscape } from "./utils.js";
import {
getCurrentBranch,
getLastCommitMessage,
getPrimaryBranch,
getStagedChanges,
hasStagedChanges,
hasTestChanges,
hasWorkingChanges,
workingTreeSummary,
} from "./git-helpers.js";
import {
createCommitMessageParts,
determineReleaseTypeFromCommit,
} from "./commit-helpers.js";
import { WorkflowState } from "./workflow-state.js";
import { getToolList } from "./tools/definitions.js";
import { handleToolCall } from "./tools/handlers.js";
export { createCommitMessageParts, determineReleaseTypeFromCommit } from "./commit-helpers.js";
export { containsTestFilesInStatus, workingTreeSummary } from "./git-helpers.js";
export { WorkflowState } from "./workflow-state.js";
export let workflowState = null;
const __filename = fileURLToPath(import.meta.url);
// Create MCP server
export const server = new Server(
{
name: "@programinglive/dev-workflow-mcp-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
prompts: {},
},
}
);
// Define tools
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: getToolList(),
}));
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
// Reload state from disk before each request to ensure consistency
if (workflowState) {
await workflowState.load();
}
return handleToolCall({
request,
normalizeRequestArgs,
workflowState,
exec,
git: {
hasWorkingChanges,
hasTestChanges,
hasStagedChanges,
getStagedChanges,
getCurrentBranch,
getPrimaryBranch,
getLastCommitMessage,
workingTreeSummary,
},
utils: { shellEscape, determineReleaseTypeFromCommit, createCommitMessageParts },
});
});
// Helper function
export function getNextStep(status) {
if (!status.featureFlowCreated) return "Describe feature flow with Mermaid";
if (!status.bugFixed) return "Mark feature/bug as fixed";
if (!status.testsCreated) return "Create tests";
if (!status.testsPassed) return "Run tests";
if (!status.documentationCreated) return "Create documentation";
if (!status.readyCheckCompleted) return "Run 'check_ready_to_commit'";
if (!status.commitAndPushCompleted) return "Run 'commit_and_push'";
if (!status.released) return "Run 'perform_release'";
return "Complete the task";
}
// Define prompts
server.setRequestHandler(ListPromptsRequestSchema, async () => {
return {
prompts: [
{
name: "workflow_reminder",
description: "Get a reminder of the complete development workflow",
},
{
name: "pre_commit_checklist",
description: "Pre-commit checklist to ensure nothing is missed",
},
],
};
});
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
const { name } = request.params;
if (name === "workflow_reminder") {
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Development Workflow Discipline:
1. 🎯 START CONSCIOUS
- Use 'start_task' to declare what you're coding
- Be clear about your intention
2. 🌀 DESIGN FLOW
- Describe your feature flow using Mermaid
- Use 'create_feature_flow' to record it
3. 🔨 CODE WITH PURPOSE
- After fixing/implementing, create tests
- Use 'mark_bug_fixed' when done
4. ✅ TESTS MUST PASS
- Run your tests with 'run_tests'
- If tests fail: FIX THEM, never skip!
- Only proceed when all tests are GREEN
5. 📝 DOCUMENT YOUR WORK
- Update README, comments, or docs
- Use 'create_documentation' when done
6. 🚀 COMMIT & PUSH
- Use 'check_ready_to_commit' to verify
- Use 'commit_and_push' (stages, commits, and pushes)
- Then use 'perform_release' to handle versioning and tags
- Use 'complete_task' to finish
Remember: No shortcuts! Each step is important for code quality.`,
},
},
],
};
}
if (name === "pre_commit_checklist") {
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Pre-Commit Checklist:
Before you commit and push, verify:
□ Feature/bug is fully implemented
□ Tests are created for the changes
□ All tests pass (GREEN) - run 'run_tests'
□ Documentation is updated
□ Code is clean (no console.logs, unused imports)
□ No TypeScript 'any' types
□ Followed project coding standards
Use 'check_ready_to_commit' to verify workflow completion.
🚫 NEVER commit if tests are failing!
✅ Only commit when everything is green!`,
},
},
],
};
}
throw new Error(`Unknown prompt: ${name}`);
});
// Start server
export async function main() {
const startTime = Date.now();
console.error(`[${new Date().toISOString()}] MCP Server starting...`);
try {
// Initialize workflow state
workflowState = new WorkflowState();
console.error(`[${new Date().toISOString()}] Loading workflow state...`);
await workflowState.load();
console.error(`[${new Date().toISOString()}] Workflow state loaded.`);
console.error(`[${new Date().toISOString()}] Ensuring primary file...`);
await workflowState.ensurePrimaryFile();
console.error(`[${new Date().toISOString()}] Workflow state initialization complete.`);
} catch (error) {
console.error(`[${new Date().toISOString()}] Warning: Failed to initialize workflow state:`, error.message);
// Continue anyway - workflow state is optional for MCP to function
workflowState = new WorkflowState();
}
const transport = new StdioServerTransport();
console.error(`[${new Date().toISOString()}] Connecting transport...`);
await server.connect(transport);
const duration = Date.now() - startTime;
console.error(`[${new Date().toISOString()}] Dev Workflow MCP Server running on stdio (startup took ${duration}ms)`);
}
// --- Lightweight CLI: dev-workflow-mcp call <toolName> [--args '<json>'] ---
async function cliMain(argv) {
const [, , subcommand, toolName, ...rest] = argv;
if (subcommand !== "call") return false;
if (!toolName || typeof toolName !== "string" || toolName.trim() === "") {
console.error("Usage: dev-workflow-mcp call <toolName> [--args '<json>']");
process.exit(2);
}
// Parse --args '<json>' if provided
let rawArgs = undefined;
for (let i = 0; i < rest.length; i++) {
if (rest[i] === "--args") {
rawArgs = rest[i + 1];
break;
}
}
try {
// Initialize workflow state similar to the server path
workflowState = new WorkflowState();
await workflowState.load();
await workflowState.ensurePrimaryFile();
const request = {
params: {
name: toolName,
arguments: rawArgs ?? {},
},
};
const response = await handleToolCall({
request,
normalizeRequestArgs,
workflowState,
exec,
git: {
hasWorkingChanges,
hasTestChanges,
hasStagedChanges,
getStagedChanges,
getCurrentBranch,
getPrimaryBranch,
getLastCommitMessage,
workingTreeSummary,
},
utils: { shellEscape, determineReleaseTypeFromCommit, createCommitMessageParts },
});
const text = response?.content?.[0]?.text || "";
console.log(text);
const isError = /^\s*[⚠️❌]/u.test(text);
process.exit(isError ? 1 : 0);
} catch (error) {
console.error("CLI error:", error.message || String(error));
process.exit(1);
}
}
const isDirectRun = process.argv[1] === __filename;
if (isDirectRun) {
// If invoked with a CLI subcommand, run it; otherwise start MCP server
if (process.argv[2] === "call") {
cliMain(process.argv);
} else {
main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});
}
}