使用说明
MCP轨道
MCP Rails是一个Ruby on Rails引擎,它为您的Rails应用程序提供模型上下文协议(MCP)支持。它允许您将ActiveRecord模型暴露给Claude等AI助手,自动生成可以通过MCP访问的CRUD操作。
特性
- 🚀 自动CRUD生成 -通过列表、获取、创建、更新和删除操作立即公开您的模型
- 🔒 内置身份验证 -支持令牌和API密钥身份验证策略
- 🎯 选择性暴露 -选择要公开的模型和属性
- 📝 只读模式 -可选地将模型公开为只读
- 🔧 易于配置 -用于模型配置的简单DSL
- 🏗️ Rails引擎 -干净、隔离的命名空间,易于安装
安装
将此行添加到应用程序的Gemfile中:
# Using GitHub (until published to RubyGems)
gem "mcp_rails", github: "EmreErdogan/mcp_rails"
# Or using local path for development
gem "mcp_rails", path: "../mcp_rails"
然后执行:
$ bundle install
$ rails generate mcp_rails:install
快速开始
1.展示你的模型
将以下内容添加到要公开的任何ActiveRecord模型中:
class Product < ApplicationRecord
include McpRails::McpExposable
expose_to_mcp
end
class Customer < ApplicationRecord
include McpRails::McpExposable
expose_to_mcp attributes: [:id, :name, :email]
end
class Order < ApplicationRecord
include McpRails::McpExposable
expose_to_mcp read_only: true
end
2.配置身份验证(可选)
编辑 config/initializers/mcp_rails.rb:
McpRails.configure do |config|
config.server_name = "my_app_mcp"
config.server_version = "1.0.0"
# For production
config.auth_strategy = :token
config.auth_token = Rails.application.credentials.mcp[:auth_token]
end
3.设置克劳德桌面
复制生成的 .mcp.json.example 到您的Claude Desktop配置:
macOS:
cp .mcp.json.example ~/Library/Application\ Support/Claude/claude_desktop_config.json
窗户:
copy .mcp.json.example %APPDATA%\Claude\claude_desktop_config.json
4.测试您的设置
# In Rails console
rails console
McpRails.server.tools.keys
# => ["list_products", "get_product", "create_product", "update_product", "delete_product", ...]
配置选项
模型曝光选项
class Product < ApplicationRecord
include McpRails::McpExposable
# Expose all attributes with all CRUD operations
expose_to_mcp
# Expose specific attributes only
expose_to_mcp attributes: [:id, :name, :price, :description]
# Read-only exposure (only list and get operations)
expose_to_mcp read_only: true
# Combine options
expose_to_mcp attributes: [:id, :name], read_only: true
end
服务器配置
McpRails.configure do |config|
# Server identification
config.server_name = "my_app_mcp"
config.server_version = "1.0.0"
config.server_instructions = "Custom instructions for AI assistants"
# Authentication
config.auth_strategy = :token # Options: :none, :token, :api_key
config.auth_token = ENV['MCP_AUTH_TOKEN']
config.api_key = ENV['MCP_API_KEY']
# Logging
config.log_requests = true # Log all MCP requests
# Rate limiting (requires additional middleware)
config.rate_limit = 100 # requests per minute
end
认证策略
令牌身份验证
config.auth_strategy = :token
config.auth_token = "your-secret-token"
客户端必须在Authorization标头中包含令牌:
Authorization: Bearer your-secret-token
API密钥验证
config.auth_strategy = :api_key
config.api_key = "your-api-key"
客户端必须在X-API-key标头中包含API密钥:
X-API-Key: your-api-key
无身份验证(仅用于开发)
config.auth_strategy = :none # ⚠️ Use only in development!
生成的工具
对于每个公开的模型,MCP Rails生成以下工具:
| 工具 | 说明 | 示例 |
|---|---|---|
list_{model}s |
列出所有记录 | list_products |
get_{model} |
按ID获取单个记录 | get_product |
create_{model} |
创建新记录 | create_product |
update_{model} |
更新现有记录 | update_product |
delete_{model} |
删除记录 | delete_product |
高级用法
自定义工具名称
即将推出:支持自定义工具命名约定。
回调和验证
所有ActiveRecord验证和回调都按预期工作:
class Product < ApplicationRecord
include McpRails::McpExposable
expose_to_mcp
validates :name, presence: true
validates :price, numericality: { greater_than: 0 }
before_save :calculate_tax
after_create :send_notification
end
范围界定和权限
即将推出:支持用户范围的查询和权限系统。
发展
签出repo后,运行:
cd mcp_rails
bundle install
rails test
在Rails应用程序中进行测试:
# In your Rails app's Gemfile
gem 'mcp_rails', path: '/path/to/mcp_rails'
测试MCP服务器
您可以直接测试MCP服务器:
# Run the server in stdio mode
cd your_rails_app
ruby bin/mcp_server
# The server expects JSON-RPC messages on stdin
# Send a test message to list tools
echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | ruby bin/mcp_server
故障排除
“名称错误:未初始化的常量McpRails::McpExposable”
安装后请确保重新启动Rails服务器:
rails restart
工具未显示
确保您的模型正在加载:
# In Rails console
Rails.application.eager_load!
McpRails::Registry.models
身份验证错误
检查您的身份验证配置:
# In Rails console
McpRails.configuration.auth_strategy
McpRails.configuration.auth_token
贡献
- 叉起来
- 创建功能分支(
git checkout -b feature/my-new-feature) - 提交您的更改(
git commit -am 'Add some feature') - 推到分支(
git push origin feature/my-new-feature) - 创建新的Pull请求
许可证
根据以下条款,gem可作为开源软件使用 MIT许可证.
致谢
内置于❤️ Rails和AI社区。特别感谢Anthropic创建了模型上下文协议。
