generate_melody
Create melodies by specifying a musical scale, root note, and length. This tool generates melodic patterns for music composition and live coding applications.
Instructions
Generate melody from scale
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scale | Yes | Scale name | |
| root | Yes | Root note | |
| length | No | Number of notes |
Implementation Reference
- Tool schema definition including input validation for scale, root note, and optional length.name: 'generate_melody', description: 'Generate melody from scale', inputSchema: { type: 'object', properties: { scale: { type: 'string', description: 'Scale name' }, root: { type: 'string', description: 'Root note' }, length: { type: 'number', description: 'Number of notes' } }, required: ['scale', 'root'] } },
- Main handler in executeTool switch statement: validates inputs, generates scale using MusicTheory, calls PatternGenerator.generateMelody, appends to current pattern or writes new, returns success message.case 'generate_melody': InputValidator.validateRootNote(args.root); InputValidator.validateScaleName(args.scale); if (args.length !== undefined) { InputValidator.validatePositiveInteger(args.length, 'length'); } const scale = this.theory.generateScale(args.root, args.scale); const melody = this.generator.generateMelody(scale, args.length || 8); const currentMelody = await this.getCurrentPatternSafe(); const newMelodyPattern = currentMelody ? currentMelody + '\n' + melody : melody; await this.writePatternSafe(newMelodyPattern); return `Generated melody in ${args.root} ${args.scale}`;
- Core melody generation logic: creates melodic line using scale notes with step/leap probabilities, random octaves, outputs Strudel note pattern with triangle synth.generateMelody(scale: string[], length: number = 8, octaveRange: [number, number] = [3, 5]): string { const notes = []; let lastNoteIndex = Math.floor(Math.random() * scale.length); for (let i = 0; i < length; i++) { // Create more musical intervals (prefer steps over leaps) const stepProbability = 0.7; const useStep = Math.random() < stepProbability; let noteIndex: number; if (useStep) { // Move by step (1 or 2 scale degrees) const step = Math.random() < 0.5 ? 1 : -1; noteIndex = (lastNoteIndex + step + scale.length) % scale.length; } else { // Leap to any note noteIndex = Math.floor(Math.random() * scale.length); } const note = scale[noteIndex]; const octave = octaveRange[0] + Math.floor(Math.random() * (octaveRange[1] - octaveRange[0] + 1)); notes.push(`${note.toLowerCase()}${octave}`); lastNoteIndex = noteIndex; } return `note("${notes.join(' ')}").s("triangle")`; }
- src/server/EnhancedMCPServerFixed.ts:571-573 (registration)Tool registration via setRequestHandler for ListToolsRequestSchema, which returns the tools array containing generate_melody.this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: this.getTools() }));