In the previous lessons, you learned how to set up and use Neo4j MCP tools to explore databases and test queries. Now it’s time to put everything together and see the real power of MCP tools in action.
One of the most exciting aspects of using MCP tools with AI agents is their ability to work collaboratively. The agent will select the correct tools in sequence, executing them one after another until the task is complete.
You can use this to your advantage by getting the agent to quickly build entire applications for you. This is sometimes called "vibe coding" - where you describe what you want, and the AI figures out how to build it.
Your goal is to instruct Copilot to build a Python program that allows a user to enter a movie genre and returns the top 5 movies in that genre.
Setting up environment variables
Before you begin, you’ll need to set up your environment variables for the Neo4j connection.
Create a .env
file in your project directory with the following variables:
NEO4J_URI=neo4j+s://ad15bc90.databases.neo4j.io:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=8GOSvyvlNxYT31B1fLF5MfzEtVKlvcvuvKJpPRvLFkg
The challenge
Ask Copilot to create this application using the following prompt:
Create a simple command line application in python that:
1. Connects to Neo4j using the Neo4j Python driver using environment variables: NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD.
2. Accepts a Genre name as a text input
2. Runs a Cypher statement that produces a list of the top five movies ordered by their imdbRating in descending order.
Rules:
* The query must filer on non-null values
* You must use tools to understand the schema information
* You must validate your Cypher statement on real data from the database before you write it to the application.
Copilot will use the MCP tools to:
-
Explore the database schema to understand the data structure
-
Write and test a Cypher query to ensure it works correctly
-
Generate the Python application that uses the Neo4j Python driver
-
Validate the solution by running test queries
The power of validation
Notice how the prompt instructs the agent to validate the Cypher statement on real data before creating the application. This is a key practice - the agent will use the read-neo4j-cypher
tool to test queries first, ensuring the final application actually works.
Testing your application
Once Copilot has built your application, test it by running the program and entering Mystery as the genre.
If everything works correctly, you should have everything you need to answer the question to complete the challenge.
Probabilistic outputs
The output is probabilistic, so the application structure may vary slightly between runs, but you should get consistent results from the database queries. That’s the goal of any good data application - reliable, accurate results regardless of how the code is structured.
Extra credit
Why not ask Copilot to write a unit test for the application?
Check your understanding
Testing Your Mystery Genre Application
When you run your Python application and enter "Mystery" as the genre, which movie should appear as one of the top-rated movies in the Mystery genre?
-
❏ The Matrix
-
✓ Inception
-
❏ The Dark Knight
-
❏ Pulp Fiction
Hint
Run your application with "Mystery" as the input, or test the Cypher query directly using the integrated Query pane. Click the window icon in the bottom right of your screen to open the Query pane and run the query manually.
Solution
The answer to this question is Inception.
Here is a sample application that was created using the MCP tools in Cursor:
#!/usr/bin/env python3
import os
import sys
from neo4j import GraphDatabase
def get_top_movies_by_genre(genre_name):
"""
Retrieve the top 5 movies by genre ordered by imdbRating in descending order.
Args:
driver: Neo4j driver instance
genre_name: Name of the genre to filter by
Returns:
List of dictionaries containing movie title and imdbRating
"""
query = """
MATCH (g:Genre)<-[:IN_GENRE]-(m:Movie)
WHERE g.name = $genre_name AND m.imdbRating IS NOT NULL
RETURN m.title AS title, m.imdbRating AS imdbRating
ORDER BY m.imdbRating DESC
LIMIT 5
"""
# Get Neo4j connection details from environment variables
neo4j_uri = os.getenv('NEO4J_URI')
neo4j_username = os.getenv('NEO4J_USERNAME')
neo4j_password = os.getenv('NEO4J_PASSWORD')
# Connect to Neo4j
with GraphDatabase.driver(neo4j_uri, auth=(neo4j_username, neo4j_password)) as driver:
# Test connection
driver.verify_connectivity()
print(f"Connected to Neo4j at {neo4j_uri}")
records, keys, summary = driver.execute_query(query, {"genre_name": genre_name})
return [record.data() for record in records]
def main():
"""Main application function"""
# Validate environment variables
neo4j_uri = os.getenv('NEO4J_URI')
neo4j_username = os.getenv('NEO4J_USERNAME')
neo4j_password = os.getenv('NEO4J_PASSWORD')
if not all([neo4j_uri, neo4j_username, neo4j_password]):
print("Error: Please set the following environment variables:")
print("- NEO4J_URI")
print("- NEO4J_USERNAME")
print("- NEO4J_PASSWORD")
sys.exit(1)
# Get genre input from user
genre_name = input("Enter a genre name: ").strip()
if not genre_name:
print("Error: Genre name cannot be empty")
sys.exit(1)
try:
# Get top movies for the specified genre
movies = get_top_movies_by_genre(genre_name)
if not movies:
print(f"No movies found for genre: {genre_name}")
else:
print(f"\nTop 5 movies in '{genre_name}' genre by IMDB rating:")
print("-" * 60)
for i, movie in enumerate(movies, 1):
print(f"{i}. {movie['title']} - IMDB Rating: {movie['imdbRating']}")
except Exception as e:
print(f"Error in application: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
Summary
You’ve experienced the power of using MCP tools to rapidly prototype and build applications. The agent was able to:
-
Understand the database schema
-
Write and validate Cypher queries
-
Generate a complete Python application
-
Test the solution end-to-end
This demonstrates how MCP tools can accelerate development workflows, allowing you to focus on the "what" while the AI handles the "how".