Skip to content

Instantly share code, notes, and snippets.

@OpenDGPS
Created April 1, 2025 16:06
Simple MCP server to query themoviedb
from typing import Any
import httpx
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("tmdb_agent")
async def make_tmdb_request(url: str) -> dict[str, Any] | None:
"""Make a request to the TMDB API with proper error handling."""
headers = {
"accept": "application/json",
"Authorization": "Bearer <api_token_developer.themoviedb.org>"
}
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, headers=headers, timeout=30.0)
response.raise_for_status()
return response.json()
except Exception:
return None
@mcp.tool()
async def get_tmdb_actors_id_by_fuzzy_name(fuzzy_name: str) -> str:
"""Get actors_id in the TMDB by fuzzy actor name.
Args:
fuzzy_name: Maybe not exact spelled name of an actor or actress
"""
actors_id_url = f"https://api.themoviedb.org/3/search/person?query={fuzzy_name}&include_adult=false&language=en-US&page=1"
actors_id_data = await make_tmdb_request(actors_id_url)
if not actors_id_data:
return f"Unable to fetch actor_id for {fuzzy_name}."
actors_id = actors_id_data['results'][0]['id']
return actors_id
@mcp.tool()
async def get_movies_by_actors_id(actors_id: str) -> str:
"""Get movie titles, years, and character by actor_id.
Args:
actor_id: TMDB actor_id of actor with credits in movies
"""
movie_credits_url = f"https://api.themoviedb.org/3/person/{actors_id}/combined_credits?language=en-US"
movie_credits_data = await make_tmdb_request(movie_credits_url)
if not movie_credits_data:
return f"Unable to fetch actor_id for {actors_id}."
casts = movie_credits_data['cast']
cast_summarys = []
for cast in casts:
cast_summary = f"""
{cast['title']}:
Character: {cast['character']}
Release Data: {cast['release_date']}
"""
cast_summarys.append(cast_summary)
return "\n---\n".join(cast_summarys)
if __name__ == "__main__":
mcp.run(transport='stdio')
@OpenDGPS
Copy link
Author

OpenDGPS commented Apr 1, 2025

Simple MCP server to query themoviedb

Based on https://modelcontextprotocol.io/quickstart/server

Add your API key for TMDB (line 11)!

Example:

Q: Which movies has Tom Whaits been in?

A: I'll help you find information about Tom Waits' movie appearances.

Tom Waits has appeared in several films throughout his career. Here are some of his notable movie appearances:

  1. The Outsiders (1983) - as Buck Merrill
  2. Rumble Fish (1983) - as Benny
  3. Down by Law (1986) - as Zack
  4. Coffee and Cigarettes (2004) - as himself in the segment "Somewhere in California"
  5. Licorice Pizza (2021) - as Rex Blau
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment