Skip to main content
Glama
uzaysozen

imdb-mcp-server

get_writers

Retrieve the writers for a specific movie using its IMDb ID. This tool provides a JSON object listing all credited writers associated with the film.

Instructions

Get the writers of a movie from IMDb. Args: imdbId: The IMDb ID of the movie to get writers for. Returns: JSON object containing the writers of the movie.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
imdb_idYes

Implementation Reference

  • The core handler function for the 'get_writers' tool. It constructs the API URL using BASE_URL, calls the make_imdb_request helper to fetch writers data for the given imdb_id, handles empty response, and returns formatted JSON.
    @mcp.tool()
    async def get_writers(imdb_id: str, ctx: Context) -> str:
        """Get the writers of a movie from IMDb.
        Args:
            imdbId: The IMDb ID of the movie to get writers for.
        Returns:
            JSON object containing the writers of the movie.
        """
        writers_url = f"{BASE_URL}/{imdb_id}/writers"
        writers_data = await make_imdb_request(writers_url, {}, ctx)
        if not writers_data:
            return "Unable to fetch writers data for this movie or movie not found."
        return json.dumps(writers_data, indent=4)
  • In the create_server function (used in HTTP mode), the FastMCP server is created and register_tools is called on it, which defines and registers all tools including 'get_writers' via their @mcp.tool() decorators.
    # Create your FastMCP server as usual
    server = FastMCP("IMDb MCP Server")
    
    # Register all tools with the server
    register_tools(server)
  • In stdio mode of main(), the FastMCP server is created and register_tools is called, registering 'get_writers'.
    server = FastMCP("IMDb MCP Server")
    register_tools(server)
  • The helper function make_imdb_request used by get_writers to perform the HTTP request to the IMDb API endpoint, including caching via cache_manager, API key handling from context or env, and error handling.
    async def make_imdb_request(url: str, querystring: dict[str, Any], ctx: Optional[Context] = None) -> Optional[Dict[str, Any]]:
        """Make a request to the IMDb API with proper error handling and caching."""
        
        # Check if it's time to clean the cache
        cache_manager.cleanup_if_needed()
        
        # Create a cache key from the URL and querystring
        cache_key = f"{url}_{str(querystring)}"
        
        # Try to get from cache first
        cached_data = cache_manager.cache.get(cache_key)
        if cached_data:
            return cached_data
        
        # Get API key from session config or fallback to environment variable
        api_key = None
        if ctx and hasattr(ctx, 'session_config') and ctx.session_config:
            api_key = ctx.session_config.rapidApiKeyImdb
        
        if not api_key:
            api_key = os.getenv("RAPID_API_KEY_IMDB")
        
        # Not in cache, make the request
        headers = {
            "x-rapidapi-key": api_key,
            "x-rapidapi-host": "imdb236.p.rapidapi.com",
        }
        
        if not api_key:
            raise ValueError("API key not found. Please set the RAPID_API_KEY_IMDB environment variable or provide rapidApiKeyImdb in the request.")
        
        try:
            response = requests.get(url, headers=headers, params=querystring, timeout=30.0)
            response.raise_for_status()
            data = response.json()
            
            # Cache the response
            cache_manager.cache.set(cache_key, data)
                
            return data
        except Exception as e:
            raise ValueError(f"Unable to fetch data from IMDb. Please try again later. Error: {e}")
  • The BASE_URL constant used in get_writers to construct the writers endpoint URL.
    BASE_URL = "https://imdb236.p.rapidapi.com/api/imdb"

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/uzaysozen/imdb-mcp-server'

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