get_track_info_by_index
Retrieve detailed track information from a MIDI file using its track index, including name, instrument, channel, duration, and note count. Specify the file path and track index to access precise data for analysis or manipulation.
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 |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"filePath": {
"description": "Absoulate File Path to midi file",
"type": "string"
},
"trackIndex": {
"description": "Track index number",
"type": "number"
}
},
"required": [
"filePath",
"trackIndex"
],
"type": "object"
}
Implementation Reference
- src/main.ts:128-148 (handler)The core handler function for the 'get_track_info_by_index' tool. Loads the MIDI file, retrieves the specific track using the helper function, extracts key track information, and returns it as a JSON string in the tool response format.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 validation using Zod for the tool parameters: filePath (string) and trackIndex (number).{ filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), },
- src/main.ts:120-149 (registration)Registration of the 'get_track_info_by_index' tool on the MCP server, including name, description, input schema, and handler.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/utils/obj-utils.ts:1-6 (helper)Helper function used by the tool handler to safely retrieve a track from the MIDI object by index, with bounds checking.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] }