Skip to main content
Glama
opensubtitles

OpenSubtitles MCP Server

Official

calculate_file_hash

Generate OpenSubtitles hash values from local movie files to enable subtitle matching and downloading through the OpenSubtitles API.

Instructions

Calculate OpenSubtitles hash for local movie files

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesPath to the movie file

Implementation Reference

  • Main handler function implementing the calculate_file_hash tool logic: validates input, checks file accessibility, computes hash using helper, formats response, and handles errors.
    export async function calculateFileHash(args) { try { // Validate input arguments const validatedArgs = HashArgsSchema.parse(args); // Resolve and validate file path const resolvedPath = resolve(validatedArgs.file_path); // Check if file exists and is readable try { await access(resolvedPath, constants.R_OK); } catch (error) { throw new Error(`File not found or not readable: ${validatedArgs.file_path}`); } // Calculate the OpenSubtitles hash const hashResult = await calculateOpenSubtitlesHash(resolvedPath); // Format response const response = { file_path: validatedArgs.file_path, resolved_path: resolvedPath, hash: hashResult.hash, size: hashResult.size, size_mb: Math.round(hashResult.size / 1024 / 1024 * 100) / 100, usage_note: "Use this hash with the 'moviehash' parameter in search_subtitles for exact file matching" }; return { content: [ { type: "text", text: JSON.stringify(response, null, 2), }, ], }; } catch (error) { console.error("Calculate file hash error:", error); let errorMessage = "Failed to calculate file hash"; if (error instanceof z.ZodError) { errorMessage = `Invalid parameters: ${error.errors.map(e => `${e.path.join('.')}: ${e.message}`).join(', ')}`; } else if (error instanceof Error) { errorMessage = error.message; } // Provide helpful error messages if (errorMessage.includes("not found")) { errorMessage += "\n\nMake sure the file path is correct and the file exists."; } else if (errorMessage.includes("too small")) { errorMessage += "\n\nThe file must be at least 128KB for hash calculation."; } return { content: [ { type: "text", text: `Error: ${errorMessage}`, }, ], }; } }
  • MCP input schema definition for the calculate_file_hash tool, specifying required file_path parameter.
    inputSchema: { type: "object", properties: { file_path: { type: "string", description: "Path to the movie file" } }, required: ["file_path"], additionalProperties: false }
  • dist/server.js:146-160 (registration)
    Tool registration in the MCP server's tools array, including name, description, and input schema.
    { name: "calculate_file_hash", description: "Calculate OpenSubtitles hash for local movie files", inputSchema: { type: "object", properties: { file_path: { type: "string", description: "Path to the movie file" } }, required: ["file_path"], additionalProperties: false } }
  • Core helper utility that computes the OpenSubtitles-specific hash algorithm using file size and first/last 64KB chunks.
    export async function calculateOpenSubtitlesHash(filePath) { try { // Get file size const stats = await stat(filePath); const fileSize = stats.size; if (fileSize < 131072) { // 128KB minimum throw new Error("File too small for OpenSubtitles hash calculation (minimum 128KB)"); } const chunkSize = 65536; // 64KB // Read first 64KB const firstChunk = await readChunk(filePath, 0, chunkSize); // Read last 64KB const lastChunk = await readChunk(filePath, fileSize - chunkSize, chunkSize); // Calculate hash using file size as initial value let hash = BigInt(fileSize); // Add values from first chunk for (let i = 0; i < firstChunk.length; i += 8) { hash += BigInt(firstChunk.readBigUInt64LE(i)); } // Add values from last chunk for (let i = 0; i < lastChunk.length; i += 8) { hash += BigInt(lastChunk.readBigUInt64LE(i)); } // Convert to 16-character hex string const hashHex = (hash & BigInt("0xFFFFFFFFFFFFFFFF")).toString(16).padStart(16, '0'); return { hash: hashHex, size: fileSize, }; } catch (error) { if (error instanceof Error) { throw new Error(`Failed to calculate hash: ${error.message}`); } throw new Error("Failed to calculate hash: Unknown error"); } }

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/opensubtitles/mcp.opensubtitles.com'

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