add_notes_by_index
Add notes to a specific track in a MIDI file by specifying the track index, note properties (pitch, name, or MIDI number), velocity, and timing in seconds or ticks.
Instructions
Add notes to midi file by track index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absoulate File Path to midi file | |
| trackIndex | Yes | Track index number | |
| notes | Yes |
Implementation Reference
- src/main.ts:225-245 (handler)The handler function for 'add_notes_by_index' tool. Loads a MIDI file, finds the specified track by index, adds each note from the input array, saves the file, and returns success.
withErrorHandling(({ filePath, trackIndex, notes }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加音符 notes.forEach(note => { track.addNote(note) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add note success', }, ] } }) - src/main.ts:217-246 (registration)Registration of the 'add_notes_by_index' tool on the MCP server with name, description, schema, and handler.
server.tool( 'add_notes_by_index', 'Add notes to midi file by track index', { filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), notes: z.array(NoteConstructorInterfaceSchema) }, withErrorHandling(({ filePath, trackIndex, notes }) => { // 读取文件 const midi = loadMidiFile(filePath) // 查找轨道 const track = getTrackByIndex(midi, trackIndex) // 添加音符 notes.forEach(note => { track.addNote(note) }) // 保存文件 saveMidiFile(midi, filePath) return { content: [ { type: 'text', text: 'add note success', }, ] } }) ) - src/types/types.ts:37-39 (schema)NoteConstructorInterfaceSchema - the Zod schema for individual notes in the array. Composed from PitchDescriptionSchema, VelocityDescriptionSchema, and TimeDescriptionSchema.
export const NoteConstructorInterfaceSchema = PitchDescriptionSchema .and(VelocityDescriptionSchema) .and(TimeDescriptionSchema) - src/utils/obj-utils.ts:1-6 (helper)getTrackByIndex helper - validates track index bounds and returns the track from the MIDI file.
export function getTrackByIndex(midi: any, trackIndex: number) { if (trackIndex < 0 || trackIndex >= midi.tracks.length) { throw new Error('Track index out of range') } return midi.tracks[trackIndex] } - src/utils/file-util.ts:5-9 (helper)loadMidiFile helper - reads a MIDI file from disk and parses it using @tonejs/midi.
export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi }