Skip to main content
Glama

Filesystem MCP Server

Official

read_media_file

Extract base64 encoded data and MIME type from image or audio files within specified directories using the Filesystem MCP Server.

Instructions

Read an image or audio file. Returns the base64 encoded data and MIME type. Only works within allowed directories.

Input Schema

NameRequiredDescriptionDefault
pathYes

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "path": { "type": "string" } }, "required": [ "path" ], "type": "object" }

Implementation Reference

  • The core handler function for the 'read_media_file' tool. It validates the file path, determines the MIME type based on the file extension, reads the file content as a Base64-encoded string using a helper function, determines the content type (image, audio, or blob), and returns the appropriately structured content for MCP.
    async (args: z.infer<typeof ReadMediaFileArgsSchema>) => { const validPath = await validatePath(args.path); const extension = path.extname(validPath).toLowerCase(); const mimeTypes: Record<string, string> = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".bmp": "image/bmp", ".svg": "image/svg+xml", ".mp3": "audio/mpeg", ".wav": "audio/wav", ".ogg": "audio/ogg", ".flac": "audio/flac", }; const mimeType = mimeTypes[extension] || "application/octet-stream"; const data = await readFileAsBase64Stream(validPath); const type = mimeType.startsWith("image/") ? "image" : mimeType.startsWith("audio/") ? "audio" // Fallback for other binary types, not officially supported by the spec but has been used for some time : "blob"; const contentItem = { type: type as 'image' | 'audio' | 'blob', data, mimeType }; return { content: [contentItem], structuredContent: { content: [contentItem] } } as unknown as CallToolResult; }
  • Zod schema defining the input arguments for the 'read_media_file' tool (path: string). Used for typing the handler function arguments.
    const ReadMediaFileArgsSchema = z.object({ path: z.string() });
  • Registration of the 'read_media_file' tool with the MCP server, including title, description, input/output schemas, annotations, and reference to the inline handler function.
    server.registerTool( "read_media_file", { title: "Read Media File", description: "Read an image or audio file. Returns the base64 encoded data and MIME type. " + "Only works within allowed directories.", inputSchema: { path: z.string() }, outputSchema: { content: z.array(z.object({ type: z.enum(["image", "audio", "blob"]), data: z.string(), mimeType: z.string() })) }, annotations: { readOnlyHint: true } }, async (args: z.infer<typeof ReadMediaFileArgsSchema>) => { const validPath = await validatePath(args.path); const extension = path.extname(validPath).toLowerCase(); const mimeTypes: Record<string, string> = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".bmp": "image/bmp", ".svg": "image/svg+xml", ".mp3": "audio/mpeg", ".wav": "audio/wav", ".ogg": "audio/ogg", ".flac": "audio/flac", }; const mimeType = mimeTypes[extension] || "application/octet-stream"; const data = await readFileAsBase64Stream(validPath); const type = mimeType.startsWith("image/") ? "image" : mimeType.startsWith("audio/") ? "audio" // Fallback for other binary types, not officially supported by the spec but has been used for some time : "blob"; const contentItem = { type: type as 'image' | 'audio' | 'blob', data, mimeType }; return { content: [contentItem], structuredContent: { content: [contentItem] } } as unknown as CallToolResult; } );
  • Helper utility function to read a file as a stream of buffers, concatenate them, and encode to Base64 string. Memory-efficient for handling binary media files. Called by the read_media_file handler.
    async function readFileAsBase64Stream(filePath: string): Promise<string> { return new Promise((resolve, reject) => { const stream = createReadStream(filePath); const chunks: Buffer[] = []; stream.on('data', (chunk) => { chunks.push(chunk as Buffer); }); stream.on('end', () => { const finalBuffer = Buffer.concat(chunks); resolve(finalBuffer.toString('base64')); }); stream.on('error', (err) => reject(err)); }); }

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/modelcontextprotocol/filesystem'

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