Skip to main content
Glama
modelcontextprotocol

Filesystem MCP Server

Official

Read Media File

read_media_file
Read-only

Read image or audio files from allowed directories and return base64-encoded data with MIME type.

Instructions

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

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYes

Implementation Reference

  • The handler function for the 'read_media_file' tool. Validates the path, determines the MIME type based on file extension, reads the file as base64 using a helper, categorizes as image/audio/blob, and returns structured content with base64 data.
    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: a single 'path' string.
    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 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 function that reads a binary file using a stream, concatenates chunks into a buffer, and returns the base64-encoded string. Used 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));
      });
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint=true, so the agent knows it's safe. The description adds useful context about allowed directories and output format (base64 + MIME type), but doesn't cover potential errors (e.g., invalid paths) or performance aspects like file size limits.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Three concise sentences: purpose, output, and constraint. Each sentence adds critical information with zero waste, and the structure is front-loaded with the core action.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the output schema exists (covering return values), annotations handle safety, and it's a simple read operation, the description is largely complete. It could mention error cases or prerequisites more explicitly, but covers key usage aspects well.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage for the single parameter 'path', the description compensates by implying the path must point to an image/audio file in allowed directories. It doesn't detail path syntax or examples, but adds meaningful constraints beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Read'), resource ('image or audio file'), and output ('Returns the base64 encoded data and MIME type'), distinguishing it from siblings like read_text_file (text only) and read_file (generic).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

It provides clear context with 'Only works within allowed directories', indicating a key constraint. However, it doesn't explicitly state when to use this vs. alternatives like read_text_file or read_file, though the media focus implies differentiation.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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