-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworkflow-prompts.ts
More file actions
512 lines (467 loc) · 13.9 KB
/
workflow-prompts.ts
File metadata and controls
512 lines (467 loc) · 13.9 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/**
* MCP Server workflow prompts for CodeQL development
*
* All prompt content is loaded from .prompt.md files in this directory.
* This file only handles prompt registration and parameter processing.
*/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { basename } from 'path';
import { loadPromptTemplate, processPromptTemplate } from './prompt-loader';
import { logger } from '../utils/logger';
/** Supported CodeQL languages for tools queries */
export const SUPPORTED_LANGUAGES = [
'actions',
'cpp',
'csharp',
'go',
'java',
'javascript',
'python',
'ruby',
'swift'
] as const;
/**
* Schema for workshop_creation_workflow prompt parameters.
* Uses z.coerce.number() for numStages to handle string inputs from VSCode slash commands.
*/
export const workshopCreationWorkflowSchema = z.object({
queryPath: z
.string()
.describe('Path to the production-grade CodeQL query (.ql or .qlref)'),
language: z
.enum(SUPPORTED_LANGUAGES)
.describe('Programming language of the query'),
workshopName: z
.string()
.optional()
.describe('Name for the workshop directory'),
numStages: z
.coerce.number()
.optional()
.describe('Number of incremental stages (default: 4-8)')
});
/**
* Register MCP workflow prompts
*
* Each prompt loads its content from a corresponding .prompt.md file
* and processes any parameter substitutions.
*/
export function registerWorkflowPrompts(server: McpServer): void {
// Test-Driven Development Prompt
server.prompt(
'test_driven_development',
'Test-driven development workflow for CodeQL queries using MCP tools',
{
language: z
.enum(SUPPORTED_LANGUAGES)
.describe('Programming language for the query'),
queryName: z.string().optional().describe('Name of the query to develop')
},
async ({ language, queryName }) => {
const template = loadPromptTemplate('ql-tdd-basic.prompt.md');
const content = processPromptTemplate(template, {
language,
queryName: queryName || '[QueryName]'
});
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: `## Context\n\n- **Language**: ${language}\n${queryName ? `- **Query Name**: ${queryName}\n` : ''}\n${content}`
}
}
]
};
}
);
// Tools Query Workflow Prompt
server.prompt(
'tools_query_workflow',
'Guide for using built-in tools queries (PrintAST, PrintCFG, CallGraphFrom, CallGraphTo) to understand code structure',
{
language: z
.enum(SUPPORTED_LANGUAGES)
.describe('Programming language for the tools queries'),
database: z.string().describe('Path to the CodeQL database'),
sourceFiles: z
.string()
.optional()
.describe(
'Comma-separated source file names for PrintAST (e.g., "main.js,utils.js")'
),
sourceFunction: z
.string()
.optional()
.describe(
'Function name for PrintCFG or CallGraphFrom (e.g., "processData")'
),
targetFunction: z
.string()
.optional()
.describe('Function name for CallGraphTo (e.g., "validate")')
},
async ({
language,
database,
sourceFiles,
sourceFunction,
targetFunction
}) => {
const template = loadPromptTemplate('tools-query-workflow.prompt.md');
const content = processPromptTemplate(template, {
language,
database
});
const contextSection = buildToolsQueryContext(
language,
database,
sourceFiles,
sourceFunction,
targetFunction
);
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + content
}
}
]
};
}
);
// Workshop Creation Workflow Prompt
server.prompt(
'workshop_creation_workflow',
'Guide for creating CodeQL query development workshops from production-grade queries',
workshopCreationWorkflowSchema.shape,
async ({ queryPath, language, workshopName, numStages }) => {
const template = loadPromptTemplate('workshop-creation-workflow.prompt.md');
// Derive workshop name from query path if not provided
const derivedName =
workshopName ||
basename(queryPath)
.replace(/\.(ql|qlref)$/, '')
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-') ||
'codeql-workshop';
const contextSection = buildWorkshopContext(
queryPath,
language,
derivedName,
numStages
);
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + template
}
}
]
};
}
);
// TDD Basic Prompt - Test-Driven Development Checklist
server.prompt(
'ql_tdd_basic',
'Test-driven CodeQL query development checklist - write tests first, implement query, iterate until tests pass',
{
language: z
.enum(SUPPORTED_LANGUAGES)
.optional()
.describe('Programming language for the query (optional)'),
queryName: z.string().optional().describe('Name of the query to develop')
},
async ({ language, queryName }) => {
const template = loadPromptTemplate('ql-tdd-basic.prompt.md');
let contextSection = '## Your Development Context\n\n';
if (language) {
contextSection += `- **Language**: ${language}\n`;
}
if (queryName) {
contextSection += `- **Query Name**: ${queryName}\n`;
}
if (language || queryName) {
contextSection += '\n';
}
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + template
}
}
]
};
}
);
// TDD Advanced Prompt - Advanced Techniques with AST/CFG/CallGraph
server.prompt(
'ql_tdd_advanced',
'Advanced test-driven CodeQL development with AST visualization, control flow, and call graph analysis',
{
language: z
.enum(SUPPORTED_LANGUAGES)
.optional()
.describe('Programming language for the query (optional)'),
queryName: z.string().optional().describe('Name of the query to develop'),
database: z
.string()
.optional()
.describe('Path to the CodeQL database for analysis')
},
async ({ language, queryName, database }) => {
const template = loadPromptTemplate('ql-tdd-advanced.prompt.md');
let contextSection = '## Your Development Context\n\n';
if (language) {
contextSection += `- **Language**: ${language}\n`;
}
if (queryName) {
contextSection += `- **Query Name**: ${queryName}\n`;
}
if (database) {
contextSection += `- **Database**: ${database}\n`;
}
if (language || queryName || database) {
contextSection += '\n';
}
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + template
}
}
]
};
}
);
// SARIF Rank False Positives Prompt
server.prompt(
'sarif_rank_false_positives',
'Analyze SARIF results to identify likely false positives in CodeQL query results',
{
queryId: z.string().optional().describe('CodeQL query/rule identifier'),
sarifPath: z
.string()
.optional()
.describe('Path to the SARIF file to analyze')
},
async ({ queryId, sarifPath }) => {
const template = loadPromptTemplate('sarif-rank-false-positives.prompt.md');
let contextSection = '## Analysis Context\n\n';
if (queryId) {
contextSection += `- **Query ID**: ${queryId}\n`;
}
if (sarifPath) {
contextSection += `- **SARIF File**: ${sarifPath}\n`;
}
if (queryId || sarifPath) {
contextSection += '\n';
}
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + template
}
}
]
};
}
);
// SARIF Rank True Positives Prompt
server.prompt(
'sarif_rank_true_positives',
'Analyze SARIF results to identify likely true positives in CodeQL query results',
{
queryId: z.string().optional().describe('CodeQL query/rule identifier'),
sarifPath: z
.string()
.optional()
.describe('Path to the SARIF file to analyze')
},
async ({ queryId, sarifPath }) => {
const template = loadPromptTemplate('sarif-rank-true-positives.prompt.md');
let contextSection = '## Analysis Context\n\n';
if (queryId) {
contextSection += `- **Query ID**: ${queryId}\n`;
}
if (sarifPath) {
contextSection += `- **SARIF File**: ${sarifPath}\n`;
}
if (queryId || sarifPath) {
contextSection += '\n';
}
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + template
}
}
]
};
}
);
// Explain CodeQL Query Prompt (for workshop learning content)
server.prompt(
'explain_codeql_query',
'Generate detailed explanation of a CodeQL query for workshop learning content - uses MCP tools to gather context and produces both verbal explanations and mermaid evaluation diagrams',
{
queryPath: z
.string()
.describe('Path to the CodeQL query file (.ql or .qlref)'),
language: z
.enum(SUPPORTED_LANGUAGES)
.describe('Programming language of the query'),
databasePath: z
.string()
.optional()
.describe('Optional path to a real CodeQL database for profiling')
},
async ({ queryPath, language, databasePath }) => {
const template = loadPromptTemplate('explain-codeql-query.prompt.md');
let contextSection = '## Query to Explain\n\n';
contextSection += `- **Query Path**: ${queryPath}\n`;
contextSection += `- **Language**: ${language}\n`;
if (databasePath) {
contextSection += `- **Database Path**: ${databasePath}\n`;
}
contextSection += '\n';
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + template
}
}
]
};
}
);
// Document CodeQL Query Prompt
server.prompt(
'document_codeql_query',
'Create or update documentation for a CodeQL query - generates standardized markdown documentation as a sibling file to the query',
{
queryPath: z
.string()
.describe('Path to the CodeQL query file (.ql or .qlref)'),
language: z
.enum(SUPPORTED_LANGUAGES)
.describe('Programming language of the query')
},
async ({ queryPath, language }) => {
const template = loadPromptTemplate('document-codeql-query.prompt.md');
const contextSection = `## Query to Document
- **Query Path**: ${queryPath}
- **Language**: ${language}
`;
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: contextSection + template
}
}
]
};
}
);
logger.info('Registered 9 workflow prompts');
}
/**
* Build context section for tools query workflow
*/
export function buildToolsQueryContext(
language: string,
database: string,
sourceFiles?: string,
sourceFunction?: string,
targetFunction?: string
): string {
const lines = [
'## Your Context',
'',
`- **Language**: ${language}`,
`- **Database**: ${database}`
];
if (sourceFiles) {
lines.push(`- **Source Files**: ${sourceFiles}`);
}
if (sourceFunction) {
lines.push(`- **Source Function**: ${sourceFunction}`);
}
if (targetFunction) {
lines.push(`- **Target Function**: ${targetFunction}`);
}
lines.push('', '## Recommended Next Steps', '');
if (sourceFiles) {
lines.push(
`1. Run \`codeql_query_run\` with queryName="PrintAST", sourceFiles="${sourceFiles}"`
);
} else {
lines.push('1. Identify source files to analyze with PrintAST');
}
if (sourceFunction) {
lines.push(
`2. Run \`codeql_query_run\` with queryName="PrintCFG" or "CallGraphFrom", sourceFunction="${sourceFunction}"`
);
} else {
lines.push(
'2. Identify key functions for CFG or call graph analysis'
);
}
if (targetFunction) {
lines.push(
`3. Run \`codeql_query_run\` with queryName="CallGraphTo", targetFunction="${targetFunction}"`
);
} else {
lines.push('3. Identify target functions to find callers');
}
lines.push('', '');
return lines.join('\n');
}
/**
* Build context section for workshop creation workflow
*/
export function buildWorkshopContext(
queryPath: string,
language: string,
workshopName: string,
numStages?: number
): string {
return `## Your Workshop Context
- **Target Query**: ${queryPath}
- **Language**: ${language}
- **Workshop Name**: ${workshopName}
- **Suggested Stages**: ${numStages || '4-8 (auto-detect based on query complexity)'}
## Immediate Actions
1. **Locate query files**: Use \`find_codeql_query_files\` with queryPath="${queryPath}"
2. **Understand query for learning content**: Use the \`explain_codeql_query\` prompt with queryPath="${queryPath}" and language="${language}"
3. **Document each workshop stage**: Use the \`document_codeql_query\` prompt to create/update documentation for each solution query
4. **Verify tests pass**: Use \`codeql_test_run\` on existing tests
5. **Run tools queries**: Generate AST/CFG understanding for workshop materials
`;
}