Skip to main content
Glama

get_notes_by_index

Extract notes from a MIDI file by specifying the track index, enabling focused analysis and manipulation of specific track data within the file.

Instructions

Get notes from midi file by track index

Input Schema

NameRequiredDescriptionDefault
filePathYesAbsoulate File Path to midi file
trackIndexYesTrack 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:151-171 (registration)
    Registration of the 'get_notes_by_index' tool, including input schema (filePath: string, trackIndex: number) and the complete inline handler function that loads the MIDI file, retrieves the specified track, extracts and serializes its notes to 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), }, ] } }) )
  • The core handler logic wrapped in error handling: loads MIDI using loadMidiFile, gets track using getTrackByIndex, maps notes to JSON, and returns 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), }, ] } })
  • Input schema defined with Zod for parameters: filePath (absolute path to MIDI file) and trackIndex (numeric index of the track).
    { filePath: z.string().describe('Absoulate File Path to midi file'), trackIndex: z.number().describe('Track index number'), },
  • Helper function used in the handler to safely retrieve a track by index from the MIDI object, 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] }
  • Helper function used in the handler to load a MIDI file from disk using @tonejs/midi and fs.
    export function loadMidiFile(filePath: string) { const midiData = fs.readFileSync(filePath) const midi = new Midi(midiData) return midi }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/xiaolaa2/midi-file-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server