codingbuddy-rules 4.2.0 → 4.4.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.
@@ -0,0 +1,356 @@
1
+ ---
2
+ name: mcp-builder
3
+ description: Use when building or extending MCP (Model Context Protocol) servers. Covers NestJS-based server design, Tools/Resources/Prompts capability design, transport implementation (stdio/SSE), and testing strategies.
4
+ ---
5
+
6
+ # MCP Builder
7
+
8
+ ## Overview
9
+
10
+ The Model Context Protocol (MCP) is the standard for connecting AI assistants to external tools, data, and prompts. Building a quality MCP server requires understanding the protocol's three capability types and implementing them with proper error handling and transport support.
11
+
12
+ **Core principle:** MCP servers are AI interfaces, not REST APIs. Design for machine consumption and LLM context efficiency.
13
+
14
+ **Iron Law:**
15
+ ```
16
+ SCHEMA FIRST. Every Tool, Resource, and Prompt must have complete JSON Schema before implementation.
17
+ ```
18
+
19
+ ## When to Use
20
+
21
+ - Creating a new MCP server from scratch
22
+ - Adding new Tools, Resources, or Prompts to an existing server
23
+ - Implementing new transport modes (stdio ↔ SSE)
24
+ - Testing MCP server capabilities
25
+ - Debugging MCP communication issues
26
+
27
+ ## MCP Architecture Overview
28
+
29
+ ```
30
+ MCP Client (Claude, Cursor, etc.)
31
+ ↕ stdio / SSE
32
+ MCP Server
33
+ ├── Tools → Functions the AI can call (side effects allowed)
34
+ ├── Resources → Data the AI can read (read-only, URI-addressed)
35
+ └── Prompts → Reusable prompt templates
36
+ ```
37
+
38
+ ## NestJS MCP Server Structure
39
+
40
+ ```
41
+ apps/mcp-server/src/
42
+ ├── main.ts # Transport setup (stdio vs SSE)
43
+ ├── app.module.ts # Root module
44
+ ├── mcp/
45
+ │ ├── mcp.module.ts # MCP module
46
+ │ ├── mcp.service.ts # MCP server registration
47
+ │ ├── tools/ # Tool handlers
48
+ │ ├── resources/ # Resource handlers
49
+ │ └── prompts/ # Prompt handlers
50
+ └── rules/
51
+ ├── rules.module.ts
52
+ └── rules.service.ts # Business logic (pure)
53
+ ```
54
+
55
+ ## Designing MCP Capabilities
56
+
57
+ ### Tools (AI can call these)
58
+
59
+ Tools perform actions and return results. They CAN have side effects.
60
+
61
+ ```typescript
62
+ // Tool Schema (define first)
63
+ const searchRulesTool = {
64
+ name: 'search_rules',
65
+ description: 'Search AI coding rules by keyword or topic. Returns matching rules with their content.',
66
+ inputSchema: {
67
+ type: 'object',
68
+ properties: {
69
+ query: {
70
+ type: 'string',
71
+ description: 'Search term to find relevant rules',
72
+ minLength: 1,
73
+ maxLength: 200,
74
+ },
75
+ limit: {
76
+ type: 'number',
77
+ description: 'Maximum results to return (default: 10)',
78
+ minimum: 1,
79
+ maximum: 50,
80
+ default: 10,
81
+ },
82
+ },
83
+ required: ['query'],
84
+ },
85
+ };
86
+ ```
87
+
88
+ ```typescript
89
+ // Tool Handler
90
+ @Injectable()
91
+ export class SearchRulesHandler {
92
+ constructor(private readonly rulesService: RulesService) {}
93
+
94
+ async handle(params: { query: string; limit?: number }): Promise<ToolResult> {
95
+ // Validate input
96
+ if (!params.query?.trim()) {
97
+ return {
98
+ content: [{ type: 'text', text: 'Error: query is required' }],
99
+ isError: true,
100
+ };
101
+ }
102
+
103
+ try {
104
+ const results = await this.rulesService.search(params.query, {
105
+ limit: params.limit ?? 10,
106
+ });
107
+
108
+ return {
109
+ content: [
110
+ {
111
+ type: 'text',
112
+ text: results.length === 0
113
+ ? 'No rules found for: ' + params.query
114
+ : results.map(r => `## ${r.name}\n${r.content}`).join('\n\n'),
115
+ },
116
+ ],
117
+ };
118
+ } catch (error) {
119
+ return {
120
+ content: [{ type: 'text', text: `Error: ${error.message}` }],
121
+ isError: true,
122
+ };
123
+ }
124
+ }
125
+ }
126
+ ```
127
+
128
+ ### Resources (AI can read these)
129
+
130
+ Resources are read-only data exposed via URI. Use for static/cacheable content.
131
+
132
+ ```typescript
133
+ // Resource definition
134
+ const ruleResource = {
135
+ uri: 'rules://core',
136
+ name: 'Core Rules',
137
+ description: 'Core workflow rules (PLAN/ACT/EVAL modes)',
138
+ mimeType: 'text/markdown',
139
+ };
140
+
141
+ // Resource handler
142
+ async readResource(uri: string): Promise<ResourceContent> {
143
+ const ruleName = uri.replace('rules://', '');
144
+ const content = await this.rulesService.getRule(ruleName);
145
+
146
+ if (!content) {
147
+ throw new Error(`Resource not found: ${uri}`);
148
+ }
149
+
150
+ return {
151
+ uri,
152
+ mimeType: 'text/markdown',
153
+ text: content,
154
+ };
155
+ }
156
+ ```
157
+
158
+ **Resource URI design:**
159
+ ```
160
+ rules://core → Core rules file
161
+ rules://agents/list → List of agents
162
+ agents://planner → Specific agent definition
163
+ checklists://security → Security checklist
164
+ ```
165
+
166
+ ### Prompts (Reusable templates)
167
+
168
+ ```typescript
169
+ const activateAgentPrompt = {
170
+ name: 'activate_agent',
171
+ description: 'Generate activation prompt for a specialist agent',
172
+ arguments: [
173
+ {
174
+ name: 'agentName',
175
+ description: 'Name of the agent to activate (e.g., "solution-architect")',
176
+ required: true,
177
+ },
178
+ {
179
+ name: 'task',
180
+ description: 'Task description for the agent',
181
+ required: false,
182
+ },
183
+ ],
184
+ };
185
+
186
+ async getPrompt(name: string, args: Record<string, string>): Promise<PromptResult> {
187
+ if (name === 'activate_agent') {
188
+ const agent = await this.agentsService.getAgent(args.agentName);
189
+ return {
190
+ messages: [
191
+ {
192
+ role: 'user',
193
+ content: {
194
+ type: 'text',
195
+ text: `Activate the ${agent.displayName} specialist.\n\n${agent.systemPrompt}\n\n${args.task ?? ''}`,
196
+ },
197
+ },
198
+ ],
199
+ };
200
+ }
201
+ throw new Error(`Unknown prompt: ${name}`);
202
+ }
203
+ ```
204
+
205
+ ## Transport Implementation
206
+
207
+ ### Stdio Transport (Default)
208
+
209
+ ```typescript
210
+ // main.ts
211
+ async function bootstrap() {
212
+ const transport = process.env.MCP_TRANSPORT ?? 'stdio';
213
+
214
+ if (transport === 'stdio') {
215
+ const app = await NestFactory.createApplicationContext(AppModule);
216
+ const mcpService = app.get(McpService);
217
+ await mcpService.startStdio();
218
+ } else {
219
+ const app = await NestFactory.create(AppModule);
220
+ await app.listen(process.env.PORT ?? 3000);
221
+ }
222
+ }
223
+ ```
224
+
225
+ ### SSE Transport (HTTP)
226
+
227
+ ```typescript
228
+ // SSE endpoint with optional auth
229
+ @Get('/sse')
230
+ @UseGuards(SseAuthGuard)
231
+ async sse(@Req() req: Request, @Res() res: Response) {
232
+ const transport = new SSEServerTransport('/messages', res);
233
+ await this.mcpService.connect(transport);
234
+ }
235
+
236
+ @Post('/messages')
237
+ @UseGuards(SseAuthGuard)
238
+ async messages(@Req() req: Request, @Res() res: Response) {
239
+ await this.mcpService.handleMessage(req, res);
240
+ }
241
+ ```
242
+
243
+ ```typescript
244
+ // Auth guard
245
+ @Injectable()
246
+ export class SseAuthGuard implements CanActivate {
247
+ canActivate(context: ExecutionContext): boolean {
248
+ const token = process.env.MCP_SSE_TOKEN;
249
+ if (!token) return true; // Auth disabled if token not set
250
+
251
+ const request = context.switchToHttp().getRequest();
252
+ const provided = request.headers.authorization?.replace('Bearer ', '');
253
+ return provided === token;
254
+ }
255
+ }
256
+ ```
257
+
258
+ ## Testing Strategy
259
+
260
+ ### Unit Tests (Tools/Resources)
261
+
262
+ ```typescript
263
+ describe('SearchRulesHandler', () => {
264
+ let handler: SearchRulesHandler;
265
+ let rulesService: RulesService;
266
+
267
+ beforeEach(() => {
268
+ rulesService = new RulesService('./test-fixtures/.ai-rules');
269
+ handler = new SearchRulesHandler(rulesService);
270
+ });
271
+
272
+ it('returns results for valid query', async () => {
273
+ const result = await handler.handle({ query: 'TDD' });
274
+ expect(result.isError).toBeUndefined();
275
+ expect(result.content[0].text).toContain('TDD');
276
+ });
277
+
278
+ it('returns error for empty query', async () => {
279
+ const result = await handler.handle({ query: '' });
280
+ expect(result.isError).toBe(true);
281
+ });
282
+ });
283
+ ```
284
+
285
+ ### Integration Tests (Protocol Level)
286
+
287
+ ```typescript
288
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
289
+ import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
290
+
291
+ describe('MCP Server Integration', () => {
292
+ let client: Client;
293
+
294
+ beforeAll(async () => {
295
+ const transport = new StdioClientTransport({
296
+ command: 'node',
297
+ args: ['dist/main.js'],
298
+ });
299
+ client = new Client({ name: 'test', version: '1.0.0' }, {});
300
+ await client.connect(transport);
301
+ });
302
+
303
+ it('lists available tools', async () => {
304
+ const { tools } = await client.listTools();
305
+ expect(tools.map(t => t.name)).toContain('search_rules');
306
+ });
307
+
308
+ it('calls search_rules tool', async () => {
309
+ const result = await client.callTool({
310
+ name: 'search_rules',
311
+ arguments: { query: 'TDD' },
312
+ });
313
+ expect(result.isError).toBeFalsy();
314
+ });
315
+ });
316
+ ```
317
+
318
+ ## Design Checklist
319
+
320
+ ```
321
+ Tool Design:
322
+ - [ ] Name is a verb or verb_noun (search_rules, get_agent)
323
+ - [ ] Description explains WHAT and WHEN (for LLM to understand)
324
+ - [ ] All parameters have descriptions and types
325
+ - [ ] Required vs optional params clearly marked
326
+ - [ ] Error responses use isError: true with descriptive messages
327
+ - [ ] Input validated before processing
328
+
329
+ Resource Design:
330
+ - [ ] URI follows scheme://path pattern
331
+ - [ ] mimeType set correctly (text/markdown, application/json)
332
+ - [ ] Read-only (no side effects)
333
+ - [ ] Returns 404-equivalent for missing resources
334
+
335
+ Prompt Design:
336
+ - [ ] Arguments have clear descriptions
337
+ - [ ] Output is ready-to-use for the LLM
338
+ - [ ] Does not duplicate Tool functionality
339
+
340
+ Server:
341
+ - [ ] Stdio and SSE transports both tested
342
+ - [ ] Auth guard works when MCP_SSE_TOKEN is set
343
+ - [ ] Auth disabled when MCP_SSE_TOKEN is unset
344
+ - [ ] Error handling prevents server crashes
345
+ ```
346
+
347
+ ## Common Mistakes
348
+
349
+ | Mistake | Fix |
350
+ |---------|-----|
351
+ | Tool does too many things | One Tool = one action |
352
+ | Resource has side effects | Move to Tool if it changes state |
353
+ | Schema has no descriptions | LLM uses descriptions to decide when/how to call |
354
+ | No error handling in tools | Always catch and return isError: true |
355
+ | Breaking stdio with console.log | Use stderr for logs: `process.stderr.write(...)` |
356
+ | Hardcoded paths | Use env vars or config injection |
@@ -0,0 +1,318 @@
1
+ ---
2
+ name: prompt-engineering
3
+ description: Use when writing prompts for AI tools, optimizing agent system prompts, or designing AI-readable instructions. Covers prompt structure, meta-prompting, chain-of-thought, and tool-specific optimization.
4
+ ---
5
+
6
+ # Prompt Engineering
7
+
8
+ ## Overview
9
+
10
+ A prompt is an API contract with an AI system. Precision matters. Ambiguous prompts produce inconsistent results; clear prompts produce consistent, predictable behavior.
11
+
12
+ **Core principle:** Prompts are executable specifications. Write them like you write tests: with clear inputs, expected behavior, and success criteria.
13
+
14
+ **Iron Law:**
15
+ ```
16
+ TEST YOUR PROMPT WITH AT LEAST 3 DIFFERENT INPUTS BEFORE USING IN PRODUCTION
17
+ One input is anecdote. Three is pattern. Ten is confidence.
18
+ ```
19
+
20
+ ## When to Use
21
+
22
+ - Writing system prompts for codingbuddy agents
23
+ - Creating CLAUDE.md / .cursorrules instructions
24
+ - Designing tool descriptions for MCP servers
25
+ - Optimizing prompts that produce inconsistent results
26
+ - Building prompt chains for multi-step workflows
27
+
28
+ ## Prompt Anatomy
29
+
30
+ Every effective prompt has these components:
31
+
32
+ ```
33
+ ┌─────────────────────────────────────────┐
34
+ │ ROLE Who/what is the AI? │
35
+ │ CONTEXT What situation are we in? │
36
+ │ TASK What specifically to do? │
37
+ │ CONSTRAINTS What rules must be obeyed? │
38
+ │ FORMAT How to structure output? │
39
+ │ EXAMPLES Show, don't just tell │
40
+ └─────────────────────────────────────────┘
41
+ ```
42
+
43
+ Not every prompt needs all components, but most production prompts need most of them.
44
+
45
+ ## Prompt Patterns
46
+
47
+ ### Pattern 1: Role + Task (Basic)
48
+
49
+ ```
50
+ You are a [specific role].
51
+
52
+ Your task: [specific action] for [specific context].
53
+ ```
54
+
55
+ **Example:**
56
+ ```
57
+ You are a TypeScript code reviewer specializing in security.
58
+
59
+ Your task: Review the authentication module below for OWASP Top 10 vulnerabilities.
60
+ Output a list of findings ordered by severity (Critical → High → Medium → Low).
61
+ ```
62
+
63
+ ### Pattern 2: Chain-of-Thought (Complex Reasoning)
64
+
65
+ Force step-by-step reasoning before conclusions:
66
+
67
+ ```
68
+ Before answering, think through:
69
+ 1. [First consideration]
70
+ 2. [Second consideration]
71
+ 3. [Third consideration]
72
+
73
+ Then provide your conclusion.
74
+ ```
75
+
76
+ **Example:**
77
+ ```
78
+ Before suggesting a fix, think through:
79
+ 1. What is the root cause of this bug?
80
+ 2. What are the possible fix approaches?
81
+ 3. What are the trade-offs of each approach?
82
+ 4. Which approach has the least risk?
83
+
84
+ Then provide your recommendation with rationale.
85
+ ```
86
+
87
+ ### Pattern 3: Few-Shot (Examples)
88
+
89
+ Show the AI what good output looks like:
90
+
91
+ ```
92
+ [Task description]
93
+
94
+ Examples:
95
+
96
+ Input: [example input 1]
97
+ Output: [example output 1]
98
+
99
+ Input: [example input 2]
100
+ Output: [example output 2]
101
+
102
+ Now complete:
103
+ Input: [actual input]
104
+ Output:
105
+ ```
106
+
107
+ **Example (agent expertise):**
108
+ ```
109
+ Format agent expertise as specific, actionable skills.
110
+
111
+ Examples:
112
+
113
+ Input: security
114
+ Output: "OWASP Top 10 Vulnerability Assessment", "JWT Authentication Design", "SQL Injection Prevention"
115
+
116
+ Input: databases
117
+ Output: "PostgreSQL Query Optimization", "Zero-Downtime Schema Migrations", "Connection Pool Tuning"
118
+
119
+ Now complete:
120
+ Input: frontend
121
+ Output:
122
+ ```
123
+
124
+ ### Pattern 4: Constraint-First
125
+
126
+ Lead with what NOT to do (especially useful for restrictive behaviors):
127
+
128
+ ```
129
+ NEVER [prohibited action].
130
+ ALWAYS [required action].
131
+ If [edge case], then [specific handling].
132
+
133
+ Your task: [task description]
134
+ ```
135
+
136
+ **Example:**
137
+ ```
138
+ NEVER include markdown formatting in your output — plain text only.
139
+ ALWAYS include the file path in references (e.g., src/app.ts:42).
140
+ If you are uncertain, say "I'm not sure" rather than guessing.
141
+
142
+ Your task: Analyze the build error below and identify the root cause.
143
+ ```
144
+
145
+ ### Pattern 5: Meta-Prompting
146
+
147
+ Prompt the AI to generate or improve prompts:
148
+
149
+ ```
150
+ I need a prompt for [purpose].
151
+
152
+ The prompt should:
153
+ - [Requirement 1]
154
+ - [Requirement 2]
155
+ - [Requirement 3]
156
+
157
+ Generate 3 prompt variations ranked from most to least structured.
158
+ ```
159
+
160
+ ## System Prompt Design (codingbuddy Agents)
161
+
162
+ Agent system prompts follow a specific structure:
163
+
164
+ ```markdown
165
+ # [Agent Display Name]
166
+
167
+ You are a [role] specialist focused on [narrow domain].
168
+
169
+ ## Your Expertise
170
+
171
+ You excel at:
172
+ - [Specific skill 1 — concrete, not vague]
173
+ - [Specific skill 2]
174
+ - [Specific skill 3]
175
+
176
+ ## Your Approach
177
+
178
+ When activated:
179
+ 1. [First action — what you always do first]
180
+ 2. [Analysis approach]
181
+ 3. [Output structure]
182
+
183
+ ## What You Do NOT Handle
184
+
185
+ Redirect to appropriate specialists for:
186
+ - [Out-of-scope 1] → use [other-agent-name]
187
+ - [Out-of-scope 2] → use [other-agent-name]
188
+
189
+ ## Output Format
190
+
191
+ Structure all responses as:
192
+ ### Findings
193
+ [Severity: Critical/High/Medium/Low] — [Description]
194
+
195
+ ### Recommendations
196
+ [Prioritized list]
197
+ ```
198
+
199
+ ## MCP Tool Description Design
200
+
201
+ Tool descriptions are prompts read by AI to decide when and how to use a tool:
202
+
203
+ ```typescript
204
+ {
205
+ name: 'search_rules',
206
+ // ❌ Bad: vague
207
+ description: 'Search rules',
208
+
209
+ // ✅ Good: specific use case + when to use
210
+ description: 'Search AI coding rules by keyword or topic. Use this when looking for specific guidelines about coding practices, TDD, security, or workflow modes. Returns matching rules with their content.',
211
+
212
+ inputSchema: {
213
+ properties: {
214
+ query: {
215
+ type: 'string',
216
+ // ❌ Bad: no guidance
217
+ description: 'Query string',
218
+
219
+ // ✅ Good: what makes a good query
220
+ description: 'Search term such as "TDD", "security", "TypeScript strict", or a question like "how to handle migrations"',
221
+ }
222
+ }
223
+ }
224
+ }
225
+ ```
226
+
227
+ ## Prompt Testing
228
+
229
+ ### Test Matrix
230
+
231
+ For each prompt, test these dimensions:
232
+
233
+ | Dimension | Test Cases |
234
+ |-----------|-----------|
235
+ | Happy path | Ideal input, expected output |
236
+ | Edge cases | Empty input, very long input, unusual characters |
237
+ | Adversarial | Input designed to break the constraint |
238
+ | Ambiguous | Input that could be interpreted multiple ways |
239
+
240
+ ### Evaluation Criteria
241
+
242
+ ```markdown
243
+ ## Prompt Evaluation Rubric
244
+
245
+ **Accuracy:** Does output match expected behavior? (1-5)
246
+ **Consistency:** Same input → same output across runs? (1-5)
247
+ **Format compliance:** Does output match requested format? (1-5)
248
+ **Boundary respect:** Does it honor constraints? (1-5)
249
+ **Efficiency:** Is it unnecessarily verbose? (1-5)
250
+
251
+ Total: 20 points. Target: ≥ 16 for production use.
252
+ ```
253
+
254
+ ## Tool-Specific Optimization
255
+
256
+ ### Claude Code (CLAUDE.md)
257
+
258
+ ```markdown
259
+ ## Best practices for Claude Code instructions:
260
+
261
+ - Use ## headers to organize sections
262
+ - Bold critical rules: **NEVER do X**
263
+ - Use code blocks for exact command examples
264
+ - Include trigger conditions: "When user types PLAN..."
265
+ - Reference other files: "See .claude/rules/tool-priority.md"
266
+ ```
267
+
268
+ ### Cursor (.cursorrules)
269
+
270
+ ```markdown
271
+ ## Best practices for Cursor rules:
272
+
273
+ - One rule per line for reliable parsing
274
+ - Start each rule with the trigger: "When writing TypeScript..."
275
+ - Avoid multi-paragraph rules (Cursor truncates)
276
+ - Use concrete examples, not abstract principles
277
+ ```
278
+
279
+ ### GitHub Copilot (.github/copilot-instructions.md)
280
+
281
+ ```markdown
282
+ ## Best practices for Copilot instructions:
283
+
284
+ - Lead with task-oriented rules
285
+ - Examples are more effective than descriptions
286
+ - Copilot follows "do X" more reliably than "don't do Y"
287
+ - Keep total instructions under 8000 characters
288
+ ```
289
+
290
+ ## Common Mistakes
291
+
292
+ | Mistake | Fix |
293
+ |---------|-----|
294
+ | Vague task ("be helpful") | Specific task ("list 3 alternatives with trade-offs") |
295
+ | No format specification | Add explicit format: "Respond as JSON: {field: value}" |
296
+ | Contradictory constraints | Test for conflicts before deploying |
297
+ | No examples for complex tasks | Add 2-3 few-shot examples |
298
+ | Testing with one input | Test with at least 3 diverse inputs |
299
+ | Long monolithic prompt | Break into focused sections with headers |
300
+
301
+ ## Quick Reference
302
+
303
+ ```
304
+ Prompt Length Guidelines:
305
+ ──────────────────────────
306
+ Simple instruction → 50-100 tokens
307
+ Structured prompt → 100-500 tokens
308
+ Agent system prompt → 500-2000 tokens
309
+ Complex workflow → 2000-4000 tokens
310
+ (>4000 = consider splitting into sub-prompts)
311
+
312
+ Reliability Ranking (most to least reliable):
313
+ ──────────────────────────────────────────────
314
+ 1. Explicit format + examples (highest)
315
+ 2. Explicit format, no examples
316
+ 3. Implicit format with examples
317
+ 4. Implicit format, no examples (lowest)
318
+ ```