|
| 1 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import type { |
| 3 | + CallToolResult, |
| 4 | + ReadResourceResult, |
| 5 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 6 | +import fs from "node:fs/promises"; |
| 7 | +import path from "node:path"; |
| 8 | +import { z } from "zod"; |
| 9 | +import ABCJS from "abcjs"; |
| 10 | +import { |
| 11 | + RESOURCE_MIME_TYPE, |
| 12 | + RESOURCE_URI_META_KEY, |
| 13 | + registerAppResource, |
| 14 | + registerAppTool, |
| 15 | +} from "@modelcontextprotocol/ext-apps/server"; |
| 16 | +import { startServer } from "./src/server-utils.js"; |
| 17 | + |
| 18 | +const DIST_DIR = path.join(import.meta.dirname, "dist"); |
| 19 | + |
| 20 | +const DEFAULT_ABC_NOTATION_INPUT = `X:1 |
| 21 | +T:Twinkle, Twinkle Little Star |
| 22 | +M:4/4 |
| 23 | +L:1/4 |
| 24 | +K:C |
| 25 | +C C G G | A A G2 | F F E E | D D C2 | |
| 26 | +G G F F | E E D2 | G G F F | E E D2 | |
| 27 | +C C G G | A A G2 | F F E E | D D C2 |`; |
| 28 | + |
| 29 | +/** |
| 30 | + * Creates a new MCP server instance with the sheet music tool and resource. |
| 31 | + */ |
| 32 | +function createServer(): McpServer { |
| 33 | + const server = new McpServer({ |
| 34 | + name: "Sheet Music Server", |
| 35 | + version: "1.0.0", |
| 36 | + }); |
| 37 | + |
| 38 | + const resourceUri = "ui://sheet-music/mcp-app.html"; |
| 39 | + |
| 40 | + // Register the play-sheet-music tool. |
| 41 | + // Validates ABC notation server-side, then the client renders via ontoolinput. |
| 42 | + registerAppTool( |
| 43 | + server, |
| 44 | + "play-sheet-music", |
| 45 | + { |
| 46 | + title: "Play Sheet Music", |
| 47 | + description: |
| 48 | + "Plays music from ABC notation with audio playback and visual sheet music. " + |
| 49 | + "Use this to compose original songs (for birthdays, holidays, or any occasion) " + |
| 50 | + "or perform well-known tunes (folk songs, nursery rhymes, hymns, classical melodies). " + |
| 51 | + "For accurate renditions of well-known tunes, look up the ABC notation from " + |
| 52 | + "abcnotation.com or thesession.org rather than recalling from memory.", |
| 53 | + inputSchema: z.object({ |
| 54 | + abcNotation: z |
| 55 | + .string() |
| 56 | + .default(DEFAULT_ABC_NOTATION_INPUT) |
| 57 | + .describe( |
| 58 | + "ABC notation string to render as sheet music with audio playback", |
| 59 | + ), |
| 60 | + }), |
| 61 | + _meta: { [RESOURCE_URI_META_KEY]: resourceUri }, |
| 62 | + }, |
| 63 | + async ({ abcNotation }): Promise<CallToolResult> => { |
| 64 | + // Validate ABC notation using abcjs parser |
| 65 | + const [{ warnings }] = ABCJS.parseOnly(abcNotation); |
| 66 | + |
| 67 | + // Check for parse warnings (abcjs reports errors as warnings) |
| 68 | + if (warnings && warnings.length > 0) { |
| 69 | + // Strip HTML markup from warning messages |
| 70 | + const messages = warnings.map((w) => w.replace(/<[^>]*>/g, "")); |
| 71 | + const error = `Invalid ABC notation:\n${messages.join("\n")}`; |
| 72 | + return { |
| 73 | + isError: true, |
| 74 | + content: [{ type: "text", text: error }], |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + return { |
| 79 | + content: [{ type: "text", text: "Input parsed successfully." }], |
| 80 | + }; |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + // Register the UI resource that serves the bundled HTML/JS/CSS. |
| 85 | + registerAppResource( |
| 86 | + server, |
| 87 | + resourceUri, |
| 88 | + resourceUri, |
| 89 | + { mimeType: RESOURCE_MIME_TYPE, description: "Sheet Music Viewer UI" }, |
| 90 | + async (): Promise<ReadResourceResult> => { |
| 91 | + const html = await fs.readFile( |
| 92 | + path.join(DIST_DIR, "mcp-app.html"), |
| 93 | + "utf-8", |
| 94 | + ); |
| 95 | + |
| 96 | + return { |
| 97 | + contents: [ |
| 98 | + { |
| 99 | + uri: resourceUri, |
| 100 | + mimeType: RESOURCE_MIME_TYPE, |
| 101 | + text: html, |
| 102 | + _meta: { |
| 103 | + ui: { |
| 104 | + csp: { |
| 105 | + // Allow loading soundfonts for audio playback |
| 106 | + connectDomains: ["https://paulrosen.github.io"], |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + }; |
| 113 | + }, |
| 114 | + ); |
| 115 | + |
| 116 | + return server; |
| 117 | +} |
| 118 | + |
| 119 | +startServer(createServer); |
0 commit comments