MongoDB Movie Database
Enables querying and analyzing the MongoDB sample_mflix movie dataset through natural language, supporting searches by title, genre, actors, directors, ratings, and providing aggregate statistics like counts and averages.
README
MongoDB Movie Database FastMCP Tools
This project provides a Python script that exposes a set of powerful tools for querying and analyzing a MongoDB movie database (specifically the sample_mflix dataset) using the fastmcp library. These tools are designed to be easily integrated with large language models (LLMs), AI agents, or any other system requiring structured, programmatic access to movie data.
Table of Contents
- Features
- Prerequisites
- MongoDB Setup
- Installation
- Usage
- FastMCP Tools
find_moviescount_moviesget_average_rating- Contributing
- License
Features
- Comprehensive Movie Search: Find movies by title, genre, actors, directors, writers, year, or various rating thresholds.
- Flexible Data Retrieval: Specify fields to return (
projection_fields) and control sorting (sort_by,sort_order_asc). - Movie Counting: Quickly count movies matching specific criteria.
- Average Rating Calculation: Compute average IMDb, Metacritic, or Rotten Tomatoes ratings for filtered movie sets.
- LLM-Friendly: Designed with
fastmcpto create a robust, self-documenting API easily consumable by LLMs. Includes special handling for stringified list arguments, addressing common LLM output formats. - Robust MongoDB Integration: Utilizes
pymongofor efficient and reliable database operations.
Prerequisites
Before running this project, ensure you have the following:
- Python 3.7+: Download Python
- MongoDB Instance: A running MongoDB instance (local or cloud-hosted like MongoDB Atlas).
sample_mflixDataset: Thesample_mflixdatabase and itsmoviescollection must be loaded into your MongoDB instance.
Optionally
- Claude Desktop
{
"mcpServers": {
"Movie Database": {
"command": "uv",
"args": [
"run",
"--with",
"fastmcp, pymongo",
"fastmcp",
"run",
"<path to>/movie-mcp/movie-mcp.py",
"<mongo connection string URI>"
]
}
}
}
MongoDB Setup
This application connects to the sample_mflix database and specifically the movies collection.
If you're using MongoDB Atlas:
- Log in to your MongoDB Atlas account.
- Navigate to your cluster.
- Go to the "..." (usually
DataorLoad Sample Data) tab. - Click "Load Sample Dataset" and select
sample_mflix. This will automatically import the necessary data.
If you're using a local MongoDB instance:
You can download the sample_mflix dataset from MongoDB's official resources (e.g., as part of the MongoDB University course materials or directly from their sample data repositories) and import it using mongoimport.
Installation
- Clone the repository:
git clone https://github.com/patw/movie-mcp.git
cd movie-mcp
- Install dependencies:
pip install -r requirements.txt
Usage
The script expects the MongoDB connection URI as a command-line argument.
- Run the script:
python movie_tools.py "mongodb://localhost:27017/"
Or, if using a MongoDB Atlas connection string:
python movie_tools.py "mongodb+srv://user:pass@clusterdomain/?retryWrites=true&w=majority"
Replace user, pass and clusterdomain with your actual MongoDB Atlas credentials and cluster details.
- FastMCP Server:
Once running, the script will start a
fastmcpserver. This server exposes the defined tools (e.g.,find_movies,count_movies) over a local HTTP endpoint (by defaulthttp://127.0.0.1:8000/tools). You can then interact with these tools programmatically, typically from an LLM agent or another Python script.
Example of how an LLM or another program might call these tools (conceptually):
# This is pseudo-code representing how an LLM agent might interact
# In a real scenario, you'd use a client library for fastmcp or direct HTTP requests.
# Example: Find movies by Bill Murray
tool_call = {
"tool_name": "find_movies",
"args": {
"actors": ["Bill Murray"],
"limit": 5,
"projection_fields": ["title", "year", "imdb.rating"]
}
}
# result = make_tool_call(tool_call)
# print(result)
# Example: Count romantic comedies from the 90s
tool_call = {
"tool_name": "count_movies",
"args": {
"genres": ["Comedy", "Romance"],
"start_year": 1990,
"end_year": 1999
}
}
# result = make_tool_call(tool_call)
# print(result)
FastMCP Tools
This section details the functions exposed as tools by fastmcp.
find_movies
Finds movies based on a variety of criteria, with options for sorting and limiting results.
def find_movies(
title: Optional[str] = None,
genres: Optional[Union[List[str], str]] = None,
actors: Optional[Union[List[str], str]] = None,
directors: Optional[Union[List[str], str]] = None,
writers: Optional[Union[List[str], str]] = None,
year: Optional[int] = None,
start_year: Optional[int] = None,
end_year: Optional[int] = None,
min_imdb_rating: Optional[float] = None,
min_metacritic_rating: Optional[int] = None,
min_tomatoes_viewer_rating: Optional[float] = None,
min_tomatoes_critic_rating: Optional[float] = None,
rated_mpaa: Optional[str] = None,
sort_by: Optional[str] = "imdb.rating",
sort_order_asc: bool = False,
limit: int = 10,
projection_fields: Optional[List[str]] = None
) -> List[Dict[str, Any]]:
Args:
title(str, optional): Movie title (case-insensitive partial match).genres(List[str] or str, optional): List of genres; movie must match all specified genres. If a single string is passed (e.g., "Comedy"), it's treated as a list of one.actors(List[str] or str, optional): List of actor names; movie must feature all specified actors (case-insensitive partial match for each name within the cast list). If a single string is passed, it's treated as a list of one.directors(List[str] or str, optional): List of director names; movie must be directed by all specified directors (case-insensitive partial match for each name). If a single string is passed, it's treated as a list of one.writers(List[str] or str, optional): List of writer names; movie must include all specified writers (case-insensitive partial match for each name). If a single string is passed, it's treated as a list of one.year(int, optional): Exact release year.start_year(int, optional): Start of a release year range (inclusive).end_year(int, optional): End of a release year range (inclusive).min_imdb_rating(float, optional): Minimum IMDb rating (e.g., 7.5).min_metacritic_rating(int, optional): Minimum Metacritic score (e.g., 70).min_tomatoes_viewer_rating(float, optional): Minimum Rotten Tomatoes viewer rating (e.g., 3.5).min_tomatoes_critic_rating(float, optional): Minimum Rotten Tomatoes critic rating (e.g., 7.0).rated_mpaa(str, optional): MPAA rating (e.g., "R", "PG-13"). Case-insensitive exact match.sort_by(str, optional): Field to sort results by. Can be a MongoDB path (e.g., "imdb.rating", "year", "title") or a short key ("imdb", "metacritic", "tomatoes_viewer", "tomatoes_critic", "imdb_votes", "tomatoes_viewer_num_reviews", "tomatoes_critic_num_reviews"). Defaults to 'imdb.rating'.sort_order_asc(bool, optional): Sort order.Falsefor descending (default, e.g., highest rated first),Truefor ascending (e.g., lowest rated first).limit(int, optional): Maximum number of results to return. Defaults to 10. Use0for no limit.projection_fields(List[str], optional): Specific fields to return for each movie (e.g.,["title", "year"]). Defaults to a standard set (title,year,plot,imdb.rating,genres).
Returns:
List[Dict[str, Any]]: A list of movie documents (or specified fields). Returns an empty list if no movies match the criteria or an error occurs.
count_movies
Counts movies based on the specified criteria.
def count_movies(
title: Optional[str] = None,
genres: Optional[Union[List[str], str]] = None,
actors: Optional[Union[List[str], str]] = None,
directors: Optional[Union[List[str], str]] = None,
writers: Optional[Union[List[str], str]] = None,
year: Optional[int] = None,
start_year: Optional[int] = None,
end_year: Optional[int] = None,
min_imdb_rating: Optional[float] = None,
min_metacritic_rating: Optional[int] = None,
min_tomatoes_viewer_rating: Optional[float] = None,
min_tomatoes_critic_rating: Optional[float] = None,
rated_mpaa: Optional[str] = None
) -> int:
Args:
(Same as the filtering arguments for the find_movies tool)
Returns:
int: The number of movies matching the criteria. Returns0if an error occurs.
get_average_rating
Calculates the average rating for movies matching the criteria, for a specific rating type.
def get_average_rating(
rating_field_key: str,
genres: Optional[Union[List[str], str]] = None,
actors: Optional[Union[List[str], str]] = None,
directors: Optional[Union[List[str], str]] = None,
writers: Optional[Union[List[str], str]] = None,
year: Optional[int] = None,
start_year: Optional[int] = None,
end_year: Optional[int] = None
) -> Optional[Dict[str, Any]]:
Args:
rating_field_key(str): The key for the rating source (e.g., "imdb", "metacritic", "tomatoes_viewer", "tomatoes_critic").- (Other filtering arguments are similar to those in
find_movies/count_movies, excludingtitle,min_ratings, andrated_mpaaas they are less common for broad average calculations).
Returns:
Optional[Dict[str, Any]]: A dictionary containing'average_rating'(float, rounded to 2 decimal places) and'movie_count'(int). ReturnsNoneif therating_field_keyis invalid, or a dict withNoneaverage_rating and0count if no movies match or an error occurs.
Contributing
Contributions are welcome! If you have suggestions for improvements, new features, or bug fixes, please open an issue or submit a pull request.
License
This project is open-sourced under the MIT License. See the LICENSE file for more details.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。