-
-
Save nasa03/ba7c3d8ceffe8ca54adb48c4dce4d9f7 to your computer and use it in GitHub Desktop.
Simple MCP server to query themoviedb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment