play_note
Play a MIDI note using Sonic Pi, specifying the note number, synth type, sustain duration, and cutoff frequency to create custom sound sequences programmatically.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cutoff | No | Filter cutoff frequency | |
| note | Yes | MIDI note number (0-127) | |
| sustain | No | Note duration in seconds | |
| synth | No | Synth to use (e.g. :saw, :beep, :prophet) |
Implementation Reference
- src/server.ts:51-68 (handler)The handler function for the play_note tool. It constructs Sonic Pi code using the provided parameters (note, synth, sustain, cutoff), sends it via OSC to the Sonic Pi server, and returns a confirmation message. Errors are logged and rethrown.async ({ note, synth = ":beep", sustain = 1, cutoff = 100 }: PlayNoteParams) => { try { const code = ` use_synth ${synth} play ${note}, sustain: ${sustain}, cutoff: ${cutoff} `; this.oscClient.send('/run-code', code); return { content: [{ type: "text", text: `Playing note ${note} with synth ${synth} (sustain: ${sustain}s, cutoff: ${cutoff})` }] }; } catch (error) { console.error('Error in play_note:', error); throw new Error('Failed to play note'); } }
- src/server.ts:45-50 (schema)Zod input schema for the play_note tool, defining validation and descriptions for parameters: note (required, 0-127), synth (optional string), sustain (optional number), cutoff (optional number).{ note: z.number().min(0).max(127).describe("MIDI note number (0-127)"), synth: z.string().optional().describe("Synth to use (e.g. :saw, :beep, :prophet)"), sustain: z.number().optional().describe("Note duration in seconds"), cutoff: z.number().optional().describe("Filter cutoff frequency") },
- src/server.ts:43-69 (registration)Registration of the play_note tool using McpServer.tool() method, specifying the tool name, input schema (Zod), and handler function.this.server.tool( "play_note", { note: z.number().min(0).max(127).describe("MIDI note number (0-127)"), synth: z.string().optional().describe("Synth to use (e.g. :saw, :beep, :prophet)"), sustain: z.number().optional().describe("Note duration in seconds"), cutoff: z.number().optional().describe("Filter cutoff frequency") }, async ({ note, synth = ":beep", sustain = 1, cutoff = 100 }: PlayNoteParams) => { try { const code = ` use_synth ${synth} play ${note}, sustain: ${sustain}, cutoff: ${cutoff} `; this.oscClient.send('/run-code', code); return { content: [{ type: "text", text: `Playing note ${note} with synth ${synth} (sustain: ${sustain}s, cutoff: ${cutoff})` }] }; } catch (error) { console.error('Error in play_note:', error); throw new Error('Failed to play note'); } } );
- src/server.ts:9-14 (schema)TypeScript interface defining the structure of parameters for the play_note handler, matching the tool schema for type safety.interface PlayNoteParams { note: number; synth?: string; sustain?: number; cutoff?: number; }