get_track_info_by_index
Retrieve detailed information about a specific track in a MIDI file by providing its index number. Get the track's name, instrument, channel, duration, and note count.
Instructions
Get track info from midi file by track index. name, instrument, channel, endOfTrackTicks, duration, durationTicks, noteCount
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absoulate File Path to midi file | |
| trackIndex | Yes | Track index number |
Implementation Reference
- src/main.ts:120-149 (registration)Tool registered with name 'get_track_info_by_index'. Uses server.tool() with a description, input schema (filePath: string, trackIndex: number), and handler wrapped in withErrorHandling.
server.tool( 'get_track_info_by_index', `Get track info from midi file by track index. name, instrument, channel, endOfTrackTicks, duration, durationTicks, noteCount`, { 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 trackInfo = { name: track.name, instrument: track.instrument.toJSON(), channel: track.channel, endOfTrackTicks: track.endOfTrackTicks, duration: track.duration, durationTicks: track.durationTicks, noteCount: track.notes.length, } return { content: [ { type: 'text', text: JSON.stringify(trackInfo), }, ] } }) ) - src/main.ts:128-148 (handler)Handler function that loads the MIDI file, gets the track by index, extracts name, instrument, channel, endOfTrackTicks, duration, durationTicks, and noteCount, then returns the info as JSON.
withErrorHandling(({ filePath, trackIndex }) => { const midi = loadMidiFile(filePath) const track = getTrackByIndex(midi, trackIndex) const trackInfo = { name: track.name, instrument: track.instrument.toJSON(), channel: track.channel, endOfTrackTicks: track.endOfTrackTicks, duration: track.duration, durationTicks: track.durationTicks, noteCount: track.notes.length, } return { content: [ { type: 'text', text: JSON.stringify(trackInfo), }, ] } }) - src/main.ts:124-127 (schema)Input schema using Zod: filePath is a string, trackIndex is a number.
{ filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), }, - src/main.ts:23-39 (helper)Error handling wrapper used by the tool. Catches any errors and returns an error response.
const withErrorHandling = <T extends Record<string, any>>(handler: (args: T) => any) => { return async (args: T) => { try { return await handler(args) } catch (e) { return { isError: true, content: [ { type: 'text', text: `Error: ${e}`, }, ] } } } } - src/utils/file-util.ts:5-9 (helper)Loads a MIDI file from disk using @tonejs/midi library.
export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi }