Skip to main content
Glama

get_media_file

Retrieve media files from Anki cards by specifying the filename to access images, audio, or other embedded content for card management.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYesMedia filename referenced in Anki cards (e.g., "image.png")

Implementation Reference

  • The handler function that implements the core logic: validates the filename for security, retrieves the base64-encoded media file content from Anki using ankiClient.media.retrieveMediaFile, determines the MIME type based on file extension, calculates approximate size, and returns appropriate MCP content (image block for images analyzable by AI, or JSON text for other files). Handles errors gracefully.
        async ({ filename }) => {
          try {
            // Validate filename (basic security check)
            if (
              !filename ||
              filename.includes('..') ||
              filename.includes('/') ||
              filename.includes('\\')
            ) {
              throw new Error('Invalid filename: must be a simple filename without path traversal');
            }
    
            // Retrieve the media file from AnkiConnect
            const base64Content = await ankiClient.media.retrieveMediaFile({ filename });
    
            if (base64Content === false) {
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify({
                      error: `File not found: ${filename}`,
                      filename,
                    }),
                  },
                ],
              };
            }
    
            // Detect MIME type from file extension
            const extension = filename.split('.').pop()?.toLowerCase() || '';
            const mimeType = MIME_TYPES[extension] || 'application/octet-stream';
    
            // Calculate size (approximate from base64 length)
            const size = Math.floor((base64Content.length * 3) / 4);
    
            // Return the file data with metadata
            const response = {
              filename,
              mimeType,
              base64Data: base64Content,
              size,
            };
    
            // For images, return as image content that Claude can analyze
            // Note: Claude can see the image but it won't display to the user
            if (mimeType.startsWith('image/')) {
              return {
                content: [
                  {
                    type: 'image',
                    data: base64Content,
                    mimeType,
                  },
                  {
                    type: 'text',
                    text: `📷 Image: ${filename}
    Format: ${mimeType}
    Size: ${(size / 1024).toFixed(1)} KB
    
    Note: I can see and analyze this image, but it won't display in the UI. I can describe what I see or answer questions about it.`,
                  },
                ],
              };
            }
    
            // For non-images, return as text with metadata
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify(response, null, 2),
                },
              ],
            };
          } catch (error) {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({
                    error: `Failed to retrieve media file: ${error instanceof Error ? error.message : String(error)}`,
                    filename,
                  }),
                },
              ],
            };
          }
        }
  • The input schema using Zod, defining a single required 'filename' parameter as a string with description.
    {
      filename: z.string().describe('Media filename referenced in Anki cards (e.g., "image.png")'),
    },
  • The MCP server.tool registration call for the 'get_media_file' tool within the registerConsolidatedTools function.
    server.tool(
      'get_media_file',
  • Constant mapping of common file extensions to MIME types, used by the handler to determine the content type for proper rendering.
    const MIME_TYPES: Record<string, string> = {
      png: 'image/png',
      jpg: 'image/jpeg',
      jpeg: 'image/jpeg',
      gif: 'image/gif',
      svg: 'image/svg+xml',
      webp: 'image/webp',
      bmp: 'image/bmp',
      ico: 'image/x-icon',
      mp3: 'audio/mpeg',
      wav: 'audio/wav',
      ogg: 'audio/ogg',
      mp4: 'video/mp4',
      webm: 'video/webm',
      pdf: 'application/pdf',
    };
Behavior1/5

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

Tool has no description.

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

Conciseness1/5

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

Tool has no description.

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

Completeness1/5

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

Tool has no description.

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

Parameters1/5

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

Tool has no description.

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

Purpose1/5

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

Tool has no description.

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

Usage Guidelines1/5

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

Tool has no description.

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/arielbk/anki-mcp'

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