get_notes_by_index
Extract notes from a specific track in a MIDI file using its index number. Provide file path and track index to get the notes.
Instructions
Get notes from midi file by track index
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absoulate File Path to midi file | |
| trackIndex | Yes | Track index number |
Implementation Reference
- src/main.ts:151-171 (registration)Registration of the 'get_notes_by_index' tool on the MCP server with Zod schema for filePath (string) and trackIndex (number), and handler that loads a MIDI file, gets the track by index, and returns notes as JSON.
server.tool( 'get_notes_by_index', 'Get notes from midi file by track index', { filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), }, withErrorHandling(({ filePath, trackIndex }) => { const midi = loadMidiFile(filePath) const track = getTrackByIndex(midi, trackIndex) const notes = track.notes.map((note: any) => note.toJSON()) return { content: [ { type: 'text', text: JSON.stringify(notes), }, ] } }) ) - src/main.ts:158-170 (handler)Handler function that loads a MIDI file, retrieves the specified track, maps all notes to JSON, and returns them as text content.
withErrorHandling(({ filePath, trackIndex }) => { const midi = loadMidiFile(filePath) const track = getTrackByIndex(midi, trackIndex) const notes = track.notes.map((note: any) => note.toJSON()) return { content: [ { type: 'text', text: JSON.stringify(notes), }, ] } }) - src/main.ts:154-157 (schema)Zod schema defining the input parameters: filePath (string) and trackIndex (number).
{ filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), }, - src/utils/obj-utils.ts:1-6 (helper)Helper function that validates trackIndex bounds and returns the track from the MIDI object.
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)Helper function that 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 }