Techdots

August 1, 2025

Adding AI to Your Rails App with MCP: Actor Collaboration Insights from a Movie Dataset

Adding AI to Your Rails App with MCP: Actor Collaboration Insights from a Movie Dataset

Can AI Understand Movie Industry Relationships Better Than Traditional Database Queries?

What if you could ask your Rails app questions like "Who has Leonardo DiCaprio worked with the most?" and get intelligent answers instead of writing complex SQL queries? This is exactly what happened when I connected Claude AI to a Rails project using the Model Context Protocol (MCP).

Instead of building another chatbot, I created something more powerful: an AI that can explore movie industry collaborations across millions of records and provide meaningful insights.

What is MCP and Why Should Rails Developers Care?

MCP (Model Context Protocol) is a bridge between your Rails backend and Large Language Models like Claude or GPT-4. Think of it as a way to give AI access to your business logic through structured tools and resources.

Here's what makes MCP different from just feeding raw data to AI:

  • Your Rails app exposes controlled access to specific functions
  • The AI can chain multiple tool calls together intelligently
  • You maintain full control over what data the AI can access
  • The AI acts like a reasoning engine that understands your domain

The Project: Letterboxing - A Massive Movie Database

For this experiment, I used Letterboxing, an open-source Rails app filled with film industry data:

Model Count
Actors 5.5 million
Movies 896,000
Crew 4.3 million
Studios 642,000
Genres 990,000

The goal was simple: let AI explore this movie world with context and intelligence, not just basic search filters.

Building the AI Tool: Actor Collaboration Analysis

I created one powerful tool called summarize_actor_collaborations that helps Claude answer questions like:

  • "Who are Tom Hanks' most frequent collaborators?"
  • "What was the first and last film Natalie Portman and Darren Aronofsky worked on together?"

How the Tool Works

The tool follows a simple process:

  1. Find all movies for a specific actor
  2. Find all other actors who appeared in those same movies
  3. Count how many times each collaborator worked together
  4. Show the first and last movie they shared

Here's the complete code:


module MCP
  class SummarizeActorCollaborationsTool < ApplicationTool
    tool_name "summarize_actor_collaborations"
    description <<~DESC
      Finds frequent collaborators for a given actor.
      Outputs each collaborator's name, how many movies they worked on together, 
      and their first + last shared movie.
    DESC
    arguments do
      required(:actor_name).filled(:string)
    end

    def call(actor_name:)
      actor = Actor.find_by("name ILIKE ?", "%#{actor_name}%")
      return "Actor not found" unless actor

      movie_ids = actor.movies.pluck(:id)

      collaborators = Actor.joins(:movies)
                           .where(movies: { id: movie_ids })
                           .where.not(id: actor.id)
                           .group("actors.id", "actors.name")
                           .select("actors.name AS collaborator_name, COUNT(*) AS shared_movie_count")
                           .order("shared_movie_count DESC")
                           .limit(10)
      collaborators.map do |c|
        shared_movies = Movie.joins(:actors)
                             .where(id: movie_ids, actors: { name: c.collaborator_name })
                             .order(:release_year)
        {
          collaborator_name:   c.collaborator_name,
          shared_movie_count:  c.shared_movie_count,
          first_movie_title:   shared_movies.first&.title,
          last_movie_title:    shared_movies.last&.title
        }
      end.to_json
    end
  end
end

  

Setting Up Claude with Your Rails App

Once you have the tool ready, connecting Claude Desktop on macOS is straightforward:

  1. Go to Settings > Agentic Editing
  2. Add an MCP server: http://localhost:3000/mcp/sse
  3. Create a profile and enable your custom tool
  4. Start asking questions!

Example in Action

When you ask Claude: "Who has worked with Natalie Portman the most, and what films did they share?"

Claude responds with structured data like this:


[
  {
    "collaborator_name": "Mila Kunis",
    "shared_movie_count": 2,
    "first_movie_title": "Black Swan",
    "last_movie_title": "Friends with Benefits"
  }
]

All this data comes directly from your live Rails app!

Important Development Setup

MCP tools need to be loaded when your Rails app starts. Make sure to enable eager loading in development:


# config/environments/development.rb
config.eager_load = !!ENV["EAGER_LOAD"]
  

Then start your Rails server with:


EAGER_LOAD=1 bin/rails s
  

Why This Beats Traditional Database Queries

This MCP approach offers several advantages over basic search filters:

Smart Relationship Understanding: The AI understands connections across different models and tables.

Ranked Results with Context: Instead of raw data, you get meaningful insights with narrative context.

Complex Logic Chaining: The AI can combine multiple steps of logic that would require complex SQL queries.

Natural Language Interface: Ask questions in plain English instead of writing database queries.

Real-World Applications: Perfect for editorial workflows, recommendation systems, and research tools.

You simply can't achieve this level of intelligent interaction with traditional tools like Ransack or basic search forms.

What Makes This Different from Regular AI Chatbots?

Most AI integrations just answer questions with pre-trained knowledge. This MCP integration:

  • Works with your live, current database
  • Respects your business logic and data relationships
  • Provides real-time insights from your actual data
  • Can be extended with multiple tools for different use cases
  • Maintains data security through controlled access

Conclusion

In less than a day, I transformed a Rails movie app into an AI-powered research tool. Claude can now explore actor collaborations like a film industry expert, uncovering patterns that would take hours of manual database work. This isn't just adding AI to your app - it's creating an intelligent teammate that understands your domain.

Ready to add AI superpowers to your Rails application? At TechDots, we help businesses integrate cutting-edge AI solutions that actually solve real problems. Let's build something amazing together!

Ready to Launch Your AI MVP with Techdots?

Techdots has helped 15+ founders transform their visions into market-ready AI products. Each started exactly where you are now - with an idea and the courage to act on it.

Techdots: Where Founder Vision Meets AI Reality

Book Meeting