|
| 1 | +/** |
| 2 | + * Video Resource Server |
| 3 | + * |
| 4 | + * Demonstrates serving binary content (video) via MCP resources. |
| 5 | + * The server fetches videos from CDN and serves them as base64 blobs. |
| 6 | + */ |
| 7 | +import { |
| 8 | + McpServer, |
| 9 | + ResourceTemplate, |
| 10 | +} from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 11 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 12 | +import type { |
| 13 | + CallToolResult, |
| 14 | + ReadResourceResult, |
| 15 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 16 | +import { z } from "zod"; |
| 17 | +import fs from "node:fs/promises"; |
| 18 | +import path from "node:path"; |
| 19 | +import { |
| 20 | + registerAppTool, |
| 21 | + registerAppResource, |
| 22 | + RESOURCE_MIME_TYPE, |
| 23 | + RESOURCE_URI_META_KEY, |
| 24 | +} from "@modelcontextprotocol/ext-apps/server"; |
| 25 | +import { startServer } from "../shared/server-utils.js"; |
| 26 | + |
| 27 | +const DIST_DIR = path.join(import.meta.dirname, "dist"); |
| 28 | +const RESOURCE_URI = "ui://video-player/mcp-app.html"; |
| 29 | + |
| 30 | +/** |
| 31 | + * Video library with different sizes for testing. |
| 32 | + */ |
| 33 | +const VIDEO_LIBRARY: Record<string, { url: string; description: string }> = { |
| 34 | + "bunny-1mb": { |
| 35 | + url: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4", |
| 36 | + description: "1MB", |
| 37 | + }, |
| 38 | + "bunny-5mb": { |
| 39 | + url: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_5MB.mp4", |
| 40 | + description: "5MB", |
| 41 | + }, |
| 42 | + "bunny-10mb": { |
| 43 | + url: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/720/Big_Buck_Bunny_720_10s_10MB.mp4", |
| 44 | + description: "10MB", |
| 45 | + }, |
| 46 | + "bunny-20mb": { |
| 47 | + url: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/720/Big_Buck_Bunny_720_10s_20MB.mp4", |
| 48 | + description: "20MB", |
| 49 | + }, |
| 50 | + "bunny-30mb": { |
| 51 | + url: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_30MB.mp4", |
| 52 | + description: "30MB", |
| 53 | + }, |
| 54 | + "bunny-150mb": { |
| 55 | + url: "https://cdn.jsdelivr.net/npm/big-buck-bunny-1080p@0.0.6/video.mp4", |
| 56 | + description: "~150MB (full 1080p)", |
| 57 | + }, |
| 58 | +}; |
| 59 | + |
| 60 | +function createServer(): McpServer { |
| 61 | + const server = new McpServer({ |
| 62 | + name: "Video Resource Server", |
| 63 | + version: "1.0.0", |
| 64 | + }); |
| 65 | + |
| 66 | + // Register video resource template |
| 67 | + // This fetches video from CDN and returns as base64 blob |
| 68 | + server.registerResource( |
| 69 | + "video", |
| 70 | + new ResourceTemplate("videos://{id}", { list: undefined }), |
| 71 | + { |
| 72 | + description: "Video served via MCP resource (base64 blob)", |
| 73 | + mimeType: "video/mp4", |
| 74 | + }, |
| 75 | + async (uri, { id }): Promise<ReadResourceResult> => { |
| 76 | + const idStr = Array.isArray(id) ? id[0] : id; |
| 77 | + const video = VIDEO_LIBRARY[idStr]; |
| 78 | + |
| 79 | + if (!video) { |
| 80 | + throw new Error( |
| 81 | + `Video not found: ${idStr}. Available: ${Object.keys(VIDEO_LIBRARY).join(", ")}`, |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + console.error(`[video-resource] Fetching: ${video.url}`); |
| 86 | + |
| 87 | + const response = await fetch(video.url); |
| 88 | + if (!response.ok) { |
| 89 | + throw new Error( |
| 90 | + `Failed to fetch video: ${response.status} ${response.statusText}`, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + const buffer = await response.arrayBuffer(); |
| 95 | + const base64 = Buffer.from(buffer).toString("base64"); |
| 96 | + |
| 97 | + console.error( |
| 98 | + `[video-resource] Size: ${buffer.byteLength} bytes -> ${base64.length} base64 chars`, |
| 99 | + ); |
| 100 | + |
| 101 | + return { |
| 102 | + contents: [ |
| 103 | + { |
| 104 | + uri: uri.href, |
| 105 | + mimeType: "video/mp4", |
| 106 | + blob: base64, |
| 107 | + }, |
| 108 | + ], |
| 109 | + }; |
| 110 | + }, |
| 111 | + ); |
| 112 | + |
| 113 | + // Register the video player tool |
| 114 | + registerAppTool( |
| 115 | + server, |
| 116 | + "play_video", |
| 117 | + { |
| 118 | + title: "Play Video via Resource", |
| 119 | + description: `Play a video loaded via MCP resource. |
| 120 | +Available videos: |
| 121 | +${Object.entries(VIDEO_LIBRARY) |
| 122 | + .map(([id, v]) => `- ${id}: ${v.description}`) |
| 123 | + .join("\n")}`, |
| 124 | + inputSchema: { |
| 125 | + videoId: z |
| 126 | + .enum(Object.keys(VIDEO_LIBRARY) as [string, ...string[]]) |
| 127 | + .describe( |
| 128 | + `Video ID to play. Available: ${Object.keys(VIDEO_LIBRARY).join(", ")}`, |
| 129 | + ), |
| 130 | + }, |
| 131 | + _meta: { [RESOURCE_URI_META_KEY]: RESOURCE_URI }, |
| 132 | + }, |
| 133 | + async ({ videoId }): Promise<CallToolResult> => { |
| 134 | + const video = VIDEO_LIBRARY[videoId]; |
| 135 | + return { |
| 136 | + content: [ |
| 137 | + { |
| 138 | + type: "text", |
| 139 | + text: JSON.stringify({ |
| 140 | + videoUri: `videos://${videoId}`, |
| 141 | + description: video.description, |
| 142 | + }), |
| 143 | + }, |
| 144 | + ], |
| 145 | + }; |
| 146 | + }, |
| 147 | + ); |
| 148 | + |
| 149 | + // Register the MCP App resource (the UI) |
| 150 | + registerAppResource( |
| 151 | + server, |
| 152 | + RESOURCE_URI, |
| 153 | + RESOURCE_URI, |
| 154 | + { mimeType: RESOURCE_MIME_TYPE }, |
| 155 | + async (): Promise<ReadResourceResult> => { |
| 156 | + const html = await fs.readFile( |
| 157 | + path.join(DIST_DIR, "mcp-app.html"), |
| 158 | + "utf-8", |
| 159 | + ); |
| 160 | + return { |
| 161 | + contents: [ |
| 162 | + { uri: RESOURCE_URI, mimeType: RESOURCE_MIME_TYPE, text: html }, |
| 163 | + ], |
| 164 | + }; |
| 165 | + }, |
| 166 | + ); |
| 167 | + |
| 168 | + return server; |
| 169 | +} |
| 170 | + |
| 171 | +async function main() { |
| 172 | + if (process.argv.includes("--stdio")) { |
| 173 | + await createServer().connect(new StdioServerTransport()); |
| 174 | + } else { |
| 175 | + const port = parseInt(process.env.PORT ?? "3105", 10); |
| 176 | + await startServer(createServer, { port, name: "Video Resource Server" }); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +main().catch((e) => { |
| 181 | + console.error(e); |
| 182 | + process.exit(1); |
| 183 | +}); |
0 commit comments