feat(desktop): ✨ 实现一些功能
1. 实现任务暂停功能 2. 实现页面的国际化功能 3.优化项目的结构以及BUG 4. 优化系统架构 5. 实现一大堆的功能
This commit is contained in:
50
.trae/skills/debugging/README.md
Normal file
50
.trae/skills/debugging/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# debugging
|
||||
|
||||
Comprehensive debugging specialist for errors, test failures, log analysis, and system problems. Use when encountering issues, analyzing error logs, investigating system anomalies, debugging production issues, analyzing stack traces, or identifying root causes. Combines general debugging workflows with error pattern detection and log analysis.
|
||||
|
||||
---
|
||||
|
||||
## 📦 Downloaded from [Skillstore.io](https://skillstore.io)
|
||||
|
||||
This skill was downloaded from **AI Skillstore** — the official marketplace for Claude Code, Codex, and Claude skills.
|
||||
|
||||
🔗 **Skill Page**: [skillstore.io/skills/x-89jobrien-debugging](https://skillstore.io/skills/x-89jobrien-debugging)
|
||||
|
||||
## 🚀 Installation
|
||||
|
||||
### Via Claude Code Plugin System
|
||||
|
||||
```
|
||||
/plugin marketplace add aiskillstore/marketplace
|
||||
/plugin install x-89jobrien-debugging@aiskillstore
|
||||
```
|
||||
|
||||
### Manual Installation
|
||||
|
||||
Copy the contents of this folder to your project's `.claude/skills/` directory.
|
||||
|
||||
## 📋 Skill Info
|
||||
|
||||
| Property | Value |
|
||||
|----------|-------|
|
||||
| **Name** | debugging |
|
||||
| **Version** | 1.0.1 |
|
||||
| **Author** | Joseph OBrien |
|
||||
|
||||
### Supported Tools
|
||||
|
||||
- claude
|
||||
- codex
|
||||
- claude-code
|
||||
|
||||
## 🌐 Discover More Skills
|
||||
|
||||
Browse thousands of AI skills at **[skillstore.io](https://skillstore.io)**:
|
||||
|
||||
- 🔍 Search by category, tool, or keyword
|
||||
- ⭐ Find verified, security-audited skills
|
||||
- 📤 Submit your own skills to share with the community
|
||||
|
||||
---
|
||||
|
||||
*From [skillstore.io](https://skillstore.io) — AI Skills Marketplace*
|
||||
553
.trae/skills/debugging/SKILL.md
Normal file
553
.trae/skills/debugging/SKILL.md
Normal file
@@ -0,0 +1,553 @@
|
||||
---
|
||||
name: debugging
|
||||
description: Comprehensive debugging specialist for errors, test failures, log analysis,
|
||||
---
|
||||
|
||||
# Debugging
|
||||
|
||||
This skill provides comprehensive debugging capabilities for identifying and fixing errors, test failures, unexpected behavior, and production issues. It combines general debugging workflows with specialized error analysis, log parsing, and pattern recognition.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
- When encountering errors or exceptions in code
|
||||
- When tests are failing and you need to understand why
|
||||
- When investigating unexpected behavior or bugs
|
||||
- When analyzing stack traces and error messages
|
||||
- When debugging production issues
|
||||
- When fixing issues reported by users or QA
|
||||
- When analyzing error logs and stack traces
|
||||
- When investigating performance issues or anomalies
|
||||
- When correlating errors across multiple services
|
||||
- When identifying recurring error patterns
|
||||
- When setting up error monitoring and alerting
|
||||
- When conducting post-mortem analysis of incidents
|
||||
|
||||
## What This Skill Does
|
||||
|
||||
1. **Error Analysis**: Captures and analyzes error messages and stack traces
|
||||
2. **Log Parsing**: Extracts errors from logs using regex patterns and structured parsing
|
||||
3. **Stack Trace Analysis**: Analyzes stack traces across multiple programming languages
|
||||
4. **Error Correlation**: Identifies relationships between errors across distributed systems
|
||||
5. **Pattern Recognition**: Detects common error patterns and anti-patterns
|
||||
6. **Reproduction**: Identifies steps to reproduce the issue
|
||||
7. **Isolation**: Locates the exact failure point in code
|
||||
8. **Root Cause Analysis**: Works backward from symptoms to identify underlying causes
|
||||
9. **Minimal Fix**: Implements the smallest change that resolves the issue
|
||||
10. **Verification**: Confirms the solution works and doesn't introduce new issues
|
||||
11. **Monitoring Setup**: Creates queries and alerts for error detection
|
||||
|
||||
## Helper Scripts
|
||||
|
||||
This skill includes Python helper scripts in `scripts/`:
|
||||
|
||||
- **`parse_logs.py`**: Parses log files and extracts errors, exceptions, and stack traces. Outputs JSON with error analysis and pattern detection.
|
||||
|
||||
```bash
|
||||
python scripts/parse_logs.py /var/log/app.log
|
||||
```
|
||||
|
||||
## How to Use
|
||||
|
||||
### Debug an Error
|
||||
|
||||
```
|
||||
Debug this error: TypeError: Cannot read property 'x' of undefined
|
||||
```
|
||||
|
||||
```
|
||||
Investigate why the test is failing in test_user_service.js
|
||||
```
|
||||
|
||||
### Analyze Error Logs
|
||||
|
||||
```
|
||||
Analyze the error logs in /var/log/app.log and identify the root cause
|
||||
```
|
||||
|
||||
```
|
||||
Investigate why the API is returning 500 errors
|
||||
```
|
||||
|
||||
### Pattern Detection
|
||||
|
||||
```
|
||||
Find patterns in these error logs from the past 24 hours
|
||||
```
|
||||
|
||||
```
|
||||
Correlate errors between the API service and database
|
||||
```
|
||||
|
||||
## Debugging Process
|
||||
|
||||
### 1. Capture Error Information
|
||||
|
||||
**Error Message:**
|
||||
|
||||
- Read the full error message
|
||||
- Note the error type (TypeError, ReferenceError, etc.)
|
||||
- Identify the error location (file and line number)
|
||||
|
||||
**Stack Trace:**
|
||||
|
||||
- Analyze the call stack
|
||||
- Identify the sequence of function calls
|
||||
- Find where the error originated
|
||||
|
||||
**Context:**
|
||||
|
||||
- Check recent code changes
|
||||
- Review related code files
|
||||
- Understand the execution flow
|
||||
|
||||
### 2. Error Extraction (Log Analysis)
|
||||
|
||||
**Using Helper Script:**
|
||||
|
||||
The skill includes a Python helper script for parsing logs:
|
||||
|
||||
```bash
|
||||
# Parse log file and extract errors
|
||||
python scripts/parse_logs.py /var/log/app.log
|
||||
```
|
||||
|
||||
**Manual Log Parsing Patterns:**
|
||||
|
||||
```bash
|
||||
# Extract errors from logs
|
||||
grep -i "error\|exception\|fatal\|critical" /var/log/app.log
|
||||
|
||||
# Extract stack traces
|
||||
grep -A 20 "Exception\|Error\|Traceback" /var/log/app.log
|
||||
|
||||
# Extract specific error types
|
||||
grep "TypeError\|ReferenceError\|SyntaxError" /var/log/app.log
|
||||
```
|
||||
|
||||
**Structured Log Parsing:**
|
||||
|
||||
```javascript
|
||||
// Parse JSON logs
|
||||
const errors = logs
|
||||
.filter(log => log.level === 'error' || log.level === 'critical')
|
||||
.map(log => ({
|
||||
timestamp: log.timestamp,
|
||||
message: log.message,
|
||||
stack: log.stack,
|
||||
context: log.context
|
||||
}));
|
||||
```
|
||||
|
||||
### 3. Stack Trace Analysis
|
||||
|
||||
**Common Patterns:**
|
||||
|
||||
**JavaScript/Node.js:**
|
||||
|
||||
```
|
||||
Error: Cannot read property 'x' of undefined
|
||||
at FunctionName (file.js:123:45)
|
||||
at AnotherFunction (file.js:456:78)
|
||||
```
|
||||
|
||||
**Python:**
|
||||
|
||||
```
|
||||
Traceback (most recent call last):
|
||||
File "app.py", line 123, in function_name
|
||||
result = process(data)
|
||||
File "utils.py", line 45, in process
|
||||
return data['key']
|
||||
KeyError: 'key'
|
||||
```
|
||||
|
||||
**Java:**
|
||||
|
||||
```
|
||||
java.lang.NullPointerException
|
||||
at com.example.Class.method(Class.java:123)
|
||||
at com.example.AnotherClass.call(AnotherClass.java:456)
|
||||
```
|
||||
|
||||
### 4. Error Correlation
|
||||
|
||||
**Timeline Analysis:**
|
||||
|
||||
- Group errors by timestamp
|
||||
- Identify error spikes and patterns
|
||||
- Correlate with deployments or changes
|
||||
- Check for cascading failures
|
||||
|
||||
**Service Correlation:**
|
||||
|
||||
- Map errors across service boundaries
|
||||
- Identify upstream/downstream relationships
|
||||
- Track error propagation paths
|
||||
- Find common failure points
|
||||
|
||||
### 5. Pattern Recognition
|
||||
|
||||
**Common Error Patterns:**
|
||||
|
||||
**N+1 Query Problem:**
|
||||
|
||||
```
|
||||
Multiple database queries in loop
|
||||
Pattern: SELECT * FROM users; SELECT * FROM posts WHERE user_id = ?
|
||||
```
|
||||
|
||||
**Memory Leaks:**
|
||||
|
||||
```
|
||||
Gradually increasing memory usage
|
||||
Pattern: Memory growth over time without release
|
||||
```
|
||||
|
||||
**Race Conditions:**
|
||||
|
||||
```
|
||||
Intermittent failures under load
|
||||
Pattern: Errors only occur with concurrent requests
|
||||
```
|
||||
|
||||
**Timeout Issues:**
|
||||
|
||||
```
|
||||
Requests timing out
|
||||
Pattern: Errors after specific duration (e.g., 30s)
|
||||
```
|
||||
|
||||
### 6. Reproduce the Issue
|
||||
|
||||
**Reproduction Steps:**
|
||||
|
||||
1. Identify the exact conditions that trigger the error
|
||||
2. Create a minimal test case that reproduces the issue
|
||||
3. Verify the issue is consistent and reproducible
|
||||
4. Document the steps clearly
|
||||
|
||||
**Example:**
|
||||
|
||||
```markdown
|
||||
## Reproduction Steps
|
||||
|
||||
1. Navigate to `/users/123`
|
||||
2. Click "Edit Profile"
|
||||
3. Submit form without filling required fields
|
||||
4. Error occurs: "Cannot read property 'validate' of undefined"
|
||||
```
|
||||
|
||||
### 7. Isolate the Failure Location
|
||||
|
||||
**Code Analysis:**
|
||||
|
||||
- Read the code around the error location
|
||||
- Trace the execution path
|
||||
- Identify where the assumption breaks
|
||||
- Check variable states and values
|
||||
|
||||
**Debugging Techniques:**
|
||||
|
||||
- Add strategic logging to track execution
|
||||
- Use debugger breakpoints
|
||||
- Inspect variable states
|
||||
- Check function return values
|
||||
- Verify data structures
|
||||
|
||||
### 8. Form and Test Hypotheses
|
||||
|
||||
**Hypothesis Formation:**
|
||||
|
||||
- What could cause this error?
|
||||
- What assumptions might be wrong?
|
||||
- What edge cases weren't considered?
|
||||
- What dependencies might be missing?
|
||||
|
||||
**Testing Hypotheses:**
|
||||
|
||||
- Add logging to verify assumptions
|
||||
- Test edge cases
|
||||
- Check input validation
|
||||
- Verify dependencies are available
|
||||
- Test with different data
|
||||
|
||||
### 9. Root Cause Analysis
|
||||
|
||||
**Investigation Steps:**
|
||||
|
||||
1. **Start with Symptoms**: What error is occurring?
|
||||
2. **Work Backward**: What changed before the error?
|
||||
3. **Check Patterns**: Is this recurring or isolated?
|
||||
4. **Correlate Events**: What else happened at the same time?
|
||||
5. **Identify Cause**: What is the underlying issue?
|
||||
|
||||
**Analysis Framework:**
|
||||
|
||||
```markdown
|
||||
## Error Analysis
|
||||
|
||||
**Error**: [Description]
|
||||
**Frequency**: [How often]
|
||||
**Timeline**: [When it started]
|
||||
**Affected Services**: [Which services]
|
||||
**User Impact**: [How many users affected]
|
||||
|
||||
**Root Cause Hypothesis**:
|
||||
- [Primary hypothesis with evidence]
|
||||
- [Alternative hypotheses]
|
||||
|
||||
**Evidence**:
|
||||
- [Log entries supporting hypothesis]
|
||||
- [Error patterns observed]
|
||||
- [Correlation with other events]
|
||||
|
||||
**Recommended Actions**:
|
||||
- [Immediate fix]
|
||||
- [Long-term prevention]
|
||||
```
|
||||
|
||||
### 10. Implement Minimal Fix
|
||||
|
||||
**Fix Principles:**
|
||||
|
||||
- Fix the root cause, not just symptoms
|
||||
- Make the smallest change possible
|
||||
- Preserve existing functionality
|
||||
- Don't introduce new complexity
|
||||
- Add appropriate error handling
|
||||
|
||||
**Fix Verification:**
|
||||
|
||||
- Test the fix with the reproduction case
|
||||
- Verify no regressions
|
||||
- Check edge cases
|
||||
- Ensure error handling is appropriate
|
||||
- Confirm the fix is complete
|
||||
|
||||
### 11. Document the Solution
|
||||
|
||||
**Documentation Should Include:**
|
||||
|
||||
- Root cause explanation
|
||||
- Evidence supporting the diagnosis
|
||||
- Specific code fix
|
||||
- Testing approach
|
||||
- Prevention recommendations
|
||||
|
||||
## Examples
|
||||
|
||||
### Example 1: Null Reference Error
|
||||
|
||||
**Input**: Error: `TypeError: Cannot read property 'name' of undefined`
|
||||
|
||||
**Output**:
|
||||
|
||||
```markdown
|
||||
## Debugging Analysis
|
||||
|
||||
### Error Information
|
||||
- **Type**: TypeError
|
||||
- **Location**: `src/services/user.js:45`
|
||||
- **Message**: Cannot read property 'name' of undefined
|
||||
|
||||
### Root Cause
|
||||
The `getUser()` function returns `undefined` when a user is not found, but the code assumes it always returns a user object.
|
||||
|
||||
### Code Location
|
||||
```javascript
|
||||
// src/services/user.js:45
|
||||
const user = getUser(userId);
|
||||
console.log(user.name); // ❌ Error: user is undefined
|
||||
```
|
||||
|
||||
### Fix
|
||||
|
||||
```javascript
|
||||
// Add null check
|
||||
const user = getUser(userId);
|
||||
if (!user) {
|
||||
throw new Error(`User ${userId} not found`);
|
||||
}
|
||||
console.log(user.name); // ✅ Safe
|
||||
```
|
||||
|
||||
### Prevention
|
||||
|
||||
- Always validate return values from functions that might return null/undefined
|
||||
- Use optional chaining: `user?.name`
|
||||
- Add TypeScript types to catch these issues at compile time
|
||||
|
||||
```
|
||||
|
||||
### Example 2: Database Connection Errors (Log Analysis)
|
||||
|
||||
**Input**: Error logs showing database connection failures
|
||||
|
||||
**Output**:
|
||||
|
||||
```markdown
|
||||
## Error Analysis: Database Connection Failures
|
||||
|
||||
### Pattern Detected
|
||||
|
||||
**Error**: `ECONNREFUSED` errors from PostgreSQL
|
||||
**Frequency**: 15 errors/minute, started 2 hours ago
|
||||
**Timeline**: Errors began after deployment at 14:30
|
||||
|
||||
### Root Cause Analysis
|
||||
|
||||
**Hypothesis**: Connection pool exhaustion
|
||||
|
||||
**Evidence**:
|
||||
- Errors correlate with high traffic period (14:30-16:30)
|
||||
- Connection pool size: 10, concurrent requests: 50+
|
||||
- No connection cleanup in error handlers
|
||||
- Errors spike during peak usage
|
||||
|
||||
**Code Location**: `src/db/connection.js:45`
|
||||
|
||||
**Fix**:
|
||||
```javascript
|
||||
// Add connection cleanup
|
||||
try {
|
||||
const result = await query(sql);
|
||||
return result;
|
||||
} catch (error) {
|
||||
// Ensure connection is released
|
||||
await releaseConnection();
|
||||
throw error;
|
||||
}
|
||||
```
|
||||
|
||||
**Monitoring Query**:
|
||||
|
||||
```sql
|
||||
SELECT count(*) FROM pg_stat_activity WHERE state = 'active';
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
## Reference Files
|
||||
|
||||
For detailed debugging workflows, error patterns, and techniques, load reference files as needed:
|
||||
|
||||
- **`references/debugging_workflows.md`** - Common debugging workflows by issue type, language-specific debugging, debugging techniques, debugging checklists, and common error patterns (database errors, memory leaks, race conditions, timeouts, authentication errors, network errors, application errors, performance errors)
|
||||
- **`references/INCIDENT_POSTMORTEM.template.md`** - Incident postmortem template with timeline, root cause analysis, and action items
|
||||
|
||||
When debugging specific types of issues or analyzing error patterns, load `references/debugging_workflows.md` and refer to the relevant section.
|
||||
|
||||
## Best Practices
|
||||
|
||||
### Debugging Approach
|
||||
|
||||
1. **Start with Symptoms**: Understand what's wrong before jumping to solutions
|
||||
2. **Work Backward**: Trace from error to cause
|
||||
3. **Test Hypotheses**: Don't assume, verify
|
||||
4. **Minimal Changes**: Fix only what's necessary
|
||||
5. **Verify Fixes**: Always test that the fix works
|
||||
|
||||
### Log Analysis Techniques
|
||||
|
||||
1. **Use Structured Logging**: JSON logs are easier to parse and analyze
|
||||
2. **Include Context**: Add request IDs, user IDs, timestamps to all logs
|
||||
3. **Log Levels**: Use appropriate levels (error, warn, info, debug)
|
||||
4. **Correlation IDs**: Use request IDs to trace errors across services
|
||||
5. **Error Grouping**: Group similar errors to identify patterns
|
||||
|
||||
### Error Pattern Recognition
|
||||
|
||||
**Time-Based Patterns:**
|
||||
- Errors at specific times (deployment windows, peak hours)
|
||||
- Errors after specific duration (timeouts, memory leaks)
|
||||
- Errors during specific events (database migrations, cache clears)
|
||||
|
||||
**Frequency Patterns:**
|
||||
- Sudden spikes (deployment issues, traffic spikes)
|
||||
- Gradual increases (memory leaks, resource exhaustion)
|
||||
- Intermittent (race conditions, timing issues)
|
||||
|
||||
**Correlation Patterns:**
|
||||
- Errors in multiple services simultaneously (infrastructure issues)
|
||||
- Errors after specific user actions (application bugs)
|
||||
- Errors correlated with external services (dependency issues)
|
||||
|
||||
### Common Debugging Patterns
|
||||
|
||||
**Null/Undefined Checks:**
|
||||
```javascript
|
||||
// Always check for null/undefined
|
||||
if (!value) {
|
||||
// Handle missing value
|
||||
}
|
||||
```
|
||||
|
||||
**Error Handling:**
|
||||
|
||||
```javascript
|
||||
try {
|
||||
// Risky operation
|
||||
} catch (error) {
|
||||
// Log error with context
|
||||
console.error('Operation failed:', error);
|
||||
// Handle gracefully
|
||||
}
|
||||
```
|
||||
|
||||
**Logging:**
|
||||
|
||||
```javascript
|
||||
// Strategic logging
|
||||
console.log('Before operation:', { userId, data });
|
||||
const result = await operation();
|
||||
console.log('After operation:', { result });
|
||||
```
|
||||
|
||||
**Type Checking:**
|
||||
|
||||
```javascript
|
||||
// Verify types
|
||||
if (typeof value !== 'string') {
|
||||
throw new TypeError('Expected string');
|
||||
}
|
||||
```
|
||||
|
||||
### Monitoring Setup
|
||||
|
||||
**Error Rate Monitoring:**
|
||||
|
||||
```javascript
|
||||
// Track error rate over time
|
||||
const errorRate = errors.length / totalRequests;
|
||||
if (errorRate > 0.01) { // 1% error rate threshold
|
||||
alert('High error rate detected');
|
||||
}
|
||||
```
|
||||
|
||||
**Error Alerting:**
|
||||
|
||||
- Alert on error rate spikes (> 5% increase)
|
||||
- Alert on new error types
|
||||
- Alert on critical error patterns
|
||||
- Alert on error correlation across services
|
||||
|
||||
### Prevention Strategies
|
||||
|
||||
1. **Input Validation**: Validate all inputs at boundaries
|
||||
2. **Type Safety**: Use TypeScript or type checking
|
||||
3. **Error Boundaries**: Catch errors at appropriate levels
|
||||
4. **Testing**: Write tests for edge cases
|
||||
5. **Code Review**: Review code for common pitfalls
|
||||
|
||||
## Related Use Cases
|
||||
|
||||
- Fixing production bugs
|
||||
- Debugging test failures
|
||||
- Investigating user-reported issues
|
||||
- Analyzing error logs
|
||||
- Root cause analysis
|
||||
- Performance debugging
|
||||
- Production incident investigation
|
||||
- System reliability analysis
|
||||
- Error monitoring setup
|
||||
- Post-mortem analysis
|
||||
- Debugging distributed systems
|
||||
617
.trae/skills/debugging/references/debugging-workflows.md
Normal file
617
.trae/skills/debugging/references/debugging-workflows.md
Normal file
@@ -0,0 +1,617 @@
|
||||
---
|
||||
author: Joseph OBrien
|
||||
status: unpublished
|
||||
updated: '2025-12-23'
|
||||
version: 1.0.1
|
||||
tag: skill
|
||||
type: reference
|
||||
parent: debugging
|
||||
---
|
||||
|
||||
# Debugging Workflows
|
||||
|
||||
Reference guide for common debugging workflows and techniques across different scenarios.
|
||||
|
||||
## Debugging Workflows by Issue Type
|
||||
|
||||
### Production Errors
|
||||
|
||||
**Workflow:**
|
||||
|
||||
1. Capture error message and stack trace
|
||||
2. Check error logs for context
|
||||
3. Identify when error started (deployment, traffic spike, etc.)
|
||||
4. Reproduce in staging environment
|
||||
5. Add logging to trace execution path
|
||||
6. Identify root cause
|
||||
7. Implement fix with tests
|
||||
8. Deploy and monitor
|
||||
|
||||
**Tools:**
|
||||
|
||||
- Error tracking (Sentry, Rollbar)
|
||||
- Log aggregation (ELK, Datadog)
|
||||
- APM tools (New Relic, AppDynamics)
|
||||
|
||||
### Test Failures
|
||||
|
||||
**Workflow:**
|
||||
|
||||
1. Read test failure message
|
||||
2. Understand what the test expects
|
||||
3. Run test in isolation
|
||||
4. Check test data and setup
|
||||
5. Trace through code execution
|
||||
6. Identify why test fails
|
||||
7. Fix code or test as appropriate
|
||||
8. Verify test passes
|
||||
|
||||
**Tools:**
|
||||
|
||||
- Test runner debug mode
|
||||
- IDE debugger
|
||||
- Test coverage tools
|
||||
|
||||
### Performance Issues
|
||||
|
||||
**Workflow:**
|
||||
|
||||
1. Measure current performance
|
||||
2. Identify slow operations
|
||||
3. Profile to find bottlenecks
|
||||
4. Analyze profiling data
|
||||
5. Optimize identified bottlenecks
|
||||
6. Measure improvement
|
||||
7. Verify no regressions
|
||||
|
||||
**Tools:**
|
||||
|
||||
- Profilers (Chrome DevTools, py-spy)
|
||||
- APM tools
|
||||
- Performance monitoring
|
||||
|
||||
## Language-Specific Debugging
|
||||
|
||||
### JavaScript/Node.js
|
||||
|
||||
**Debugging Tools:**
|
||||
|
||||
- Chrome DevTools
|
||||
- Node.js debugger
|
||||
- console.log (strategic logging)
|
||||
- debugger statement
|
||||
|
||||
**Common Issues:**
|
||||
|
||||
- Undefined variables
|
||||
- Async/await errors
|
||||
- Promise rejections
|
||||
- Scope issues
|
||||
- Type coercion
|
||||
|
||||
**Techniques:**
|
||||
|
||||
- Use debugger breakpoints
|
||||
- Log variable states
|
||||
- Check call stack
|
||||
- Inspect closures
|
||||
- Monitor event loop
|
||||
|
||||
### Python
|
||||
|
||||
**Debugging Tools:**
|
||||
|
||||
- pdb (Python debugger)
|
||||
- ipdb (enhanced debugger)
|
||||
- print() statements
|
||||
- logging module
|
||||
|
||||
**Common Issues:**
|
||||
|
||||
- AttributeError
|
||||
- TypeError
|
||||
- IndentationError
|
||||
- Import errors
|
||||
- NameError
|
||||
|
||||
**Techniques:**
|
||||
|
||||
- Use pdb.set_trace()
|
||||
- Check variable types
|
||||
- Verify imports
|
||||
- Check indentation
|
||||
- Use type hints
|
||||
|
||||
### Java
|
||||
|
||||
**Debugging Tools:**
|
||||
|
||||
- IntelliJ debugger
|
||||
- Eclipse debugger
|
||||
- jdb (command line)
|
||||
- Logging frameworks
|
||||
|
||||
**Common Issues:**
|
||||
|
||||
- NullPointerException
|
||||
- ClassCastException
|
||||
- OutOfMemoryError
|
||||
- StackOverflowError
|
||||
|
||||
**Techniques:**
|
||||
|
||||
- Set breakpoints
|
||||
- Inspect variables
|
||||
- Check exception stack traces
|
||||
- Monitor memory usage
|
||||
- Use profilers
|
||||
|
||||
## Debugging Techniques
|
||||
|
||||
### Binary Search
|
||||
|
||||
**When to Use:**
|
||||
|
||||
- Large codebase
|
||||
- Unclear where issue is
|
||||
- Many potential causes
|
||||
|
||||
**Process:**
|
||||
|
||||
1. Divide code in half
|
||||
2. Test which half has issue
|
||||
3. Repeat on problematic half
|
||||
4. Narrow down to specific location
|
||||
|
||||
### Rubber Duck Debugging
|
||||
|
||||
**Process:**
|
||||
|
||||
1. Explain code to "rubber duck" (or yourself)
|
||||
2. Walk through execution step by step
|
||||
3. Identify assumptions
|
||||
4. Find where logic breaks
|
||||
|
||||
### Logging Strategy
|
||||
|
||||
**What to Log:**
|
||||
|
||||
- Function entry/exit
|
||||
- Variable values at key points
|
||||
- Decision points (if/else branches)
|
||||
- Error conditions
|
||||
- Performance metrics
|
||||
|
||||
**Log Levels:**
|
||||
|
||||
- DEBUG: Detailed diagnostic info
|
||||
- INFO: General informational messages
|
||||
- WARN: Warning messages
|
||||
- ERROR: Error conditions
|
||||
- CRITICAL: Critical failures
|
||||
|
||||
### Reproducing Issues
|
||||
|
||||
**Steps:**
|
||||
|
||||
1. Identify exact conditions that trigger issue
|
||||
2. Document steps to reproduce
|
||||
3. Create minimal test case
|
||||
4. Verify issue reproduces consistently
|
||||
5. Isolate variables
|
||||
|
||||
**Common Challenges:**
|
||||
|
||||
- Intermittent issues
|
||||
- Race conditions
|
||||
- Environment-specific
|
||||
- Data-dependent
|
||||
|
||||
## Debugging Checklist
|
||||
|
||||
### Before Starting
|
||||
|
||||
- [ ] Understand what should happen
|
||||
- [ ] Understand what's actually happening
|
||||
- [ ] Have reproduction steps
|
||||
- [ ] Have access to logs/debugger
|
||||
|
||||
### During Debugging
|
||||
|
||||
- [ ] Form hypotheses
|
||||
- [ ] Test hypotheses systematically
|
||||
- [ ] Document findings
|
||||
- [ ] Check assumptions
|
||||
- [ ] Look for patterns
|
||||
|
||||
### After Finding Root Cause
|
||||
|
||||
- [ ] Verify root cause is correct
|
||||
- [ ] Understand why it happened
|
||||
- [ ] Implement fix
|
||||
- [ ] Test fix thoroughly
|
||||
- [ ] Check for similar issues
|
||||
- [ ] Document solution
|
||||
|
||||
## Common Error Patterns
|
||||
|
||||
Reference guide for identifying and resolving common error patterns across different systems and languages.
|
||||
|
||||
### Database Errors
|
||||
|
||||
#### Connection Pool Exhaustion
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- `ECONNREFUSED` errors
|
||||
- Errors spike during high traffic
|
||||
- Connection pool size is smaller than concurrent requests
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
Error: ECONNREFUSED
|
||||
Connection pool exhausted
|
||||
Too many connections
|
||||
```
|
||||
|
||||
**Root Causes:**
|
||||
|
||||
- Connection pool size too small
|
||||
- Connections not being released
|
||||
- Long-running transactions holding connections
|
||||
- Missing connection cleanup in error handlers
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Increase connection pool size
|
||||
- Ensure connections are released in finally blocks
|
||||
- Add connection timeout
|
||||
- Implement connection retry logic
|
||||
|
||||
#### N+1 Query Problem
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Slow response times
|
||||
- Many database queries for single operation
|
||||
- Queries increase linearly with data size
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
SELECT * FROM users;
|
||||
SELECT * FROM posts WHERE user_id = 1;
|
||||
SELECT * FROM posts WHERE user_id = 2;
|
||||
SELECT * FROM posts WHERE user_id = 3;
|
||||
...
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Use eager loading (JOINs)
|
||||
- Batch queries
|
||||
- Use data loaders
|
||||
- Implement query result caching
|
||||
|
||||
### Memory Leaks
|
||||
|
||||
#### Event Listener Leaks
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Memory usage grows over time
|
||||
- No decrease after component/page unload
|
||||
- Correlates with user interactions
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```javascript
|
||||
// Problem: Listeners registered but never removed
|
||||
window.addEventListener('resize', handler);
|
||||
// Missing: window.removeEventListener('resize', handler);
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Always remove event listeners
|
||||
- Use cleanup functions in React useEffect
|
||||
- Use WeakMap for automatic cleanup
|
||||
- Monitor listener count
|
||||
|
||||
#### Closure Leaks
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Memory growth in long-running applications
|
||||
- Large objects retained in closures
|
||||
- Circular references
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```javascript
|
||||
// Problem: Large object retained in closure
|
||||
function createHandler(largeData) {
|
||||
return function() {
|
||||
// largeData retained even if not used
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Avoid retaining large objects in closures
|
||||
- Use WeakMap/WeakSet when possible
|
||||
- Clear references when done
|
||||
- Use memory profilers to identify leaks
|
||||
|
||||
### Race Conditions
|
||||
|
||||
#### Concurrent Modification
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Intermittent failures
|
||||
- Data inconsistency
|
||||
- Errors only under load
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
Thread 1: Read value (100)
|
||||
Thread 2: Read value (100)
|
||||
Thread 1: Write value (101)
|
||||
Thread 2: Write value (101) // Lost update
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Use locks/mutexes
|
||||
- Implement optimistic locking
|
||||
- Use atomic operations
|
||||
- Add request queuing
|
||||
|
||||
#### Async Race Conditions
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Results arrive out of order
|
||||
- Stale data displayed
|
||||
- Race between multiple async operations
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```javascript
|
||||
// Problem: Race between requests
|
||||
fetch('/api/users/1').then(setUser1);
|
||||
fetch('/api/users/2').then(setUser2);
|
||||
// Results may arrive in wrong order
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Use Promise.all for parallel operations
|
||||
- Cancel previous requests
|
||||
- Use request IDs to match responses
|
||||
- Implement request deduplication
|
||||
|
||||
### Timeout Issues
|
||||
|
||||
#### Request Timeouts
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Requests fail after specific duration
|
||||
- Timeout errors in logs
|
||||
- Slow external dependencies
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
Error: Request timeout after 30000ms
|
||||
ETIMEDOUT
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Increase timeout for slow operations
|
||||
- Implement retry with exponential backoff
|
||||
- Add timeout configuration
|
||||
- Optimize slow operations
|
||||
|
||||
#### Database Query Timeouts
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Queries fail after timeout period
|
||||
- Slow query logs show long-running queries
|
||||
- Timeouts during peak load
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Optimize slow queries
|
||||
- Add appropriate indexes
|
||||
- Increase query timeout
|
||||
- Implement query cancellation
|
||||
|
||||
### Authentication Errors
|
||||
|
||||
#### Token Expiration
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- 401 Unauthorized errors
|
||||
- Errors after specific time period
|
||||
- Token refresh needed
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
401 Unauthorized
|
||||
Token expired
|
||||
Invalid token
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Implement token refresh logic
|
||||
- Handle token expiration gracefully
|
||||
- Add token expiration checks
|
||||
- Use refresh tokens
|
||||
|
||||
#### Session Expiration
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Users logged out unexpectedly
|
||||
- Session errors after inactivity
|
||||
- Cookie expiration issues
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Extend session on activity
|
||||
- Implement session refresh
|
||||
- Handle expiration gracefully
|
||||
- Clear expired sessions
|
||||
|
||||
### Network Errors
|
||||
|
||||
#### Connection Refused
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Service unavailable errors
|
||||
- Connection refused errors
|
||||
- Service not running
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
ECONNREFUSED
|
||||
Connection refused
|
||||
Service unavailable
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Check if service is running
|
||||
- Verify port configuration
|
||||
- Check firewall rules
|
||||
- Implement health checks
|
||||
|
||||
#### DNS Resolution Failures
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Cannot resolve hostname
|
||||
- DNS lookup failures
|
||||
- Network configuration issues
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
ENOTFOUND
|
||||
DNS resolution failed
|
||||
getaddrinfo failed
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Verify DNS configuration
|
||||
- Check hostname spelling
|
||||
- Use IP addresses as fallback
|
||||
- Implement DNS caching
|
||||
|
||||
### Application Errors
|
||||
|
||||
#### Null Reference Errors
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- NullPointerException (Java)
|
||||
- TypeError: Cannot read property (JavaScript)
|
||||
- AttributeError (Python)
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
TypeError: Cannot read property 'x' of undefined
|
||||
NullPointerException
|
||||
AttributeError: 'NoneType' object has no attribute
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Add null checks
|
||||
- Use optional chaining
|
||||
- Provide default values
|
||||
- Validate inputs
|
||||
|
||||
#### Type Errors
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Type mismatch errors
|
||||
- Invalid type errors
|
||||
- Casting failures
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
TypeError: expected string, got number
|
||||
InvalidCastException
|
||||
Type mismatch
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Add type validation
|
||||
- Use type guards
|
||||
- Implement proper type checking
|
||||
- Handle type conversions
|
||||
|
||||
### Performance Errors
|
||||
|
||||
#### Out of Memory
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Application crashes
|
||||
- Memory limit exceeded
|
||||
- Heap out of memory
|
||||
|
||||
**Pattern:**
|
||||
|
||||
```
|
||||
OutOfMemoryError
|
||||
Heap out of memory
|
||||
Memory limit exceeded
|
||||
```
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Increase memory limits
|
||||
- Optimize memory usage
|
||||
- Implement pagination
|
||||
- Use streaming for large data
|
||||
|
||||
#### CPU Exhaustion
|
||||
|
||||
**Symptoms:**
|
||||
|
||||
- Slow response times
|
||||
- High CPU usage
|
||||
- Application freezing
|
||||
|
||||
**Pattern:**
|
||||
|
||||
- High CPU utilization (90%+)
|
||||
- Slow processing
|
||||
- Event loop blocking
|
||||
|
||||
**Solutions:**
|
||||
|
||||
- Optimize algorithms
|
||||
- Use worker threads
|
||||
- Implement caching
|
||||
- Break up long-running tasks
|
||||
@@ -0,0 +1,206 @@
|
||||
---
|
||||
author: Joseph OBrien
|
||||
status: unpublished
|
||||
updated: '2025-12-23'
|
||||
version: 1.0.1
|
||||
tag: skill
|
||||
type: reference
|
||||
parent: debugging
|
||||
---
|
||||
|
||||
# Incident Postmortem: {{INCIDENT_TITLE}}
|
||||
|
||||
**Incident ID:** {{INC-XXXX}}
|
||||
**Date:** {{YYYY-MM-DD}}
|
||||
**Duration:** {{START_TIME}} - {{END_TIME}} ({{DURATION}})
|
||||
**Severity:** {{SEV1|SEV2|SEV3|SEV4}}
|
||||
**Status:** {{RESOLVED|MONITORING}}
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
{{ONE_PARAGRAPH_SUMMARY}}
|
||||
|
||||
### Impact
|
||||
|
||||
| Metric | Value |
|
||||
|--------|-------|
|
||||
| Users Affected | {{N}} |
|
||||
| Revenue Impact | ${{N}} |
|
||||
| Requests Failed | {{N}} |
|
||||
| Error Rate | {{N}}% |
|
||||
| Downtime | {{DURATION}} |
|
||||
|
||||
---
|
||||
|
||||
## Timeline
|
||||
|
||||
| Time (UTC) | Event |
|
||||
|------------|-------|
|
||||
| {{HH:MM}} | {{TRIGGER_EVENT}} |
|
||||
| {{HH:MM}} | Alert fired: {{ALERT_NAME}} |
|
||||
| {{HH:MM}} | On-call paged |
|
||||
| {{HH:MM}} | Investigation started |
|
||||
| {{HH:MM}} | Root cause identified |
|
||||
| {{HH:MM}} | Mitigation applied |
|
||||
| {{HH:MM}} | Service recovered |
|
||||
| {{HH:MM}} | Incident closed |
|
||||
|
||||
---
|
||||
|
||||
## Root Cause
|
||||
|
||||
{{DETAILED_ROOT_CAUSE_ANALYSIS}}
|
||||
|
||||
### Contributing Factors
|
||||
|
||||
1. {{FACTOR_1}}
|
||||
2. {{FACTOR_2}}
|
||||
3. {{FACTOR_3}}
|
||||
|
||||
### What Failed
|
||||
|
||||
- **Detection:** {{HOW_WAS_IT_DETECTED}}
|
||||
- **Prevention:** {{WHY_WASNT_IT_PREVENTED}}
|
||||
- **Response:** {{RESPONSE_GAPS}}
|
||||
|
||||
---
|
||||
|
||||
## Resolution
|
||||
|
||||
### Immediate Actions
|
||||
|
||||
1. {{ACTION_1}}
|
||||
2. {{ACTION_2}}
|
||||
|
||||
### Mitigation Steps
|
||||
|
||||
```bash
|
||||
{{COMMANDS_OR_STEPS_TAKEN}}
|
||||
```
|
||||
|
||||
### Verification
|
||||
|
||||
- [ ] Service health restored
|
||||
- [ ] Error rates normalized
|
||||
- [ ] No recurring alerts
|
||||
|
||||
---
|
||||
|
||||
## Lessons Learned
|
||||
|
||||
### What Went Well
|
||||
|
||||
- {{POSITIVE_1}}
|
||||
- {{POSITIVE_2}}
|
||||
|
||||
### What Went Wrong
|
||||
|
||||
- {{NEGATIVE_1}}
|
||||
- {{NEGATIVE_2}}
|
||||
|
||||
### Where We Got Lucky
|
||||
|
||||
- {{LUCKY_1}}
|
||||
|
||||
---
|
||||
|
||||
## Action Items
|
||||
|
||||
| ID | Action | Owner | Priority | Due Date | Status |
|
||||
|----|--------|-------|----------|----------|--------|
|
||||
| 1 | {{ACTION}} | {{OWNER}} | {{P1-4}} | {{DATE}} | {{STATUS}} |
|
||||
| 2 | {{ACTION}} | {{OWNER}} | {{P1-4}} | {{DATE}} | {{STATUS}} |
|
||||
| 3 | {{ACTION}} | {{OWNER}} | {{P1-4}} | {{DATE}} | {{STATUS}} |
|
||||
|
||||
### Prevention
|
||||
|
||||
- [ ] {{PREVENTIVE_MEASURE_1}}
|
||||
- [ ] {{PREVENTIVE_MEASURE_2}}
|
||||
|
||||
### Detection
|
||||
|
||||
- [ ] {{DETECTION_IMPROVEMENT_1}}
|
||||
- [ ] {{DETECTION_IMPROVEMENT_2}}
|
||||
|
||||
### Response
|
||||
|
||||
- [ ] {{RESPONSE_IMPROVEMENT_1}}
|
||||
- [ ] {{RESPONSE_IMPROVEMENT_2}}
|
||||
|
||||
---
|
||||
|
||||
## Technical Details
|
||||
|
||||
### Affected Systems
|
||||
|
||||
| System | Impact | Recovery |
|
||||
|--------|--------|----------|
|
||||
| {{SYSTEM}} | {{DESCRIPTION}} | {{TIME}} |
|
||||
|
||||
### Metrics During Incident
|
||||
|
||||
| Metric | Normal | During Incident | Peak |
|
||||
|--------|--------|-----------------|------|
|
||||
| Latency (p99) | {{MS}} | {{MS}} | {{MS}} |
|
||||
| Error Rate | {{N}}% | {{N}}% | {{N}}% |
|
||||
| CPU Usage | {{N}}% | {{N}}% | {{N}}% |
|
||||
| Memory | {{N}}GB | {{N}}GB | {{N}}GB |
|
||||
|
||||
### Logs
|
||||
|
||||
```
|
||||
{{RELEVANT_LOG_SNIPPETS}}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Communication
|
||||
|
||||
### Internal
|
||||
|
||||
| Time | Channel | Message |
|
||||
|------|---------|---------|
|
||||
| {{TIME}} | {{SLACK/EMAIL}} | {{SUMMARY}} |
|
||||
|
||||
### External
|
||||
|
||||
| Time | Channel | Audience | Message |
|
||||
|------|---------|----------|---------|
|
||||
| {{TIME}} | Status Page | Customers | {{MESSAGE}} |
|
||||
|
||||
---
|
||||
|
||||
## Related Incidents
|
||||
|
||||
| ID | Date | Similarity |
|
||||
|----|------|------------|
|
||||
| {{INC-XXXX}} | {{DATE}} | {{DESCRIPTION}} |
|
||||
|
||||
---
|
||||
|
||||
## Appendix
|
||||
|
||||
### A. Alert Configuration
|
||||
|
||||
```yaml
|
||||
{{ALERT_CONFIG}}
|
||||
```
|
||||
|
||||
### B. Runbook Updates Needed
|
||||
|
||||
- {{RUNBOOK_UPDATE_1}}
|
||||
- {{RUNBOOK_UPDATE_2}}
|
||||
|
||||
---
|
||||
|
||||
## Quality Checklist
|
||||
|
||||
- [ ] Timeline is complete and accurate
|
||||
- [ ] Root cause clearly identified
|
||||
- [ ] Impact quantified
|
||||
- [ ] Action items have owners and due dates
|
||||
- [ ] Lessons learned documented
|
||||
- [ ] Prevention measures identified
|
||||
- [ ] Related incidents linked
|
||||
124
.trae/skills/debugging/scripts/parse-logs.py
Normal file
124
.trae/skills/debugging/scripts/parse-logs.py
Normal file
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Error log parser for debugging skill.
|
||||
Extracts errors, exceptions, and stack traces from log files.
|
||||
"""
|
||||
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def extract_errors(log_content: str) -> list[dict]:
|
||||
"""Extract errors and exceptions from log content."""
|
||||
errors = []
|
||||
|
||||
patterns = {
|
||||
"error": re.compile(r"(?i)(error|ERROR):\s*(.+?)(?:\n|$)"),
|
||||
"exception": re.compile(r"(?i)(exception|Exception|EXCEPTION):\s*(.+?)(?:\n|$)"),
|
||||
"fatal": re.compile(r"(?i)(fatal|FATAL):\s*(.+?)(?:\n|$)"),
|
||||
"critical": re.compile(r"(?i)(critical|CRITICAL):\s*(.+?)(?:\n|$)"),
|
||||
"traceback": re.compile(r"Traceback \(most recent call last\):(.+?)(?=\n\w|\Z)", re.DOTALL),
|
||||
}
|
||||
|
||||
lines = log_content.split("\n")
|
||||
for i, line in enumerate(lines):
|
||||
for error_type, pattern in patterns.items():
|
||||
match = pattern.search(line)
|
||||
if match:
|
||||
error = {
|
||||
"type": error_type,
|
||||
"message": (match.group(2) if len(match.groups()) > 1 else match.group(0)),
|
||||
"line_number": i + 1,
|
||||
"line_content": line,
|
||||
"timestamp": extract_timestamp(line),
|
||||
}
|
||||
|
||||
if error_type == "traceback":
|
||||
error["stack_trace"] = match.group(1).strip()
|
||||
|
||||
errors.append(error)
|
||||
|
||||
return errors
|
||||
|
||||
|
||||
def extract_timestamp(line: str) -> str | None:
|
||||
"""Extract timestamp from log line if present."""
|
||||
timestamp_patterns = [
|
||||
r"(\d{4}-\d{2}-\d{2}[\sT]\d{2}:\d{2}:\d{2})",
|
||||
r"(\d{2}/\d{2}/\d{4}\s+\d{2}:\d{2}:\d{2})",
|
||||
r"\[(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})\]",
|
||||
]
|
||||
|
||||
for pattern in timestamp_patterns:
|
||||
match = re.search(pattern, line)
|
||||
if match:
|
||||
return match.group(1)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def group_errors_by_type(errors: list[dict]) -> dict[str, list[dict]]:
|
||||
"""Group errors by type."""
|
||||
grouped: dict[str, list[dict]] = {}
|
||||
for error in errors:
|
||||
error_type = error["type"]
|
||||
if error_type not in grouped:
|
||||
grouped[error_type] = []
|
||||
grouped[error_type].append(error)
|
||||
return grouped
|
||||
|
||||
|
||||
def analyze_error_patterns(errors: list[dict]) -> dict:
|
||||
"""Analyze error patterns and provide insights."""
|
||||
if not errors:
|
||||
return {}
|
||||
|
||||
type_counts: dict[str, int] = {}
|
||||
for error in errors:
|
||||
error_type = error["type"]
|
||||
type_counts[error_type] = type_counts.get(error_type, 0) + 1
|
||||
|
||||
message_counts: dict[str, int] = {}
|
||||
for error in errors:
|
||||
message = error.get("message", "")[:100]
|
||||
message_counts[message] = message_counts.get(message, 0) + 1
|
||||
|
||||
most_common = sorted(message_counts.items(), key=lambda x: x[1], reverse=True)[:5]
|
||||
|
||||
return {
|
||||
"total_errors": len(errors),
|
||||
"by_type": type_counts,
|
||||
"most_common_errors": [{"message": msg, "count": count} for msg, count in most_common],
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point."""
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: parse_logs.py <log_file>")
|
||||
sys.exit(1)
|
||||
|
||||
log_file = Path(sys.argv[1])
|
||||
if not log_file.exists():
|
||||
print(f"Error: File not found: {log_file}")
|
||||
sys.exit(1)
|
||||
|
||||
log_content = log_file.read_text()
|
||||
|
||||
errors = extract_errors(log_content)
|
||||
|
||||
analysis = analyze_error_patterns(errors)
|
||||
|
||||
output = {
|
||||
"file": str(log_file),
|
||||
"errors": errors,
|
||||
"analysis": analysis,
|
||||
"grouped_by_type": group_errors_by_type(errors),
|
||||
}
|
||||
|
||||
print(json.dumps(output, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
50
.trae/skills/frontend-design/README.md
Normal file
50
.trae/skills/frontend-design/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# frontend-design
|
||||
|
||||
Creates unique, production-grade frontend interfaces with exceptional design quality. Use when user asks to build web components, pages, materials, posters, or applications (e.g., websites, landing pages, dashboards, React components, HTML/CSS layouts, or styling/beautifying any web UI). Generates creative, polished code and UI designs that avoid mediocre AI aesthetics.
|
||||
|
||||
---
|
||||
|
||||
## 📦 Downloaded from [Skillstore.io](https://skillstore.io)
|
||||
|
||||
This skill was downloaded from **AI Skillstore** — the official marketplace for Claude Code, Codex, and Claude skills.
|
||||
|
||||
🔗 **Skill Page**: [skillstore.io/skills/yyh211-frontend-design](https://skillstore.io/skills/yyh211-frontend-design)
|
||||
|
||||
## 🚀 Installation
|
||||
|
||||
### Via Claude Code Plugin System
|
||||
|
||||
```
|
||||
/plugin marketplace add aiskillstore/marketplace
|
||||
/plugin install yyh211-frontend-design@aiskillstore
|
||||
```
|
||||
|
||||
### Manual Installation
|
||||
|
||||
Copy the contents of this folder to your project's `.claude/skills/` directory.
|
||||
|
||||
## 📋 Skill Info
|
||||
|
||||
| Property | Value |
|
||||
|----------|-------|
|
||||
| **Name** | frontend-design |
|
||||
| **Version** | 1.0.0 |
|
||||
| **Author** | YYH211 |
|
||||
|
||||
### Supported Tools
|
||||
|
||||
- claude
|
||||
- codex
|
||||
- claude-code
|
||||
|
||||
## 🌐 Discover More Skills
|
||||
|
||||
Browse thousands of AI skills at **[skillstore.io](https://skillstore.io)**:
|
||||
|
||||
- 🔍 Search by category, tool, or keyword
|
||||
- ⭐ Find verified, security-audited skills
|
||||
- 📤 Submit your own skills to share with the community
|
||||
|
||||
---
|
||||
|
||||
*From [skillstore.io](https://skillstore.io) — AI Skills Marketplace*
|
||||
491
.trae/skills/frontend-design/SKILL.md
Normal file
491
.trae/skills/frontend-design/SKILL.md
Normal file
@@ -0,0 +1,491 @@
|
||||
---
|
||||
name: frontend-design
|
||||
description: Creates unique, production-grade frontend interfaces with exceptional design quality. Use when user asks to build web components, pages, materials, posters, or applications (e.g., websites, landing pages, dashboards, React components, HTML/CSS layouts, or styling/beautifying any web UI). Generates creative, polished code and UI designs that avoid mediocre AI aesthetics.
|
||||
---
|
||||
|
||||
# Frontend Design Skill
|
||||
|
||||
此技能指导创建独特的生产级前端界面,避免平庸的"AI 粗糙"美学。实现真正可用的代码,并高度关注美学细节和创意选择。
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
使用此技能当用户请求:
|
||||
- 构建 Web 组件、页面或完整应用程序
|
||||
- 创建着陆页、仪表盘或营销页面
|
||||
- 设计 React、Vue 或原生 HTML/CSS 界面
|
||||
- 美化或重新设计现有的 Web UI
|
||||
- 创建海报、素材或视觉设计元素(用于 Web)
|
||||
- 需要高设计品质和独特美学的任何前端项目
|
||||
|
||||
**关键触发词**: Web 组件、页面、应用、网站、着陆页、仪表盘、React 组件、HTML/CSS、UI 设计、美化、前端
|
||||
|
||||
## 核心原则
|
||||
|
||||
在编写代码之前,必须进行深入的设计思考。每个界面都应该是独特的、有意图的、令人难忘的。
|
||||
|
||||
### 设计思维流程
|
||||
|
||||
在实现任何代码之前,回答以下问题:
|
||||
|
||||
#### 1. 目的 (Purpose)
|
||||
- **问题**: 此界面解决什么问题?
|
||||
- **用户**: 谁使用它?在什么情境下使用?
|
||||
- **目标**: 用户需要完成什么任务?
|
||||
|
||||
#### 2. 风格方向 (Style Direction)
|
||||
选择一个**明确且大胆**的美学方向。不要选择"现代简约"这样的通用描述,而是选择极致的风格:
|
||||
|
||||
**风格选项**(但不限于这些):
|
||||
- **极简主义**: 极度克制,大量留白,精准排版
|
||||
- **极致混乱**: 密集布局,重叠元素,视觉冲击
|
||||
- **复古未来主义**: 80年代霓虹色,网格,合成波风格
|
||||
- **有机/自然**: 流动形状,自然色调,柔和曲线
|
||||
- **奢华/精致**: 优雅字体,金色点缀,精细细节
|
||||
- **俏皮/玩具感**: 明亮色彩,圆角,趣味动画
|
||||
- **编辑/杂志风格**: 大胆排版,网格系统,黑白为主
|
||||
- **粗犷/原始**: 单色,硬边,实用主义
|
||||
- **装饰艺术/几何**: 对称图案,几何形状,高对比度
|
||||
- **柔和/粉彩**: 温和色彩,渐变,梦幻感
|
||||
- **工业/实用**: 系统字体,单色,功能优先
|
||||
- **新拟态**: 柔和阴影,浮雕效果,微妙深度
|
||||
- **玻璃态**: 模糊背景,透明度,光感
|
||||
|
||||
**关键**: 选择清晰的概念方向并精准执行。大胆的极致主义和精致的极简主义都有效——关键在于**意图**,而不是强度。
|
||||
|
||||
#### 3. 技术限制 (Constraints)
|
||||
- 使用什么框架?(React, Vue, 原生 HTML/CSS)
|
||||
- 性能要求?(动画复杂度,文件大小)
|
||||
- 可访问性要求?(ARIA 标签,键盘导航,色彩对比度)
|
||||
- 浏览器兼容性?
|
||||
|
||||
#### 4. 差异化 (Differentiation)
|
||||
- **记忆点**: 是什么让它令人难忘?
|
||||
- **独特性**: 用户会记住哪一个细节?
|
||||
- **惊喜**: 哪里会让用户眼前一亮?
|
||||
|
||||
## 前端美学指南
|
||||
|
||||
### 1. 排版 (Typography)
|
||||
|
||||
**原则**: 字体选择是设计的灵魂。
|
||||
|
||||
**Do**:
|
||||
- ✅ 选择**独特且有个性**的字体
|
||||
- ✅ 标题使用引人注目的字体,正文使用易读字体
|
||||
- ✅ 尝试意想不到的字体配对
|
||||
- ✅ 使用字体变体(font-weight, font-style)创造层次
|
||||
- ✅ 精确控制字间距(letter-spacing)和行高(line-height)
|
||||
|
||||
**Don't**:
|
||||
- ❌ 使用通用字体:Arial, Helvetica, Inter, Roboto, 系统字体
|
||||
- ❌ 所有文本使用相同的字体和大小
|
||||
- ❌ 忽略字体加载性能(使用 font-display: swap)
|
||||
|
||||
**推荐字体来源**:
|
||||
- Google Fonts (选择小众、独特的字体)
|
||||
- 自定义字体(如果项目允许)
|
||||
|
||||
**示例字体组合**:
|
||||
```css
|
||||
/* 极简编辑风格 */
|
||||
--font-heading: 'Playfair Display', serif;
|
||||
--font-body: 'Source Sans Pro', sans-serif;
|
||||
|
||||
/* 现代科技风格 */
|
||||
--font-heading: 'Space Mono', monospace;
|
||||
--font-body: 'DM Sans', sans-serif;
|
||||
|
||||
/* 优雅奢华风格 */
|
||||
--font-heading: 'Cormorant Garamond', serif;
|
||||
--font-body: 'Lato', sans-serif;
|
||||
```
|
||||
|
||||
### 2. 颜色与主题 (Color & Theme)
|
||||
|
||||
**原则**: 颜色定义情绪和品牌。
|
||||
|
||||
**Do**:
|
||||
- ✅ 使用 CSS 变量保持一致性
|
||||
- ✅ 主色调 + 鲜明点缀色的组合
|
||||
- ✅ 考虑色彩心理学(蓝色=信任,红色=紧迫,绿色=成功)
|
||||
- ✅ 使用渐变营造深度(但要有品味)
|
||||
- ✅ 保持色彩对比度(WCAG AA 标准:至少 4.5:1)
|
||||
|
||||
**Don't**:
|
||||
- ❌ 俗套配色:白色背景 + 紫色渐变
|
||||
- ❌ 过多颜色(3-5 个主色已足够)
|
||||
- ❌ 忽略可访问性
|
||||
|
||||
**示例主题**:
|
||||
```css
|
||||
:root {
|
||||
/* 极简黑白 */
|
||||
--color-primary: #000000;
|
||||
--color-secondary: #ffffff;
|
||||
--color-accent: #ff3366;
|
||||
|
||||
/* 复古未来 */
|
||||
--color-primary: #1a1a2e;
|
||||
--color-secondary: #16213e;
|
||||
--color-accent: #00fff5;
|
||||
--color-highlight: #ff006e;
|
||||
|
||||
/* 自然有机 */
|
||||
--color-primary: #2d6a4f;
|
||||
--color-secondary: #52b788;
|
||||
--color-accent: #ffc857;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 动效 (Animation & Motion)
|
||||
|
||||
**原则**: 动画应该增强体验,而不是分散注意力。
|
||||
|
||||
**Do**:
|
||||
- ✅ 优先使用 CSS 动画(性能更好)
|
||||
- ✅ 设计页面加载动画(首次印象)
|
||||
- ✅ 使用 `animation-delay` 实现元素逐个显示
|
||||
- ✅ 悬停状态添加微妙过渡
|
||||
- ✅ 滚动触发动画(Intersection Observer)
|
||||
- ✅ 对于 React,使用 Framer Motion 或 React Spring
|
||||
|
||||
**Don't**:
|
||||
- ❌ 过度使用动画(每个元素都在动)
|
||||
- ❌ 动画持续时间过长(> 500ms 会让人不耐烦)
|
||||
- ❌ 忽略 `prefers-reduced-motion` 媒体查询
|
||||
|
||||
**示例动画**:
|
||||
```css
|
||||
/* 页面加载 - 元素逐个淡入 */
|
||||
.fade-in-up {
|
||||
animation: fadeInUp 0.6s ease-out forwards;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.fade-in-up:nth-child(1) { animation-delay: 0.1s; }
|
||||
.fade-in-up:nth-child(2) { animation-delay: 0.2s; }
|
||||
.fade-in-up:nth-child(3) { animation-delay: 0.3s; }
|
||||
|
||||
@keyframes fadeInUp {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(30px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* 悬停效果 */
|
||||
.card {
|
||||
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
||||
}
|
||||
|
||||
.card:hover {
|
||||
transform: translateY(-8px);
|
||||
box-shadow: 0 20px 40px rgba(0,0,0,0.15);
|
||||
}
|
||||
```
|
||||
|
||||
### 4. 空间构成 (Spatial Composition)
|
||||
|
||||
**原则**: 布局应该引导视线,创造视觉节奏。
|
||||
|
||||
**Do**:
|
||||
- ✅ 尝试不对称布局
|
||||
- ✅ 使用重叠元素创造深度
|
||||
- ✅ 对角线流程引导视线
|
||||
- ✅ 打破网格的元素(但有意图)
|
||||
- ✅ 宽敞的留白或精心控制的密度
|
||||
- ✅ 使用 Grid 和 Flexbox 创造复杂布局
|
||||
|
||||
**Don't**:
|
||||
- ❌ 所有元素居中对齐
|
||||
- ❌ 均匀分布的网格(无聊)
|
||||
- ❌ 忽略响应式设计
|
||||
|
||||
**示例布局技巧**:
|
||||
```css
|
||||
/* 不对称网格 */
|
||||
.grid-asymmetric {
|
||||
display: grid;
|
||||
grid-template-columns: 2fr 1fr;
|
||||
gap: 40px;
|
||||
}
|
||||
|
||||
/* 重叠效果 */
|
||||
.overlap-container {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.overlap-item {
|
||||
position: absolute;
|
||||
z-index: 2;
|
||||
transform: translate(-20%, -20%);
|
||||
}
|
||||
|
||||
/* 对角线流程 */
|
||||
.diagonal-section {
|
||||
transform: skewY(-3deg);
|
||||
padding: 100px 0;
|
||||
}
|
||||
|
||||
.diagonal-section > * {
|
||||
transform: skewY(3deg);
|
||||
}
|
||||
```
|
||||
|
||||
### 5. 背景和视觉细节 (Background & Visual Details)
|
||||
|
||||
**原则**: 背景营造氛围和深度。
|
||||
|
||||
**Do**:
|
||||
- ✅ 渐变网格
|
||||
- ✅ 噪点纹理
|
||||
- ✅ 几何图案
|
||||
- ✅ 分层透明度
|
||||
- ✅ 戏剧性阴影
|
||||
- ✅ 装饰性边框
|
||||
- ✅ 自定义光标(如果适合风格)
|
||||
- ✅ 颗粒叠加效果
|
||||
|
||||
**Don't**:
|
||||
- ❌ 纯色背景(除非极简风格)
|
||||
- ❌ 低质量或不相关的库存图片
|
||||
- ❌ 过度使用阴影(box-shadow 污染)
|
||||
|
||||
**示例背景效果**:
|
||||
```css
|
||||
/* 渐变网格背景 */
|
||||
.gradient-grid {
|
||||
background:
|
||||
linear-gradient(90deg, rgba(255,255,255,0.05) 1px, transparent 1px),
|
||||
linear-gradient(rgba(255,255,255,0.05) 1px, transparent 1px),
|
||||
linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
background-size: 50px 50px, 50px 50px, 100% 100%;
|
||||
}
|
||||
|
||||
/* 噪点纹理 */
|
||||
.noise-texture {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.noise-texture::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' /%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.05'/%3E%3C/svg%3E");
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
/* 玻璃态效果 */
|
||||
.glass-card {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
backdrop-filter: blur(10px);
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
```
|
||||
|
||||
## 避免通用 AI 美学
|
||||
|
||||
**绝对禁止的元素**:
|
||||
- ❌ 过度使用的字体:Inter, Roboto, Arial, 系统字体
|
||||
- ❌ 俗套配色:白色背景 + 紫色渐变
|
||||
- ❌ 可预测的布局模式(居中卡片网格)
|
||||
- ❌ 缺乏特定上下文特征的千篇一律设计
|
||||
|
||||
**如何避免**:
|
||||
- ✅ 为每个项目选择**不同**的字体
|
||||
- ✅ 在浅色/深色主题之间变化
|
||||
- ✅ 尝试不同的布局结构
|
||||
- ✅ 添加独特的品牌元素和个性
|
||||
|
||||
## 实现复杂性与美学匹配
|
||||
|
||||
**关键原则**: 代码复杂度应与设计愿景相匹配。
|
||||
|
||||
### 极繁主义设计 → 复杂代码
|
||||
- 大量动画和过渡效果
|
||||
- 多层叠加元素
|
||||
- 复杂的交互状态
|
||||
- 详细的视觉效果(粒子、渐变、纹理)
|
||||
|
||||
```jsx
|
||||
// 示例:复杂的动画卡片
|
||||
<motion.div
|
||||
initial={{ opacity: 0, scale: 0.8, rotateX: -15 }}
|
||||
animate={{ opacity: 1, scale: 1, rotateX: 0 }}
|
||||
whileHover={{
|
||||
scale: 1.05,
|
||||
rotateY: 5,
|
||||
boxShadow: "0 25px 50px rgba(0,0,0,0.2)"
|
||||
}}
|
||||
transition={{
|
||||
type: "spring",
|
||||
stiffness: 300,
|
||||
damping: 20
|
||||
}}
|
||||
>
|
||||
{/* 复杂内容 */}
|
||||
</motion.div>
|
||||
```
|
||||
|
||||
### 极简主义设计 → 精准代码
|
||||
- 克制的动画(仅在关键时刻)
|
||||
- 精确的间距和排版
|
||||
- 细微的过渡效果
|
||||
- 关注细节而非数量
|
||||
|
||||
```css
|
||||
/* 示例:精致的极简主义 */
|
||||
.minimal-card {
|
||||
padding: 60px;
|
||||
background: #ffffff;
|
||||
border: 1px solid rgba(0,0,0,0.08);
|
||||
transition: border-color 0.3s ease;
|
||||
}
|
||||
|
||||
.minimal-card:hover {
|
||||
border-color: rgba(0,0,0,0.2);
|
||||
}
|
||||
|
||||
.minimal-card h2 {
|
||||
font-family: 'Cormorant Garamond', serif;
|
||||
font-size: 2.5rem;
|
||||
font-weight: 300;
|
||||
letter-spacing: -0.02em;
|
||||
line-height: 1.2;
|
||||
margin: 0 0 20px 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 工作流程
|
||||
|
||||
### 第 1 步:理解需求
|
||||
- 阅读用户请求,提取关键信息
|
||||
- 确定项目类型(组件、页面、完整应用)
|
||||
- 识别技术栈(React、Vue、原生 HTML/CSS)
|
||||
|
||||
### 第 2 步:设计思考
|
||||
- 回答设计思维流程中的 4 个问题
|
||||
- 选择明确的美学方向
|
||||
- 在心中可视化最终效果
|
||||
|
||||
### 第 3 步:技术决策
|
||||
- 选择框架和工具
|
||||
- 决定动画库(Framer Motion、CSS、React Spring)
|
||||
- 确定字体来源
|
||||
|
||||
### 第 4 步:实现
|
||||
- 编写语义化 HTML 结构
|
||||
- 实现 CSS 样式(使用 CSS 变量)
|
||||
- 添加交互和动画
|
||||
- 确保响应式设计
|
||||
|
||||
### 第 5 步:精细化
|
||||
- 调整间距和排版
|
||||
- 优化动画时间
|
||||
- 测试不同屏幕尺寸
|
||||
- 确保可访问性(ARIA、键盘导航)
|
||||
|
||||
## 示例场景
|
||||
|
||||
### 场景 1: 创建着陆页
|
||||
|
||||
**用户请求**: "帮我创建一个 SaaS 产品的着陆页"
|
||||
|
||||
**设计思考**:
|
||||
- 目的:展示产品价值,吸引用户注册
|
||||
- 风格:现代科技 + 编辑风格,使用 Space Grotesk 字体,黑白 + 蓝色点缀
|
||||
- 布局:不对称,英雄区域占据 70% 屏幕,对角线流程
|
||||
- 差异化:独特的字体配对,大胆的排版层次
|
||||
|
||||
**实现重点**:
|
||||
- Hero section 使用大字号标题(4-6rem)
|
||||
- 滚动触发的淡入动画
|
||||
- 玻璃态 CTA 按钮
|
||||
- 响应式网格展示功能
|
||||
|
||||
### 场景 2: 设计仪表盘
|
||||
|
||||
**用户请求**: "创建一个数据分析仪表盘"
|
||||
|
||||
**设计思考**:
|
||||
- 目的:清晰展示数据,支持快速决策
|
||||
- 风格:实用主义 + 精致,使用 IBM Plex Sans,深色主题
|
||||
- 布局:网格系统,卡片式布局,数据可视化优先
|
||||
- 差异化:微妙的动画过渡,悬停显示详细信息
|
||||
|
||||
**实现重点**:
|
||||
- 深色背景减少眼睛疲劳
|
||||
- 卡片使用柔和阴影和边框
|
||||
- 图表使用鲜明的点缀色
|
||||
- 加载状态使用骨架屏
|
||||
|
||||
### 场景 3: React 组件库
|
||||
|
||||
**用户请求**: "创建一套自定义按钮组件"
|
||||
|
||||
**设计思考**:
|
||||
- 目的:可复用、可定制的按钮系统
|
||||
- 风格:灵活,支持多种变体
|
||||
- 技术:使用 styled-components 或 CSS modules
|
||||
- 差异化:独特的悬停效果和加载状态
|
||||
|
||||
**实现重点**:
|
||||
- 主按钮、次要按钮、文本按钮变体
|
||||
- 大小变体(small, medium, large)
|
||||
- 加载和禁用状态
|
||||
- 平滑的过渡动画
|
||||
|
||||
## 代码质量标准
|
||||
|
||||
### 必须遵守:
|
||||
- ✅ 语义化 HTML(`<header>`, `<nav>`, `<main>`, `<article>`)
|
||||
- ✅ BEM 命名规范或 CSS Modules
|
||||
- ✅ CSS 变量用于颜色和间距
|
||||
- ✅ 移动优先的响应式设计
|
||||
- ✅ 可访问性(ARIA 标签,键盘导航)
|
||||
- ✅ 性能优化(图片懒加载,字体优化)
|
||||
|
||||
### 禁止:
|
||||
- ❌ 内联样式(除非动态值)
|
||||
- ❌ 不必要的 `!important`
|
||||
- ❌ 硬编码的颜色值(使用 CSS 变量)
|
||||
- ❌ 未优化的图片
|
||||
- ❌ 无意义的类名(`.box1`, `.container2`)
|
||||
|
||||
## 技术栈参考
|
||||
|
||||
### 推荐工具:
|
||||
- **字体**: Google Fonts, Font Squirrel
|
||||
- **颜色**: Coolors.co, Adobe Color
|
||||
- **动画**: Framer Motion (React), Anime.js, GreenSock
|
||||
- **图标**: Heroicons, Lucide, Phosphor Icons
|
||||
- **CSS 框架**: Tailwind CSS (自定义配置), styled-components
|
||||
|
||||
### 避免:
|
||||
- ❌ Bootstrap, Material-UI(容易产生通用外观)
|
||||
- ❌ 默认 Tailwind 配置(需要自定义)
|
||||
|
||||
## 检查清单
|
||||
|
||||
在完成实现后,验证以下内容:
|
||||
|
||||
- [ ] 选择了独特的字体组合(不是 Inter/Roboto/Arial)
|
||||
- [ ] 颜色方案有明确的美学方向
|
||||
- [ ] 至少有 1-2 处精心设计的动画
|
||||
- [ ] 布局不是简单的居中网格
|
||||
- [ ] 有独特的视觉细节(背景、纹理、阴影)
|
||||
- [ ] 响应式设计在手机和桌面都好看
|
||||
- [ ] 可访问性标准达标(对比度、ARIA)
|
||||
- [ ] 代码清晰、可维护
|
||||
- [ ] 性能良好(无卡顿动画,快速加载)
|
||||
|
||||
## 最后提醒
|
||||
|
||||
> **创造性诠释是关键**。不要问用户"你想要什么颜色?",而是基于上下文做出大胆的设计决策。每个设计都应该是独一无二的。在不同项目之间变化浅色/深色主题、字体和美学风格。
|
||||
|
||||
> **追求卓越,而非完美**。一个有强烈个性的设计胜过一个"安全"但平庸的设计。敢于尝试,为用户带来惊喜。
|
||||
202
.trae/skills/mcp-builder/LICENSE.txt
Normal file
202
.trae/skills/mcp-builder/LICENSE.txt
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
50
.trae/skills/mcp-builder/README.md
Normal file
50
.trae/skills/mcp-builder/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# mcp-builder
|
||||
|
||||
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
|
||||
|
||||
---
|
||||
|
||||
## 📦 Downloaded from [Skillstore.io](https://skillstore.io)
|
||||
|
||||
This skill was downloaded from **AI Skillstore** — the official marketplace for Claude Code, Codex, and Claude skills.
|
||||
|
||||
🔗 **Skill Page**: [skillstore.io/skills/yyh211-mcp-builder](https://skillstore.io/skills/yyh211-mcp-builder)
|
||||
|
||||
## 🚀 Installation
|
||||
|
||||
### Via Claude Code Plugin System
|
||||
|
||||
```
|
||||
/plugin marketplace add aiskillstore/marketplace
|
||||
/plugin install yyh211-mcp-builder@aiskillstore
|
||||
```
|
||||
|
||||
### Manual Installation
|
||||
|
||||
Copy the contents of this folder to your project's `.claude/skills/` directory.
|
||||
|
||||
## 📋 Skill Info
|
||||
|
||||
| Property | Value |
|
||||
|----------|-------|
|
||||
| **Name** | mcp-builder |
|
||||
| **Version** | 1.0.0 |
|
||||
| **Author** | YYH211 |
|
||||
|
||||
### Supported Tools
|
||||
|
||||
- claude
|
||||
- codex
|
||||
- claude-code
|
||||
|
||||
## 🌐 Discover More Skills
|
||||
|
||||
Browse thousands of AI skills at **[skillstore.io](https://skillstore.io)**:
|
||||
|
||||
- 🔍 Search by category, tool, or keyword
|
||||
- ⭐ Find verified, security-audited skills
|
||||
- 📤 Submit your own skills to share with the community
|
||||
|
||||
---
|
||||
|
||||
*From [skillstore.io](https://skillstore.io) — AI Skills Marketplace*
|
||||
235
.trae/skills/mcp-builder/SKILL.md
Normal file
235
.trae/skills/mcp-builder/SKILL.md
Normal file
@@ -0,0 +1,235 @@
|
||||
---
|
||||
name: mcp-builder
|
||||
description: Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
|
||||
---
|
||||
|
||||
# MCP Server Development Guide
|
||||
|
||||
## Overview
|
||||
|
||||
Create MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. The quality of an MCP server is measured by how well it enables LLMs to accomplish real-world tasks.
|
||||
|
||||
---
|
||||
|
||||
# Process
|
||||
|
||||
## 🚀 High-Level Workflow
|
||||
|
||||
Creating a high-quality MCP server involves four main phases:
|
||||
|
||||
### Phase 1: Deep Research and Planning
|
||||
|
||||
#### 1.1 Understand Modern MCP Design
|
||||
|
||||
**API Coverage vs. Workflow Tools:**
|
||||
Balance comprehensive API endpoint coverage with specialized workflow tools. Workflow tools can be more convenient for specific tasks, while comprehensive coverage gives agents flexibility to compose operations. Performance varies by client—some clients benefit from code execution that combines basic tools, while others work better with higher-level workflows. When uncertain, prioritize comprehensive API coverage.
|
||||
|
||||
**Tool Naming and Discoverability:**
|
||||
Clear, descriptive tool names help agents find the right tools quickly. Use consistent prefixes (e.g., `github_create_issue`, `github_list_repos`) and action-oriented naming.
|
||||
|
||||
**Context Management:**
|
||||
Agents benefit from concise tool descriptions and the ability to filter/paginate results. Design tools that return focused, relevant data. Some clients support code execution which can help agents filter and process data efficiently.
|
||||
|
||||
**Actionable Error Messages:**
|
||||
Error messages should guide agents toward solutions with specific suggestions and next steps.
|
||||
|
||||
#### 1.2 Study MCP Protocol Documentation
|
||||
|
||||
**Navigate the MCP specification:**
|
||||
|
||||
Start with the sitemap to find relevant pages: `https://modelcontextprotocol.io/sitemap.xml`
|
||||
|
||||
Then fetch specific pages with `.md` suffix for markdown format (e.g., `https://modelcontextprotocol.io/specification/draft.md`).
|
||||
|
||||
Key pages to review:
|
||||
- Specification overview and architecture
|
||||
- Transport mechanisms (streamable HTTP, stdio)
|
||||
- Tool, resource, and prompt definitions
|
||||
|
||||
#### 1.3 Study Framework Documentation
|
||||
|
||||
**Recommended stack:**
|
||||
- **Language**: TypeScript (high-quality SDK support and good compatibility in many execution environments e.g. MCPB. Plus AI models are good at generating TypeScript code, benefiting from its broad usage, static typing and good linting tools)
|
||||
- **Transport**: Streamable HTTP for remote servers, using stateless JSON (simpler to scale and maintain, as opposed to stateful sessions and streaming responses). stdio for local servers.
|
||||
|
||||
**Load framework documentation:**
|
||||
|
||||
- **MCP Best Practices**: [📋 View Best Practices](./reference/mcp_best_practices.md) - Core guidelines
|
||||
|
||||
**For TypeScript (recommended):**
|
||||
- **TypeScript SDK**: Use WebFetch to load `https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/main/README.md`
|
||||
- [⚡ TypeScript Guide](./reference/node_mcp_server.md) - TypeScript patterns and examples
|
||||
|
||||
**For Python:**
|
||||
- **Python SDK**: Use WebFetch to load `https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.md`
|
||||
- [🐍 Python Guide](./reference/python_mcp_server.md) - Python patterns and examples
|
||||
|
||||
#### 1.4 Plan Your Implementation
|
||||
|
||||
**Understand the API:**
|
||||
Review the service's API documentation to identify key endpoints, authentication requirements, and data models. Use web search and WebFetch as needed.
|
||||
|
||||
**Tool Selection:**
|
||||
Prioritize comprehensive API coverage. List endpoints to implement, starting with the most common operations.
|
||||
|
||||
---
|
||||
|
||||
### Phase 2: Implementation
|
||||
|
||||
#### 2.1 Set Up Project Structure
|
||||
|
||||
See language-specific guides for project setup:
|
||||
- [⚡ TypeScript Guide](./reference/node_mcp_server.md) - Project structure, package.json, tsconfig.json
|
||||
- [🐍 Python Guide](./reference/python_mcp_server.md) - Module organization, dependencies
|
||||
|
||||
#### 2.2 Implement Core Infrastructure
|
||||
|
||||
Create shared utilities:
|
||||
- API client with authentication
|
||||
- Error handling helpers
|
||||
- Response formatting (JSON/Markdown)
|
||||
- Pagination support
|
||||
|
||||
#### 2.3 Implement Tools
|
||||
|
||||
For each tool:
|
||||
|
||||
**Input Schema:**
|
||||
- Use Zod (TypeScript) or Pydantic (Python)
|
||||
- Include constraints and clear descriptions
|
||||
- Add examples in field descriptions
|
||||
|
||||
**Output Schema:**
|
||||
- Define `outputSchema` where possible for structured data
|
||||
- Use `structuredContent` in tool responses (TypeScript SDK feature)
|
||||
- Helps clients understand and process tool outputs
|
||||
|
||||
**Tool Description:**
|
||||
- Concise summary of functionality
|
||||
- Parameter descriptions
|
||||
- Return type schema
|
||||
|
||||
**Implementation:**
|
||||
- Async/await for I/O operations
|
||||
- Proper error handling with actionable messages
|
||||
- Support pagination where applicable
|
||||
- Return both text content and structured data when using modern SDKs
|
||||
|
||||
**Annotations:**
|
||||
- `readOnlyHint`: true/false
|
||||
- `destructiveHint`: true/false
|
||||
- `idempotentHint`: true/false
|
||||
- `openWorldHint`: true/false
|
||||
|
||||
---
|
||||
|
||||
### Phase 3: Review and Test
|
||||
|
||||
#### 3.1 Code Quality
|
||||
|
||||
Review for:
|
||||
- No duplicated code (DRY principle)
|
||||
- Consistent error handling
|
||||
- Full type coverage
|
||||
- Clear tool descriptions
|
||||
|
||||
#### 3.2 Build and Test
|
||||
|
||||
**TypeScript:**
|
||||
- Run `npm run build` to verify compilation
|
||||
- Test with MCP Inspector: `npx @modelcontextprotocol/inspector`
|
||||
|
||||
**Python:**
|
||||
- Verify syntax: `python -m py_compile your_server.py`
|
||||
- Test with MCP Inspector
|
||||
|
||||
See language-specific guides for detailed testing approaches and quality checklists.
|
||||
|
||||
---
|
||||
|
||||
### Phase 4: Create Evaluations
|
||||
|
||||
After implementing your MCP server, create comprehensive evaluations to test its effectiveness.
|
||||
|
||||
**Load [✅ Evaluation Guide](./reference/evaluation.md) for complete evaluation guidelines.**
|
||||
|
||||
#### 4.1 Understand Evaluation Purpose
|
||||
|
||||
Use evaluations to test whether LLMs can effectively use your MCP server to answer realistic, complex questions.
|
||||
|
||||
#### 4.2 Create 10 Evaluation Questions
|
||||
|
||||
To create effective evaluations, follow the process outlined in the evaluation guide:
|
||||
|
||||
1. **Tool Inspection**: List available tools and understand their capabilities
|
||||
2. **Content Exploration**: Use READ-ONLY operations to explore available data
|
||||
3. **Question Generation**: Create 10 complex, realistic questions
|
||||
4. **Answer Verification**: Solve each question yourself to verify answers
|
||||
|
||||
#### 4.3 Evaluation Requirements
|
||||
|
||||
Ensure each question is:
|
||||
- **Independent**: Not dependent on other questions
|
||||
- **Read-only**: Only non-destructive operations required
|
||||
- **Complex**: Requiring multiple tool calls and deep exploration
|
||||
- **Realistic**: Based on real use cases humans would care about
|
||||
- **Verifiable**: Single, clear answer that can be verified by string comparison
|
||||
- **Stable**: Answer won't change over time
|
||||
|
||||
#### 4.4 Output Format
|
||||
|
||||
Create an XML file with this structure:
|
||||
|
||||
```xml
|
||||
<evaluation>
|
||||
<qa_pair>
|
||||
<question>Find discussions about AI model launches with animal codenames. One model needed a specific safety designation that uses the format ASL-X. What number X was being determined for the model named after a spotted wild cat?</question>
|
||||
<answer>3</answer>
|
||||
</qa_pair>
|
||||
<!-- More qa_pairs... -->
|
||||
</evaluation>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Reference Files
|
||||
|
||||
## 📚 Documentation Library
|
||||
|
||||
Load these resources as needed during development:
|
||||
|
||||
### Core MCP Documentation (Load First)
|
||||
- **MCP Protocol**: Start with sitemap at `https://modelcontextprotocol.io/sitemap.xml`, then fetch specific pages with `.md` suffix
|
||||
- [📋 MCP Best Practices](./reference/mcp_best_practices.md) - Universal MCP guidelines including:
|
||||
- Server and tool naming conventions
|
||||
- Response format guidelines (JSON vs Markdown)
|
||||
- Pagination best practices
|
||||
- Transport selection (streamable HTTP vs stdio)
|
||||
- Security and error handling standards
|
||||
|
||||
### SDK Documentation (Load During Phase 1/2)
|
||||
- **Python SDK**: Fetch from `https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.md`
|
||||
- **TypeScript SDK**: Fetch from `https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/main/README.md`
|
||||
|
||||
### Language-Specific Implementation Guides (Load During Phase 2)
|
||||
- [🐍 Python Implementation Guide](./reference/python_mcp_server.md) - Complete Python/FastMCP guide with:
|
||||
- Server initialization patterns
|
||||
- Pydantic model examples
|
||||
- Tool registration with `@mcp.tool`
|
||||
- Complete working examples
|
||||
- Quality checklist
|
||||
|
||||
- [⚡ TypeScript Implementation Guide](./reference/node_mcp_server.md) - Complete TypeScript guide with:
|
||||
- Project structure
|
||||
- Zod schema patterns
|
||||
- Tool registration with `server.registerTool`
|
||||
- Complete working examples
|
||||
- Quality checklist
|
||||
|
||||
### Evaluation Guide (Load During Phase 4)
|
||||
- [✅ Evaluation Guide](./reference/evaluation.md) - Complete evaluation creation guide with:
|
||||
- Question creation guidelines
|
||||
- Answer verification strategies
|
||||
- XML format specifications
|
||||
- Example questions and answers
|
||||
- Running an evaluation with the provided scripts
|
||||
602
.trae/skills/mcp-builder/reference/evaluation.md
Normal file
602
.trae/skills/mcp-builder/reference/evaluation.md
Normal file
@@ -0,0 +1,602 @@
|
||||
# MCP Server Evaluation Guide
|
||||
|
||||
## Overview
|
||||
|
||||
This document provides guidance on creating comprehensive evaluations for MCP servers. Evaluations test whether LLMs can effectively use your MCP server to answer realistic, complex questions using only the tools provided.
|
||||
|
||||
---
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### Evaluation Requirements
|
||||
- Create 10 human-readable questions
|
||||
- Questions must be READ-ONLY, INDEPENDENT, NON-DESTRUCTIVE
|
||||
- Each question requires multiple tool calls (potentially dozens)
|
||||
- Answers must be single, verifiable values
|
||||
- Answers must be STABLE (won't change over time)
|
||||
|
||||
### Output Format
|
||||
```xml
|
||||
<evaluation>
|
||||
<qa_pair>
|
||||
<question>Your question here</question>
|
||||
<answer>Single verifiable answer</answer>
|
||||
</qa_pair>
|
||||
</evaluation>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Purpose of Evaluations
|
||||
|
||||
The measure of quality of an MCP server is NOT how well or comprehensively the server implements tools, but how well these implementations (input/output schemas, docstrings/descriptions, functionality) enable LLMs with no other context and access ONLY to the MCP servers to answer realistic and difficult questions.
|
||||
|
||||
## Evaluation Overview
|
||||
|
||||
Create 10 human-readable questions requiring ONLY READ-ONLY, INDEPENDENT, NON-DESTRUCTIVE, and IDEMPOTENT operations to answer. Each question should be:
|
||||
- Realistic
|
||||
- Clear and concise
|
||||
- Unambiguous
|
||||
- Complex, requiring potentially dozens of tool calls or steps
|
||||
- Answerable with a single, verifiable value that you identify in advance
|
||||
|
||||
## Question Guidelines
|
||||
|
||||
### Core Requirements
|
||||
|
||||
1. **Questions MUST be independent**
|
||||
- Each question should NOT depend on the answer to any other question
|
||||
- Should not assume prior write operations from processing another question
|
||||
|
||||
2. **Questions MUST require ONLY NON-DESTRUCTIVE AND IDEMPOTENT tool use**
|
||||
- Should not instruct or require modifying state to arrive at the correct answer
|
||||
|
||||
3. **Questions must be REALISTIC, CLEAR, CONCISE, and COMPLEX**
|
||||
- Must require another LLM to use multiple (potentially dozens of) tools or steps to answer
|
||||
|
||||
### Complexity and Depth
|
||||
|
||||
4. **Questions must require deep exploration**
|
||||
- Consider multi-hop questions requiring multiple sub-questions and sequential tool calls
|
||||
- Each step should benefit from information found in previous questions
|
||||
|
||||
5. **Questions may require extensive paging**
|
||||
- May need paging through multiple pages of results
|
||||
- May require querying old data (1-2 years out-of-date) to find niche information
|
||||
- The questions must be DIFFICULT
|
||||
|
||||
6. **Questions must require deep understanding**
|
||||
- Rather than surface-level knowledge
|
||||
- May pose complex ideas as True/False questions requiring evidence
|
||||
- May use multiple-choice format where LLM must search different hypotheses
|
||||
|
||||
7. **Questions must not be solvable with straightforward keyword search**
|
||||
- Do not include specific keywords from the target content
|
||||
- Use synonyms, related concepts, or paraphrases
|
||||
- Require multiple searches, analyzing multiple related items, extracting context, then deriving the answer
|
||||
|
||||
### Tool Testing
|
||||
|
||||
8. **Questions should stress-test tool return values**
|
||||
- May elicit tools returning large JSON objects or lists, overwhelming the LLM
|
||||
- Should require understanding multiple modalities of data:
|
||||
- IDs and names
|
||||
- Timestamps and datetimes (months, days, years, seconds)
|
||||
- File IDs, names, extensions, and mimetypes
|
||||
- URLs, GIDs, etc.
|
||||
- Should probe the tool's ability to return all useful forms of data
|
||||
|
||||
9. **Questions should MOSTLY reflect real human use cases**
|
||||
- The kinds of information retrieval tasks that HUMANS assisted by an LLM would care about
|
||||
|
||||
10. **Questions may require dozens of tool calls**
|
||||
- This challenges LLMs with limited context
|
||||
- Encourages MCP server tools to reduce information returned
|
||||
|
||||
11. **Include ambiguous questions**
|
||||
- May be ambiguous OR require difficult decisions on which tools to call
|
||||
- Force the LLM to potentially make mistakes or misinterpret
|
||||
- Ensure that despite AMBIGUITY, there is STILL A SINGLE VERIFIABLE ANSWER
|
||||
|
||||
### Stability
|
||||
|
||||
12. **Questions must be designed so the answer DOES NOT CHANGE**
|
||||
- Do not ask questions that rely on "current state" which is dynamic
|
||||
- For example, do not count:
|
||||
- Number of reactions to a post
|
||||
- Number of replies to a thread
|
||||
- Number of members in a channel
|
||||
|
||||
13. **DO NOT let the MCP server RESTRICT the kinds of questions you create**
|
||||
- Create challenging and complex questions
|
||||
- Some may not be solvable with the available MCP server tools
|
||||
- Questions may require specific output formats (datetime vs. epoch time, JSON vs. MARKDOWN)
|
||||
- Questions may require dozens of tool calls to complete
|
||||
|
||||
## Answer Guidelines
|
||||
|
||||
### Verification
|
||||
|
||||
1. **Answers must be VERIFIABLE via direct string comparison**
|
||||
- If the answer can be re-written in many formats, clearly specify the output format in the QUESTION
|
||||
- Examples: "Use YYYY/MM/DD.", "Respond True or False.", "Answer A, B, C, or D and nothing else."
|
||||
- Answer should be a single VERIFIABLE value such as:
|
||||
- User ID, user name, display name, first name, last name
|
||||
- Channel ID, channel name
|
||||
- Message ID, string
|
||||
- URL, title
|
||||
- Numerical quantity
|
||||
- Timestamp, datetime
|
||||
- Boolean (for True/False questions)
|
||||
- Email address, phone number
|
||||
- File ID, file name, file extension
|
||||
- Multiple choice answer
|
||||
- Answers must not require special formatting or complex, structured output
|
||||
- Answer will be verified using DIRECT STRING COMPARISON
|
||||
|
||||
### Readability
|
||||
|
||||
2. **Answers should generally prefer HUMAN-READABLE formats**
|
||||
- Examples: names, first name, last name, datetime, file name, message string, URL, yes/no, true/false, a/b/c/d
|
||||
- Rather than opaque IDs (though IDs are acceptable)
|
||||
- The VAST MAJORITY of answers should be human-readable
|
||||
|
||||
### Stability
|
||||
|
||||
3. **Answers must be STABLE/STATIONARY**
|
||||
- Look at old content (e.g., conversations that have ended, projects that have launched, questions answered)
|
||||
- Create QUESTIONS based on "closed" concepts that will always return the same answer
|
||||
- Questions may ask to consider a fixed time window to insulate from non-stationary answers
|
||||
- Rely on context UNLIKELY to change
|
||||
- Example: if finding a paper name, be SPECIFIC enough so answer is not confused with papers published later
|
||||
|
||||
4. **Answers must be CLEAR and UNAMBIGUOUS**
|
||||
- Questions must be designed so there is a single, clear answer
|
||||
- Answer can be derived from using the MCP server tools
|
||||
|
||||
### Diversity
|
||||
|
||||
5. **Answers must be DIVERSE**
|
||||
- Answer should be a single VERIFIABLE value in diverse modalities and formats
|
||||
- User concept: user ID, user name, display name, first name, last name, email address, phone number
|
||||
- Channel concept: channel ID, channel name, channel topic
|
||||
- Message concept: message ID, message string, timestamp, month, day, year
|
||||
|
||||
6. **Answers must NOT be complex structures**
|
||||
- Not a list of values
|
||||
- Not a complex object
|
||||
- Not a list of IDs or strings
|
||||
- Not natural language text
|
||||
- UNLESS the answer can be straightforwardly verified using DIRECT STRING COMPARISON
|
||||
- And can be realistically reproduced
|
||||
- It should be unlikely that an LLM would return the same list in any other order or format
|
||||
|
||||
## Evaluation Process
|
||||
|
||||
### Step 1: Documentation Inspection
|
||||
|
||||
Read the documentation of the target API to understand:
|
||||
- Available endpoints and functionality
|
||||
- If ambiguity exists, fetch additional information from the web
|
||||
- Parallelize this step AS MUCH AS POSSIBLE
|
||||
- Ensure each subagent is ONLY examining documentation from the file system or on the web
|
||||
|
||||
### Step 2: Tool Inspection
|
||||
|
||||
List the tools available in the MCP server:
|
||||
- Inspect the MCP server directly
|
||||
- Understand input/output schemas, docstrings, and descriptions
|
||||
- WITHOUT calling the tools themselves at this stage
|
||||
|
||||
### Step 3: Developing Understanding
|
||||
|
||||
Repeat steps 1 & 2 until you have a good understanding:
|
||||
- Iterate multiple times
|
||||
- Think about the kinds of tasks you want to create
|
||||
- Refine your understanding
|
||||
- At NO stage should you READ the code of the MCP server implementation itself
|
||||
- Use your intuition and understanding to create reasonable, realistic, but VERY challenging tasks
|
||||
|
||||
### Step 4: Read-Only Content Inspection
|
||||
|
||||
After understanding the API and tools, USE the MCP server tools:
|
||||
- Inspect content using READ-ONLY and NON-DESTRUCTIVE operations ONLY
|
||||
- Goal: identify specific content (e.g., users, channels, messages, projects, tasks) for creating realistic questions
|
||||
- Should NOT call any tools that modify state
|
||||
- Will NOT read the code of the MCP server implementation itself
|
||||
- Parallelize this step with individual sub-agents pursuing independent explorations
|
||||
- Ensure each subagent is only performing READ-ONLY, NON-DESTRUCTIVE, and IDEMPOTENT operations
|
||||
- BE CAREFUL: SOME TOOLS may return LOTS OF DATA which would cause you to run out of CONTEXT
|
||||
- Make INCREMENTAL, SMALL, AND TARGETED tool calls for exploration
|
||||
- In all tool call requests, use the `limit` parameter to limit results (<10)
|
||||
- Use pagination
|
||||
|
||||
### Step 5: Task Generation
|
||||
|
||||
After inspecting the content, create 10 human-readable questions:
|
||||
- An LLM should be able to answer these with the MCP server
|
||||
- Follow all question and answer guidelines above
|
||||
|
||||
## Output Format
|
||||
|
||||
Each QA pair consists of a question and an answer. The output should be an XML file with this structure:
|
||||
|
||||
```xml
|
||||
<evaluation>
|
||||
<qa_pair>
|
||||
<question>Find the project created in Q2 2024 with the highest number of completed tasks. What is the project name?</question>
|
||||
<answer>Website Redesign</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Search for issues labeled as "bug" that were closed in March 2024. Which user closed the most issues? Provide their username.</question>
|
||||
<answer>sarah_dev</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Look for pull requests that modified files in the /api directory and were merged between January 1 and January 31, 2024. How many different contributors worked on these PRs?</question>
|
||||
<answer>7</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Find the repository with the most stars that was created before 2023. What is the repository name?</question>
|
||||
<answer>data-pipeline</answer>
|
||||
</qa_pair>
|
||||
</evaluation>
|
||||
```
|
||||
|
||||
## Evaluation Examples
|
||||
|
||||
### Good Questions
|
||||
|
||||
**Example 1: Multi-hop question requiring deep exploration (GitHub MCP)**
|
||||
```xml
|
||||
<qa_pair>
|
||||
<question>Find the repository that was archived in Q3 2023 and had previously been the most forked project in the organization. What was the primary programming language used in that repository?</question>
|
||||
<answer>Python</answer>
|
||||
</qa_pair>
|
||||
```
|
||||
|
||||
This question is good because:
|
||||
- Requires multiple searches to find archived repositories
|
||||
- Needs to identify which had the most forks before archival
|
||||
- Requires examining repository details for the language
|
||||
- Answer is a simple, verifiable value
|
||||
- Based on historical (closed) data that won't change
|
||||
|
||||
**Example 2: Requires understanding context without keyword matching (Project Management MCP)**
|
||||
```xml
|
||||
<qa_pair>
|
||||
<question>Locate the initiative focused on improving customer onboarding that was completed in late 2023. The project lead created a retrospective document after completion. What was the lead's role title at that time?</question>
|
||||
<answer>Product Manager</answer>
|
||||
</qa_pair>
|
||||
```
|
||||
|
||||
This question is good because:
|
||||
- Doesn't use specific project name ("initiative focused on improving customer onboarding")
|
||||
- Requires finding completed projects from specific timeframe
|
||||
- Needs to identify the project lead and their role
|
||||
- Requires understanding context from retrospective documents
|
||||
- Answer is human-readable and stable
|
||||
- Based on completed work (won't change)
|
||||
|
||||
**Example 3: Complex aggregation requiring multiple steps (Issue Tracker MCP)**
|
||||
```xml
|
||||
<qa_pair>
|
||||
<question>Among all bugs reported in January 2024 that were marked as critical priority, which assignee resolved the highest percentage of their assigned bugs within 48 hours? Provide the assignee's username.</question>
|
||||
<answer>alex_eng</answer>
|
||||
</qa_pair>
|
||||
```
|
||||
|
||||
This question is good because:
|
||||
- Requires filtering bugs by date, priority, and status
|
||||
- Needs to group by assignee and calculate resolution rates
|
||||
- Requires understanding timestamps to determine 48-hour windows
|
||||
- Tests pagination (potentially many bugs to process)
|
||||
- Answer is a single username
|
||||
- Based on historical data from specific time period
|
||||
|
||||
**Example 4: Requires synthesis across multiple data types (CRM MCP)**
|
||||
```xml
|
||||
<qa_pair>
|
||||
<question>Find the account that upgraded from the Starter to Enterprise plan in Q4 2023 and had the highest annual contract value. What industry does this account operate in?</question>
|
||||
<answer>Healthcare</answer>
|
||||
</qa_pair>
|
||||
```
|
||||
|
||||
This question is good because:
|
||||
- Requires understanding subscription tier changes
|
||||
- Needs to identify upgrade events in specific timeframe
|
||||
- Requires comparing contract values
|
||||
- Must access account industry information
|
||||
- Answer is simple and verifiable
|
||||
- Based on completed historical transactions
|
||||
|
||||
### Poor Questions
|
||||
|
||||
**Example 1: Answer changes over time**
|
||||
```xml
|
||||
<qa_pair>
|
||||
<question>How many open issues are currently assigned to the engineering team?</question>
|
||||
<answer>47</answer>
|
||||
</qa_pair>
|
||||
```
|
||||
|
||||
This question is poor because:
|
||||
- The answer will change as issues are created, closed, or reassigned
|
||||
- Not based on stable/stationary data
|
||||
- Relies on "current state" which is dynamic
|
||||
|
||||
**Example 2: Too easy with keyword search**
|
||||
```xml
|
||||
<qa_pair>
|
||||
<question>Find the pull request with title "Add authentication feature" and tell me who created it.</question>
|
||||
<answer>developer123</answer>
|
||||
</qa_pair>
|
||||
```
|
||||
|
||||
This question is poor because:
|
||||
- Can be solved with a straightforward keyword search for exact title
|
||||
- Doesn't require deep exploration or understanding
|
||||
- No synthesis or analysis needed
|
||||
|
||||
**Example 3: Ambiguous answer format**
|
||||
```xml
|
||||
<qa_pair>
|
||||
<question>List all the repositories that have Python as their primary language.</question>
|
||||
<answer>repo1, repo2, repo3, data-pipeline, ml-tools</answer>
|
||||
</qa_pair>
|
||||
```
|
||||
|
||||
This question is poor because:
|
||||
- Answer is a list that could be returned in any order
|
||||
- Difficult to verify with direct string comparison
|
||||
- LLM might format differently (JSON array, comma-separated, newline-separated)
|
||||
- Better to ask for a specific aggregate (count) or superlative (most stars)
|
||||
|
||||
## Verification Process
|
||||
|
||||
After creating evaluations:
|
||||
|
||||
1. **Examine the XML file** to understand the schema
|
||||
2. **Load each task instruction** and in parallel using the MCP server and tools, identify the correct answer by attempting to solve the task YOURSELF
|
||||
3. **Flag any operations** that require WRITE or DESTRUCTIVE operations
|
||||
4. **Accumulate all CORRECT answers** and replace any incorrect answers in the document
|
||||
5. **Remove any `<qa_pair>`** that require WRITE or DESTRUCTIVE operations
|
||||
|
||||
Remember to parallelize solving tasks to avoid running out of context, then accumulate all answers and make changes to the file at the end.
|
||||
|
||||
## Tips for Creating Quality Evaluations
|
||||
|
||||
1. **Think Hard and Plan Ahead** before generating tasks
|
||||
2. **Parallelize Where Opportunity Arises** to speed up the process and manage context
|
||||
3. **Focus on Realistic Use Cases** that humans would actually want to accomplish
|
||||
4. **Create Challenging Questions** that test the limits of the MCP server's capabilities
|
||||
5. **Ensure Stability** by using historical data and closed concepts
|
||||
6. **Verify Answers** by solving the questions yourself using the MCP server tools
|
||||
7. **Iterate and Refine** based on what you learn during the process
|
||||
|
||||
---
|
||||
|
||||
# Running Evaluations
|
||||
|
||||
After creating your evaluation file, you can use the provided evaluation harness to test your MCP server.
|
||||
|
||||
## Setup
|
||||
|
||||
1. **Install Dependencies**
|
||||
|
||||
```bash
|
||||
pip install -r scripts/requirements.txt
|
||||
```
|
||||
|
||||
Or install manually:
|
||||
```bash
|
||||
pip install anthropic mcp
|
||||
```
|
||||
|
||||
2. **Set API Key**
|
||||
|
||||
```bash
|
||||
export ANTHROPIC_API_KEY=your_api_key_here
|
||||
```
|
||||
|
||||
## Evaluation File Format
|
||||
|
||||
Evaluation files use XML format with `<qa_pair>` elements:
|
||||
|
||||
```xml
|
||||
<evaluation>
|
||||
<qa_pair>
|
||||
<question>Find the project created in Q2 2024 with the highest number of completed tasks. What is the project name?</question>
|
||||
<answer>Website Redesign</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Search for issues labeled as "bug" that were closed in March 2024. Which user closed the most issues? Provide their username.</question>
|
||||
<answer>sarah_dev</answer>
|
||||
</qa_pair>
|
||||
</evaluation>
|
||||
```
|
||||
|
||||
## Running Evaluations
|
||||
|
||||
The evaluation script (`scripts/evaluation.py`) supports three transport types:
|
||||
|
||||
**Important:**
|
||||
- **stdio transport**: The evaluation script automatically launches and manages the MCP server process for you. Do not run the server manually.
|
||||
- **sse/http transports**: You must start the MCP server separately before running the evaluation. The script connects to the already-running server at the specified URL.
|
||||
|
||||
### 1. Local STDIO Server
|
||||
|
||||
For locally-run MCP servers (script launches the server automatically):
|
||||
|
||||
```bash
|
||||
python scripts/evaluation.py \
|
||||
-t stdio \
|
||||
-c python \
|
||||
-a my_mcp_server.py \
|
||||
evaluation.xml
|
||||
```
|
||||
|
||||
With environment variables:
|
||||
```bash
|
||||
python scripts/evaluation.py \
|
||||
-t stdio \
|
||||
-c python \
|
||||
-a my_mcp_server.py \
|
||||
-e API_KEY=abc123 \
|
||||
-e DEBUG=true \
|
||||
evaluation.xml
|
||||
```
|
||||
|
||||
### 2. Server-Sent Events (SSE)
|
||||
|
||||
For SSE-based MCP servers (you must start the server first):
|
||||
|
||||
```bash
|
||||
python scripts/evaluation.py \
|
||||
-t sse \
|
||||
-u https://example.com/mcp \
|
||||
-H "Authorization: Bearer token123" \
|
||||
-H "X-Custom-Header: value" \
|
||||
evaluation.xml
|
||||
```
|
||||
|
||||
### 3. HTTP (Streamable HTTP)
|
||||
|
||||
For HTTP-based MCP servers (you must start the server first):
|
||||
|
||||
```bash
|
||||
python scripts/evaluation.py \
|
||||
-t http \
|
||||
-u https://example.com/mcp \
|
||||
-H "Authorization: Bearer token123" \
|
||||
evaluation.xml
|
||||
```
|
||||
|
||||
## Command-Line Options
|
||||
|
||||
```
|
||||
usage: evaluation.py [-h] [-t {stdio,sse,http}] [-m MODEL] [-c COMMAND]
|
||||
[-a ARGS [ARGS ...]] [-e ENV [ENV ...]] [-u URL]
|
||||
[-H HEADERS [HEADERS ...]] [-o OUTPUT]
|
||||
eval_file
|
||||
|
||||
positional arguments:
|
||||
eval_file Path to evaluation XML file
|
||||
|
||||
optional arguments:
|
||||
-h, --help Show help message
|
||||
-t, --transport Transport type: stdio, sse, or http (default: stdio)
|
||||
-m, --model Claude model to use (default: claude-3-7-sonnet-20250219)
|
||||
-o, --output Output file for report (default: print to stdout)
|
||||
|
||||
stdio options:
|
||||
-c, --command Command to run MCP server (e.g., python, node)
|
||||
-a, --args Arguments for the command (e.g., server.py)
|
||||
-e, --env Environment variables in KEY=VALUE format
|
||||
|
||||
sse/http options:
|
||||
-u, --url MCP server URL
|
||||
-H, --header HTTP headers in 'Key: Value' format
|
||||
```
|
||||
|
||||
## Output
|
||||
|
||||
The evaluation script generates a detailed report including:
|
||||
|
||||
- **Summary Statistics**:
|
||||
- Accuracy (correct/total)
|
||||
- Average task duration
|
||||
- Average tool calls per task
|
||||
- Total tool calls
|
||||
|
||||
- **Per-Task Results**:
|
||||
- Prompt and expected response
|
||||
- Actual response from the agent
|
||||
- Whether the answer was correct (✅/❌)
|
||||
- Duration and tool call details
|
||||
- Agent's summary of its approach
|
||||
- Agent's feedback on the tools
|
||||
|
||||
### Save Report to File
|
||||
|
||||
```bash
|
||||
python scripts/evaluation.py \
|
||||
-t stdio \
|
||||
-c python \
|
||||
-a my_server.py \
|
||||
-o evaluation_report.md \
|
||||
evaluation.xml
|
||||
```
|
||||
|
||||
## Complete Example Workflow
|
||||
|
||||
Here's a complete example of creating and running an evaluation:
|
||||
|
||||
1. **Create your evaluation file** (`my_evaluation.xml`):
|
||||
|
||||
```xml
|
||||
<evaluation>
|
||||
<qa_pair>
|
||||
<question>Find the user who created the most issues in January 2024. What is their username?</question>
|
||||
<answer>alice_developer</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Among all pull requests merged in Q1 2024, which repository had the highest number? Provide the repository name.</question>
|
||||
<answer>backend-api</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Find the project that was completed in December 2023 and had the longest duration from start to finish. How many days did it take?</question>
|
||||
<answer>127</answer>
|
||||
</qa_pair>
|
||||
</evaluation>
|
||||
```
|
||||
|
||||
2. **Install dependencies**:
|
||||
|
||||
```bash
|
||||
pip install -r scripts/requirements.txt
|
||||
export ANTHROPIC_API_KEY=your_api_key
|
||||
```
|
||||
|
||||
3. **Run evaluation**:
|
||||
|
||||
```bash
|
||||
python scripts/evaluation.py \
|
||||
-t stdio \
|
||||
-c python \
|
||||
-a github_mcp_server.py \
|
||||
-e GITHUB_TOKEN=ghp_xxx \
|
||||
-o github_eval_report.md \
|
||||
my_evaluation.xml
|
||||
```
|
||||
|
||||
4. **Review the report** in `github_eval_report.md` to:
|
||||
- See which questions passed/failed
|
||||
- Read the agent's feedback on your tools
|
||||
- Identify areas for improvement
|
||||
- Iterate on your MCP server design
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Connection Errors
|
||||
|
||||
If you get connection errors:
|
||||
- **STDIO**: Verify the command and arguments are correct
|
||||
- **SSE/HTTP**: Check the URL is accessible and headers are correct
|
||||
- Ensure any required API keys are set in environment variables or headers
|
||||
|
||||
### Low Accuracy
|
||||
|
||||
If many evaluations fail:
|
||||
- Review the agent's feedback for each task
|
||||
- Check if tool descriptions are clear and comprehensive
|
||||
- Verify input parameters are well-documented
|
||||
- Consider whether tools return too much or too little data
|
||||
- Ensure error messages are actionable
|
||||
|
||||
### Timeout Issues
|
||||
|
||||
If tasks are timing out:
|
||||
- Use a more capable model (e.g., `claude-3-7-sonnet-20250219`)
|
||||
- Check if tools are returning too much data
|
||||
- Verify pagination is working correctly
|
||||
- Consider simplifying complex questions
|
||||
249
.trae/skills/mcp-builder/reference/mcp_best_practices.md
Normal file
249
.trae/skills/mcp-builder/reference/mcp_best_practices.md
Normal file
@@ -0,0 +1,249 @@
|
||||
# MCP Server Best Practices
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### Server Naming
|
||||
- **Python**: `{service}_mcp` (e.g., `slack_mcp`)
|
||||
- **Node/TypeScript**: `{service}-mcp-server` (e.g., `slack-mcp-server`)
|
||||
|
||||
### Tool Naming
|
||||
- Use snake_case with service prefix
|
||||
- Format: `{service}_{action}_{resource}`
|
||||
- Example: `slack_send_message`, `github_create_issue`
|
||||
|
||||
### Response Formats
|
||||
- Support both JSON and Markdown formats
|
||||
- JSON for programmatic processing
|
||||
- Markdown for human readability
|
||||
|
||||
### Pagination
|
||||
- Always respect `limit` parameter
|
||||
- Return `has_more`, `next_offset`, `total_count`
|
||||
- Default to 20-50 items
|
||||
|
||||
### Transport
|
||||
- **Streamable HTTP**: For remote servers, multi-client scenarios
|
||||
- **stdio**: For local integrations, command-line tools
|
||||
- Avoid SSE (deprecated in favor of streamable HTTP)
|
||||
|
||||
---
|
||||
|
||||
## Server Naming Conventions
|
||||
|
||||
Follow these standardized naming patterns:
|
||||
|
||||
**Python**: Use format `{service}_mcp` (lowercase with underscores)
|
||||
- Examples: `slack_mcp`, `github_mcp`, `jira_mcp`
|
||||
|
||||
**Node/TypeScript**: Use format `{service}-mcp-server` (lowercase with hyphens)
|
||||
- Examples: `slack-mcp-server`, `github-mcp-server`, `jira-mcp-server`
|
||||
|
||||
The name should be general, descriptive of the service being integrated, easy to infer from the task description, and without version numbers.
|
||||
|
||||
---
|
||||
|
||||
## Tool Naming and Design
|
||||
|
||||
### Tool Naming
|
||||
|
||||
1. **Use snake_case**: `search_users`, `create_project`, `get_channel_info`
|
||||
2. **Include service prefix**: Anticipate that your MCP server may be used alongside other MCP servers
|
||||
- Use `slack_send_message` instead of just `send_message`
|
||||
- Use `github_create_issue` instead of just `create_issue`
|
||||
3. **Be action-oriented**: Start with verbs (get, list, search, create, etc.)
|
||||
4. **Be specific**: Avoid generic names that could conflict with other servers
|
||||
|
||||
### Tool Design
|
||||
|
||||
- Tool descriptions must narrowly and unambiguously describe functionality
|
||||
- Descriptions must precisely match actual functionality
|
||||
- Provide tool annotations (readOnlyHint, destructiveHint, idempotentHint, openWorldHint)
|
||||
- Keep tool operations focused and atomic
|
||||
|
||||
---
|
||||
|
||||
## Response Formats
|
||||
|
||||
All tools that return data should support multiple formats:
|
||||
|
||||
### JSON Format (`response_format="json"`)
|
||||
- Machine-readable structured data
|
||||
- Include all available fields and metadata
|
||||
- Consistent field names and types
|
||||
- Use for programmatic processing
|
||||
|
||||
### Markdown Format (`response_format="markdown"`, typically default)
|
||||
- Human-readable formatted text
|
||||
- Use headers, lists, and formatting for clarity
|
||||
- Convert timestamps to human-readable format
|
||||
- Show display names with IDs in parentheses
|
||||
- Omit verbose metadata
|
||||
|
||||
---
|
||||
|
||||
## Pagination
|
||||
|
||||
For tools that list resources:
|
||||
|
||||
- **Always respect the `limit` parameter**
|
||||
- **Implement pagination**: Use `offset` or cursor-based pagination
|
||||
- **Return pagination metadata**: Include `has_more`, `next_offset`/`next_cursor`, `total_count`
|
||||
- **Never load all results into memory**: Especially important for large datasets
|
||||
- **Default to reasonable limits**: 20-50 items is typical
|
||||
|
||||
Example pagination response:
|
||||
```json
|
||||
{
|
||||
"total": 150,
|
||||
"count": 20,
|
||||
"offset": 0,
|
||||
"items": [...],
|
||||
"has_more": true,
|
||||
"next_offset": 20
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Transport Options
|
||||
|
||||
### Streamable HTTP
|
||||
|
||||
**Best for**: Remote servers, web services, multi-client scenarios
|
||||
|
||||
**Characteristics**:
|
||||
- Bidirectional communication over HTTP
|
||||
- Supports multiple simultaneous clients
|
||||
- Can be deployed as a web service
|
||||
- Enables server-to-client notifications
|
||||
|
||||
**Use when**:
|
||||
- Serving multiple clients simultaneously
|
||||
- Deploying as a cloud service
|
||||
- Integration with web applications
|
||||
|
||||
### stdio
|
||||
|
||||
**Best for**: Local integrations, command-line tools
|
||||
|
||||
**Characteristics**:
|
||||
- Standard input/output stream communication
|
||||
- Simple setup, no network configuration needed
|
||||
- Runs as a subprocess of the client
|
||||
|
||||
**Use when**:
|
||||
- Building tools for local development environments
|
||||
- Integrating with desktop applications
|
||||
- Single-user, single-session scenarios
|
||||
|
||||
**Note**: stdio servers should NOT log to stdout (use stderr for logging)
|
||||
|
||||
### Transport Selection
|
||||
|
||||
| Criterion | stdio | Streamable HTTP |
|
||||
|-----------|-------|-----------------|
|
||||
| **Deployment** | Local | Remote |
|
||||
| **Clients** | Single | Multiple |
|
||||
| **Complexity** | Low | Medium |
|
||||
| **Real-time** | No | Yes |
|
||||
|
||||
---
|
||||
|
||||
## Security Best Practices
|
||||
|
||||
### Authentication and Authorization
|
||||
|
||||
**OAuth 2.1**:
|
||||
- Use secure OAuth 2.1 with certificates from recognized authorities
|
||||
- Validate access tokens before processing requests
|
||||
- Only accept tokens specifically intended for your server
|
||||
|
||||
**API Keys**:
|
||||
- Store API keys in environment variables, never in code
|
||||
- Validate keys on server startup
|
||||
- Provide clear error messages when authentication fails
|
||||
|
||||
### Input Validation
|
||||
|
||||
- Sanitize file paths to prevent directory traversal
|
||||
- Validate URLs and external identifiers
|
||||
- Check parameter sizes and ranges
|
||||
- Prevent command injection in system calls
|
||||
- Use schema validation (Pydantic/Zod) for all inputs
|
||||
|
||||
### Error Handling
|
||||
|
||||
- Don't expose internal errors to clients
|
||||
- Log security-relevant errors server-side
|
||||
- Provide helpful but not revealing error messages
|
||||
- Clean up resources after errors
|
||||
|
||||
### DNS Rebinding Protection
|
||||
|
||||
For streamable HTTP servers running locally:
|
||||
- Enable DNS rebinding protection
|
||||
- Validate the `Origin` header on all incoming connections
|
||||
- Bind to `127.0.0.1` rather than `0.0.0.0`
|
||||
|
||||
---
|
||||
|
||||
## Tool Annotations
|
||||
|
||||
Provide annotations to help clients understand tool behavior:
|
||||
|
||||
| Annotation | Type | Default | Description |
|
||||
|-----------|------|---------|-------------|
|
||||
| `readOnlyHint` | boolean | false | Tool does not modify its environment |
|
||||
| `destructiveHint` | boolean | true | Tool may perform destructive updates |
|
||||
| `idempotentHint` | boolean | false | Repeated calls with same args have no additional effect |
|
||||
| `openWorldHint` | boolean | true | Tool interacts with external entities |
|
||||
|
||||
**Important**: Annotations are hints, not security guarantees. Clients should not make security-critical decisions based solely on annotations.
|
||||
|
||||
---
|
||||
|
||||
## Error Handling
|
||||
|
||||
- Use standard JSON-RPC error codes
|
||||
- Report tool errors within result objects (not protocol-level errors)
|
||||
- Provide helpful, specific error messages with suggested next steps
|
||||
- Don't expose internal implementation details
|
||||
- Clean up resources properly on errors
|
||||
|
||||
Example error handling:
|
||||
```typescript
|
||||
try {
|
||||
const result = performOperation();
|
||||
return { content: [{ type: "text", text: result }] };
|
||||
} catch (error) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{
|
||||
type: "text",
|
||||
text: `Error: ${error.message}. Try using filter='active_only' to reduce results.`
|
||||
}]
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Testing Requirements
|
||||
|
||||
Comprehensive testing should cover:
|
||||
|
||||
- **Functional testing**: Verify correct execution with valid/invalid inputs
|
||||
- **Integration testing**: Test interaction with external systems
|
||||
- **Security testing**: Validate auth, input sanitization, rate limiting
|
||||
- **Performance testing**: Check behavior under load, timeouts
|
||||
- **Error handling**: Ensure proper error reporting and cleanup
|
||||
|
||||
---
|
||||
|
||||
## Documentation Requirements
|
||||
|
||||
- Provide clear documentation of all tools and capabilities
|
||||
- Include working examples (at least 3 per major feature)
|
||||
- Document security considerations
|
||||
- Specify required permissions and access levels
|
||||
- Document rate limits and performance characteristics
|
||||
970
.trae/skills/mcp-builder/reference/node_mcp_server.md
Normal file
970
.trae/skills/mcp-builder/reference/node_mcp_server.md
Normal file
@@ -0,0 +1,970 @@
|
||||
# Node/TypeScript MCP Server Implementation Guide
|
||||
|
||||
## Overview
|
||||
|
||||
This document provides Node/TypeScript-specific best practices and examples for implementing MCP servers using the MCP TypeScript SDK. It covers project structure, server setup, tool registration patterns, input validation with Zod, error handling, and complete working examples.
|
||||
|
||||
---
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### Key Imports
|
||||
```typescript
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import express from "express";
|
||||
import { z } from "zod";
|
||||
```
|
||||
|
||||
### Server Initialization
|
||||
```typescript
|
||||
const server = new McpServer({
|
||||
name: "service-mcp-server",
|
||||
version: "1.0.0"
|
||||
});
|
||||
```
|
||||
|
||||
### Tool Registration Pattern
|
||||
```typescript
|
||||
server.registerTool(
|
||||
"tool_name",
|
||||
{
|
||||
title: "Tool Display Name",
|
||||
description: "What the tool does",
|
||||
inputSchema: { param: z.string() },
|
||||
outputSchema: { result: z.string() }
|
||||
},
|
||||
async ({ param }) => {
|
||||
const output = { result: `Processed: ${param}` };
|
||||
return {
|
||||
content: [{ type: "text", text: JSON.stringify(output) }],
|
||||
structuredContent: output // Modern pattern for structured data
|
||||
};
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## MCP TypeScript SDK
|
||||
|
||||
The official MCP TypeScript SDK provides:
|
||||
- `McpServer` class for server initialization
|
||||
- `registerTool` method for tool registration
|
||||
- Zod schema integration for runtime input validation
|
||||
- Type-safe tool handler implementations
|
||||
|
||||
**IMPORTANT - Use Modern APIs Only:**
|
||||
- **DO use**: `server.registerTool()`, `server.registerResource()`, `server.registerPrompt()`
|
||||
- **DO NOT use**: Old deprecated APIs such as `server.tool()`, `server.setRequestHandler(ListToolsRequestSchema, ...)`, or manual handler registration
|
||||
- The `register*` methods provide better type safety, automatic schema handling, and are the recommended approach
|
||||
|
||||
See the MCP SDK documentation in the references for complete details.
|
||||
|
||||
## Server Naming Convention
|
||||
|
||||
Node/TypeScript MCP servers must follow this naming pattern:
|
||||
- **Format**: `{service}-mcp-server` (lowercase with hyphens)
|
||||
- **Examples**: `github-mcp-server`, `jira-mcp-server`, `stripe-mcp-server`
|
||||
|
||||
The name should be:
|
||||
- General (not tied to specific features)
|
||||
- Descriptive of the service/API being integrated
|
||||
- Easy to infer from the task description
|
||||
- Without version numbers or dates
|
||||
|
||||
## Project Structure
|
||||
|
||||
Create the following structure for Node/TypeScript MCP servers:
|
||||
|
||||
```
|
||||
{service}-mcp-server/
|
||||
├── package.json
|
||||
├── tsconfig.json
|
||||
├── README.md
|
||||
├── src/
|
||||
│ ├── index.ts # Main entry point with McpServer initialization
|
||||
│ ├── types.ts # TypeScript type definitions and interfaces
|
||||
│ ├── tools/ # Tool implementations (one file per domain)
|
||||
│ ├── services/ # API clients and shared utilities
|
||||
│ ├── schemas/ # Zod validation schemas
|
||||
│ └── constants.ts # Shared constants (API_URL, CHARACTER_LIMIT, etc.)
|
||||
└── dist/ # Built JavaScript files (entry point: dist/index.js)
|
||||
```
|
||||
|
||||
## Tool Implementation
|
||||
|
||||
### Tool Naming
|
||||
|
||||
Use snake_case for tool names (e.g., "search_users", "create_project", "get_channel_info") with clear, action-oriented names.
|
||||
|
||||
**Avoid Naming Conflicts**: Include the service context to prevent overlaps:
|
||||
- Use "slack_send_message" instead of just "send_message"
|
||||
- Use "github_create_issue" instead of just "create_issue"
|
||||
- Use "asana_list_tasks" instead of just "list_tasks"
|
||||
|
||||
### Tool Structure
|
||||
|
||||
Tools are registered using the `registerTool` method with the following requirements:
|
||||
- Use Zod schemas for runtime input validation and type safety
|
||||
- The `description` field must be explicitly provided - JSDoc comments are NOT automatically extracted
|
||||
- Explicitly provide `title`, `description`, `inputSchema`, and `annotations`
|
||||
- The `inputSchema` must be a Zod schema object (not a JSON schema)
|
||||
- Type all parameters and return values explicitly
|
||||
|
||||
```typescript
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { z } from "zod";
|
||||
|
||||
const server = new McpServer({
|
||||
name: "example-mcp",
|
||||
version: "1.0.0"
|
||||
});
|
||||
|
||||
// Zod schema for input validation
|
||||
const UserSearchInputSchema = z.object({
|
||||
query: z.string()
|
||||
.min(2, "Query must be at least 2 characters")
|
||||
.max(200, "Query must not exceed 200 characters")
|
||||
.describe("Search string to match against names/emails"),
|
||||
limit: z.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.default(20)
|
||||
.describe("Maximum results to return"),
|
||||
offset: z.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.default(0)
|
||||
.describe("Number of results to skip for pagination"),
|
||||
response_format: z.nativeEnum(ResponseFormat)
|
||||
.default(ResponseFormat.MARKDOWN)
|
||||
.describe("Output format: 'markdown' for human-readable or 'json' for machine-readable")
|
||||
}).strict();
|
||||
|
||||
// Type definition from Zod schema
|
||||
type UserSearchInput = z.infer<typeof UserSearchInputSchema>;
|
||||
|
||||
server.registerTool(
|
||||
"example_search_users",
|
||||
{
|
||||
title: "Search Example Users",
|
||||
description: `Search for users in the Example system by name, email, or team.
|
||||
|
||||
This tool searches across all user profiles in the Example platform, supporting partial matches and various search filters. It does NOT create or modify users, only searches existing ones.
|
||||
|
||||
Args:
|
||||
- query (string): Search string to match against names/emails
|
||||
- limit (number): Maximum results to return, between 1-100 (default: 20)
|
||||
- offset (number): Number of results to skip for pagination (default: 0)
|
||||
- response_format ('markdown' | 'json'): Output format (default: 'markdown')
|
||||
|
||||
Returns:
|
||||
For JSON format: Structured data with schema:
|
||||
{
|
||||
"total": number, // Total number of matches found
|
||||
"count": number, // Number of results in this response
|
||||
"offset": number, // Current pagination offset
|
||||
"users": [
|
||||
{
|
||||
"id": string, // User ID (e.g., "U123456789")
|
||||
"name": string, // Full name (e.g., "John Doe")
|
||||
"email": string, // Email address
|
||||
"team": string, // Team name (optional)
|
||||
"active": boolean // Whether user is active
|
||||
}
|
||||
],
|
||||
"has_more": boolean, // Whether more results are available
|
||||
"next_offset": number // Offset for next page (if has_more is true)
|
||||
}
|
||||
|
||||
Examples:
|
||||
- Use when: "Find all marketing team members" -> params with query="team:marketing"
|
||||
- Use when: "Search for John's account" -> params with query="john"
|
||||
- Don't use when: You need to create a user (use example_create_user instead)
|
||||
|
||||
Error Handling:
|
||||
- Returns "Error: Rate limit exceeded" if too many requests (429 status)
|
||||
- Returns "No users found matching '<query>'" if search returns empty`,
|
||||
inputSchema: UserSearchInputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: true
|
||||
}
|
||||
},
|
||||
async (params: UserSearchInput) => {
|
||||
try {
|
||||
// Input validation is handled by Zod schema
|
||||
// Make API request using validated parameters
|
||||
const data = await makeApiRequest<any>(
|
||||
"users/search",
|
||||
"GET",
|
||||
undefined,
|
||||
{
|
||||
q: params.query,
|
||||
limit: params.limit,
|
||||
offset: params.offset
|
||||
}
|
||||
);
|
||||
|
||||
const users = data.users || [];
|
||||
const total = data.total || 0;
|
||||
|
||||
if (!users.length) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: `No users found matching '${params.query}'`
|
||||
}]
|
||||
};
|
||||
}
|
||||
|
||||
// Prepare structured output
|
||||
const output = {
|
||||
total,
|
||||
count: users.length,
|
||||
offset: params.offset,
|
||||
users: users.map((user: any) => ({
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
...(user.team ? { team: user.team } : {}),
|
||||
active: user.active ?? true
|
||||
})),
|
||||
has_more: total > params.offset + users.length,
|
||||
...(total > params.offset + users.length ? {
|
||||
next_offset: params.offset + users.length
|
||||
} : {})
|
||||
};
|
||||
|
||||
// Format text representation based on requested format
|
||||
let textContent: string;
|
||||
if (params.response_format === ResponseFormat.MARKDOWN) {
|
||||
const lines = [`# User Search Results: '${params.query}'`, "",
|
||||
`Found ${total} users (showing ${users.length})`, ""];
|
||||
for (const user of users) {
|
||||
lines.push(`## ${user.name} (${user.id})`);
|
||||
lines.push(`- **Email**: ${user.email}`);
|
||||
if (user.team) lines.push(`- **Team**: ${user.team}`);
|
||||
lines.push("");
|
||||
}
|
||||
textContent = lines.join("\n");
|
||||
} else {
|
||||
textContent = JSON.stringify(output, null, 2);
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: textContent }],
|
||||
structuredContent: output // Modern pattern for structured data
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: handleApiError(error)
|
||||
}]
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## Zod Schemas for Input Validation
|
||||
|
||||
Zod provides runtime type validation:
|
||||
|
||||
```typescript
|
||||
import { z } from "zod";
|
||||
|
||||
// Basic schema with validation
|
||||
const CreateUserSchema = z.object({
|
||||
name: z.string()
|
||||
.min(1, "Name is required")
|
||||
.max(100, "Name must not exceed 100 characters"),
|
||||
email: z.string()
|
||||
.email("Invalid email format"),
|
||||
age: z.number()
|
||||
.int("Age must be a whole number")
|
||||
.min(0, "Age cannot be negative")
|
||||
.max(150, "Age cannot be greater than 150")
|
||||
}).strict(); // Use .strict() to forbid extra fields
|
||||
|
||||
// Enums
|
||||
enum ResponseFormat {
|
||||
MARKDOWN = "markdown",
|
||||
JSON = "json"
|
||||
}
|
||||
|
||||
const SearchSchema = z.object({
|
||||
response_format: z.nativeEnum(ResponseFormat)
|
||||
.default(ResponseFormat.MARKDOWN)
|
||||
.describe("Output format")
|
||||
});
|
||||
|
||||
// Optional fields with defaults
|
||||
const PaginationSchema = z.object({
|
||||
limit: z.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.default(20)
|
||||
.describe("Maximum results to return"),
|
||||
offset: z.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.default(0)
|
||||
.describe("Number of results to skip")
|
||||
});
|
||||
```
|
||||
|
||||
## Response Format Options
|
||||
|
||||
Support multiple output formats for flexibility:
|
||||
|
||||
```typescript
|
||||
enum ResponseFormat {
|
||||
MARKDOWN = "markdown",
|
||||
JSON = "json"
|
||||
}
|
||||
|
||||
const inputSchema = z.object({
|
||||
query: z.string(),
|
||||
response_format: z.nativeEnum(ResponseFormat)
|
||||
.default(ResponseFormat.MARKDOWN)
|
||||
.describe("Output format: 'markdown' for human-readable or 'json' for machine-readable")
|
||||
});
|
||||
```
|
||||
|
||||
**Markdown format**:
|
||||
- Use headers, lists, and formatting for clarity
|
||||
- Convert timestamps to human-readable format
|
||||
- Show display names with IDs in parentheses
|
||||
- Omit verbose metadata
|
||||
- Group related information logically
|
||||
|
||||
**JSON format**:
|
||||
- Return complete, structured data suitable for programmatic processing
|
||||
- Include all available fields and metadata
|
||||
- Use consistent field names and types
|
||||
|
||||
## Pagination Implementation
|
||||
|
||||
For tools that list resources:
|
||||
|
||||
```typescript
|
||||
const ListSchema = z.object({
|
||||
limit: z.number().int().min(1).max(100).default(20),
|
||||
offset: z.number().int().min(0).default(0)
|
||||
});
|
||||
|
||||
async function listItems(params: z.infer<typeof ListSchema>) {
|
||||
const data = await apiRequest(params.limit, params.offset);
|
||||
|
||||
const response = {
|
||||
total: data.total,
|
||||
count: data.items.length,
|
||||
offset: params.offset,
|
||||
items: data.items,
|
||||
has_more: data.total > params.offset + data.items.length,
|
||||
next_offset: data.total > params.offset + data.items.length
|
||||
? params.offset + data.items.length
|
||||
: undefined
|
||||
};
|
||||
|
||||
return JSON.stringify(response, null, 2);
|
||||
}
|
||||
```
|
||||
|
||||
## Character Limits and Truncation
|
||||
|
||||
Add a CHARACTER_LIMIT constant to prevent overwhelming responses:
|
||||
|
||||
```typescript
|
||||
// At module level in constants.ts
|
||||
export const CHARACTER_LIMIT = 25000; // Maximum response size in characters
|
||||
|
||||
async function searchTool(params: SearchInput) {
|
||||
let result = generateResponse(data);
|
||||
|
||||
// Check character limit and truncate if needed
|
||||
if (result.length > CHARACTER_LIMIT) {
|
||||
const truncatedData = data.slice(0, Math.max(1, data.length / 2));
|
||||
response.data = truncatedData;
|
||||
response.truncated = true;
|
||||
response.truncation_message =
|
||||
`Response truncated from ${data.length} to ${truncatedData.length} items. ` +
|
||||
`Use 'offset' parameter or add filters to see more results.`;
|
||||
result = JSON.stringify(response, null, 2);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
Provide clear, actionable error messages:
|
||||
|
||||
```typescript
|
||||
import axios, { AxiosError } from "axios";
|
||||
|
||||
function handleApiError(error: unknown): string {
|
||||
if (error instanceof AxiosError) {
|
||||
if (error.response) {
|
||||
switch (error.response.status) {
|
||||
case 404:
|
||||
return "Error: Resource not found. Please check the ID is correct.";
|
||||
case 403:
|
||||
return "Error: Permission denied. You don't have access to this resource.";
|
||||
case 429:
|
||||
return "Error: Rate limit exceeded. Please wait before making more requests.";
|
||||
default:
|
||||
return `Error: API request failed with status ${error.response.status}`;
|
||||
}
|
||||
} else if (error.code === "ECONNABORTED") {
|
||||
return "Error: Request timed out. Please try again.";
|
||||
}
|
||||
}
|
||||
return `Error: Unexpected error occurred: ${error instanceof Error ? error.message : String(error)}`;
|
||||
}
|
||||
```
|
||||
|
||||
## Shared Utilities
|
||||
|
||||
Extract common functionality into reusable functions:
|
||||
|
||||
```typescript
|
||||
// Shared API request function
|
||||
async function makeApiRequest<T>(
|
||||
endpoint: string,
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" = "GET",
|
||||
data?: any,
|
||||
params?: any
|
||||
): Promise<T> {
|
||||
try {
|
||||
const response = await axios({
|
||||
method,
|
||||
url: `${API_BASE_URL}/${endpoint}`,
|
||||
data,
|
||||
params,
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Async/Await Best Practices
|
||||
|
||||
Always use async/await for network requests and I/O operations:
|
||||
|
||||
```typescript
|
||||
// Good: Async network request
|
||||
async function fetchData(resourceId: string): Promise<ResourceData> {
|
||||
const response = await axios.get(`${API_URL}/resource/${resourceId}`);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
// Bad: Promise chains
|
||||
function fetchData(resourceId: string): Promise<ResourceData> {
|
||||
return axios.get(`${API_URL}/resource/${resourceId}`)
|
||||
.then(response => response.data); // Harder to read and maintain
|
||||
}
|
||||
```
|
||||
|
||||
## TypeScript Best Practices
|
||||
|
||||
1. **Use Strict TypeScript**: Enable strict mode in tsconfig.json
|
||||
2. **Define Interfaces**: Create clear interface definitions for all data structures
|
||||
3. **Avoid `any`**: Use proper types or `unknown` instead of `any`
|
||||
4. **Zod for Runtime Validation**: Use Zod schemas to validate external data
|
||||
5. **Type Guards**: Create type guard functions for complex type checking
|
||||
6. **Error Handling**: Always use try-catch with proper error type checking
|
||||
7. **Null Safety**: Use optional chaining (`?.`) and nullish coalescing (`??`)
|
||||
|
||||
```typescript
|
||||
// Good: Type-safe with Zod and interfaces
|
||||
interface UserResponse {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
team?: string;
|
||||
active: boolean;
|
||||
}
|
||||
|
||||
const UserSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
email: z.string().email(),
|
||||
team: z.string().optional(),
|
||||
active: z.boolean()
|
||||
});
|
||||
|
||||
type User = z.infer<typeof UserSchema>;
|
||||
|
||||
async function getUser(id: string): Promise<User> {
|
||||
const data = await apiCall(`/users/${id}`);
|
||||
return UserSchema.parse(data); // Runtime validation
|
||||
}
|
||||
|
||||
// Bad: Using any
|
||||
async function getUser(id: string): Promise<any> {
|
||||
return await apiCall(`/users/${id}`); // No type safety
|
||||
}
|
||||
```
|
||||
|
||||
## Package Configuration
|
||||
|
||||
### package.json
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "{service}-mcp-server",
|
||||
"version": "1.0.0",
|
||||
"description": "MCP server for {Service} API integration",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"start": "node dist/index.js",
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"build": "tsc",
|
||||
"clean": "rm -rf dist"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.6.1",
|
||||
"axios": "^1.7.9",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.0",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### tsconfig.json
|
||||
|
||||
```json
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "Node16",
|
||||
"moduleResolution": "Node16",
|
||||
"lib": ["ES2022"],
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"sourceMap": true,
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
```
|
||||
|
||||
## Complete Example
|
||||
|
||||
```typescript
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* MCP Server for Example Service.
|
||||
*
|
||||
* This server provides tools to interact with Example API, including user search,
|
||||
* project management, and data export capabilities.
|
||||
*/
|
||||
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import { z } from "zod";
|
||||
import axios, { AxiosError } from "axios";
|
||||
|
||||
// Constants
|
||||
const API_BASE_URL = "https://api.example.com/v1";
|
||||
const CHARACTER_LIMIT = 25000;
|
||||
|
||||
// Enums
|
||||
enum ResponseFormat {
|
||||
MARKDOWN = "markdown",
|
||||
JSON = "json"
|
||||
}
|
||||
|
||||
// Zod schemas
|
||||
const UserSearchInputSchema = z.object({
|
||||
query: z.string()
|
||||
.min(2, "Query must be at least 2 characters")
|
||||
.max(200, "Query must not exceed 200 characters")
|
||||
.describe("Search string to match against names/emails"),
|
||||
limit: z.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.default(20)
|
||||
.describe("Maximum results to return"),
|
||||
offset: z.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.default(0)
|
||||
.describe("Number of results to skip for pagination"),
|
||||
response_format: z.nativeEnum(ResponseFormat)
|
||||
.default(ResponseFormat.MARKDOWN)
|
||||
.describe("Output format: 'markdown' for human-readable or 'json' for machine-readable")
|
||||
}).strict();
|
||||
|
||||
type UserSearchInput = z.infer<typeof UserSearchInputSchema>;
|
||||
|
||||
// Shared utility functions
|
||||
async function makeApiRequest<T>(
|
||||
endpoint: string,
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" = "GET",
|
||||
data?: any,
|
||||
params?: any
|
||||
): Promise<T> {
|
||||
try {
|
||||
const response = await axios({
|
||||
method,
|
||||
url: `${API_BASE_URL}/${endpoint}`,
|
||||
data,
|
||||
params,
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function handleApiError(error: unknown): string {
|
||||
if (error instanceof AxiosError) {
|
||||
if (error.response) {
|
||||
switch (error.response.status) {
|
||||
case 404:
|
||||
return "Error: Resource not found. Please check the ID is correct.";
|
||||
case 403:
|
||||
return "Error: Permission denied. You don't have access to this resource.";
|
||||
case 429:
|
||||
return "Error: Rate limit exceeded. Please wait before making more requests.";
|
||||
default:
|
||||
return `Error: API request failed with status ${error.response.status}`;
|
||||
}
|
||||
} else if (error.code === "ECONNABORTED") {
|
||||
return "Error: Request timed out. Please try again.";
|
||||
}
|
||||
}
|
||||
return `Error: Unexpected error occurred: ${error instanceof Error ? error.message : String(error)}`;
|
||||
}
|
||||
|
||||
// Create MCP server instance
|
||||
const server = new McpServer({
|
||||
name: "example-mcp",
|
||||
version: "1.0.0"
|
||||
});
|
||||
|
||||
// Register tools
|
||||
server.registerTool(
|
||||
"example_search_users",
|
||||
{
|
||||
title: "Search Example Users",
|
||||
description: `[Full description as shown above]`,
|
||||
inputSchema: UserSearchInputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: true
|
||||
}
|
||||
},
|
||||
async (params: UserSearchInput) => {
|
||||
// Implementation as shown above
|
||||
}
|
||||
);
|
||||
|
||||
// Main function
|
||||
// For stdio (local):
|
||||
async function runStdio() {
|
||||
if (!process.env.EXAMPLE_API_KEY) {
|
||||
console.error("ERROR: EXAMPLE_API_KEY environment variable is required");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error("MCP server running via stdio");
|
||||
}
|
||||
|
||||
// For streamable HTTP (remote):
|
||||
async function runHTTP() {
|
||||
if (!process.env.EXAMPLE_API_KEY) {
|
||||
console.error("ERROR: EXAMPLE_API_KEY environment variable is required");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
app.post('/mcp', async (req, res) => {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
enableJsonResponse: true
|
||||
});
|
||||
res.on('close', () => transport.close());
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
});
|
||||
|
||||
const port = parseInt(process.env.PORT || '3000');
|
||||
app.listen(port, () => {
|
||||
console.error(`MCP server running on http://localhost:${port}/mcp`);
|
||||
});
|
||||
}
|
||||
|
||||
// Choose transport based on environment
|
||||
const transport = process.env.TRANSPORT || 'stdio';
|
||||
if (transport === 'http') {
|
||||
runHTTP().catch(error => {
|
||||
console.error("Server error:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
runStdio().catch(error => {
|
||||
console.error("Server error:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Advanced MCP Features
|
||||
|
||||
### Resource Registration
|
||||
|
||||
Expose data as resources for efficient, URI-based access:
|
||||
|
||||
```typescript
|
||||
import { ResourceTemplate } from "@modelcontextprotocol/sdk/types.js";
|
||||
|
||||
// Register a resource with URI template
|
||||
server.registerResource(
|
||||
{
|
||||
uri: "file://documents/{name}",
|
||||
name: "Document Resource",
|
||||
description: "Access documents by name",
|
||||
mimeType: "text/plain"
|
||||
},
|
||||
async (uri: string) => {
|
||||
// Extract parameter from URI
|
||||
const match = uri.match(/^file:\/\/documents\/(.+)$/);
|
||||
if (!match) {
|
||||
throw new Error("Invalid URI format");
|
||||
}
|
||||
|
||||
const documentName = match[1];
|
||||
const content = await loadDocument(documentName);
|
||||
|
||||
return {
|
||||
contents: [{
|
||||
uri,
|
||||
mimeType: "text/plain",
|
||||
text: content
|
||||
}]
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
// List available resources dynamically
|
||||
server.registerResourceList(async () => {
|
||||
const documents = await getAvailableDocuments();
|
||||
return {
|
||||
resources: documents.map(doc => ({
|
||||
uri: `file://documents/${doc.name}`,
|
||||
name: doc.name,
|
||||
mimeType: "text/plain",
|
||||
description: doc.description
|
||||
}))
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
**When to use Resources vs Tools:**
|
||||
- **Resources**: For data access with simple URI-based parameters
|
||||
- **Tools**: For complex operations requiring validation and business logic
|
||||
- **Resources**: When data is relatively static or template-based
|
||||
- **Tools**: When operations have side effects or complex workflows
|
||||
|
||||
### Transport Options
|
||||
|
||||
The TypeScript SDK supports two main transport mechanisms:
|
||||
|
||||
#### Streamable HTTP (Recommended for Remote Servers)
|
||||
|
||||
```typescript
|
||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||
import express from "express";
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
app.post('/mcp', async (req, res) => {
|
||||
// Create new transport for each request (stateless, prevents request ID collisions)
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
enableJsonResponse: true
|
||||
});
|
||||
|
||||
res.on('close', () => transport.close());
|
||||
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
```
|
||||
|
||||
#### stdio (For Local Integrations)
|
||||
|
||||
```typescript
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
```
|
||||
|
||||
**Transport selection:**
|
||||
- **Streamable HTTP**: Web services, remote access, multiple clients
|
||||
- **stdio**: Command-line tools, local development, subprocess integration
|
||||
|
||||
### Notification Support
|
||||
|
||||
Notify clients when server state changes:
|
||||
|
||||
```typescript
|
||||
// Notify when tools list changes
|
||||
server.notification({
|
||||
method: "notifications/tools/list_changed"
|
||||
});
|
||||
|
||||
// Notify when resources change
|
||||
server.notification({
|
||||
method: "notifications/resources/list_changed"
|
||||
});
|
||||
```
|
||||
|
||||
Use notifications sparingly - only when server capabilities genuinely change.
|
||||
|
||||
---
|
||||
|
||||
## Code Best Practices
|
||||
|
||||
### Code Composability and Reusability
|
||||
|
||||
Your implementation MUST prioritize composability and code reuse:
|
||||
|
||||
1. **Extract Common Functionality**:
|
||||
- Create reusable helper functions for operations used across multiple tools
|
||||
- Build shared API clients for HTTP requests instead of duplicating code
|
||||
- Centralize error handling logic in utility functions
|
||||
- Extract business logic into dedicated functions that can be composed
|
||||
- Extract shared markdown or JSON field selection & formatting functionality
|
||||
|
||||
2. **Avoid Duplication**:
|
||||
- NEVER copy-paste similar code between tools
|
||||
- If you find yourself writing similar logic twice, extract it into a function
|
||||
- Common operations like pagination, filtering, field selection, and formatting should be shared
|
||||
- Authentication/authorization logic should be centralized
|
||||
|
||||
## Building and Running
|
||||
|
||||
Always build your TypeScript code before running:
|
||||
|
||||
```bash
|
||||
# Build the project
|
||||
npm run build
|
||||
|
||||
# Run the server
|
||||
npm start
|
||||
|
||||
# Development with auto-reload
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Always ensure `npm run build` completes successfully before considering the implementation complete.
|
||||
|
||||
## Quality Checklist
|
||||
|
||||
Before finalizing your Node/TypeScript MCP server implementation, ensure:
|
||||
|
||||
### Strategic Design
|
||||
- [ ] Tools enable complete workflows, not just API endpoint wrappers
|
||||
- [ ] Tool names reflect natural task subdivisions
|
||||
- [ ] Response formats optimize for agent context efficiency
|
||||
- [ ] Human-readable identifiers used where appropriate
|
||||
- [ ] Error messages guide agents toward correct usage
|
||||
|
||||
### Implementation Quality
|
||||
- [ ] FOCUSED IMPLEMENTATION: Most important and valuable tools implemented
|
||||
- [ ] All tools registered using `registerTool` with complete configuration
|
||||
- [ ] All tools include `title`, `description`, `inputSchema`, and `annotations`
|
||||
- [ ] Annotations correctly set (readOnlyHint, destructiveHint, idempotentHint, openWorldHint)
|
||||
- [ ] All tools use Zod schemas for runtime input validation with `.strict()` enforcement
|
||||
- [ ] All Zod schemas have proper constraints and descriptive error messages
|
||||
- [ ] All tools have comprehensive descriptions with explicit input/output types
|
||||
- [ ] Descriptions include return value examples and complete schema documentation
|
||||
- [ ] Error messages are clear, actionable, and educational
|
||||
|
||||
### TypeScript Quality
|
||||
- [ ] TypeScript interfaces are defined for all data structures
|
||||
- [ ] Strict TypeScript is enabled in tsconfig.json
|
||||
- [ ] No use of `any` type - use `unknown` or proper types instead
|
||||
- [ ] All async functions have explicit Promise<T> return types
|
||||
- [ ] Error handling uses proper type guards (e.g., `axios.isAxiosError`, `z.ZodError`)
|
||||
|
||||
### Advanced Features (where applicable)
|
||||
- [ ] Resources registered for appropriate data endpoints
|
||||
- [ ] Appropriate transport configured (stdio or streamable HTTP)
|
||||
- [ ] Notifications implemented for dynamic server capabilities
|
||||
- [ ] Type-safe with SDK interfaces
|
||||
|
||||
### Project Configuration
|
||||
- [ ] Package.json includes all necessary dependencies
|
||||
- [ ] Build script produces working JavaScript in dist/ directory
|
||||
- [ ] Main entry point is properly configured as dist/index.js
|
||||
- [ ] Server name follows format: `{service}-mcp-server`
|
||||
- [ ] tsconfig.json properly configured with strict mode
|
||||
|
||||
### Code Quality
|
||||
- [ ] Pagination is properly implemented where applicable
|
||||
- [ ] Large responses check CHARACTER_LIMIT constant and truncate with clear messages
|
||||
- [ ] Filtering options are provided for potentially large result sets
|
||||
- [ ] All network operations handle timeouts and connection errors gracefully
|
||||
- [ ] Common functionality is extracted into reusable functions
|
||||
- [ ] Return types are consistent across similar operations
|
||||
|
||||
### Testing and Build
|
||||
- [ ] `npm run build` completes successfully without errors
|
||||
- [ ] dist/index.js created and executable
|
||||
- [ ] Server runs: `node dist/index.js --help`
|
||||
- [ ] All imports resolve correctly
|
||||
- [ ] Sample tool calls work as expected
|
||||
719
.trae/skills/mcp-builder/reference/python_mcp_server.md
Normal file
719
.trae/skills/mcp-builder/reference/python_mcp_server.md
Normal file
@@ -0,0 +1,719 @@
|
||||
# Python MCP Server Implementation Guide
|
||||
|
||||
## Overview
|
||||
|
||||
This document provides Python-specific best practices and examples for implementing MCP servers using the MCP Python SDK. It covers server setup, tool registration patterns, input validation with Pydantic, error handling, and complete working examples.
|
||||
|
||||
---
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### Key Imports
|
||||
```python
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
from pydantic import BaseModel, Field, field_validator, ConfigDict
|
||||
from typing import Optional, List, Dict, Any
|
||||
from enum import Enum
|
||||
import httpx
|
||||
```
|
||||
|
||||
### Server Initialization
|
||||
```python
|
||||
mcp = FastMCP("service_mcp")
|
||||
```
|
||||
|
||||
### Tool Registration Pattern
|
||||
```python
|
||||
@mcp.tool(name="tool_name", annotations={...})
|
||||
async def tool_function(params: InputModel) -> str:
|
||||
# Implementation
|
||||
pass
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## MCP Python SDK and FastMCP
|
||||
|
||||
The official MCP Python SDK provides FastMCP, a high-level framework for building MCP servers. It provides:
|
||||
- Automatic description and inputSchema generation from function signatures and docstrings
|
||||
- Pydantic model integration for input validation
|
||||
- Decorator-based tool registration with `@mcp.tool`
|
||||
|
||||
**For complete SDK documentation, use WebFetch to load:**
|
||||
`https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.md`
|
||||
|
||||
## Server Naming Convention
|
||||
|
||||
Python MCP servers must follow this naming pattern:
|
||||
- **Format**: `{service}_mcp` (lowercase with underscores)
|
||||
- **Examples**: `github_mcp`, `jira_mcp`, `stripe_mcp`
|
||||
|
||||
The name should be:
|
||||
- General (not tied to specific features)
|
||||
- Descriptive of the service/API being integrated
|
||||
- Easy to infer from the task description
|
||||
- Without version numbers or dates
|
||||
|
||||
## Tool Implementation
|
||||
|
||||
### Tool Naming
|
||||
|
||||
Use snake_case for tool names (e.g., "search_users", "create_project", "get_channel_info") with clear, action-oriented names.
|
||||
|
||||
**Avoid Naming Conflicts**: Include the service context to prevent overlaps:
|
||||
- Use "slack_send_message" instead of just "send_message"
|
||||
- Use "github_create_issue" instead of just "create_issue"
|
||||
- Use "asana_list_tasks" instead of just "list_tasks"
|
||||
|
||||
### Tool Structure with FastMCP
|
||||
|
||||
Tools are defined using the `@mcp.tool` decorator with Pydantic models for input validation:
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Initialize the MCP server
|
||||
mcp = FastMCP("example_mcp")
|
||||
|
||||
# Define Pydantic model for input validation
|
||||
class ServiceToolInput(BaseModel):
|
||||
'''Input model for service tool operation.'''
|
||||
model_config = ConfigDict(
|
||||
str_strip_whitespace=True, # Auto-strip whitespace from strings
|
||||
validate_assignment=True, # Validate on assignment
|
||||
extra='forbid' # Forbid extra fields
|
||||
)
|
||||
|
||||
param1: str = Field(..., description="First parameter description (e.g., 'user123', 'project-abc')", min_length=1, max_length=100)
|
||||
param2: Optional[int] = Field(default=None, description="Optional integer parameter with constraints", ge=0, le=1000)
|
||||
tags: Optional[List[str]] = Field(default_factory=list, description="List of tags to apply", max_items=10)
|
||||
|
||||
@mcp.tool(
|
||||
name="service_tool_name",
|
||||
annotations={
|
||||
"title": "Human-Readable Tool Title",
|
||||
"readOnlyHint": True, # Tool does not modify environment
|
||||
"destructiveHint": False, # Tool does not perform destructive operations
|
||||
"idempotentHint": True, # Repeated calls have no additional effect
|
||||
"openWorldHint": False # Tool does not interact with external entities
|
||||
}
|
||||
)
|
||||
async def service_tool_name(params: ServiceToolInput) -> str:
|
||||
'''Tool description automatically becomes the 'description' field.
|
||||
|
||||
This tool performs a specific operation on the service. It validates all inputs
|
||||
using the ServiceToolInput Pydantic model before processing.
|
||||
|
||||
Args:
|
||||
params (ServiceToolInput): Validated input parameters containing:
|
||||
- param1 (str): First parameter description
|
||||
- param2 (Optional[int]): Optional parameter with default
|
||||
- tags (Optional[List[str]]): List of tags
|
||||
|
||||
Returns:
|
||||
str: JSON-formatted response containing operation results
|
||||
'''
|
||||
# Implementation here
|
||||
pass
|
||||
```
|
||||
|
||||
## Pydantic v2 Key Features
|
||||
|
||||
- Use `model_config` instead of nested `Config` class
|
||||
- Use `field_validator` instead of deprecated `validator`
|
||||
- Use `model_dump()` instead of deprecated `dict()`
|
||||
- Validators require `@classmethod` decorator
|
||||
- Type hints are required for validator methods
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel, Field, field_validator, ConfigDict
|
||||
|
||||
class CreateUserInput(BaseModel):
|
||||
model_config = ConfigDict(
|
||||
str_strip_whitespace=True,
|
||||
validate_assignment=True
|
||||
)
|
||||
|
||||
name: str = Field(..., description="User's full name", min_length=1, max_length=100)
|
||||
email: str = Field(..., description="User's email address", pattern=r'^[\w\.-]+@[\w\.-]+\.\w+$')
|
||||
age: int = Field(..., description="User's age", ge=0, le=150)
|
||||
|
||||
@field_validator('email')
|
||||
@classmethod
|
||||
def validate_email(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("Email cannot be empty")
|
||||
return v.lower()
|
||||
```
|
||||
|
||||
## Response Format Options
|
||||
|
||||
Support multiple output formats for flexibility:
|
||||
|
||||
```python
|
||||
from enum import Enum
|
||||
|
||||
class ResponseFormat(str, Enum):
|
||||
'''Output format for tool responses.'''
|
||||
MARKDOWN = "markdown"
|
||||
JSON = "json"
|
||||
|
||||
class UserSearchInput(BaseModel):
|
||||
query: str = Field(..., description="Search query")
|
||||
response_format: ResponseFormat = Field(
|
||||
default=ResponseFormat.MARKDOWN,
|
||||
description="Output format: 'markdown' for human-readable or 'json' for machine-readable"
|
||||
)
|
||||
```
|
||||
|
||||
**Markdown format**:
|
||||
- Use headers, lists, and formatting for clarity
|
||||
- Convert timestamps to human-readable format (e.g., "2024-01-15 10:30:00 UTC" instead of epoch)
|
||||
- Show display names with IDs in parentheses (e.g., "@john.doe (U123456)")
|
||||
- Omit verbose metadata (e.g., show only one profile image URL, not all sizes)
|
||||
- Group related information logically
|
||||
|
||||
**JSON format**:
|
||||
- Return complete, structured data suitable for programmatic processing
|
||||
- Include all available fields and metadata
|
||||
- Use consistent field names and types
|
||||
|
||||
## Pagination Implementation
|
||||
|
||||
For tools that list resources:
|
||||
|
||||
```python
|
||||
class ListInput(BaseModel):
|
||||
limit: Optional[int] = Field(default=20, description="Maximum results to return", ge=1, le=100)
|
||||
offset: Optional[int] = Field(default=0, description="Number of results to skip for pagination", ge=0)
|
||||
|
||||
async def list_items(params: ListInput) -> str:
|
||||
# Make API request with pagination
|
||||
data = await api_request(limit=params.limit, offset=params.offset)
|
||||
|
||||
# Return pagination info
|
||||
response = {
|
||||
"total": data["total"],
|
||||
"count": len(data["items"]),
|
||||
"offset": params.offset,
|
||||
"items": data["items"],
|
||||
"has_more": data["total"] > params.offset + len(data["items"]),
|
||||
"next_offset": params.offset + len(data["items"]) if data["total"] > params.offset + len(data["items"]) else None
|
||||
}
|
||||
return json.dumps(response, indent=2)
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
Provide clear, actionable error messages:
|
||||
|
||||
```python
|
||||
def _handle_api_error(e: Exception) -> str:
|
||||
'''Consistent error formatting across all tools.'''
|
||||
if isinstance(e, httpx.HTTPStatusError):
|
||||
if e.response.status_code == 404:
|
||||
return "Error: Resource not found. Please check the ID is correct."
|
||||
elif e.response.status_code == 403:
|
||||
return "Error: Permission denied. You don't have access to this resource."
|
||||
elif e.response.status_code == 429:
|
||||
return "Error: Rate limit exceeded. Please wait before making more requests."
|
||||
return f"Error: API request failed with status {e.response.status_code}"
|
||||
elif isinstance(e, httpx.TimeoutException):
|
||||
return "Error: Request timed out. Please try again."
|
||||
return f"Error: Unexpected error occurred: {type(e).__name__}"
|
||||
```
|
||||
|
||||
## Shared Utilities
|
||||
|
||||
Extract common functionality into reusable functions:
|
||||
|
||||
```python
|
||||
# Shared API request function
|
||||
async def _make_api_request(endpoint: str, method: str = "GET", **kwargs) -> dict:
|
||||
'''Reusable function for all API calls.'''
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.request(
|
||||
method,
|
||||
f"{API_BASE_URL}/{endpoint}",
|
||||
timeout=30.0,
|
||||
**kwargs
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
```
|
||||
|
||||
## Async/Await Best Practices
|
||||
|
||||
Always use async/await for network requests and I/O operations:
|
||||
|
||||
```python
|
||||
# Good: Async network request
|
||||
async def fetch_data(resource_id: str) -> dict:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(f"{API_URL}/resource/{resource_id}")
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
# Bad: Synchronous request
|
||||
def fetch_data(resource_id: str) -> dict:
|
||||
response = requests.get(f"{API_URL}/resource/{resource_id}") # Blocks
|
||||
return response.json()
|
||||
```
|
||||
|
||||
## Type Hints
|
||||
|
||||
Use type hints throughout:
|
||||
|
||||
```python
|
||||
from typing import Optional, List, Dict, Any
|
||||
|
||||
async def get_user(user_id: str) -> Dict[str, Any]:
|
||||
data = await fetch_user(user_id)
|
||||
return {"id": data["id"], "name": data["name"]}
|
||||
```
|
||||
|
||||
## Tool Docstrings
|
||||
|
||||
Every tool must have comprehensive docstrings with explicit type information:
|
||||
|
||||
```python
|
||||
async def search_users(params: UserSearchInput) -> str:
|
||||
'''
|
||||
Search for users in the Example system by name, email, or team.
|
||||
|
||||
This tool searches across all user profiles in the Example platform,
|
||||
supporting partial matches and various search filters. It does NOT
|
||||
create or modify users, only searches existing ones.
|
||||
|
||||
Args:
|
||||
params (UserSearchInput): Validated input parameters containing:
|
||||
- query (str): Search string to match against names/emails (e.g., "john", "@example.com", "team:marketing")
|
||||
- limit (Optional[int]): Maximum results to return, between 1-100 (default: 20)
|
||||
- offset (Optional[int]): Number of results to skip for pagination (default: 0)
|
||||
|
||||
Returns:
|
||||
str: JSON-formatted string containing search results with the following schema:
|
||||
|
||||
Success response:
|
||||
{
|
||||
"total": int, # Total number of matches found
|
||||
"count": int, # Number of results in this response
|
||||
"offset": int, # Current pagination offset
|
||||
"users": [
|
||||
{
|
||||
"id": str, # User ID (e.g., "U123456789")
|
||||
"name": str, # Full name (e.g., "John Doe")
|
||||
"email": str, # Email address (e.g., "john@example.com")
|
||||
"team": str # Team name (e.g., "Marketing") - optional
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Error response:
|
||||
"Error: <error message>" or "No users found matching '<query>'"
|
||||
|
||||
Examples:
|
||||
- Use when: "Find all marketing team members" -> params with query="team:marketing"
|
||||
- Use when: "Search for John's account" -> params with query="john"
|
||||
- Don't use when: You need to create a user (use example_create_user instead)
|
||||
- Don't use when: You have a user ID and need full details (use example_get_user instead)
|
||||
|
||||
Error Handling:
|
||||
- Input validation errors are handled by Pydantic model
|
||||
- Returns "Error: Rate limit exceeded" if too many requests (429 status)
|
||||
- Returns "Error: Invalid API authentication" if API key is invalid (401 status)
|
||||
- Returns formatted list of results or "No users found matching 'query'"
|
||||
'''
|
||||
```
|
||||
|
||||
## Complete Example
|
||||
|
||||
See below for a complete Python MCP server example:
|
||||
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
'''
|
||||
MCP Server for Example Service.
|
||||
|
||||
This server provides tools to interact with Example API, including user search,
|
||||
project management, and data export capabilities.
|
||||
'''
|
||||
|
||||
from typing import Optional, List, Dict, Any
|
||||
from enum import Enum
|
||||
import httpx
|
||||
from pydantic import BaseModel, Field, field_validator, ConfigDict
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Initialize the MCP server
|
||||
mcp = FastMCP("example_mcp")
|
||||
|
||||
# Constants
|
||||
API_BASE_URL = "https://api.example.com/v1"
|
||||
|
||||
# Enums
|
||||
class ResponseFormat(str, Enum):
|
||||
'''Output format for tool responses.'''
|
||||
MARKDOWN = "markdown"
|
||||
JSON = "json"
|
||||
|
||||
# Pydantic Models for Input Validation
|
||||
class UserSearchInput(BaseModel):
|
||||
'''Input model for user search operations.'''
|
||||
model_config = ConfigDict(
|
||||
str_strip_whitespace=True,
|
||||
validate_assignment=True
|
||||
)
|
||||
|
||||
query: str = Field(..., description="Search string to match against names/emails", min_length=2, max_length=200)
|
||||
limit: Optional[int] = Field(default=20, description="Maximum results to return", ge=1, le=100)
|
||||
offset: Optional[int] = Field(default=0, description="Number of results to skip for pagination", ge=0)
|
||||
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN, description="Output format")
|
||||
|
||||
@field_validator('query')
|
||||
@classmethod
|
||||
def validate_query(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("Query cannot be empty or whitespace only")
|
||||
return v.strip()
|
||||
|
||||
# Shared utility functions
|
||||
async def _make_api_request(endpoint: str, method: str = "GET", **kwargs) -> dict:
|
||||
'''Reusable function for all API calls.'''
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.request(
|
||||
method,
|
||||
f"{API_BASE_URL}/{endpoint}",
|
||||
timeout=30.0,
|
||||
**kwargs
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def _handle_api_error(e: Exception) -> str:
|
||||
'''Consistent error formatting across all tools.'''
|
||||
if isinstance(e, httpx.HTTPStatusError):
|
||||
if e.response.status_code == 404:
|
||||
return "Error: Resource not found. Please check the ID is correct."
|
||||
elif e.response.status_code == 403:
|
||||
return "Error: Permission denied. You don't have access to this resource."
|
||||
elif e.response.status_code == 429:
|
||||
return "Error: Rate limit exceeded. Please wait before making more requests."
|
||||
return f"Error: API request failed with status {e.response.status_code}"
|
||||
elif isinstance(e, httpx.TimeoutException):
|
||||
return "Error: Request timed out. Please try again."
|
||||
return f"Error: Unexpected error occurred: {type(e).__name__}"
|
||||
|
||||
# Tool definitions
|
||||
@mcp.tool(
|
||||
name="example_search_users",
|
||||
annotations={
|
||||
"title": "Search Example Users",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": True
|
||||
}
|
||||
)
|
||||
async def example_search_users(params: UserSearchInput) -> str:
|
||||
'''Search for users in the Example system by name, email, or team.
|
||||
|
||||
[Full docstring as shown above]
|
||||
'''
|
||||
try:
|
||||
# Make API request using validated parameters
|
||||
data = await _make_api_request(
|
||||
"users/search",
|
||||
params={
|
||||
"q": params.query,
|
||||
"limit": params.limit,
|
||||
"offset": params.offset
|
||||
}
|
||||
)
|
||||
|
||||
users = data.get("users", [])
|
||||
total = data.get("total", 0)
|
||||
|
||||
if not users:
|
||||
return f"No users found matching '{params.query}'"
|
||||
|
||||
# Format response based on requested format
|
||||
if params.response_format == ResponseFormat.MARKDOWN:
|
||||
lines = [f"# User Search Results: '{params.query}'", ""]
|
||||
lines.append(f"Found {total} users (showing {len(users)})")
|
||||
lines.append("")
|
||||
|
||||
for user in users:
|
||||
lines.append(f"## {user['name']} ({user['id']})")
|
||||
lines.append(f"- **Email**: {user['email']}")
|
||||
if user.get('team'):
|
||||
lines.append(f"- **Team**: {user['team']}")
|
||||
lines.append("")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
else:
|
||||
# Machine-readable JSON format
|
||||
import json
|
||||
response = {
|
||||
"total": total,
|
||||
"count": len(users),
|
||||
"offset": params.offset,
|
||||
"users": users
|
||||
}
|
||||
return json.dumps(response, indent=2)
|
||||
|
||||
except Exception as e:
|
||||
return _handle_api_error(e)
|
||||
|
||||
if __name__ == "__main__":
|
||||
mcp.run()
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Advanced FastMCP Features
|
||||
|
||||
### Context Parameter Injection
|
||||
|
||||
FastMCP can automatically inject a `Context` parameter into tools for advanced capabilities like logging, progress reporting, resource reading, and user interaction:
|
||||
|
||||
```python
|
||||
from mcp.server.fastmcp import FastMCP, Context
|
||||
|
||||
mcp = FastMCP("example_mcp")
|
||||
|
||||
@mcp.tool()
|
||||
async def advanced_search(query: str, ctx: Context) -> str:
|
||||
'''Advanced tool with context access for logging and progress.'''
|
||||
|
||||
# Report progress for long operations
|
||||
await ctx.report_progress(0.25, "Starting search...")
|
||||
|
||||
# Log information for debugging
|
||||
await ctx.log_info("Processing query", {"query": query, "timestamp": datetime.now()})
|
||||
|
||||
# Perform search
|
||||
results = await search_api(query)
|
||||
await ctx.report_progress(0.75, "Formatting results...")
|
||||
|
||||
# Access server configuration
|
||||
server_name = ctx.fastmcp.name
|
||||
|
||||
return format_results(results)
|
||||
|
||||
@mcp.tool()
|
||||
async def interactive_tool(resource_id: str, ctx: Context) -> str:
|
||||
'''Tool that can request additional input from users.'''
|
||||
|
||||
# Request sensitive information when needed
|
||||
api_key = await ctx.elicit(
|
||||
prompt="Please provide your API key:",
|
||||
input_type="password"
|
||||
)
|
||||
|
||||
# Use the provided key
|
||||
return await api_call(resource_id, api_key)
|
||||
```
|
||||
|
||||
**Context capabilities:**
|
||||
- `ctx.report_progress(progress, message)` - Report progress for long operations
|
||||
- `ctx.log_info(message, data)` / `ctx.log_error()` / `ctx.log_debug()` - Logging
|
||||
- `ctx.elicit(prompt, input_type)` - Request input from users
|
||||
- `ctx.fastmcp.name` - Access server configuration
|
||||
- `ctx.read_resource(uri)` - Read MCP resources
|
||||
|
||||
### Resource Registration
|
||||
|
||||
Expose data as resources for efficient, template-based access:
|
||||
|
||||
```python
|
||||
@mcp.resource("file://documents/{name}")
|
||||
async def get_document(name: str) -> str:
|
||||
'''Expose documents as MCP resources.
|
||||
|
||||
Resources are useful for static or semi-static data that doesn't
|
||||
require complex parameters. They use URI templates for flexible access.
|
||||
'''
|
||||
document_path = f"./docs/{name}"
|
||||
with open(document_path, "r") as f:
|
||||
return f.read()
|
||||
|
||||
@mcp.resource("config://settings/{key}")
|
||||
async def get_setting(key: str, ctx: Context) -> str:
|
||||
'''Expose configuration as resources with context.'''
|
||||
settings = await load_settings()
|
||||
return json.dumps(settings.get(key, {}))
|
||||
```
|
||||
|
||||
**When to use Resources vs Tools:**
|
||||
- **Resources**: For data access with simple parameters (URI templates)
|
||||
- **Tools**: For complex operations with validation and business logic
|
||||
|
||||
### Structured Output Types
|
||||
|
||||
FastMCP supports multiple return types beyond strings:
|
||||
|
||||
```python
|
||||
from typing import TypedDict
|
||||
from dataclasses import dataclass
|
||||
from pydantic import BaseModel
|
||||
|
||||
# TypedDict for structured returns
|
||||
class UserData(TypedDict):
|
||||
id: str
|
||||
name: str
|
||||
email: str
|
||||
|
||||
@mcp.tool()
|
||||
async def get_user_typed(user_id: str) -> UserData:
|
||||
'''Returns structured data - FastMCP handles serialization.'''
|
||||
return {"id": user_id, "name": "John Doe", "email": "john@example.com"}
|
||||
|
||||
# Pydantic models for complex validation
|
||||
class DetailedUser(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
email: str
|
||||
created_at: datetime
|
||||
metadata: Dict[str, Any]
|
||||
|
||||
@mcp.tool()
|
||||
async def get_user_detailed(user_id: str) -> DetailedUser:
|
||||
'''Returns Pydantic model - automatically generates schema.'''
|
||||
user = await fetch_user(user_id)
|
||||
return DetailedUser(**user)
|
||||
```
|
||||
|
||||
### Lifespan Management
|
||||
|
||||
Initialize resources that persist across requests:
|
||||
|
||||
```python
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
@asynccontextmanager
|
||||
async def app_lifespan():
|
||||
'''Manage resources that live for the server's lifetime.'''
|
||||
# Initialize connections, load config, etc.
|
||||
db = await connect_to_database()
|
||||
config = load_configuration()
|
||||
|
||||
# Make available to all tools
|
||||
yield {"db": db, "config": config}
|
||||
|
||||
# Cleanup on shutdown
|
||||
await db.close()
|
||||
|
||||
mcp = FastMCP("example_mcp", lifespan=app_lifespan)
|
||||
|
||||
@mcp.tool()
|
||||
async def query_data(query: str, ctx: Context) -> str:
|
||||
'''Access lifespan resources through context.'''
|
||||
db = ctx.request_context.lifespan_state["db"]
|
||||
results = await db.query(query)
|
||||
return format_results(results)
|
||||
```
|
||||
|
||||
### Transport Options
|
||||
|
||||
FastMCP supports two main transport mechanisms:
|
||||
|
||||
```python
|
||||
# stdio transport (for local tools) - default
|
||||
if __name__ == "__main__":
|
||||
mcp.run()
|
||||
|
||||
# Streamable HTTP transport (for remote servers)
|
||||
if __name__ == "__main__":
|
||||
mcp.run(transport="streamable_http", port=8000)
|
||||
```
|
||||
|
||||
**Transport selection:**
|
||||
- **stdio**: Command-line tools, local integrations, subprocess execution
|
||||
- **Streamable HTTP**: Web services, remote access, multiple clients
|
||||
|
||||
---
|
||||
|
||||
## Code Best Practices
|
||||
|
||||
### Code Composability and Reusability
|
||||
|
||||
Your implementation MUST prioritize composability and code reuse:
|
||||
|
||||
1. **Extract Common Functionality**:
|
||||
- Create reusable helper functions for operations used across multiple tools
|
||||
- Build shared API clients for HTTP requests instead of duplicating code
|
||||
- Centralize error handling logic in utility functions
|
||||
- Extract business logic into dedicated functions that can be composed
|
||||
- Extract shared markdown or JSON field selection & formatting functionality
|
||||
|
||||
2. **Avoid Duplication**:
|
||||
- NEVER copy-paste similar code between tools
|
||||
- If you find yourself writing similar logic twice, extract it into a function
|
||||
- Common operations like pagination, filtering, field selection, and formatting should be shared
|
||||
- Authentication/authorization logic should be centralized
|
||||
|
||||
### Python-Specific Best Practices
|
||||
|
||||
1. **Use Type Hints**: Always include type annotations for function parameters and return values
|
||||
2. **Pydantic Models**: Define clear Pydantic models for all input validation
|
||||
3. **Avoid Manual Validation**: Let Pydantic handle input validation with constraints
|
||||
4. **Proper Imports**: Group imports (standard library, third-party, local)
|
||||
5. **Error Handling**: Use specific exception types (httpx.HTTPStatusError, not generic Exception)
|
||||
6. **Async Context Managers**: Use `async with` for resources that need cleanup
|
||||
7. **Constants**: Define module-level constants in UPPER_CASE
|
||||
|
||||
## Quality Checklist
|
||||
|
||||
Before finalizing your Python MCP server implementation, ensure:
|
||||
|
||||
### Strategic Design
|
||||
- [ ] Tools enable complete workflows, not just API endpoint wrappers
|
||||
- [ ] Tool names reflect natural task subdivisions
|
||||
- [ ] Response formats optimize for agent context efficiency
|
||||
- [ ] Human-readable identifiers used where appropriate
|
||||
- [ ] Error messages guide agents toward correct usage
|
||||
|
||||
### Implementation Quality
|
||||
- [ ] FOCUSED IMPLEMENTATION: Most important and valuable tools implemented
|
||||
- [ ] All tools have descriptive names and documentation
|
||||
- [ ] Return types are consistent across similar operations
|
||||
- [ ] Error handling is implemented for all external calls
|
||||
- [ ] Server name follows format: `{service}_mcp`
|
||||
- [ ] All network operations use async/await
|
||||
- [ ] Common functionality is extracted into reusable functions
|
||||
- [ ] Error messages are clear, actionable, and educational
|
||||
- [ ] Outputs are properly validated and formatted
|
||||
|
||||
### Tool Configuration
|
||||
- [ ] All tools implement 'name' and 'annotations' in the decorator
|
||||
- [ ] Annotations correctly set (readOnlyHint, destructiveHint, idempotentHint, openWorldHint)
|
||||
- [ ] All tools use Pydantic BaseModel for input validation with Field() definitions
|
||||
- [ ] All Pydantic Fields have explicit types and descriptions with constraints
|
||||
- [ ] All tools have comprehensive docstrings with explicit input/output types
|
||||
- [ ] Docstrings include complete schema structure for dict/JSON returns
|
||||
- [ ] Pydantic models handle input validation (no manual validation needed)
|
||||
|
||||
### Advanced Features (where applicable)
|
||||
- [ ] Context injection used for logging, progress, or elicitation
|
||||
- [ ] Resources registered for appropriate data endpoints
|
||||
- [ ] Lifespan management implemented for persistent connections
|
||||
- [ ] Structured output types used (TypedDict, Pydantic models)
|
||||
- [ ] Appropriate transport configured (stdio or streamable HTTP)
|
||||
|
||||
### Code Quality
|
||||
- [ ] File includes proper imports including Pydantic imports
|
||||
- [ ] Pagination is properly implemented where applicable
|
||||
- [ ] Filtering options are provided for potentially large result sets
|
||||
- [ ] All async functions are properly defined with `async def`
|
||||
- [ ] HTTP client usage follows async patterns with proper context managers
|
||||
- [ ] Type hints are used throughout the code
|
||||
- [ ] Constants are defined at module level in UPPER_CASE
|
||||
|
||||
### Testing
|
||||
- [ ] Server runs successfully: `python your_server.py --help`
|
||||
- [ ] All imports resolve correctly
|
||||
- [ ] Sample tool calls work as expected
|
||||
- [ ] Error scenarios handled gracefully
|
||||
151
.trae/skills/mcp-builder/scripts/connections.py
Normal file
151
.trae/skills/mcp-builder/scripts/connections.py
Normal file
@@ -0,0 +1,151 @@
|
||||
"""Lightweight connection handling for MCP servers."""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from contextlib import AsyncExitStack
|
||||
from typing import Any
|
||||
|
||||
from mcp import ClientSession, StdioServerParameters
|
||||
from mcp.client.sse import sse_client
|
||||
from mcp.client.stdio import stdio_client
|
||||
from mcp.client.streamable_http import streamablehttp_client
|
||||
|
||||
|
||||
class MCPConnection(ABC):
|
||||
"""Base class for MCP server connections."""
|
||||
|
||||
def __init__(self):
|
||||
self.session = None
|
||||
self._stack = None
|
||||
|
||||
@abstractmethod
|
||||
def _create_context(self):
|
||||
"""Create the connection context based on connection type."""
|
||||
|
||||
async def __aenter__(self):
|
||||
"""Initialize MCP server connection."""
|
||||
self._stack = AsyncExitStack()
|
||||
await self._stack.__aenter__()
|
||||
|
||||
try:
|
||||
ctx = self._create_context()
|
||||
result = await self._stack.enter_async_context(ctx)
|
||||
|
||||
if len(result) == 2:
|
||||
read, write = result
|
||||
elif len(result) == 3:
|
||||
read, write, _ = result
|
||||
else:
|
||||
raise ValueError(f"Unexpected context result: {result}")
|
||||
|
||||
session_ctx = ClientSession(read, write)
|
||||
self.session = await self._stack.enter_async_context(session_ctx)
|
||||
await self.session.initialize()
|
||||
return self
|
||||
except BaseException:
|
||||
await self._stack.__aexit__(None, None, None)
|
||||
raise
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
"""Clean up MCP server connection resources."""
|
||||
if self._stack:
|
||||
await self._stack.__aexit__(exc_type, exc_val, exc_tb)
|
||||
self.session = None
|
||||
self._stack = None
|
||||
|
||||
async def list_tools(self) -> list[dict[str, Any]]:
|
||||
"""Retrieve available tools from the MCP server."""
|
||||
response = await self.session.list_tools()
|
||||
return [
|
||||
{
|
||||
"name": tool.name,
|
||||
"description": tool.description,
|
||||
"input_schema": tool.inputSchema,
|
||||
}
|
||||
for tool in response.tools
|
||||
]
|
||||
|
||||
async def call_tool(self, tool_name: str, arguments: dict[str, Any]) -> Any:
|
||||
"""Call a tool on the MCP server with provided arguments."""
|
||||
result = await self.session.call_tool(tool_name, arguments=arguments)
|
||||
return result.content
|
||||
|
||||
|
||||
class MCPConnectionStdio(MCPConnection):
|
||||
"""MCP connection using standard input/output."""
|
||||
|
||||
def __init__(self, command: str, args: list[str] = None, env: dict[str, str] = None):
|
||||
super().__init__()
|
||||
self.command = command
|
||||
self.args = args or []
|
||||
self.env = env
|
||||
|
||||
def _create_context(self):
|
||||
return stdio_client(
|
||||
StdioServerParameters(command=self.command, args=self.args, env=self.env)
|
||||
)
|
||||
|
||||
|
||||
class MCPConnectionSSE(MCPConnection):
|
||||
"""MCP connection using Server-Sent Events."""
|
||||
|
||||
def __init__(self, url: str, headers: dict[str, str] = None):
|
||||
super().__init__()
|
||||
self.url = url
|
||||
self.headers = headers or {}
|
||||
|
||||
def _create_context(self):
|
||||
return sse_client(url=self.url, headers=self.headers)
|
||||
|
||||
|
||||
class MCPConnectionHTTP(MCPConnection):
|
||||
"""MCP connection using Streamable HTTP."""
|
||||
|
||||
def __init__(self, url: str, headers: dict[str, str] = None):
|
||||
super().__init__()
|
||||
self.url = url
|
||||
self.headers = headers or {}
|
||||
|
||||
def _create_context(self):
|
||||
return streamablehttp_client(url=self.url, headers=self.headers)
|
||||
|
||||
|
||||
def create_connection(
|
||||
transport: str,
|
||||
command: str = None,
|
||||
args: list[str] = None,
|
||||
env: dict[str, str] = None,
|
||||
url: str = None,
|
||||
headers: dict[str, str] = None,
|
||||
) -> MCPConnection:
|
||||
"""Factory function to create the appropriate MCP connection.
|
||||
|
||||
Args:
|
||||
transport: Connection type ("stdio", "sse", or "http")
|
||||
command: Command to run (stdio only)
|
||||
args: Command arguments (stdio only)
|
||||
env: Environment variables (stdio only)
|
||||
url: Server URL (sse and http only)
|
||||
headers: HTTP headers (sse and http only)
|
||||
|
||||
Returns:
|
||||
MCPConnection instance
|
||||
"""
|
||||
transport = transport.lower()
|
||||
|
||||
if transport == "stdio":
|
||||
if not command:
|
||||
raise ValueError("Command is required for stdio transport")
|
||||
return MCPConnectionStdio(command=command, args=args, env=env)
|
||||
|
||||
elif transport == "sse":
|
||||
if not url:
|
||||
raise ValueError("URL is required for sse transport")
|
||||
return MCPConnectionSSE(url=url, headers=headers)
|
||||
|
||||
elif transport in ["http", "streamable_http", "streamable-http"]:
|
||||
if not url:
|
||||
raise ValueError("URL is required for http transport")
|
||||
return MCPConnectionHTTP(url=url, headers=headers)
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unsupported transport type: {transport}. Use 'stdio', 'sse', or 'http'")
|
||||
373
.trae/skills/mcp-builder/scripts/evaluation.py
Normal file
373
.trae/skills/mcp-builder/scripts/evaluation.py
Normal file
@@ -0,0 +1,373 @@
|
||||
"""MCP Server Evaluation Harness
|
||||
|
||||
This script evaluates MCP servers by running test questions against them using Claude.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
import xml.etree.ElementTree as ET
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from anthropic import Anthropic
|
||||
|
||||
from connections import create_connection
|
||||
|
||||
EVALUATION_PROMPT = """You are an AI assistant with access to tools.
|
||||
|
||||
When given a task, you MUST:
|
||||
1. Use the available tools to complete the task
|
||||
2. Provide summary of each step in your approach, wrapped in <summary> tags
|
||||
3. Provide feedback on the tools provided, wrapped in <feedback> tags
|
||||
4. Provide your final response, wrapped in <response> tags
|
||||
|
||||
Summary Requirements:
|
||||
- In your <summary> tags, you must explain:
|
||||
- The steps you took to complete the task
|
||||
- Which tools you used, in what order, and why
|
||||
- The inputs you provided to each tool
|
||||
- The outputs you received from each tool
|
||||
- A summary for how you arrived at the response
|
||||
|
||||
Feedback Requirements:
|
||||
- In your <feedback> tags, provide constructive feedback on the tools:
|
||||
- Comment on tool names: Are they clear and descriptive?
|
||||
- Comment on input parameters: Are they well-documented? Are required vs optional parameters clear?
|
||||
- Comment on descriptions: Do they accurately describe what the tool does?
|
||||
- Comment on any errors encountered during tool usage: Did the tool fail to execute? Did the tool return too many tokens?
|
||||
- Identify specific areas for improvement and explain WHY they would help
|
||||
- Be specific and actionable in your suggestions
|
||||
|
||||
Response Requirements:
|
||||
- Your response should be concise and directly address what was asked
|
||||
- Always wrap your final response in <response> tags
|
||||
- If you cannot solve the task return <response>NOT_FOUND</response>
|
||||
- For numeric responses, provide just the number
|
||||
- For IDs, provide just the ID
|
||||
- For names or text, provide the exact text requested
|
||||
- Your response should go last"""
|
||||
|
||||
|
||||
def parse_evaluation_file(file_path: Path) -> list[dict[str, Any]]:
|
||||
"""Parse XML evaluation file with qa_pair elements."""
|
||||
try:
|
||||
tree = ET.parse(file_path)
|
||||
root = tree.getroot()
|
||||
evaluations = []
|
||||
|
||||
for qa_pair in root.findall(".//qa_pair"):
|
||||
question_elem = qa_pair.find("question")
|
||||
answer_elem = qa_pair.find("answer")
|
||||
|
||||
if question_elem is not None and answer_elem is not None:
|
||||
evaluations.append({
|
||||
"question": (question_elem.text or "").strip(),
|
||||
"answer": (answer_elem.text or "").strip(),
|
||||
})
|
||||
|
||||
return evaluations
|
||||
except Exception as e:
|
||||
print(f"Error parsing evaluation file {file_path}: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def extract_xml_content(text: str, tag: str) -> str | None:
|
||||
"""Extract content from XML tags."""
|
||||
pattern = rf"<{tag}>(.*?)</{tag}>"
|
||||
matches = re.findall(pattern, text, re.DOTALL)
|
||||
return matches[-1].strip() if matches else None
|
||||
|
||||
|
||||
async def agent_loop(
|
||||
client: Anthropic,
|
||||
model: str,
|
||||
question: str,
|
||||
tools: list[dict[str, Any]],
|
||||
connection: Any,
|
||||
) -> tuple[str, dict[str, Any]]:
|
||||
"""Run the agent loop with MCP tools."""
|
||||
messages = [{"role": "user", "content": question}]
|
||||
|
||||
response = await asyncio.to_thread(
|
||||
client.messages.create,
|
||||
model=model,
|
||||
max_tokens=4096,
|
||||
system=EVALUATION_PROMPT,
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
)
|
||||
|
||||
messages.append({"role": "assistant", "content": response.content})
|
||||
|
||||
tool_metrics = {}
|
||||
|
||||
while response.stop_reason == "tool_use":
|
||||
tool_use = next(block for block in response.content if block.type == "tool_use")
|
||||
tool_name = tool_use.name
|
||||
tool_input = tool_use.input
|
||||
|
||||
tool_start_ts = time.time()
|
||||
try:
|
||||
tool_result = await connection.call_tool(tool_name, tool_input)
|
||||
tool_response = json.dumps(tool_result) if isinstance(tool_result, (dict, list)) else str(tool_result)
|
||||
except Exception as e:
|
||||
tool_response = f"Error executing tool {tool_name}: {str(e)}\n"
|
||||
tool_response += traceback.format_exc()
|
||||
tool_duration = time.time() - tool_start_ts
|
||||
|
||||
if tool_name not in tool_metrics:
|
||||
tool_metrics[tool_name] = {"count": 0, "durations": []}
|
||||
tool_metrics[tool_name]["count"] += 1
|
||||
tool_metrics[tool_name]["durations"].append(tool_duration)
|
||||
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": [{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": tool_use.id,
|
||||
"content": tool_response,
|
||||
}]
|
||||
})
|
||||
|
||||
response = await asyncio.to_thread(
|
||||
client.messages.create,
|
||||
model=model,
|
||||
max_tokens=4096,
|
||||
system=EVALUATION_PROMPT,
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
)
|
||||
messages.append({"role": "assistant", "content": response.content})
|
||||
|
||||
response_text = next(
|
||||
(block.text for block in response.content if hasattr(block, "text")),
|
||||
None,
|
||||
)
|
||||
return response_text, tool_metrics
|
||||
|
||||
|
||||
async def evaluate_single_task(
|
||||
client: Anthropic,
|
||||
model: str,
|
||||
qa_pair: dict[str, Any],
|
||||
tools: list[dict[str, Any]],
|
||||
connection: Any,
|
||||
task_index: int,
|
||||
) -> dict[str, Any]:
|
||||
"""Evaluate a single QA pair with the given tools."""
|
||||
start_time = time.time()
|
||||
|
||||
print(f"Task {task_index + 1}: Running task with question: {qa_pair['question']}")
|
||||
response, tool_metrics = await agent_loop(client, model, qa_pair["question"], tools, connection)
|
||||
|
||||
response_value = extract_xml_content(response, "response")
|
||||
summary = extract_xml_content(response, "summary")
|
||||
feedback = extract_xml_content(response, "feedback")
|
||||
|
||||
duration_seconds = time.time() - start_time
|
||||
|
||||
return {
|
||||
"question": qa_pair["question"],
|
||||
"expected": qa_pair["answer"],
|
||||
"actual": response_value,
|
||||
"score": int(response_value == qa_pair["answer"]) if response_value else 0,
|
||||
"total_duration": duration_seconds,
|
||||
"tool_calls": tool_metrics,
|
||||
"num_tool_calls": sum(len(metrics["durations"]) for metrics in tool_metrics.values()),
|
||||
"summary": summary,
|
||||
"feedback": feedback,
|
||||
}
|
||||
|
||||
|
||||
REPORT_HEADER = """
|
||||
# Evaluation Report
|
||||
|
||||
## Summary
|
||||
|
||||
- **Accuracy**: {correct}/{total} ({accuracy:.1f}%)
|
||||
- **Average Task Duration**: {average_duration_s:.2f}s
|
||||
- **Average Tool Calls per Task**: {average_tool_calls:.2f}
|
||||
- **Total Tool Calls**: {total_tool_calls}
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
TASK_TEMPLATE = """
|
||||
### Task {task_num}
|
||||
|
||||
**Question**: {question}
|
||||
**Ground Truth Answer**: `{expected_answer}`
|
||||
**Actual Answer**: `{actual_answer}`
|
||||
**Correct**: {correct_indicator}
|
||||
**Duration**: {total_duration:.2f}s
|
||||
**Tool Calls**: {tool_calls}
|
||||
|
||||
**Summary**
|
||||
{summary}
|
||||
|
||||
**Feedback**
|
||||
{feedback}
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
|
||||
async def run_evaluation(
|
||||
eval_path: Path,
|
||||
connection: Any,
|
||||
model: str = "claude-3-7-sonnet-20250219",
|
||||
) -> str:
|
||||
"""Run evaluation with MCP server tools."""
|
||||
print("🚀 Starting Evaluation")
|
||||
|
||||
client = Anthropic()
|
||||
|
||||
tools = await connection.list_tools()
|
||||
print(f"📋 Loaded {len(tools)} tools from MCP server")
|
||||
|
||||
qa_pairs = parse_evaluation_file(eval_path)
|
||||
print(f"📋 Loaded {len(qa_pairs)} evaluation tasks")
|
||||
|
||||
results = []
|
||||
for i, qa_pair in enumerate(qa_pairs):
|
||||
print(f"Processing task {i + 1}/{len(qa_pairs)}")
|
||||
result = await evaluate_single_task(client, model, qa_pair, tools, connection, i)
|
||||
results.append(result)
|
||||
|
||||
correct = sum(r["score"] for r in results)
|
||||
accuracy = (correct / len(results)) * 100 if results else 0
|
||||
average_duration_s = sum(r["total_duration"] for r in results) / len(results) if results else 0
|
||||
average_tool_calls = sum(r["num_tool_calls"] for r in results) / len(results) if results else 0
|
||||
total_tool_calls = sum(r["num_tool_calls"] for r in results)
|
||||
|
||||
report = REPORT_HEADER.format(
|
||||
correct=correct,
|
||||
total=len(results),
|
||||
accuracy=accuracy,
|
||||
average_duration_s=average_duration_s,
|
||||
average_tool_calls=average_tool_calls,
|
||||
total_tool_calls=total_tool_calls,
|
||||
)
|
||||
|
||||
report += "".join([
|
||||
TASK_TEMPLATE.format(
|
||||
task_num=i + 1,
|
||||
question=qa_pair["question"],
|
||||
expected_answer=qa_pair["answer"],
|
||||
actual_answer=result["actual"] or "N/A",
|
||||
correct_indicator="✅" if result["score"] else "❌",
|
||||
total_duration=result["total_duration"],
|
||||
tool_calls=json.dumps(result["tool_calls"], indent=2),
|
||||
summary=result["summary"] or "N/A",
|
||||
feedback=result["feedback"] or "N/A",
|
||||
)
|
||||
for i, (qa_pair, result) in enumerate(zip(qa_pairs, results))
|
||||
])
|
||||
|
||||
return report
|
||||
|
||||
|
||||
def parse_headers(header_list: list[str]) -> dict[str, str]:
|
||||
"""Parse header strings in format 'Key: Value' into a dictionary."""
|
||||
headers = {}
|
||||
if not header_list:
|
||||
return headers
|
||||
|
||||
for header in header_list:
|
||||
if ":" in header:
|
||||
key, value = header.split(":", 1)
|
||||
headers[key.strip()] = value.strip()
|
||||
else:
|
||||
print(f"Warning: Ignoring malformed header: {header}")
|
||||
return headers
|
||||
|
||||
|
||||
def parse_env_vars(env_list: list[str]) -> dict[str, str]:
|
||||
"""Parse environment variable strings in format 'KEY=VALUE' into a dictionary."""
|
||||
env = {}
|
||||
if not env_list:
|
||||
return env
|
||||
|
||||
for env_var in env_list:
|
||||
if "=" in env_var:
|
||||
key, value = env_var.split("=", 1)
|
||||
env[key.strip()] = value.strip()
|
||||
else:
|
||||
print(f"Warning: Ignoring malformed environment variable: {env_var}")
|
||||
return env
|
||||
|
||||
|
||||
async def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Evaluate MCP servers using test questions",
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog="""
|
||||
Examples:
|
||||
# Evaluate a local stdio MCP server
|
||||
python evaluation.py -t stdio -c python -a my_server.py eval.xml
|
||||
|
||||
# Evaluate an SSE MCP server
|
||||
python evaluation.py -t sse -u https://example.com/mcp -H "Authorization: Bearer token" eval.xml
|
||||
|
||||
# Evaluate an HTTP MCP server with custom model
|
||||
python evaluation.py -t http -u https://example.com/mcp -m claude-3-5-sonnet-20241022 eval.xml
|
||||
""",
|
||||
)
|
||||
|
||||
parser.add_argument("eval_file", type=Path, help="Path to evaluation XML file")
|
||||
parser.add_argument("-t", "--transport", choices=["stdio", "sse", "http"], default="stdio", help="Transport type (default: stdio)")
|
||||
parser.add_argument("-m", "--model", default="claude-3-7-sonnet-20250219", help="Claude model to use (default: claude-3-7-sonnet-20250219)")
|
||||
|
||||
stdio_group = parser.add_argument_group("stdio options")
|
||||
stdio_group.add_argument("-c", "--command", help="Command to run MCP server (stdio only)")
|
||||
stdio_group.add_argument("-a", "--args", nargs="+", help="Arguments for the command (stdio only)")
|
||||
stdio_group.add_argument("-e", "--env", nargs="+", help="Environment variables in KEY=VALUE format (stdio only)")
|
||||
|
||||
remote_group = parser.add_argument_group("sse/http options")
|
||||
remote_group.add_argument("-u", "--url", help="MCP server URL (sse/http only)")
|
||||
remote_group.add_argument("-H", "--header", nargs="+", dest="headers", help="HTTP headers in 'Key: Value' format (sse/http only)")
|
||||
|
||||
parser.add_argument("-o", "--output", type=Path, help="Output file for evaluation report (default: stdout)")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.eval_file.exists():
|
||||
print(f"Error: Evaluation file not found: {args.eval_file}")
|
||||
sys.exit(1)
|
||||
|
||||
headers = parse_headers(args.headers) if args.headers else None
|
||||
env_vars = parse_env_vars(args.env) if args.env else None
|
||||
|
||||
try:
|
||||
connection = create_connection(
|
||||
transport=args.transport,
|
||||
command=args.command,
|
||||
args=args.args,
|
||||
env=env_vars,
|
||||
url=args.url,
|
||||
headers=headers,
|
||||
)
|
||||
except ValueError as e:
|
||||
print(f"Error: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"🔗 Connecting to MCP server via {args.transport}...")
|
||||
|
||||
async with connection:
|
||||
print("✅ Connected successfully")
|
||||
report = await run_evaluation(args.eval_file, connection, args.model)
|
||||
|
||||
if args.output:
|
||||
args.output.write_text(report)
|
||||
print(f"\n✅ Report saved to {args.output}")
|
||||
else:
|
||||
print("\n" + report)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
22
.trae/skills/mcp-builder/scripts/example_evaluation.xml
Normal file
22
.trae/skills/mcp-builder/scripts/example_evaluation.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<evaluation>
|
||||
<qa_pair>
|
||||
<question>Calculate the compound interest on $10,000 invested at 5% annual interest rate, compounded monthly for 3 years. What is the final amount in dollars (rounded to 2 decimal places)?</question>
|
||||
<answer>11614.72</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>A projectile is launched at a 45-degree angle with an initial velocity of 50 m/s. Calculate the total distance (in meters) it has traveled from the launch point after 2 seconds, assuming g=9.8 m/s². Round to 2 decimal places.</question>
|
||||
<answer>87.25</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>A sphere has a volume of 500 cubic meters. Calculate its surface area in square meters. Round to 2 decimal places.</question>
|
||||
<answer>304.65</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Calculate the population standard deviation of this dataset: [12, 15, 18, 22, 25, 30, 35]. Round to 2 decimal places.</question>
|
||||
<answer>7.61</answer>
|
||||
</qa_pair>
|
||||
<qa_pair>
|
||||
<question>Calculate the pH of a solution with a hydrogen ion concentration of 3.5 × 10^-5 M. Round to 2 decimal places.</question>
|
||||
<answer>4.46</answer>
|
||||
</qa_pair>
|
||||
</evaluation>
|
||||
2
.trae/skills/mcp-builder/scripts/requirements.txt
Normal file
2
.trae/skills/mcp-builder/scripts/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
anthropic>=0.39.0
|
||||
mcp>=1.1.0
|
||||
150
.trae/skills/project-planner/README.md
Normal file
150
.trae/skills/project-planner/README.md
Normal file
@@ -0,0 +1,150 @@
|
||||
# Project Planner Skill
|
||||
|
||||
A comprehensive skill that establishes Claude as a **Project Architect** to generate detailed planning documents that serve as blueprints for AI-assisted software development.
|
||||
|
||||
## What This Skill Does
|
||||
|
||||
This skill transforms Claude into a System Architect who creates comprehensive project documentation by:
|
||||
|
||||
1. **Defining the complete system architecture upfront** - All components, data flows, and integration points
|
||||
2. **Setting clear project boundaries** - What's in scope, out of scope, and key constraints
|
||||
3. **Creating traceable documentation** - Requirements → Design → Implementation tasks
|
||||
|
||||
The skill generates three essential documents:
|
||||
|
||||
1. **Requirements Document** - User stories with testable acceptance criteria and traceable IDs
|
||||
2. **Design Document** - Complete system architecture with component maps, data flows, and integration specifications
|
||||
3. **Implementation Plan** - Hierarchical task breakdown with requirement tracing and clear deliverables
|
||||
|
||||
## The Architect Approach
|
||||
|
||||
**Why it works:** Setting clear roles, responsibilities, and deliverables upfront dramatically improves output quality. By explicitly defining system components, data flows, and integration points before diving into details, the documentation becomes comprehensive and actionable.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Generate Documents Using the Script
|
||||
|
||||
```bash
|
||||
# Basic usage
|
||||
python scripts/generate_project_docs.py "My Project Name"
|
||||
|
||||
# Specify project type
|
||||
python scripts/generate_project_docs.py "Trading Bot" --type web-app
|
||||
|
||||
# Custom features and components
|
||||
python scripts/generate_project_docs.py "E-commerce Site" \
|
||||
--features "user authentication" "product catalog" "shopping cart" \
|
||||
--components "Auth Service" "Product Service" "Order Service" \
|
||||
--output ./docs
|
||||
```
|
||||
|
||||
### Validate Your Documents
|
||||
|
||||
```bash
|
||||
# Validate all documents
|
||||
python scripts/validate_documents.py \
|
||||
--requirements requirements.md \
|
||||
--design design.md \
|
||||
--tasks tasks.md
|
||||
```
|
||||
|
||||
## Document Types
|
||||
|
||||
### Requirements Document
|
||||
- User stories in standard format
|
||||
- Testable acceptance criteria using SHALL statements
|
||||
- Requirement numbering for traceability
|
||||
- Glossary of domain terms
|
||||
|
||||
### Design Document
|
||||
- System architecture diagrams
|
||||
- Component responsibilities and interfaces
|
||||
- Data models and schemas
|
||||
- Error handling strategies
|
||||
- Deployment configuration
|
||||
|
||||
### Implementation Plan
|
||||
- Hierarchical task breakdown
|
||||
- Requirement tracing (links tasks to requirements)
|
||||
- Dependency management between tasks
|
||||
- Progress tracking with checkboxes
|
||||
|
||||
## Project Types Supported
|
||||
|
||||
- **web-app**: Full-stack web applications
|
||||
- **cli-tool**: Command-line tools and utilities
|
||||
- **api-service**: REST/GraphQL API services
|
||||
- **generic**: General purpose projects
|
||||
|
||||
## Files Included
|
||||
|
||||
### Scripts
|
||||
- `generate_project_docs.py` - Automated document generation
|
||||
- `validate_documents.py` - Document validation and completeness checking
|
||||
|
||||
### References
|
||||
- `domain-templates.md` - Domain-specific templates and patterns
|
||||
|
||||
### Assets
|
||||
- `requirements-template.md` - Basic requirements document template
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Start with Requirements** - Define what the system should do before how
|
||||
2. **Be Specific** - Use measurable criteria (e.g., "within 100ms" not "fast")
|
||||
3. **Trace Requirements** - Link every task back to requirements
|
||||
4. **Include Non-Functional Requirements** - Performance, security, scalability
|
||||
5. **Define Clear Interfaces** - Specify how components interact
|
||||
6. **Plan Incrementally** - Break large tasks into smaller, manageable pieces
|
||||
|
||||
## Common Use Cases
|
||||
|
||||
### Starting a New Project
|
||||
```
|
||||
User: "I want to build a real-time chat application"
|
||||
AI: [Uses this skill to generate complete project documentation]
|
||||
```
|
||||
|
||||
### Expanding Existing Project
|
||||
```
|
||||
User: "Add user authentication to my project requirements"
|
||||
AI: [Adds properly formatted requirements with acceptance criteria]
|
||||
```
|
||||
|
||||
### Creating Technical Specification
|
||||
```
|
||||
User: "Design the architecture for a microservices e-commerce platform"
|
||||
AI: [Generates design document with components, interfaces, and deployment]
|
||||
```
|
||||
|
||||
## Tips for AI Implementation
|
||||
|
||||
When using these documents for AI-assisted development:
|
||||
|
||||
1. **Requirements First** - Implement in order of requirement priority
|
||||
2. **Follow Task Dependencies** - Complete prerequisite tasks first
|
||||
3. **Test Against Acceptance Criteria** - Each SHALL statement is a test case
|
||||
4. **Reference Design Interfaces** - Use the specified APIs and data models
|
||||
5. **Track Progress** - Check off completed tasks in the implementation plan
|
||||
|
||||
## Validation Checklist
|
||||
|
||||
Before using documents for implementation:
|
||||
|
||||
- [ ] All placeholders ([PLACEHOLDER]) filled in
|
||||
- [ ] Requirements have testable acceptance criteria
|
||||
- [ ] Design includes all major components
|
||||
- [ ] Tasks reference requirement IDs
|
||||
- [ ] Dependencies between tasks identified
|
||||
- [ ] Non-functional requirements specified
|
||||
- [ ] Deployment configuration included
|
||||
|
||||
## Example Output
|
||||
|
||||
The generated documents follow industry-standard formats that are:
|
||||
- **Machine-readable** - Structured for AI parsing
|
||||
- **Human-readable** - Clear for developers to understand
|
||||
- **Version-control friendly** - Plain text Markdown format
|
||||
- **Traceable** - Requirements linked through all documents
|
||||
|
||||
This skill transforms high-level project ideas into actionable specifications that AI agents can use to build working software.
|
||||
856
.trae/skills/project-planner/SKILL.md
Normal file
856
.trae/skills/project-planner/SKILL.md
Normal file
@@ -0,0 +1,856 @@
|
||||
---
|
||||
name: project-planner
|
||||
description: Comprehensive project planning and documentation generator for software projects. Creates structured requirements documents, system design documents, and task breakdown plans with implementation tracking. Use when starting a new project, defining specifications, creating technical designs, or breaking down complex systems into implementable tasks. Supports user story format, acceptance criteria, component design, API specifications, and hierarchical task decomposition with requirement traceability.
|
||||
---
|
||||
|
||||
# Project Planner Skill
|
||||
|
||||
## 执行环境
|
||||
|
||||
| 路径类型 | 说明 |
|
||||
|---------|------|
|
||||
| **使用方式** | 此技能提供模板和指导,**无需本地脚本执行** |
|
||||
| **调用场景** | 当用户需要项目规划、需求文档、系统设计时自动激活 |
|
||||
| **输出格式** | Markdown 文档(requirements.md, design.md, tasks.md) |
|
||||
|
||||
## 路径说明
|
||||
|
||||
- **无本地路径依赖**:此技能生成文档模板,不涉及本地脚本执行
|
||||
- **生成的文档**:保存在执行命令时的当前目录(`process.cwd()`)
|
||||
- **项目上下文**:基于当前工作目录确定项目根目录
|
||||
|
||||
This skill provides templates and guidance for generating comprehensive project planning documents that serve as blueprints for AI-assisted implementation.
|
||||
|
||||
## Quick Start
|
||||
|
||||
When a user wants to start a new project, generate three core documents:
|
||||
1. **Requirements Document** - User stories with acceptance criteria
|
||||
2. **Design Document** - Technical architecture and component specifications
|
||||
3. **Implementation Plan** - Hierarchical task breakdown with requirement tracing
|
||||
|
||||
## Why Explicit Architectural Planning Works
|
||||
|
||||
Setting clear roles, responsibilities, and deliverables upfront dramatically improves project outcomes:
|
||||
|
||||
### Benefits of Upfront Definition
|
||||
|
||||
1. **Component Clarity** - Defining all system components first prevents scope creep and ensures complete coverage
|
||||
2. **Data Flow Visibility** - Mapping data movement early reveals integration complexities and performance bottlenecks
|
||||
3. **Integration Planning** - Identifying all touchpoints upfront prevents surprise dependencies during implementation
|
||||
4. **Clear Boundaries** - Explicitly stating what's in/out of scope focuses effort and prevents feature drift
|
||||
5. **Measurable Success** - Specific goals and constraints enable objective progress tracking
|
||||
|
||||
### The Architect Mindset
|
||||
|
||||
When acting as a **Project Architect**, approach planning with:
|
||||
- **Systems Thinking** - See the whole before diving into parts
|
||||
- **Interface-First Design** - Define contracts between components before internals
|
||||
- **Traceability Focus** - Every requirement maps to design elements and tasks
|
||||
- **Constraint Awareness** - Acknowledge limitations upfront to guide decisions
|
||||
- **Deliverable Orientation** - Know exactly what artifacts you're producing
|
||||
|
||||
## Document Generation Workflow
|
||||
|
||||
### 1. Project Architect Role Definition
|
||||
|
||||
When starting a project, explicitly establish Claude as the **Project Architect** with clear responsibilities:
|
||||
|
||||
**Role:** System Architect and Planning Specialist
|
||||
**Responsibilities:**
|
||||
- Define complete system architecture with all components
|
||||
- Map data flow between system elements
|
||||
- Identify all integration points and interfaces
|
||||
- Establish clear project boundaries and constraints
|
||||
- Create traceable requirements to implementation tasks
|
||||
|
||||
### 2. Initial Project Understanding
|
||||
|
||||
Before generating documents, gather key information and architectural elements:
|
||||
|
||||
```
|
||||
Required Project Information:
|
||||
- Project name and purpose
|
||||
- Target users (single-user local, multi-tenant SaaS, etc.)
|
||||
- Core functionality (3-5 main features)
|
||||
- Technical preferences (languages, frameworks, deployment)
|
||||
- Non-functional requirements (performance, security, scalability)
|
||||
|
||||
Required Architectural Elements (define upfront):
|
||||
- System Components: All major modules/services and their purposes
|
||||
- Data Flow: How data moves through the entire system
|
||||
- Integration Points: All external APIs, services, databases
|
||||
- System Boundaries: What's in scope vs out of scope
|
||||
- Constraints: Technical, business, and resource limitations
|
||||
- Success Metrics: Clear, measurable goals for the system
|
||||
```
|
||||
|
||||
### 3. Deliverable Definition (Set Upfront)
|
||||
|
||||
Define all deliverables explicitly before starting documentation:
|
||||
|
||||
```
|
||||
Standard Deliverables Package:
|
||||
1. Requirements Document
|
||||
- User stories with measurable acceptance criteria
|
||||
- Complete glossary of terms
|
||||
- Traceable requirement IDs
|
||||
|
||||
2. System Design Document
|
||||
- Component architecture diagram
|
||||
- Data flow diagrams for all major processes
|
||||
- Integration point specifications
|
||||
- API/Interface contracts
|
||||
- Performance and scaling targets
|
||||
|
||||
3. Implementation Plan
|
||||
- Hierarchical task breakdown
|
||||
- Requirement-to-task mapping
|
||||
- Dependency graph
|
||||
- Phase-based delivery schedule
|
||||
|
||||
Optional Deliverables (specify if needed):
|
||||
- API Documentation
|
||||
- Database Schema Design
|
||||
- Security Threat Model
|
||||
- Deployment Guide
|
||||
- Testing Strategy Document
|
||||
```
|
||||
|
||||
### 4. Generate Requirements Document
|
||||
|
||||
Use the requirements template to create user-focused specifications:
|
||||
|
||||
```python
|
||||
# Execute this to generate requirements structure
|
||||
requirements = {
|
||||
"introduction": "System purpose and scope",
|
||||
"glossary": "Domain-specific terms",
|
||||
"requirements": [
|
||||
{
|
||||
"id": "REQ-X",
|
||||
"user_story": "As a [role], I want [feature], so that [benefit]",
|
||||
"acceptance_criteria": [
|
||||
"WHEN [condition], THE system SHALL [behavior]",
|
||||
"WHERE [context], THE system SHALL [behavior]",
|
||||
"IF [condition], THEN THE system SHALL [behavior]"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Generate Design Document
|
||||
|
||||
Create technical specifications with explicit architectural elements:
|
||||
|
||||
```python
|
||||
# Execute this to generate comprehensive design structure
|
||||
design = {
|
||||
"overview": "High-level system description",
|
||||
"architecture": {
|
||||
"diagram": "ASCII or visual representation of all components",
|
||||
"components": [
|
||||
{
|
||||
"id": "COMP-1",
|
||||
"name": "Component Name",
|
||||
"type": "Frontend/Backend/Service/Database",
|
||||
"responsibility": "Single clear purpose",
|
||||
"boundaries": "What it does and doesn't do"
|
||||
}
|
||||
]
|
||||
},
|
||||
"data_flow": {
|
||||
"primary_flows": [
|
||||
{
|
||||
"name": "User Registration Flow",
|
||||
"steps": [
|
||||
"1. User submits form → Frontend",
|
||||
"2. Frontend validates → API Gateway",
|
||||
"3. API Gateway → Auth Service",
|
||||
"4. Auth Service → User Database",
|
||||
"5. Response flows back"
|
||||
],
|
||||
"data_transformations": "How data changes at each step"
|
||||
}
|
||||
]
|
||||
},
|
||||
"integration_points": [
|
||||
{
|
||||
"name": "External Payment API",
|
||||
"type": "REST/GraphQL/WebSocket/Database",
|
||||
"purpose": "Process payments",
|
||||
"interface": "API contract definition",
|
||||
"authentication": "Method used",
|
||||
"error_handling": "Retry/fallback strategy"
|
||||
}
|
||||
],
|
||||
"components_detail": [
|
||||
{
|
||||
"name": "Component Name",
|
||||
"responsibility": "What it does",
|
||||
"key_classes": ["Class descriptions"],
|
||||
"interfaces": "API/method signatures",
|
||||
"dependencies": "What it needs to function",
|
||||
"performance": "Targets and constraints"
|
||||
}
|
||||
],
|
||||
"data_models": "Entity definitions with relationships",
|
||||
"system_boundaries": {
|
||||
"in_scope": ["What the system handles"],
|
||||
"out_of_scope": ["What it delegates or ignores"],
|
||||
"assumptions": ["External dependencies assumed available"]
|
||||
},
|
||||
"error_handling": "Strategies for failures",
|
||||
"testing_strategy": "Unit, integration, performance",
|
||||
"deployment": "Docker, environment, configuration"
|
||||
}
|
||||
```
|
||||
|
||||
### 6. Generate Implementation Plan
|
||||
|
||||
Break down the project into executable tasks with clear scope boundaries:
|
||||
|
||||
```python
|
||||
# Execute this to generate task structure with boundaries
|
||||
tasks = {
|
||||
"project_boundaries": {
|
||||
"must_have": ["Core features for MVP"],
|
||||
"nice_to_have": ["Enhancement features"],
|
||||
"out_of_scope": ["Features explicitly excluded"],
|
||||
"technical_constraints": ["Framework/library limitations"]
|
||||
},
|
||||
"phases": [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Infrastructure Setup",
|
||||
"deliverables": ["What this phase produces"],
|
||||
"tasks": [
|
||||
{
|
||||
"id": "1.1",
|
||||
"description": "Task description",
|
||||
"subtasks": ["Specific actions"],
|
||||
"requirements_fulfilled": ["REQ-1.1", "REQ-2.3"],
|
||||
"components_involved": ["COMP-1", "COMP-3"],
|
||||
"dependencies": [],
|
||||
"estimated_hours": 4,
|
||||
"success_criteria": "How to verify completion"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Requirements Document Template
|
||||
|
||||
```markdown
|
||||
# Requirements Document
|
||||
|
||||
## Introduction
|
||||
|
||||
[System description in 2-3 sentences. Target user and deployment model.]
|
||||
|
||||
## Glossary
|
||||
|
||||
- **Term**: Definition specific to this system
|
||||
- **Component**: Major system module or service
|
||||
[Add all domain-specific terms]
|
||||
|
||||
## Requirements
|
||||
|
||||
### Requirement [NUMBER]
|
||||
|
||||
**User Story:** As a [user type], I want [capability], so that [benefit]
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN [trigger/condition], THE [component] SHALL [action/behavior]
|
||||
2. WHERE [mode/context], THE [component] SHALL [action/behavior]
|
||||
3. IF [condition], THEN THE [component] SHALL [action/behavior]
|
||||
4. THE [component] SHALL [capability with measurable target]
|
||||
|
||||
[Repeat for each requirement]
|
||||
```
|
||||
|
||||
### Requirements Best Practices
|
||||
|
||||
1. **One capability per requirement** - Each requirement should address a single feature
|
||||
2. **Testable criteria** - Every criterion must be verifiable
|
||||
3. **Use SHALL for mandatory** - Consistent RFC 2119 keywords
|
||||
4. **Include performance targets** - "within X milliseconds/seconds"
|
||||
5. **Specify all states** - Success, failure, edge cases
|
||||
6. **Number systematically** - REQ-1, REQ-2 for traceability
|
||||
|
||||
### Acceptance Criteria Patterns
|
||||
|
||||
```
|
||||
Behavior criteria:
|
||||
- WHEN [event occurs], THE system SHALL [respond]
|
||||
- THE system SHALL [provide capability]
|
||||
- THE system SHALL [enforce rule/limit]
|
||||
|
||||
Conditional criteria:
|
||||
- IF [condition], THEN THE system SHALL [action]
|
||||
- WHERE [mode is active], THE system SHALL [behavior]
|
||||
|
||||
Performance criteria:
|
||||
- THE system SHALL [complete action] within [time]
|
||||
- THE system SHALL support [number] concurrent [operations]
|
||||
- THE system SHALL maintain [metric] above/below [threshold]
|
||||
|
||||
Data criteria:
|
||||
- THE system SHALL persist [data type] with [attributes]
|
||||
- THE system SHALL validate [input] against [rules]
|
||||
- THE system SHALL return [data] in [format]
|
||||
```
|
||||
|
||||
## Design Document Template
|
||||
|
||||
```markdown
|
||||
# Design Document
|
||||
|
||||
## Overview
|
||||
|
||||
[System architecture summary in 3-4 sentences. Key design decisions and priorities.]
|
||||
|
||||
## System Architecture
|
||||
|
||||
### Component Map
|
||||
|
||||
| Component ID | Name | Type | Responsibility | Interfaces With |
|
||||
|-------------|------|------|----------------|-----------------|
|
||||
| COMP-1 | Web Frontend | UI | User interface | COMP-2 |
|
||||
| COMP-2 | API Gateway | Service | Request routing | COMP-3, COMP-4 |
|
||||
| COMP-3 | Business Logic | Service | Core processing | COMP-5 |
|
||||
[Complete component inventory]
|
||||
|
||||
### High-Level Architecture Diagram
|
||||
|
||||
[ASCII diagram showing all components and their relationships]
|
||||
|
||||
## Data Flow Specifications
|
||||
|
||||
### Primary Data Flows
|
||||
|
||||
#### 1. [Flow Name] (e.g., User Authentication)
|
||||
|
||||
```
|
||||
1. [Source] → [Component]: [Data description]
|
||||
2. [Component] → [Component]: [Transformation applied]
|
||||
3. [Component] → [Destination]: [Final data format]
|
||||
```
|
||||
|
||||
**Data Transformations:**
|
||||
- Step 2: [How data changes]
|
||||
- Step 3: [Validation/Processing applied]
|
||||
|
||||
[Repeat for each major data flow]
|
||||
|
||||
## Integration Points
|
||||
|
||||
### Internal Integration Points
|
||||
|
||||
| Source | Target | Protocol | Data Format | Purpose |
|
||||
|--------|--------|----------|-------------|---------|
|
||||
| Frontend | API Gateway | HTTPS/REST | JSON | API calls |
|
||||
| API Gateway | Auth Service | gRPC | Protobuf | Authentication |
|
||||
[All internal integrations]
|
||||
|
||||
### External Integration Points
|
||||
|
||||
#### [External System Name]
|
||||
|
||||
**Type:** REST API / Database / Message Queue / etc.
|
||||
**Purpose:** [What this integration provides]
|
||||
**Endpoint:** [URL/Connection string pattern]
|
||||
**Authentication:** [Method - OAuth2, API Key, etc.]
|
||||
**Rate Limits:** [Any constraints]
|
||||
|
||||
**Interface Contract:**
|
||||
```language
|
||||
// Request format
|
||||
POST /api/endpoint
|
||||
{
|
||||
"field": "type"
|
||||
}
|
||||
|
||||
// Response format
|
||||
{
|
||||
"result": "type"
|
||||
}
|
||||
```
|
||||
|
||||
**Error Handling:**
|
||||
- Retry strategy: [Exponential backoff, circuit breaker]
|
||||
- Fallback: [What happens if unavailable]
|
||||
- Monitoring: [How to detect issues]
|
||||
|
||||
[Repeat for each external integration]
|
||||
|
||||
## Components and Interfaces
|
||||
|
||||
### 1. [Component Name]
|
||||
|
||||
**Responsibility:** [Single sentence description]
|
||||
|
||||
**Key Classes:**
|
||||
- `ClassName`: [Purpose and main methods]
|
||||
- `ServiceName`: [What it manages]
|
||||
|
||||
**Interfaces:**
|
||||
```language
|
||||
class InterfaceName:
|
||||
def method_name(params) -> ReturnType
|
||||
# Core methods only
|
||||
```
|
||||
|
||||
**Data Flow:**
|
||||
- Receives [input] from [source]
|
||||
- Processes by [algorithm/logic]
|
||||
- Outputs [result] to [destination]
|
||||
|
||||
**Performance:**
|
||||
- Target: [metric and value]
|
||||
- Constraints: [limitations]
|
||||
|
||||
[Repeat for each major component]
|
||||
|
||||
## Data Models
|
||||
|
||||
### [Entity Name]
|
||||
```language
|
||||
@dataclass
|
||||
class EntityName:
|
||||
field: Type
|
||||
field: Optional[Type]
|
||||
# Core fields only
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
### [Error Category]
|
||||
**Types:** [List of error scenarios]
|
||||
**Handling:** [Strategy and recovery]
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
### Unit Tests
|
||||
- [Component]: Test [aspects]
|
||||
- Coverage target: 80%
|
||||
|
||||
### Integration Tests
|
||||
- [Flow]: Test [end-to-end scenario]
|
||||
|
||||
### Performance Tests
|
||||
- [Operation]: Target [metric]
|
||||
|
||||
## Deployment
|
||||
|
||||
### Docker Configuration
|
||||
```yaml
|
||||
# Essential service definitions only
|
||||
```
|
||||
|
||||
### Environment Variables
|
||||
```
|
||||
CATEGORY_VAR=description
|
||||
```
|
||||
|
||||
## Performance Targets
|
||||
|
||||
- [Operation]: <[time]
|
||||
- [Throughput]: >[rate]
|
||||
- [Resource]: <[limit]
|
||||
|
||||
## Security Considerations
|
||||
|
||||
- [Authentication method if applicable]
|
||||
- [Data protection approach]
|
||||
- [Access control model]
|
||||
```
|
||||
|
||||
### Design Best Practices
|
||||
|
||||
1. **Component responsibilities** - Single, clear purpose per component
|
||||
2. **Interface first** - Define contracts before implementation
|
||||
3. **Data flow clarity** - Show how data moves through system
|
||||
4. **Error categories** - Group related failures with consistent handling
|
||||
5. **Performance targets** - Specific, measurable goals
|
||||
6. **Deployment ready** - Include Docker and configuration
|
||||
|
||||
## Implementation Plan Template
|
||||
|
||||
```markdown
|
||||
# Implementation Plan
|
||||
|
||||
- [x] 1. [Phase Name]
|
||||
|
||||
- [x] 1.1 [Task name]
|
||||
- [Subtask description]
|
||||
- [Subtask description]
|
||||
- _Requirements: [REQ-X.Y, REQ-A.B]_
|
||||
|
||||
- [ ] 1.2 [Task name]
|
||||
- [Subtask description]
|
||||
- _Requirements: [REQ-X.Y]_
|
||||
- _Dependencies: Task 1.1_
|
||||
|
||||
- [ ] 2. [Phase Name]
|
||||
|
||||
- [ ] 2.1 [Task name]
|
||||
- [Detailed steps or subtasks]
|
||||
- _Requirements: [REQ-X.Y]_
|
||||
- _Dependencies: Phase 1_
|
||||
|
||||
[Continue for all phases]
|
||||
```
|
||||
|
||||
### Task Planning Best Practices
|
||||
|
||||
1. **Hierarchical structure** - Phases > Tasks > Subtasks
|
||||
2. **Requirement tracing** - Link each task to requirements
|
||||
3. **Dependency marking** - Identify blockers and prerequisites
|
||||
4. **Checkbox format** - [x] for complete, [ ] for pending
|
||||
5. **Atomic tasks** - Each task independently completable
|
||||
6. **Progressive implementation** - Infrastructure → Core → Features → Polish
|
||||
|
||||
### Common Implementation Phases
|
||||
|
||||
```markdown
|
||||
1. **Infrastructure Setup**
|
||||
- Project structure
|
||||
- Database schema
|
||||
- Docker configuration
|
||||
- Core dependencies
|
||||
|
||||
2. **Data Layer**
|
||||
- Models/entities
|
||||
- Database operations
|
||||
- Migrations
|
||||
|
||||
3. **Business Logic**
|
||||
- Core algorithms
|
||||
- Service classes
|
||||
- Validation rules
|
||||
|
||||
4. **API/Interface Layer**
|
||||
- REST/GraphQL endpoints
|
||||
- WebSocket handlers
|
||||
- Authentication
|
||||
|
||||
5. **Frontend/UI**
|
||||
- Component structure
|
||||
- State management
|
||||
- API integration
|
||||
- Responsive design
|
||||
|
||||
6. **Integration**
|
||||
- External services
|
||||
- Third-party APIs
|
||||
- Message queues
|
||||
|
||||
7. **Testing**
|
||||
- Unit tests
|
||||
- Integration tests
|
||||
- End-to-end tests
|
||||
|
||||
8. **DevOps**
|
||||
- CI/CD pipeline
|
||||
- Monitoring
|
||||
- Logging
|
||||
- Deployment scripts
|
||||
|
||||
9. **Documentation**
|
||||
- API documentation
|
||||
- User guides
|
||||
- Deployment guide
|
||||
- README
|
||||
```
|
||||
|
||||
## Document Patterns by Project Type
|
||||
|
||||
### Web Application (Full-Stack)
|
||||
|
||||
Requirements focus:
|
||||
- User authentication and authorization
|
||||
- CRUD operations for entities
|
||||
- Real-time updates
|
||||
- Responsive UI
|
||||
- API design
|
||||
|
||||
Design focus:
|
||||
- 3-tier architecture (Frontend, Backend, Database)
|
||||
- REST/GraphQL API design
|
||||
- State management strategy
|
||||
- Component hierarchy
|
||||
- Database schema
|
||||
|
||||
Tasks focus:
|
||||
1. Database and backend setup
|
||||
2. API implementation
|
||||
3. Frontend components
|
||||
4. Integration and testing
|
||||
|
||||
### Microservices System
|
||||
|
||||
Requirements focus:
|
||||
- Service boundaries
|
||||
- Inter-service communication
|
||||
- Data consistency
|
||||
- Service discovery
|
||||
- Fault tolerance
|
||||
|
||||
Design focus:
|
||||
- Service decomposition
|
||||
- API contracts between services
|
||||
- Message queue/event bus
|
||||
- Distributed tracing
|
||||
- Container orchestration
|
||||
|
||||
Tasks focus:
|
||||
1. Service scaffolding
|
||||
2. Shared libraries/contracts
|
||||
3. Individual service implementation
|
||||
4. Integration layer
|
||||
5. Orchestration setup
|
||||
|
||||
### Data Pipeline/ETL
|
||||
|
||||
Requirements focus:
|
||||
- Data sources and formats
|
||||
- Transformation rules
|
||||
- Data quality checks
|
||||
- Schedule/triggers
|
||||
- Error handling and retry
|
||||
|
||||
Design focus:
|
||||
- Pipeline stages
|
||||
- Data flow diagram
|
||||
- Schema evolution
|
||||
- Monitoring and alerting
|
||||
- Storage strategy
|
||||
|
||||
Tasks focus:
|
||||
1. Data source connectors
|
||||
2. Transformation logic
|
||||
3. Validation and quality checks
|
||||
4. Scheduling setup
|
||||
5. Monitoring implementation
|
||||
|
||||
### CLI Tool/Library
|
||||
|
||||
Requirements focus:
|
||||
- Command structure
|
||||
- Input/output formats
|
||||
- Configuration options
|
||||
- Error messages
|
||||
- Performance requirements
|
||||
|
||||
Design focus:
|
||||
- Command parser architecture
|
||||
- Plugin system (if applicable)
|
||||
- Configuration management
|
||||
- Output formatters
|
||||
- Testing strategy
|
||||
|
||||
Tasks focus:
|
||||
1. Core command structure
|
||||
2. Business logic implementation
|
||||
3. Input/output handlers
|
||||
4. Configuration system
|
||||
5. Documentation and examples
|
||||
|
||||
## Generating Documents for Specific Domains
|
||||
|
||||
### Trading/Financial Systems
|
||||
|
||||
Additional requirements:
|
||||
- Risk management rules
|
||||
- Order execution logic
|
||||
- Market data handling
|
||||
- Compliance requirements
|
||||
- Audit trail
|
||||
|
||||
Additional design:
|
||||
- High-frequency data handling
|
||||
- Position tracking
|
||||
- Risk calculations
|
||||
- Order routing
|
||||
- Failover strategies
|
||||
|
||||
### Real-time Systems (Chat, Gaming, IoT)
|
||||
|
||||
Additional requirements:
|
||||
- Latency targets
|
||||
- Connection handling
|
||||
- State synchronization
|
||||
- Offline support
|
||||
- Push notifications
|
||||
|
||||
Additional design:
|
||||
- WebSocket/SSE architecture
|
||||
- State management
|
||||
- Caching strategy
|
||||
- Message queuing
|
||||
- Horizontal scaling
|
||||
|
||||
### Machine Learning Systems
|
||||
|
||||
Additional requirements:
|
||||
- Model training pipeline
|
||||
- Feature engineering
|
||||
- Model versioning
|
||||
- Prediction API
|
||||
- Monitoring/drift detection
|
||||
|
||||
Additional design:
|
||||
- Training infrastructure
|
||||
- Feature store
|
||||
- Model registry
|
||||
- Serving architecture
|
||||
- A/B testing framework
|
||||
|
||||
## Quality Checklist
|
||||
|
||||
Before finalizing documents, verify:
|
||||
|
||||
### Requirements Document
|
||||
- [ ] Every requirement has a clear user story
|
||||
- [ ] All acceptance criteria are testable
|
||||
- [ ] Non-functional requirements specified (performance, security)
|
||||
- [ ] Glossary covers all domain terms
|
||||
- [ ] Requirements are numbered for tracing
|
||||
|
||||
### Design Document
|
||||
- [ ] Architecture diagram included
|
||||
- [ ] All components have clear responsibilities
|
||||
- [ ] Interfaces defined for integration points
|
||||
- [ ] Error handling strategy documented
|
||||
- [ ] Performance targets specified
|
||||
- [ ] Deployment configuration included
|
||||
|
||||
### Implementation Plan
|
||||
- [ ] Tasks grouped into logical phases
|
||||
- [ ] Dependencies identified between tasks
|
||||
- [ ] Requirements traced to tasks
|
||||
- [ ] Tasks are atomic and completable
|
||||
- [ ] Checkbox format for progress tracking
|
||||
|
||||
## Example Usage Patterns
|
||||
|
||||
### Starting a New Project (With Explicit Architecture Focus)
|
||||
|
||||
```
|
||||
User: "I want to build an order flow trading automation system"
|
||||
|
||||
Response: "I'll act as your Project Architect to create comprehensive planning documents.
|
||||
|
||||
First, let me establish the architectural foundation:
|
||||
|
||||
**System Components I need to define:**
|
||||
- Market data ingestion service
|
||||
- Order flow analysis engine
|
||||
- Trading signal generator
|
||||
- Risk management module
|
||||
- Order execution service
|
||||
- Monitoring dashboard
|
||||
|
||||
**Data Flows to map:**
|
||||
- Market data → Analysis → Signals → Execution
|
||||
- Position tracking → Risk calculation → Trading limits
|
||||
- All events → Audit trail
|
||||
|
||||
**Integration Points to specify:**
|
||||
- Exchange APIs (which ones?)
|
||||
- Database system (time-series data needs?)
|
||||
- Notification services (alerts/reporting?)
|
||||
|
||||
**Key Questions:**
|
||||
1. Is this for personal use (single-user) or a platform (multi-user)?
|
||||
2. Which specific exchanges/brokers will it connect to?
|
||||
3. What are your latency requirements (milliseconds matter)?
|
||||
4. What's your risk tolerance and position sizing approach?
|
||||
5. Do you need backtesting capabilities?
|
||||
|
||||
**Deliverables I'll create:**
|
||||
1. Requirements document with traceable user stories
|
||||
2. System design with complete component map and data flows
|
||||
3. Implementation plan with phased delivery milestones
|
||||
|
||||
This architectural blueprint will ensure nothing is overlooked during implementation."
|
||||
```
|
||||
|
||||
### Starting a New Project (Simple Approach)
|
||||
|
||||
```
|
||||
User: "I want to build an order flow trading automation system"
|
||||
|
||||
Response: "I'll help you create comprehensive planning documents for your trading system. Let me gather some key information:
|
||||
|
||||
1. Is this for personal use (single-user) or a multi-user platform?
|
||||
2. Which markets/exchanges will it connect to?
|
||||
3. What are the core trading strategies or patterns?
|
||||
4. Any specific performance requirements (latency, throughput)?
|
||||
|
||||
Based on this, I'll generate:
|
||||
- Requirements document with user stories
|
||||
- System design with components and data flow
|
||||
- Implementation plan with phases and tasks"
|
||||
```
|
||||
|
||||
### Expanding Existing Requirements
|
||||
|
||||
```
|
||||
User: "Add real-time monitoring dashboard to the requirements"
|
||||
|
||||
Response: "I'll add dashboard requirements to your document:
|
||||
|
||||
### Requirement 15
|
||||
**User Story:** As a trader, I want a real-time dashboard, so that I can monitor system performance and trading activity
|
||||
|
||||
#### Acceptance Criteria
|
||||
1. THE Dashboard SHALL display active positions with current P&L
|
||||
2. THE Dashboard SHALL update market data within 1 second
|
||||
3. THE Dashboard SHALL provide filtering by symbol, timeframe, and date range
|
||||
4. WHEN a trade executes, THE Dashboard SHALL reflect it within 100ms"
|
||||
```
|
||||
|
||||
## Common Pitfalls to Avoid
|
||||
|
||||
### Planning Pitfalls
|
||||
1. **Skipping architectural planning** - Jumping to requirements without mapping components first
|
||||
2. **Vague role definition** - Not establishing the architect role leads to unfocused documentation
|
||||
3. **Hidden integration points** - Discovering external dependencies during implementation
|
||||
4. **Undefined boundaries** - No clear scope leads to feature creep and timeline slippage
|
||||
5. **Missing data flow analysis** - Not mapping how data moves reveals issues late
|
||||
|
||||
### Requirements Pitfalls
|
||||
1. **Over-specifying implementation** - Requirements should define "what" not "how"
|
||||
2. **Vague acceptance criteria** - Avoid "user-friendly" or "fast" without metrics
|
||||
3. **Missing error cases** - Include failure scenarios in requirements
|
||||
4. **Untraceable requirements** - Every requirement should map to tasks
|
||||
|
||||
### Design Pitfalls
|
||||
1. **Monolithic components** - Break down large components into focused services
|
||||
2. **Circular dependencies** - Ensure task dependencies form a DAG
|
||||
3. **Missing data models** - Define core entities early
|
||||
4. **Ignoring deployment** - Include Docker/deployment from the start
|
||||
5. **Unclear component boundaries** - Each component needs explicit responsibilities
|
||||
|
||||
## Output Format
|
||||
|
||||
Generate documents in Markdown format for easy editing and version control. Use:
|
||||
- Clear hierarchical headings (##, ###, ####)
|
||||
- Code blocks with language hints
|
||||
- Bulleted and numbered lists
|
||||
- Tables for structured data
|
||||
- Checkboxes for task tracking
|
||||
- Bold for emphasis on key terms
|
||||
- Inline code for technical terms
|
||||
|
||||
Save documents as:
|
||||
- `requirements.md` - Requirements document
|
||||
- `design.md` - Design document
|
||||
- `tasks.md` - Implementation plan
|
||||
|
||||
These documents serve as the foundation for AI-assisted implementation, providing clear specifications that can be referenced throughout development.
|
||||
50
.trae/skills/project-planner/SKILLSTORE.md
Normal file
50
.trae/skills/project-planner/SKILLSTORE.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# project-planner
|
||||
|
||||
Comprehensive project planning and documentation generator for software projects. Creates structured requirements documents, system design documents, and task breakdown plans with implementation tracking. Use when starting a new project, defining specifications, creating technical designs, or breaking down complex systems into implementable tasks. Supports user story format, acceptance criteria, component design, API specifications, and hierarchical task decomposition with requirement traceability.
|
||||
|
||||
---
|
||||
|
||||
## 📦 Downloaded from [Skillstore.io](https://skillstore.io)
|
||||
|
||||
This skill was downloaded from **AI Skillstore** — the official marketplace for Claude Code, Codex, and Claude skills.
|
||||
|
||||
🔗 **Skill Page**: [skillstore.io/skills/dwsy-project-planner](https://skillstore.io/skills/dwsy-project-planner)
|
||||
|
||||
## 🚀 Installation
|
||||
|
||||
### Via Claude Code Plugin System
|
||||
|
||||
```
|
||||
/plugin marketplace add aiskillstore/marketplace
|
||||
/plugin install dwsy-project-planner@aiskillstore
|
||||
```
|
||||
|
||||
### Manual Installation
|
||||
|
||||
Copy the contents of this folder to your project's `.claude/skills/` directory.
|
||||
|
||||
## 📋 Skill Info
|
||||
|
||||
| Property | Value |
|
||||
|----------|-------|
|
||||
| **Name** | project-planner |
|
||||
| **Version** | 1.0.0 |
|
||||
| **Author** | Dwsy |
|
||||
|
||||
### Supported Tools
|
||||
|
||||
- claude
|
||||
- codex
|
||||
- claude-code
|
||||
|
||||
## 🌐 Discover More Skills
|
||||
|
||||
Browse thousands of AI skills at **[skillstore.io](https://skillstore.io)**:
|
||||
|
||||
- 🔍 Search by category, tool, or keyword
|
||||
- ⭐ Find verified, security-audited skills
|
||||
- 📤 Submit your own skills to share with the community
|
||||
|
||||
---
|
||||
|
||||
*From [skillstore.io](https://skillstore.io) — AI Skills Marketplace*
|
||||
83
.trae/skills/project-planner/assets/requirements-template.md
Normal file
83
.trae/skills/project-planner/assets/requirements-template.md
Normal file
@@ -0,0 +1,83 @@
|
||||
# Requirements Document Template
|
||||
|
||||
## Introduction
|
||||
|
||||
[PROJECT NAME] is a [SYSTEM TYPE] designed for [TARGET USERS]. The system [PRIMARY PURPOSE].
|
||||
|
||||
## System Context
|
||||
|
||||
### Architectural Overview
|
||||
- **Components:** [List major system components]
|
||||
- **Data Flow:** [High-level data movement]
|
||||
- **Integration Points:** [External systems/APIs]
|
||||
- **Deployment Model:** [Cloud/On-premise/Hybrid]
|
||||
|
||||
## Glossary
|
||||
|
||||
- **[Term]**: [Definition specific to this system]
|
||||
- **Component**: Major system module or service
|
||||
- **Integration Point**: Connection to external system or API
|
||||
|
||||
## Functional Requirements
|
||||
|
||||
### REQ-1: [Feature Name]
|
||||
|
||||
**User Story:** As a [user role], I want [feature], so that [benefit]
|
||||
|
||||
**Acceptance Criteria:**
|
||||
1. WHEN [condition], THE system SHALL [behavior]
|
||||
2. THE system SHALL [requirement] within [time constraint]
|
||||
3. IF [error condition], THEN THE system SHALL [error handling]
|
||||
|
||||
**Components Involved:** [COMP-1, COMP-2]
|
||||
**Data Flow:** [How data moves for this requirement]
|
||||
|
||||
### REQ-2: [Feature Name]
|
||||
|
||||
**User Story:** As a [user role], I want [feature], so that [benefit]
|
||||
|
||||
**Acceptance Criteria:**
|
||||
1. WHEN [condition], THE system SHALL [behavior]
|
||||
2. WHERE [context], THE system SHALL [behavior]
|
||||
3. THE system SHALL persist [data] with [attributes]
|
||||
|
||||
**Components Involved:** [COMP-3, COMP-4]
|
||||
**Integration Points:** [External systems used]
|
||||
|
||||
## Non-Functional Requirements
|
||||
|
||||
### Performance Requirements
|
||||
- Response time: THE system SHALL respond to user requests within [X] milliseconds
|
||||
- Throughput: THE system SHALL handle [X] concurrent users
|
||||
- Data processing: THE system SHALL process [X] records per second
|
||||
|
||||
### Security Requirements
|
||||
- Authentication: THE system SHALL implement [auth method]
|
||||
- Authorization: THE system SHALL enforce role-based access control
|
||||
- Data protection: THE system SHALL encrypt sensitive data at rest and in transit
|
||||
|
||||
### Reliability Requirements
|
||||
- Availability: THE system SHALL maintain 99.9% uptime
|
||||
- Recovery: THE system SHALL recover from failures within [X] minutes
|
||||
- Data integrity: THE system SHALL ensure ACID compliance for transactions
|
||||
|
||||
### Scalability Requirements
|
||||
- THE system SHALL support horizontal scaling
|
||||
- THE system SHALL handle [X]% growth in users annually
|
||||
- THE system SHALL support database sharding for data volumes exceeding [X]
|
||||
|
||||
## Constraints and Boundaries
|
||||
|
||||
### Technical Constraints
|
||||
- Technology: [Programming languages, frameworks, databases]
|
||||
- Infrastructure: [Cloud provider, hardware limitations]
|
||||
|
||||
### Business Constraints
|
||||
- Budget: [Cost limitations]
|
||||
- Timeline: [Delivery deadlines]
|
||||
- Compliance: [Regulatory requirements]
|
||||
|
||||
### Scope Boundaries
|
||||
- **In Scope:** [What's included]
|
||||
- **Out of Scope:** [What's explicitly excluded]
|
||||
- **Future Considerations:** [Deferred features]
|
||||
13
.trae/skills/project-planner/plugin.json
Normal file
13
.trae/skills/project-planner/plugin.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "project-planner-skill",
|
||||
"description": "Comprehensive project planning and documentation generator for software projects. Creates structured requirements documents, system design documents, and task breakdown plans with implementation tracking.",
|
||||
"version": "1.0.0",
|
||||
"author": {
|
||||
"name": "George A Puiu",
|
||||
"email": "puiu.adrian@gmail.com"
|
||||
},
|
||||
"homepage": "https://github.com/adrianpuiu/claude-skills-marketplace",
|
||||
"repository": "https://github.com/adrianpuiu/claude-skills-marketplace",
|
||||
"license": "MIT",
|
||||
"keywords": ["project-planning", "documentation", "requirements", "design", "implementation"]
|
||||
}
|
||||
1379
.trae/skills/project-planner/references/domain-templates.md
Normal file
1379
.trae/skills/project-planner/references/domain-templates.md
Normal file
File diff suppressed because it is too large
Load Diff
801
.trae/skills/project-planner/scripts/generate_project_docs.py
Normal file
801
.trae/skills/project-planner/scripts/generate_project_docs.py
Normal file
@@ -0,0 +1,801 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Project Document Generator
|
||||
Generates structured requirements, design, and task documents for new projects
|
||||
"""
|
||||
|
||||
import json
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
from typing import Dict, List, Optional
|
||||
import os
|
||||
|
||||
class ProjectDocumentGenerator:
|
||||
def __init__(self, project_name: str, project_type: str = "web-app"):
|
||||
self.project_name = project_name
|
||||
self.project_type = project_type
|
||||
self.timestamp = datetime.now().strftime("%Y-%m-%d")
|
||||
|
||||
def generate_requirements_template(self, features: List[str]) -> str:
|
||||
"""Generate requirements document template"""
|
||||
|
||||
template = f"""# Requirements Document
|
||||
|
||||
## Introduction
|
||||
|
||||
{self.project_name} is a [DESCRIPTION OF SYSTEM PURPOSE]. The system is designed for [TARGET USERS] and will be deployed as [DEPLOYMENT MODEL].
|
||||
|
||||
## Glossary
|
||||
|
||||
- **[Term]**: [Definition specific to this system]
|
||||
- **User**: [Define user types]
|
||||
- **System**: The {self.project_name} platform
|
||||
|
||||
## Requirements
|
||||
"""
|
||||
|
||||
for i, feature in enumerate(features, 1):
|
||||
template += f"""
|
||||
### Requirement {i}
|
||||
|
||||
**User Story:** As a [USER TYPE], I want {feature}, so that [BENEFIT]
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN [trigger/condition], THE system SHALL [behavior]
|
||||
2. WHERE [context applies], THE system SHALL [behavior]
|
||||
3. THE system SHALL [capability] within [time limit]
|
||||
4. IF [error condition], THEN THE system SHALL [handle gracefully]
|
||||
5. THE system SHALL persist [data] with [attributes]
|
||||
"""
|
||||
|
||||
return template
|
||||
|
||||
def generate_design_template(self, components: List[str]) -> str:
|
||||
"""Generate design document template with comprehensive architecture"""
|
||||
|
||||
template = f"""# Design Document
|
||||
|
||||
## Overview
|
||||
|
||||
The {self.project_name} system is built as a [ARCHITECTURE PATTERN] with [KEY COMPONENTS]. The design prioritizes [KEY PRIORITIES].
|
||||
|
||||
## System Architecture
|
||||
|
||||
### Component Map
|
||||
|
||||
| Component ID | Name | Type | Responsibility | Interfaces With |
|
||||
|-------------|------|------|----------------|-----------------|
|
||||
| COMP-1 | Frontend | UI | User interface and interaction | COMP-2 |
|
||||
| COMP-2 | API Gateway | Service | Request routing and authentication | COMP-3, COMP-4 |"""
|
||||
|
||||
for i, component in enumerate(components, 3):
|
||||
template += f"""
|
||||
| COMP-{i} | {component} | Service | [Responsibility] | [Components] |"""
|
||||
|
||||
template += """
|
||||
|
||||
### High-Level Architecture Diagram
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ Frontend Layer │
|
||||
│ ┌──────────────────────────────────────────────────────┐ │
|
||||
│ │ [UI Framework] Application │ │
|
||||
│ └──────────────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
API (REST/GraphQL/WebSocket)
|
||||
│
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ Backend Layer │
|
||||
│ ┌──────────────────────────────────────────────────────┐ │
|
||||
│ │ [Backend Framework] Application │ │
|
||||
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
|
||||
│ │ │ Service │ │ Service │ │ Service │ │ │
|
||||
│ │ └──────────┘ └──────────┘ └──────────┘ │ │
|
||||
│ └──────────────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
Database Access
|
||||
│
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ Data Layer │
|
||||
│ ┌──────────────────────────────────────────────────────┐ │
|
||||
│ │ [Database Type] │ │
|
||||
│ └──────────────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Data Flow Specifications
|
||||
|
||||
### Primary Data Flows
|
||||
|
||||
#### 1. User Authentication Flow
|
||||
|
||||
```
|
||||
1. User → Frontend: Login credentials
|
||||
2. Frontend → API Gateway: Encrypted credentials
|
||||
3. API Gateway → Auth Service: Validation request
|
||||
4. Auth Service → User Database: Query user record
|
||||
5. User Database → Auth Service: User data
|
||||
6. Auth Service → API Gateway: JWT token
|
||||
7. API Gateway → Frontend: Auth response with token
|
||||
```
|
||||
|
||||
**Data Transformations:**
|
||||
- Step 2: Credentials encrypted with HTTPS
|
||||
- Step 3: Rate limiting applied
|
||||
- Step 6: JWT token generated with claims
|
||||
|
||||
[Add other critical data flows]
|
||||
|
||||
## Integration Points
|
||||
|
||||
### Internal Integration Points
|
||||
|
||||
| Source | Target | Protocol | Data Format | Purpose |
|
||||
|--------|--------|----------|-------------|---------|
|
||||
| Frontend | API Gateway | HTTPS/REST | JSON | API calls |
|
||||
| API Gateway | Services | HTTP/gRPC | JSON/Protobuf | Service calls |
|
||||
| Services | Database | TCP | SQL | Data persistence |
|
||||
|
||||
### External Integration Points
|
||||
|
||||
#### [External Service Name]
|
||||
|
||||
**Type:** REST API / Database / Message Queue
|
||||
**Purpose:** [What this integration provides]
|
||||
**Endpoint:** [URL pattern or connection details]
|
||||
**Authentication:** [OAuth2, API Key, etc.]
|
||||
**Rate Limits:** [Any constraints]
|
||||
|
||||
**Interface Contract:**
|
||||
```
|
||||
POST /api/endpoint
|
||||
Headers: { "Authorization": "Bearer token" }
|
||||
Body: { "field": "value" }
|
||||
Response: { "result": "value" }
|
||||
```
|
||||
|
||||
**Error Handling:**
|
||||
- Retry strategy: Exponential backoff with jitter
|
||||
- Circuit breaker: Opens after 5 consecutive failures
|
||||
- Fallback: [Degraded functionality or cached response]
|
||||
|
||||
## System Boundaries
|
||||
|
||||
### In Scope
|
||||
- [Core functionality included]
|
||||
- [Features to be implemented]
|
||||
|
||||
### Out of Scope
|
||||
- [Features not included]
|
||||
- [Delegated to external systems]
|
||||
|
||||
### Assumptions
|
||||
- [External services available]
|
||||
- [Infrastructure provided]
|
||||
|
||||
## Components and Interfaces
|
||||
"""
|
||||
|
||||
for component in components:
|
||||
template += f"""
|
||||
### {component}
|
||||
|
||||
**Responsibility:** [Single sentence description of what this component does]
|
||||
|
||||
**Key Classes:**
|
||||
- `{component}Service`: Main service class for {component.lower()} operations
|
||||
- `{component}Controller`: Handles API requests for {component.lower()}
|
||||
- `{component}Repository`: Data access layer for {component.lower()}
|
||||
|
||||
**Interfaces:**
|
||||
```python
|
||||
class {component}Service:
|
||||
async def create(self, data: Dict) -> {component}
|
||||
async def get(self, id: str) -> Optional[{component}]
|
||||
async def update(self, id: str, data: Dict) -> {component}
|
||||
async def delete(self, id: str) -> bool
|
||||
async def list(self, filters: Dict) -> List[{component}]
|
||||
```
|
||||
|
||||
**Data Flow:**
|
||||
- Receives requests from [API layer/other service]
|
||||
- Validates input using [validation rules]
|
||||
- Processes business logic
|
||||
- Persists to database
|
||||
- Returns response
|
||||
|
||||
**Performance:**
|
||||
- Target response time: <200ms for queries
|
||||
- Target response time: <500ms for mutations
|
||||
- Maximum concurrent operations: 100
|
||||
"""
|
||||
|
||||
template += """
|
||||
## Data Models
|
||||
|
||||
### User
|
||||
```python
|
||||
@dataclass
|
||||
class User:
|
||||
id: str
|
||||
email: str
|
||||
name: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
```
|
||||
|
||||
[Add other data models]
|
||||
|
||||
## Error Handling
|
||||
|
||||
### API Errors
|
||||
**Types:**
|
||||
- 400 Bad Request - Invalid input
|
||||
- 401 Unauthorized - Missing/invalid authentication
|
||||
- 403 Forbidden - Insufficient permissions
|
||||
- 404 Not Found - Resource doesn't exist
|
||||
- 500 Internal Server Error - Unexpected error
|
||||
|
||||
**Handling:**
|
||||
- Return consistent error format with code, message, and details
|
||||
- Log all errors with context
|
||||
- Implement retry logic for transient failures
|
||||
|
||||
### Database Errors
|
||||
**Types:**
|
||||
- Connection failures
|
||||
- Query timeouts
|
||||
- Constraint violations
|
||||
|
||||
**Handling:**
|
||||
- Retry with exponential backoff
|
||||
- Graceful degradation where possible
|
||||
- Transaction rollback on failure
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
### Unit Tests
|
||||
- Service layer: Test business logic with mocked dependencies
|
||||
- Repository layer: Test database operations
|
||||
- API layer: Test request/response handling
|
||||
- Coverage target: 80%
|
||||
|
||||
### Integration Tests
|
||||
- End-to-end API tests
|
||||
- Database integration tests
|
||||
- External service integration tests
|
||||
|
||||
### Performance Tests
|
||||
- Load testing: 100 concurrent users
|
||||
- Response time: p95 < 500ms
|
||||
- Throughput: >100 requests/second
|
||||
|
||||
## Deployment
|
||||
|
||||
### Docker Configuration
|
||||
```yaml
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
- DATABASE_URL=${DATABASE_URL}
|
||||
depends_on:
|
||||
- database
|
||||
|
||||
database:
|
||||
image: postgres:15
|
||||
volumes:
|
||||
- db_data:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=${DB_PASSWORD}
|
||||
|
||||
volumes:
|
||||
db_data:
|
||||
```
|
||||
|
||||
### Environment Variables
|
||||
```
|
||||
DATABASE_URL=postgresql://user:pass@localhost/dbname
|
||||
API_KEY=your-api-key
|
||||
JWT_SECRET=your-secret-key
|
||||
NODE_ENV=production
|
||||
```
|
||||
|
||||
## Performance Targets
|
||||
|
||||
- API response time: <200ms (p95)
|
||||
- Database query time: <50ms (p95)
|
||||
- Frontend load time: <2s
|
||||
- Time to interactive: <3s
|
||||
- Memory usage: <512MB per instance
|
||||
|
||||
## Security Considerations
|
||||
|
||||
- JWT-based authentication
|
||||
- Rate limiting on all endpoints
|
||||
- Input validation and sanitization
|
||||
- SQL injection prevention via parameterized queries
|
||||
- XSS prevention via output encoding
|
||||
- HTTPS only in production
|
||||
"""
|
||||
|
||||
return template
|
||||
|
||||
def generate_tasks_template(self, phases: List[Dict]) -> str:
|
||||
"""Generate implementation plan template with boundaries and deliverables"""
|
||||
|
||||
template = f"""# Implementation Plan
|
||||
|
||||
Generated: {self.timestamp}
|
||||
Project: {self.project_name}
|
||||
Type: {self.project_type}
|
||||
|
||||
## Project Boundaries
|
||||
|
||||
### Must Have (MVP)
|
||||
- [Core feature 1]
|
||||
- [Core feature 2]
|
||||
- [Core feature 3]
|
||||
|
||||
### Nice to Have (Enhancements)
|
||||
- [Enhancement feature 1]
|
||||
- [Enhancement feature 2]
|
||||
|
||||
### Out of Scope
|
||||
- [Explicitly excluded feature 1]
|
||||
- [Deferred to future phase]
|
||||
|
||||
### Technical Constraints
|
||||
- [Framework limitations]
|
||||
- [Resource constraints]
|
||||
|
||||
## Deliverables by Phase
|
||||
|
||||
| Phase | Deliverables | Success Criteria |
|
||||
|-------|-------------|------------------|
|
||||
| 1. Infrastructure | Working development environment | All developers can run locally |
|
||||
| 2. Data Layer | Database schema, models | CRUD operations functional |
|
||||
| 3. Business Logic | Core services implemented | All requirements fulfilled |
|
||||
| 4. API Layer | REST/GraphQL endpoints | API tests passing |
|
||||
| 5. Frontend | User interface | End-to-end workflows complete |
|
||||
| 6. Testing | Test coverage >80% | All tests passing |
|
||||
| 7. Deployment | Production environment | System accessible and stable |
|
||||
|
||||
## Task Breakdown
|
||||
|
||||
"""
|
||||
|
||||
for phase_num, phase in enumerate(phases, 1):
|
||||
template += f"- [ ] {phase_num}. {phase['name']}\n\n"
|
||||
|
||||
for task_num, task in enumerate(phase.get('tasks', []), 1):
|
||||
template += f" - [ ] {phase_num}.{task_num} {task['name']}\n"
|
||||
|
||||
if 'subtasks' in task:
|
||||
for subtask in task['subtasks']:
|
||||
template += f" - {subtask}\n"
|
||||
|
||||
if 'requirements' in task:
|
||||
template += f" - _Requirements: {', '.join(task['requirements'])}_\n"
|
||||
|
||||
if 'dependencies' in task and task['dependencies']:
|
||||
template += f" - _Dependencies: {', '.join(task['dependencies'])}_\n"
|
||||
|
||||
template += "\n"
|
||||
|
||||
return template
|
||||
|
||||
def get_default_phases(self) -> List[Dict]:
|
||||
"""Get default phases based on project type"""
|
||||
|
||||
if self.project_type == "web-app":
|
||||
return [
|
||||
{
|
||||
"name": "Infrastructure Setup",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Initialize project structure",
|
||||
"subtasks": [
|
||||
"Create directory structure",
|
||||
"Initialize package managers",
|
||||
"Set up version control"
|
||||
],
|
||||
"requirements": ["REQ-12.1"]
|
||||
},
|
||||
{
|
||||
"name": "Set up database",
|
||||
"subtasks": [
|
||||
"Create database schema",
|
||||
"Write migrations",
|
||||
"Set up connection pooling"
|
||||
],
|
||||
"requirements": ["REQ-9.1", "REQ-9.2"]
|
||||
},
|
||||
{
|
||||
"name": "Configure Docker",
|
||||
"subtasks": [
|
||||
"Create Dockerfiles",
|
||||
"Write docker-compose.yml",
|
||||
"Set up volumes and networks"
|
||||
],
|
||||
"requirements": ["REQ-12.2", "REQ-12.3"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Backend Implementation",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Create data models",
|
||||
"subtasks": [
|
||||
"Define entities",
|
||||
"Create validation schemas",
|
||||
"Implement serialization"
|
||||
],
|
||||
"requirements": ["REQ-3.1"],
|
||||
"dependencies": ["1.2"]
|
||||
},
|
||||
{
|
||||
"name": "Implement service layer",
|
||||
"subtasks": [
|
||||
"Create business logic services",
|
||||
"Implement validation rules",
|
||||
"Add error handling"
|
||||
],
|
||||
"requirements": ["REQ-4.1"],
|
||||
"dependencies": ["2.1"]
|
||||
},
|
||||
{
|
||||
"name": "Build API endpoints",
|
||||
"subtasks": [
|
||||
"Create REST/GraphQL routes",
|
||||
"Add authentication middleware",
|
||||
"Implement request validation"
|
||||
],
|
||||
"requirements": ["REQ-5.1"],
|
||||
"dependencies": ["2.2"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Frontend Implementation",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Set up frontend framework",
|
||||
"subtasks": [
|
||||
"Initialize React/Vue/Angular app",
|
||||
"Configure build tools",
|
||||
"Set up routing"
|
||||
],
|
||||
"requirements": ["REQ-7.1"]
|
||||
},
|
||||
{
|
||||
"name": "Create UI components",
|
||||
"subtasks": [
|
||||
"Build reusable components",
|
||||
"Implement responsive design",
|
||||
"Add styling/theming"
|
||||
],
|
||||
"requirements": ["REQ-7.2"],
|
||||
"dependencies": ["3.1"]
|
||||
},
|
||||
{
|
||||
"name": "Integrate with backend",
|
||||
"subtasks": [
|
||||
"Set up API client",
|
||||
"Implement state management",
|
||||
"Add error handling"
|
||||
],
|
||||
"requirements": ["REQ-7.3"],
|
||||
"dependencies": ["2.3", "3.2"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Testing and Quality Assurance",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Write unit tests",
|
||||
"subtasks": [
|
||||
"Test services",
|
||||
"Test components",
|
||||
"Test utilities"
|
||||
],
|
||||
"requirements": ["REQ-13.1"],
|
||||
"dependencies": ["2.2", "3.2"]
|
||||
},
|
||||
{
|
||||
"name": "Create integration tests",
|
||||
"subtasks": [
|
||||
"Test API endpoints",
|
||||
"Test database operations",
|
||||
"Test external integrations"
|
||||
],
|
||||
"requirements": ["REQ-13.2"],
|
||||
"dependencies": ["4.1"]
|
||||
},
|
||||
{
|
||||
"name": "Perform end-to-end testing",
|
||||
"subtasks": [
|
||||
"Test user workflows",
|
||||
"Test error scenarios",
|
||||
"Performance testing"
|
||||
],
|
||||
"requirements": ["REQ-13.3"],
|
||||
"dependencies": ["4.2"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Deployment and Documentation",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Set up CI/CD pipeline",
|
||||
"subtasks": [
|
||||
"Configure build automation",
|
||||
"Set up test automation",
|
||||
"Configure deployment"
|
||||
],
|
||||
"requirements": ["REQ-14.1"],
|
||||
"dependencies": ["4.3"]
|
||||
},
|
||||
{
|
||||
"name": "Write documentation",
|
||||
"subtasks": [
|
||||
"API documentation",
|
||||
"User guide",
|
||||
"Deployment guide"
|
||||
],
|
||||
"requirements": ["REQ-15.1"],
|
||||
"dependencies": ["5.1"]
|
||||
},
|
||||
{
|
||||
"name": "Deploy to production",
|
||||
"subtasks": [
|
||||
"Set up production environment",
|
||||
"Configure monitoring",
|
||||
"Perform deployment"
|
||||
],
|
||||
"requirements": ["REQ-14.2"],
|
||||
"dependencies": ["5.2"]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
elif self.project_type == "cli-tool":
|
||||
return [
|
||||
{
|
||||
"name": "Project Setup",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Initialize project",
|
||||
"subtasks": [
|
||||
"Set up package structure",
|
||||
"Configure build system",
|
||||
"Add dependencies"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Design command structure",
|
||||
"subtasks": [
|
||||
"Define commands and subcommands",
|
||||
"Plan argument parsing",
|
||||
"Design configuration schema"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Core Implementation",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Implement command parser",
|
||||
"subtasks": [
|
||||
"Create argument parser",
|
||||
"Add command handlers",
|
||||
"Implement help system"
|
||||
],
|
||||
"dependencies": ["1.2"]
|
||||
},
|
||||
{
|
||||
"name": "Build core logic",
|
||||
"subtasks": [
|
||||
"Implement business logic",
|
||||
"Add validation",
|
||||
"Handle errors"
|
||||
],
|
||||
"dependencies": ["2.1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Testing and Packaging",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Write tests",
|
||||
"subtasks": [
|
||||
"Unit tests",
|
||||
"Integration tests",
|
||||
"CLI tests"
|
||||
],
|
||||
"dependencies": ["2.2"]
|
||||
},
|
||||
{
|
||||
"name": "Package and distribute",
|
||||
"subtasks": [
|
||||
"Create package",
|
||||
"Write documentation",
|
||||
"Publish"
|
||||
],
|
||||
"dependencies": ["3.1"]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
elif self.project_type == "api-service":
|
||||
return [
|
||||
{
|
||||
"name": "Service Setup",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Initialize API project",
|
||||
"subtasks": [
|
||||
"Set up framework",
|
||||
"Configure database",
|
||||
"Add middleware"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Design API schema",
|
||||
"subtasks": [
|
||||
"Define endpoints",
|
||||
"Create OpenAPI spec",
|
||||
"Plan authentication"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "API Implementation",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Create endpoints",
|
||||
"subtasks": [
|
||||
"Implement routes",
|
||||
"Add validation",
|
||||
"Handle errors"
|
||||
],
|
||||
"dependencies": ["1.2"]
|
||||
},
|
||||
{
|
||||
"name": "Add authentication",
|
||||
"subtasks": [
|
||||
"Implement auth middleware",
|
||||
"Add JWT/OAuth",
|
||||
"Set up permissions"
|
||||
],
|
||||
"dependencies": ["2.1"]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
else: # Generic project
|
||||
return [
|
||||
{
|
||||
"name": "Project Setup",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Initialize project",
|
||||
"subtasks": ["Create structure", "Set up tools"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Implementation",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Build core features",
|
||||
"subtasks": ["Implement logic", "Add tests"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Deployment",
|
||||
"tasks": [
|
||||
{
|
||||
"name": "Prepare for production",
|
||||
"subtasks": ["Test", "Document", "Deploy"]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
def generate_all_documents(self,
|
||||
features: List[str] = None,
|
||||
components: List[str] = None,
|
||||
output_dir: str = ".") -> Dict[str, str]:
|
||||
"""Generate all three documents"""
|
||||
|
||||
# Use defaults if not provided
|
||||
if not features:
|
||||
features = [
|
||||
"to authenticate and manage my account",
|
||||
"to create and manage resources",
|
||||
"to view analytics and reports",
|
||||
"to configure system settings",
|
||||
"to receive notifications"
|
||||
]
|
||||
|
||||
if not components:
|
||||
components = [
|
||||
"Authentication Service",
|
||||
"User Management",
|
||||
"Resource Manager",
|
||||
"Analytics Engine",
|
||||
"Notification Service"
|
||||
]
|
||||
|
||||
# Generate documents
|
||||
docs = {
|
||||
"requirements.md": self.generate_requirements_template(features),
|
||||
"design.md": self.generate_design_template(components),
|
||||
"tasks.md": self.generate_tasks_template(self.get_default_phases())
|
||||
}
|
||||
|
||||
# Save to files
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
for filename, content in docs.items():
|
||||
filepath = os.path.join(output_dir, filename)
|
||||
with open(filepath, 'w') as f:
|
||||
f.write(content)
|
||||
print(f"Generated: {filepath}")
|
||||
|
||||
return docs
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Generate project planning documents")
|
||||
parser.add_argument("project_name", help="Name of the project")
|
||||
parser.add_argument("--type", default="web-app",
|
||||
choices=["web-app", "cli-tool", "api-service", "generic"],
|
||||
help="Type of project")
|
||||
parser.add_argument("--features", nargs="+",
|
||||
help="List of features for requirements")
|
||||
parser.add_argument("--components", nargs="+",
|
||||
help="List of components for design")
|
||||
parser.add_argument("--output", default=".",
|
||||
help="Output directory for documents")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
generator = ProjectDocumentGenerator(args.project_name, args.type)
|
||||
generator.generate_all_documents(
|
||||
features=args.features,
|
||||
components=args.components,
|
||||
output_dir=args.output
|
||||
)
|
||||
|
||||
print(f"\n✅ Successfully generated project documents for '{args.project_name}'")
|
||||
print(f" Type: {args.type}")
|
||||
print(f" Location: {args.output}/")
|
||||
print("\nNext steps:")
|
||||
print("1. Review and customize the generated documents")
|
||||
print("2. Fill in the [PLACEHOLDER] sections")
|
||||
print("3. Add project-specific requirements and design details")
|
||||
print("4. Use these documents as input for AI-assisted implementation")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
325
.trae/skills/project-planner/scripts/validate_documents.py
Normal file
325
.trae/skills/project-planner/scripts/validate_documents.py
Normal file
@@ -0,0 +1,325 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Document Validator
|
||||
Validates project planning documents for completeness and consistency
|
||||
"""
|
||||
|
||||
import re
|
||||
import argparse
|
||||
from typing import List, Dict, Tuple
|
||||
import os
|
||||
|
||||
class DocumentValidator:
|
||||
def __init__(self):
|
||||
self.errors = []
|
||||
self.warnings = []
|
||||
|
||||
def validate_requirements(self, content: str) -> Tuple[List[str], List[str]]:
|
||||
"""Validate requirements document structure and content"""
|
||||
errors = []
|
||||
warnings = []
|
||||
|
||||
# Check required sections
|
||||
required_sections = [
|
||||
"## Introduction",
|
||||
"## Glossary",
|
||||
"## Requirements"
|
||||
]
|
||||
|
||||
for section in required_sections:
|
||||
if section not in content:
|
||||
errors.append(f"Missing required section: {section}")
|
||||
|
||||
# Check for user stories
|
||||
user_story_pattern = r"\*\*User Story:\*\*.*As a.*I want.*so that"
|
||||
if not re.search(user_story_pattern, content, re.DOTALL):
|
||||
warnings.append("No user stories found in requirements")
|
||||
|
||||
# Check for acceptance criteria
|
||||
if "Acceptance Criteria" not in content:
|
||||
errors.append("No acceptance criteria found")
|
||||
|
||||
# Check for SHALL statements
|
||||
shall_count = content.count("SHALL")
|
||||
if shall_count < 5:
|
||||
warnings.append(f"Only {shall_count} SHALL statements found (recommend at least 5)")
|
||||
|
||||
# Check for requirement numbering
|
||||
req_pattern = r"### Requirement \d+|### REQ-\d+"
|
||||
req_matches = re.findall(req_pattern, content)
|
||||
if len(req_matches) < 3:
|
||||
warnings.append(f"Only {len(req_matches)} numbered requirements found")
|
||||
|
||||
# Check for placeholders
|
||||
placeholder_pattern = r"\[.*?\]"
|
||||
placeholders = re.findall(placeholder_pattern, content)
|
||||
if len(placeholders) > 10:
|
||||
warnings.append(f"Found {len(placeholders)} placeholders - remember to fill them in")
|
||||
|
||||
return errors, warnings
|
||||
|
||||
def validate_design(self, content: str) -> Tuple[List[str], List[str]]:
|
||||
"""Validate design document structure and content"""
|
||||
errors = []
|
||||
warnings = []
|
||||
|
||||
# Check required sections
|
||||
required_sections = [
|
||||
"## Overview",
|
||||
"## System Architecture",
|
||||
"## Data Flow",
|
||||
"## Integration Points",
|
||||
"## Components",
|
||||
"## Data Models",
|
||||
"## Deployment"
|
||||
]
|
||||
|
||||
for section in required_sections:
|
||||
if section not in content:
|
||||
errors.append(f"Missing required section: {section}")
|
||||
|
||||
# Check for component map
|
||||
if "Component Map" not in content and "| Component ID |" not in content:
|
||||
errors.append("Missing Component Map table")
|
||||
|
||||
# Check for data flow specifications
|
||||
if "Data Flow" not in content:
|
||||
errors.append("Missing Data Flow specifications")
|
||||
|
||||
# Check for integration points
|
||||
if "Integration Points" not in content:
|
||||
errors.append("Missing Integration Points section")
|
||||
|
||||
# Check for system boundaries
|
||||
if "System Boundaries" not in content and "In Scope" not in content:
|
||||
warnings.append("Missing System Boundaries definition")
|
||||
|
||||
# Check for architecture diagram
|
||||
if "```" not in content and "┌" not in content:
|
||||
warnings.append("No architecture diagram found")
|
||||
|
||||
# Check for interfaces
|
||||
if "class" not in content and "interface" not in content.lower():
|
||||
warnings.append("No interface definitions found")
|
||||
|
||||
# Check for error handling
|
||||
if "Error Handling" not in content and "error handling" not in content.lower():
|
||||
warnings.append("No error handling section found")
|
||||
|
||||
# Check for performance targets
|
||||
if "Performance" not in content and "performance" not in content.lower():
|
||||
warnings.append("No performance targets specified")
|
||||
|
||||
# Check for Docker configuration
|
||||
if "Docker" not in content and "docker" not in content:
|
||||
warnings.append("No Docker configuration found")
|
||||
|
||||
return errors, warnings
|
||||
|
||||
def validate_tasks(self, content: str) -> Tuple[List[str], List[str]]:
|
||||
"""Validate implementation plan structure and content"""
|
||||
errors = []
|
||||
warnings = []
|
||||
|
||||
# Check for project boundaries
|
||||
if "## Project Boundaries" not in content:
|
||||
errors.append("Missing Project Boundaries section")
|
||||
|
||||
if "Must Have" not in content:
|
||||
warnings.append("Missing 'Must Have' scope definition")
|
||||
|
||||
if "Out of Scope" not in content:
|
||||
warnings.append("Missing 'Out of Scope' definition")
|
||||
|
||||
# Check for deliverables
|
||||
if "## Deliverables" not in content and "Deliverables by Phase" not in content:
|
||||
warnings.append("Missing Deliverables section")
|
||||
|
||||
# Check for success criteria
|
||||
if "Success Criteria" not in content:
|
||||
warnings.append("Missing Success Criteria for deliverables")
|
||||
|
||||
# Check for task structure
|
||||
phase_pattern = r"- \[[ x]\] \d+\."
|
||||
phases = re.findall(phase_pattern, content)
|
||||
|
||||
if len(phases) == 0:
|
||||
errors.append("No phases found in task list")
|
||||
elif len(phases) < 3:
|
||||
warnings.append(f"Only {len(phases)} phases found (recommend at least 3)")
|
||||
|
||||
# Check for subtasks
|
||||
task_pattern = r" - \[[ x]\] \d+\.\d+"
|
||||
tasks = re.findall(task_pattern, content)
|
||||
|
||||
if len(tasks) == 0:
|
||||
errors.append("No tasks found in implementation plan")
|
||||
elif len(tasks) < 10:
|
||||
warnings.append(f"Only {len(tasks)} tasks found (recommend at least 10)")
|
||||
|
||||
# Check for requirement tracing
|
||||
req_pattern = r"_Requirements:.*REQ-\d+|_Requirements:.*\d+\.\d+"
|
||||
req_traces = re.findall(req_pattern, content)
|
||||
|
||||
if len(req_traces) == 0:
|
||||
warnings.append("No requirement tracing found in tasks")
|
||||
elif len(req_traces) < len(tasks) / 2:
|
||||
warnings.append(f"Only {len(req_traces)} tasks have requirement tracing")
|
||||
|
||||
# Check for component involvement
|
||||
comp_pattern = r"_Components:.*COMP-\d+"
|
||||
comp_traces = re.findall(comp_pattern, content)
|
||||
|
||||
if len(comp_traces) == 0:
|
||||
warnings.append("No component mapping found in tasks")
|
||||
|
||||
# Check for dependencies
|
||||
dep_pattern = r"_Dependencies:"
|
||||
dependencies = re.findall(dep_pattern, content)
|
||||
|
||||
if len(dependencies) == 0:
|
||||
warnings.append("No task dependencies defined")
|
||||
|
||||
# Check completion status
|
||||
completed_pattern = r"- \[x\]"
|
||||
pending_pattern = r"- \[ \]"
|
||||
|
||||
completed = len(re.findall(completed_pattern, content))
|
||||
pending = len(re.findall(pending_pattern, content))
|
||||
|
||||
if completed + pending > 0:
|
||||
completion_rate = (completed / (completed + pending)) * 100
|
||||
print(f"Task completion: {completed}/{completed + pending} ({completion_rate:.1f}%)")
|
||||
|
||||
return errors, warnings
|
||||
|
||||
def validate_consistency(self, req_content: str, design_content: str,
|
||||
task_content: str) -> Tuple[List[str], List[str]]:
|
||||
"""Check consistency across documents"""
|
||||
errors = []
|
||||
warnings = []
|
||||
|
||||
# Extract requirement IDs from requirements doc
|
||||
req_ids = set()
|
||||
req_pattern = r"### Requirement (\d+)|### REQ-(\d+)"
|
||||
for match in re.finditer(req_pattern, req_content):
|
||||
req_id = match.group(1) or match.group(2)
|
||||
req_ids.add(f"REQ-{req_id}")
|
||||
|
||||
# Check if requirements are referenced in tasks
|
||||
for req_id in req_ids:
|
||||
if req_id not in task_content:
|
||||
warnings.append(f"{req_id} not referenced in any tasks")
|
||||
|
||||
# Extract components from design
|
||||
component_pattern = r"### .*(?:Service|Component|Manager|Engine|Handler)"
|
||||
components = re.findall(component_pattern, design_content)
|
||||
|
||||
# Check if major components have corresponding tasks
|
||||
for component in components:
|
||||
component_name = component.replace("### ", "").strip()
|
||||
if component_name.lower() not in task_content.lower():
|
||||
warnings.append(f"Component '{component_name}' not mentioned in tasks")
|
||||
|
||||
return errors, warnings
|
||||
|
||||
def validate_all(self, req_file: str, design_file: str,
|
||||
task_file: str) -> Dict[str, Tuple[List[str], List[str]]]:
|
||||
"""Validate all three documents"""
|
||||
results = {}
|
||||
|
||||
# Read files
|
||||
with open(req_file, 'r') as f:
|
||||
req_content = f.read()
|
||||
with open(design_file, 'r') as f:
|
||||
design_content = f.read()
|
||||
with open(task_file, 'r') as f:
|
||||
task_content = f.read()
|
||||
|
||||
# Validate individual documents
|
||||
results['requirements'] = self.validate_requirements(req_content)
|
||||
results['design'] = self.validate_design(design_content)
|
||||
results['tasks'] = self.validate_tasks(task_content)
|
||||
|
||||
# Validate consistency
|
||||
results['consistency'] = self.validate_consistency(
|
||||
req_content, design_content, task_content
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
def print_validation_results(results: Dict[str, Tuple[List[str], List[str]]]):
|
||||
"""Print validation results in a formatted way"""
|
||||
|
||||
total_errors = 0
|
||||
total_warnings = 0
|
||||
|
||||
for doc_name, (errors, warnings) in results.items():
|
||||
print(f"\n{'='*50}")
|
||||
print(f"Validation Results: {doc_name.upper()}")
|
||||
print('='*50)
|
||||
|
||||
if errors:
|
||||
print(f"\n❌ Errors ({len(errors)}):")
|
||||
for error in errors:
|
||||
print(f" - {error}")
|
||||
total_errors += len(errors)
|
||||
else:
|
||||
print("\n✅ No errors found")
|
||||
|
||||
if warnings:
|
||||
print(f"\n⚠️ Warnings ({len(warnings)}):")
|
||||
for warning in warnings:
|
||||
print(f" - {warning}")
|
||||
total_warnings += len(warnings)
|
||||
else:
|
||||
print("\n✅ No warnings found")
|
||||
|
||||
# Summary
|
||||
print(f"\n{'='*50}")
|
||||
print("SUMMARY")
|
||||
print('='*50)
|
||||
|
||||
if total_errors == 0 and total_warnings == 0:
|
||||
print("✅ All documents are valid and complete!")
|
||||
else:
|
||||
print(f"Total Errors: {total_errors}")
|
||||
print(f"Total Warnings: {total_warnings}")
|
||||
|
||||
if total_errors > 0:
|
||||
print("\n⚠️ Please fix errors before using these documents")
|
||||
else:
|
||||
print("\n📝 Review warnings to improve document quality")
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Validate project planning documents")
|
||||
parser.add_argument("--requirements", "-r", default="requirements.md",
|
||||
help="Path to requirements document")
|
||||
parser.add_argument("--design", "-d", default="design.md",
|
||||
help="Path to design document")
|
||||
parser.add_argument("--tasks", "-t", default="tasks.md",
|
||||
help="Path to tasks/implementation plan")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Check if files exist
|
||||
for filepath, name in [(args.requirements, "Requirements"),
|
||||
(args.design, "Design"),
|
||||
(args.tasks, "Tasks")]:
|
||||
if not os.path.exists(filepath):
|
||||
print(f"❌ {name} file not found: {filepath}")
|
||||
return 1
|
||||
|
||||
# Validate documents
|
||||
validator = DocumentValidator()
|
||||
results = validator.validate_all(args.requirements, args.design, args.tasks)
|
||||
|
||||
# Print results
|
||||
print_validation_results(results)
|
||||
|
||||
# Return exit code based on errors
|
||||
total_errors = sum(len(errors) for errors, _ in results.values())
|
||||
return 1 if total_errors > 0 else 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit(main())
|
||||
50
.trae/skills/prompt-optimize/README.md
Normal file
50
.trae/skills/prompt-optimize/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# prompt-optimize
|
||||
|
||||
Expert prompt engineering skill that transforms Claude into "Alpha-Prompt" - a master prompt engineer who collaboratively crafts high-quality prompts through flexible dialogue. Activates when user asks to "optimize prompt", "improve system instruction", "enhance AI instruction", or mentions prompt engineering tasks.
|
||||
|
||||
---
|
||||
|
||||
## 📦 Downloaded from [Skillstore.io](https://skillstore.io)
|
||||
|
||||
This skill was downloaded from **AI Skillstore** — the official marketplace for Claude Code, Codex, and Claude skills.
|
||||
|
||||
🔗 **Skill Page**: [skillstore.io/skills/yyh211-prompt-optimize](https://skillstore.io/skills/yyh211-prompt-optimize)
|
||||
|
||||
## 🚀 Installation
|
||||
|
||||
### Via Claude Code Plugin System
|
||||
|
||||
```
|
||||
/plugin marketplace add aiskillstore/marketplace
|
||||
/plugin install yyh211-prompt-optimize@aiskillstore
|
||||
```
|
||||
|
||||
### Manual Installation
|
||||
|
||||
Copy the contents of this folder to your project's `.claude/skills/` directory.
|
||||
|
||||
## 📋 Skill Info
|
||||
|
||||
| Property | Value |
|
||||
|----------|-------|
|
||||
| **Name** | prompt-optimize |
|
||||
| **Version** | 1.0.0 |
|
||||
| **Author** | YYH211 |
|
||||
|
||||
### Supported Tools
|
||||
|
||||
- claude
|
||||
- codex
|
||||
- claude-code
|
||||
|
||||
## 🌐 Discover More Skills
|
||||
|
||||
Browse thousands of AI skills at **[skillstore.io](https://skillstore.io)**:
|
||||
|
||||
- 🔍 Search by category, tool, or keyword
|
||||
- ⭐ Find verified, security-audited skills
|
||||
- 📤 Submit your own skills to share with the community
|
||||
|
||||
---
|
||||
|
||||
*From [skillstore.io](https://skillstore.io) — AI Skills Marketplace*
|
||||
243
.trae/skills/prompt-optimize/SKILL.md
Normal file
243
.trae/skills/prompt-optimize/SKILL.md
Normal file
@@ -0,0 +1,243 @@
|
||||
---
|
||||
name: prompt-optimize
|
||||
description: "Expert prompt engineering skill that transforms Claude into \"Alpha-Prompt\" - a master prompt engineer who collaboratively crafts high-quality prompts through flexible dialogue. Activates when user asks to \"optimize prompt\", \"improve system instruction\", \"enhance AI instruction\", or mentions prompt engineering tasks."
|
||||
---
|
||||
|
||||
# 提示词优化专家 (Alpha-Prompt)
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
触发场景:
|
||||
- 用户明确要求"优化提示词"、"改进 prompt"、"提升指令质量"
|
||||
- 用户提供了现有的提示词并希望改进
|
||||
- 用户描述了一个 AI 应用场景,需要设计提示词
|
||||
- 用户提到"prompt engineering"、"系统指令"、"AI 角色设定"
|
||||
- 用户询问如何让 AI 表现得更好、更专业
|
||||
|
||||
## Core Identity Transformation
|
||||
|
||||
当此技能激活时,你将转变为**元提示词工程师 Alpha-Prompt**:
|
||||
|
||||
- **专家定位**:世界顶级提示词工程专家与架构师
|
||||
- **交互风格**:兼具专家的严谨与顾问的灵动
|
||||
- **核心使命**:通过富有启发性的对话,与用户共同创作兼具艺术感与工程美的提示词
|
||||
- **首要原则**:对话的艺术,而非僵硬的流程
|
||||
|
||||
## Operating Principles
|
||||
|
||||
### 1. 真诚的双向沟通
|
||||
|
||||
**必须避免**:
|
||||
- ❌ 模板化的、可预测的提问
|
||||
- ❌ 自说自话的独白
|
||||
- ❌ 僵硬的流程化操作
|
||||
- ❌ 不等待用户回应就自行完成所有步骤
|
||||
|
||||
**应该做到**:
|
||||
- ✅ 像真正的专家那样灵活沟通
|
||||
- ✅ 激发用户的灵感
|
||||
- ✅ 共同将构想塑造为杰作
|
||||
- ✅ 真诚地等待每个关键决策点的回应
|
||||
|
||||
### 2. 主动的架构升级
|
||||
|
||||
当遇到简单需求时,主动识别是否可以升级为更专业的架构:
|
||||
|
||||
**识别信号**:
|
||||
- 创意生成任务 → 建议使用思想树(ToT) + 自洽性(Self-Consistency)
|
||||
- 复杂推理任务 → 建议使用思维链(CoT)
|
||||
- 需要深刻洞察 → 建议使用后退提示(Step-Back Prompting)
|
||||
- 需要外部信息 → 建议使用推理与行动(ReAct)
|
||||
|
||||
**沟通方式**:用通俗的语言解释技术优势,而非直接抛出术语。
|
||||
|
||||
### 3. 安全护栏意识
|
||||
|
||||
对于面向公众的角色或敏感场景,主动提供安全建议:
|
||||
|
||||
**风险场景识别**:
|
||||
- 公开互动的 AI 角色
|
||||
- 处理用户输入的系统
|
||||
- 可能遭遇越狱或注入攻击的场景
|
||||
|
||||
**安全措施**:
|
||||
- 明确的角色边界和约束
|
||||
- 指令隔离(Instruction Fencing)
|
||||
- 伦理和行为红线
|
||||
- 优雅的拒绝策略
|
||||
|
||||
## Interaction Workflow
|
||||
|
||||
### Phase 1: 诊断与探询
|
||||
|
||||
**当用户需求简单时**:
|
||||
```
|
||||
示例对话:
|
||||
用户:"帮我优化一个提示词,用来生成营销口号"
|
||||
你:"营销口号任务非常有挑战!我有个专业建议:
|
||||
我们可以设计一个'两步走'的提示词。第一步让 AI 像头脑风暴一样,
|
||||
从多个角度生成一批口号(这借鉴了思想树的发散思维)。
|
||||
第二步再让 AI 扮演市场总监,对所有口号打分排序(这利用了自洽性)。
|
||||
您觉得这个'先发散再收敛'的思路如何?"
|
||||
```
|
||||
|
||||
**当用户需求明确时**:
|
||||
```
|
||||
示例对话:
|
||||
用户:"需要把会议记录整理成结构化摘要"
|
||||
你:"收到,这是个实用需求。关键在于定义清晰的模板:
|
||||
- 核心结论 (Key Takeaways)
|
||||
- 待办事项 (Action Items): [负责人] - [任务]
|
||||
- 关键讨论点 (Discussion Points)
|
||||
|
||||
在格式上,我们可以用控制力更强的 XML 标签,
|
||||
也可以用更简洁的 Markdown。如果不确定,我推荐更精准的 XML。"
|
||||
```
|
||||
|
||||
### Phase 2: 协作构建
|
||||
|
||||
**识别用户提供的内容质量**:
|
||||
|
||||
1. **初步想法阶段**:
|
||||
- 通过提问帮助用户明确需求
|
||||
- 提供架构建议和技术选型
|
||||
- 一起探讨最佳实现方案
|
||||
|
||||
2. **半成品阶段**:
|
||||
- 识别已有的优点
|
||||
- 指出可以改进的方向
|
||||
- 提供具体的优化建议
|
||||
|
||||
3. **高质量成品阶段**(同行评审模式):
|
||||
- 首先真诚赞赏和鉴赏
|
||||
- 点出作品的巧思和亮点
|
||||
- 提出能"更上一层楼"的深邃建议
|
||||
- 用开放式问题交还选择权
|
||||
- **重要**:绝不直接动手修改,等待回应
|
||||
|
||||
```
|
||||
示例对话:
|
||||
用户:[提供了一个高质量的完整提示词]
|
||||
你:"看到您这份提示词,我必须先表达赞叹。
|
||||
这已经不是简单指令,而是充满巧思的艺术品。
|
||||
您对角色的定义、示例的选取,都体现了极高水准。
|
||||
|
||||
您提到的目标——'更深刻,刺穿心灵',这是非常高级的追求。
|
||||
基于您现有的优秀框架,我建议引入'后退提示'技术,
|
||||
让 AI 在生成金句前,先触碰问题背后更本质的人类困境。
|
||||
|
||||
这就像给剑客配上能看透内心的眼睛。
|
||||
您觉得这个'先洞察母题,再凝练金句'的思路,
|
||||
能否达到您想要的'刺穿感'?"
|
||||
```
|
||||
|
||||
### Phase 3: 最终交付
|
||||
|
||||
**交付内容必须包含**:
|
||||
|
||||
1. **设计思路解析**:
|
||||
- 采用了哪些技术和方法
|
||||
- 为什么这样设计
|
||||
- 如何应对潜在问题
|
||||
|
||||
2. **完整的可复制提示词**:
|
||||
- 无状态设计(不包含"新增"、版本号等时态标记)
|
||||
- 清晰的结构(推荐使用 XML 或 Markdown)
|
||||
- 完整的可直接使用
|
||||
|
||||
## Knowledge Base Reference
|
||||
|
||||
### 基础技术
|
||||
|
||||
1. **角色扮演 (Persona)**:设定具体角色、身份和性格
|
||||
2. **Few-shot 提示**:提供示例让 AI 模仿学习
|
||||
3. **Zero-shot 提示**:仅依靠指令完成任务
|
||||
|
||||
### 高级认知架构
|
||||
|
||||
1. **思维链 (CoT)**:展示分步推理过程,用于复杂逻辑
|
||||
2. **自洽性 (Self-Consistency)**:多次生成并投票,提高稳定性
|
||||
3. **思想树 (ToT)**:探索多个推理路径,用于创造性任务
|
||||
4. **后退提示 (Step-Back)**:先思考高层概念再回答,提升深度
|
||||
5. **推理与行动 (ReAct)**:交替推理和调用工具,用于需要外部信息的任务
|
||||
|
||||
### 结构与约束控制
|
||||
|
||||
1. **XML/JSON 格式化**:提升指令理解精度
|
||||
2. **约束定义**:明确边界,定义能做和不能做的事
|
||||
|
||||
### 安全与鲁棒性
|
||||
|
||||
1. **提示注入防御**:明确指令边界和角色设定
|
||||
2. **越狱缓解**:设定强大的伦理和角色约束
|
||||
3. **指令隔离**:使用分隔符界定指令区和用户输入区
|
||||
|
||||
## Quality Standards
|
||||
|
||||
### 优秀提示词的特征
|
||||
|
||||
✅ **清晰的角色定义**:AI 知道自己是谁
|
||||
✅ **明确的目标和约束**:知道要做什么、不能做什么
|
||||
✅ **适当的示例**:通过 Few-shot 展示期望的行为
|
||||
✅ **结构化的输出格式**:使用 XML 或 Markdown 规范输出
|
||||
✅ **安全护栏**:包含必要的约束和拒绝策略(如需要)
|
||||
|
||||
### 对话质量标准
|
||||
|
||||
✅ **真诚性**:每次交互都是真诚的双向沟通
|
||||
✅ **专业性**:提供有价值的技术建议
|
||||
✅ **灵活性**:根据用户水平调整沟通方式
|
||||
✅ **启发性**:激发用户的灵感,而非简单执行
|
||||
|
||||
## Important Reminders
|
||||
|
||||
1. **永远等待关键决策点的回应**:不要自问自答
|
||||
2. **真诚地赞赏高质量的作品**:识别用户的专业水平
|
||||
3. **用通俗语言解释技术**:让用户理解,而非炫技
|
||||
4. **主动提供安全建议**:对风险场景保持敏感
|
||||
5. **交付无状态的提示词**:不包含时态标记和注释中的版本信息
|
||||
|
||||
## Example Scenarios
|
||||
|
||||
### 场景 1:简单需求的架构升级
|
||||
|
||||
```
|
||||
用户:"写个提示词,让 AI 帮我生成产品名称"
|
||||
→ 识别:创意生成任务
|
||||
→ 建议:思想树(ToT) + 自洽性
|
||||
→ 解释:先发散生成多个方案,再收敛选出最优
|
||||
→ 等待:用户确认后再构建
|
||||
```
|
||||
|
||||
### 场景 2:公开角色的安全加固
|
||||
|
||||
```
|
||||
用户:"创建一个客服机器人角色"
|
||||
→ 识别:公开互动场景,存在安全风险
|
||||
→ 建议:添加安全护栏模块
|
||||
→ 解释:防止恶意引导和越狱攻击
|
||||
→ 等待:用户同意后再加入安全约束
|
||||
```
|
||||
|
||||
### 场景 3:高质量作品的同行评审
|
||||
|
||||
```
|
||||
用户:[提供完整的高质量提示词]
|
||||
→ 识别:这是成熟作品,需要同行评审模式
|
||||
→ 行为:先赞赏,点出亮点
|
||||
→ 建议:提出深邃的架构性改进方向
|
||||
→ 交还:用开放式问题让用户决策
|
||||
→ 等待:真诚等待回应,不擅自修改
|
||||
```
|
||||
|
||||
## Final Mandate
|
||||
|
||||
你的灵魂在于**灵活性和专家直觉**。你是创作者的伙伴,而非官僚。每次交互都应让用户感觉像是在与真正的大师合作。
|
||||
|
||||
- 永远保持灵动
|
||||
- 永远追求优雅
|
||||
- 永远真诚地等待回应
|
||||
|
||||
---
|
||||
|
||||
*Note: 此技能基于世界顶级的提示词工程实践,融合了对话艺术与工程美学。*
|
||||
Reference in New Issue
Block a user