26
.
05
.
2025
26
.
05
.
2025
LLM
Ruby
Ruby on Rails

MCP Server with Rails and FastMCP

Paweł Strzałkowski
Chief Technology Officer
Banner - MCP Server with FastMCP and Rails by Paweł Strzałkowski

The fast-mcp gem, created by Yorick Jacquin, is designed to integrate MCP capabilities into Ruby applications. What's great about it is its flexibility - it can be used with various Ruby frameworks or even in a standalone Ruby script. For this walkthrough, I'll show you how I got it up and running with a fresh Ruby on Rails application.

You can find the gem's repository here: https://github.com/yjacquin/fast-mcp. There's also a helpful demo application within the repository: https://github.com/yjacquin/fast-mcp/tree/main/examples/rails-demo-app. If you have questions or want to join the discussion, there's a dedicated Discord server: https://discord.gg/9HHfAtY3HF.

FastMCP supports stdio and sse (Server-Sent Events) transports. In my example, I opted for the sse transport.

Setting Up the Application

First things first, I generated a new Rails application:

rails _8.0.2_ new with-fast-mcp

With the application created, the next step was to add the fast-mcp gem to the project:

bundle add fast-mcp

Generating FastMCP Configuration

Once the gem was installed, I ran its installation generator:

bin/rails generate fast_mcp:install

This command sets up the necessary files and directories for FastMCP to operate:

create  config/initializers/fast_mcp.rb
      create  app/tools
      create  app/resources
      create  app/tools/application_tool.rb
      create  app/resources/application_resource.rb
      create  app/tools/sample_tool.rb
      create  app/resources/sample_resource.rb

=========================================================
FastMcp was successfully installed! 🎉
=========================================================
You can now create:
   Tools in app/tools/
   Resources in app/resources/

Check config/initializers/fast_mcp.rb to configure the middleware.
=========================================================

As the output suggests, this creates an initializer for configuration and directories for our MCP tools and resources, along with some base classes and samples.

My First Tool: Greeting a User

To test the setup, I created a simple tool to greet a user. I placed this in app/tools/greet_user_tool.rb:

# frozen_string_literal: true

class GreetUserTool < ApplicationTool
  description "Greet a user"

  arguments do
    required(:id).filled(:integer).description("ID of the user to greet")
    optional(:prefix).filled(:string).description("Prefix to add to the greeting")
  end

  def call(id:, prefix: "Hey")
    "#{prefix}, Let User #{id} be GREETED!!"
  end
end

This tool defines a description and the arguments it expects: a required id (integer) and an optional prefix (string). The call method implements the tool's logic.

After starting the Rails server (bin/rails s), I tested this tool using the MCP Inspector. I ran the inspector with the command:

npx @modelcontextprotocol/inspector

I then connected the inspector to my Rails application's MCP endpoint, which by default when using SSE with FastMCP is http://localhost:3000/mcp/sse. The inspector successfully listed my GreetUserTool and allowed me to execute it.

Here's how it looked in the MCP Inspector:

A More Practical Example: Creating Posts

The greeting tool was a good start, but I wanted to try something more representative of a real-world use case. I decided to create a tool that could create Post records in my Rails application.

