Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
自然言語で ActiveRecord を操作する試み
Search
dak2
October 06, 2025
0
25
自然言語で ActiveRecord を操作する試み
Stdio の MCP Server を使って自然言語で ActiveRecord を操作してレコードを取得する試みについてのスライドです
dak2
October 06, 2025
Tweet
Share
More Decks by dak2
See All by dak2
DoD x RBS
dak2
0
1
Featured
See All Featured
Balancing Empowerment & Direction
lara
4
680
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Building Adaptive Systems
keathley
43
2.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
2.7k
Docker and Python
trallard
46
3.6k
RailsConf 2023
tenderlove
30
1.2k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
53k
What's in a price? How to price your products and services
michaelherold
246
12k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Transcript
2025/10/06 【リジェクトConライク】Re:cycle〜Kaigi on Rails 2025編〜 株式会社タイミー 神山大智 自然言語で ActiveRecord を操作する試み
@_dak2_
自己紹介 Daichi Kamiyama (@dak2) • 株式会社タイミー(2024年6月入社) • バックエンドエンジニア ◦ 働き手であるワーカーの時間と事業者の時間のマッチング部
分を最適化するための改善や機能開発に従事 ◦ 最近は生成AIに対して丁寧にお伺いすることを生業にしてい るが、生成AIのアウトプットをレビューする認知負荷の高さに 疲弊している
今日伝えたいこと 自然言語で ActiveRecord は 操作できる!! *1 *1 本番環境で検証できてないです
目次 • 動機の話 • MCP との出会い • MCP サーバーを作る •
自然言語でクエリしよう
ある日思ったんですよね
自然言語でレコード を参照できると嬉しそう
rails c とか rails runner で取得して コピペしたら良いよね
値を取得するために 都度クエリを叩かないと
面倒だネ!
MCP を使えば なんとかできるんじゃない?
そもそも MCP って何?
AI アプリケーションと外部システム を繋ぐ機構のこと https://modelcontextprotocol.io/docs/getting-started/intro
None
AI エージェントが Google カレンダー とか Notion にアクセスして、パーソナ ライズされたレスポンスを返せる
MCP のユースケースって 何があるの?
https://github.com/modelcontextprotocol/servers
modelcontextprotocol/servers に MCP Server の実例、いわゆる “Real World MCP” が掲載されています
• Heroku • Notion • Prisma • TiDB • AWS,
Azure, Google Cloud Run, etc…
MCP のユースケース 増えていってる
9/23 に chrome-dev-tools MCP が発 表されました ブラウザのコンソールログやネットワークログを取得したり、スクリーンショッ トを撮影したりすることで、デバッグ情報を AI エージェントに提供できるみた
いです。便利そう https://developer.chrome.com/blog/chrome-devtools-mcp?hl=ja
データベース周りの MCP は どんなものがある?
https://github.com/googleapis/genai-toolbox
https://github.com/pingcap/pytidb
https://github.com/aliyun/alibabacloud-adb-mysql-mcp-server
https://github.com/niledatabase/nile-mcp-server
MCP でデータベースと接続する ユースケースはありそう
MCP 経由でデータベース見に行かなく ても、Rails の ActiveRecord を介して 見に行った方が早そう (接続時の認証まわり面倒そうですし)
ActiveRecord を介してレコードを取得 する MCP サーバーを作ろう
MCP サーバーを 自作していく
gem あるかな?
• fast-mcp ◦ 割と機能が揃ってる • mcp-rb ◦ Sinatra like の
DSL でシンプルに書ける • ruby-sdk ◦ MCP の Ruby 公式 SDK
複雑なものを 作るわけではないので拘りがない => 公式を使っておこう
前提 • 生成 AI の GUI は Claude Desktop ◦
=> Client • MCP サーバーは stdio • GUI から Rails プロジェクトのレコー ドを取得する
補足 MCP サーバーには • stdio ◦ 標準入出力なのでプロセスとして動かす • Streamable HTTP
◦ HTTP POST/GET を通じてやり取り
Let’s building
https://github.com/modelcontextprotocol/ruby-sdk/tree/main/examples 1. 実現方法を確認
https://github.com/modelcontextprotocol/ruby-sdk/blob/main/examples/stdio_server.rb 1. 実現方法を確認
とてもシンプルな構成で、Tool 定義し て stdio サーバーを立ち上げる 1. 実現方法を確認
ここで道筋の整理 🛤 • ActiveRecord を介してレコードを取 得できる Tool を定義 • Stdio
Server に定義した Tool を設定 • 標準入出力として呼び出し • Claude Desktop と連携 1. 実現方法を確認
https://github.com/modelcontextprotocol/ruby-sdk/blob/main/examples/stdio_server.rb 2. Tool を定義
https://github.com/dak2/activerecord-mcp/blob/main/lib/activerecord-mcp/tools/select_records.rb 2. Tool を定義
https://github.com/dak2/activerecord-mcp/blob/main/lib/activerecord-mcp/tools/select_records.rb 2. Tool を定義
https://github.com/dak2/activerecord-mcp/blob/main/lib/activerecord-mcp/tools/select_records.rb 2. Tool を定義
Open3#capture3 を使って別ディ レクトリの Rails アプリケーション で ActiveRecord を実行する 2. Tool
を定義
少し Open3 の説明
https://docs.ruby-lang.org/en/3.4/Open3.html#method-i-capture3 2-1. Open3
https://docs.ruby-lang.org/en/3.4/Open3.html#method-c-popen3 2-1. Open3
ActiveRecord を実行する別の Rails ア プリケーションのディレクトリを指定 する必要がある 2-1. Open3
https://docs.ruby-lang.org/en/3.4/Process.html#method-c-spawn 2-1. Open3
https://github.com/dak2/activerecord-mcp/blob/main/lib/activerecord-mcp/tools/select_re cords.rb 2-1. Open3
2-1. Open3 つまり、 大いなる力を使う ということです
https://github.com/dak2/activerecord-mcp/blob/main/exe/activerecord-mcp 3. Stdio MCP Server の立ち上げ設定
exe ディレクトリに定義した Stdio MCP Server をクライアント側、つまり Claude Desktop から呼んでもらう 3.
Stdio MCP Server の立ち上げ設定
~/Library/Application Support/Claude/claude_desktop_config.json 4. Claude Desktop と繋ぎ込み
別の Rails アプリケーションと言及して いましたが、posts だけのテーブルを 持った rails new に毛が生えた程度のア プリで実験してます
補足
デモ
注意事項
• SQL 文はちゃんと検証した方が良い • 本番環境で試していないです ◦ できると便利そうではある。ちょっと怖いけど • 認証周り(Authorization)未対応です •
かなり力技の実装になってると思いますw
まとめ
• 自然言語でクエリできる MCP サーバーも サクッと作れる • Open3 は用法用量を守って使った方が良 さそう •
modelcontextprotocol/servers 見ると 様々なユースケースがあるので見てみると 面白い