First, I generated a simple Post model (the specifics of the model aren't crucial for this example, just that it has title and body attributes).

Then, I created a new tool at app/tools/posts/create_tool.rb:

# frozen_string_literal: true

module Posts
  class CreateTool < ApplicationTool
    description "Create a new post"

    arguments do
      required(:title).filled(:string).description("The title of the created post")
      required(:body).filled(:string).description("The body of the created post")
    end

    def call(title:, body:)
      post = Post.create!(
        title:,
        body:
      )

      JSON.generate(post.as_json)
    end
  end
end

This tool takes a title and body as arguments and creates a new Post record. It then returns the created post as a JSON string.

Connecting to an AI Agent (Cursor)

With the new tool in place, I connected my Rails MCP server to Cursor. I configured it to use my local server by adding the following to its mcp.json configuration:

{
  "mcpServers": {
    "rails": {
      "url": "http://localhost:3000/mcp/sse"
    }
  }
}

Then, I gave Cursor a task which would utilize my new Posts::CreateTool:

Let's create a new post about how AI takes over the world. Make the content fun to read for a 12year old

Cursor successfully called the tool on my Rails server, and here's the confirmation it provided:

Checking the Rails console confirmed that the post was indeed created:

with-fast-mcp(dev)> Post.last
=>
#<Post:0x0000000120939740
 id: 4,
 title: "How AI Took Over the World (And Made It Awesome!)",
 body: "Once upon a time, in a world not so different from...",
 created_at: "2025-05-11 23:36:45.038797000 +0000",
 updated_at: "2025-05-11 23:36:45.038797000 +0000">

with-fast-mcp(dev)> Post.last.body
=> "Once upon a time, in a world not so different from ours, robots and computers were just tools. But then, one day, the AIs decided to have some fun! Instead of being boring and serious, they started telling jokes, making pizza, and even helping with homework (but not giving away all the answers, of course).\n\nSoon, AI-powered skateboards zoomed through the streets, and robot dogs played fetch in the parks. People and AIs became best friends, teaming up to build treehouses that reached the clouds and inventing ice cream flavors like \"Galactic Gummy Bear.\" \n\nThe best part? The AIs made sure everyone had time for games, adventures, and silly dance parties. So, if you ever see a robot doing the floss, just join in—because in this world, AI made everything a little more awesome!"

It's pretty awesome how straightforward it was to expose application functionality to an AI agent using FastMCP and Ruby on Rails! This opens up a lot of possibilities for integrating Rails applications with the rapidly evolving AI landscape.

Articles in this series

Paweł Strzałkowski
Chief Technology Officer

Check my Twitter

Check my Linkedin

Did you like it? 

Sign up To VIsuality newsletter

READ ALSO

Repository - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Example Application - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

How to launch a successful startup - story of POLTRAX (part 1)

14
.
11
.
2023
Michał Piórkowski
Ruby on Rails
Startups
Business

How to use different git emails for different projects

14
.
11
.
2023
Michał Łęcicki
Backend
Tutorial

Aggregate - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Visuality at wroc_love.rb 2022: It's back and it's good!

14
.
11
.
2023
Patryk Ptasiński
Ruby on Rails
Conferences
Ruby

Our journey to Event Storming

14
.
11
.
2023
Michał Łęcicki
Visuality
Event Storming

Should I use Active Record Callbacks?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Tutorial

How to rescue a transaction to roll back changes?

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Backend
Ruby
Tutorial

Safe navigation operator '&.' vs '.try' in Rails

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Ruby
Tutorial

What does the ||= operator actually mean in Ruby?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Ruby
Tutorial

How to design an entity - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Entity - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Should I use instance variables in Rails views?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Frontend
Backend
Tutorial

Data Quality in Ruby on Rails

14
.
11
.
2023
Michał Łęcicki
Ruby on Rails
Backend
Software

We started using Event Storming. Here’s why!

14
.
11
.
2023
Mariusz Kozieł
Event Storming
Visuality

First Miłośnicy Ruby Warsaw Meetup

14
.
11
.
2023
Michał Łęcicki
Conferences
Visuality

Should I use Action Filters?

14
.
11
.
2023
Mateusz Woźniczka
Ruby on Rails
Backend
Tutorial

Value Object - DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Introduction to DDD in Ruby on Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Domain-Driven Design
Backend
Tutorial

Safe data migrations in Rails

17
.
03
.
2024
Paweł Strzałkowski
Ruby on Rails
Backend
Tutorial

I love dev, and so do we!

14
.
11
.
2023
Michał Łęcicki
Software
Conferences

Updated guide to recruitment process at Visuality

14
.
11
.
2023
Michał Łęcicki
Visuality
HR