TM

tonksthebear/mcp-rails

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

31 stars 6 forks 质量 55 趋势 11MIT

mcp-rails 是 Ruby on Rails 的一款杰作,它建立在 mcp-rb 库之上,能够无缝集成 MCP(模型上下文协议)服务器到你的 Rails 应用中。

概览

mcp-rails 是 Ruby on Rails 的一个宝石,构建在 MCP-RB 库之上,能够无缝集成 MCP(模型上下文协议)服务器到你的 Rails 应用中。它通过允许你为路由添加 MCP 专属的元数据来增强 Rails 路由,并生成一个有效的 Ruby MCP 服务器(在 tmp/mcp/server.rb),LLM 代理如 Goose 可以连接到该服务器。此外,它还提供了一种强大的方式来定义和管理控制器中的强参数,既是 MCP 服务器配置,也是 Rails 强参数强制执行。 这灵感来源于Gaggle的创作过程中。 - : Tag Rails 通过 mcp: true 或特定动作(例如 mcp: [:index, :create])路由,将它们暴露给 MCP 服务器。- : 生成一个 Ruby MCP 服务器,格式为 tmp/mcp/server.rb,供 LLM 代理与你的应用交互。- : 在控制器中定义允许参数,包含丰富的元数据(如类型、示例、所需标志),这些参数既用于 MCP 服务器生成,也用于 Rails 强参数。

概述(AI 翻译)

通过MCP(模型上下文协议)集成,增强LLM代理的Rails路由和参数处理能力。

mcp-rails 是 Ruby on Rails 的一个宝石,构建在 mcp-rb 库之上,能够无缝集成 MCP(模型上下文协议)服务器到你的 Rails 应用中。它通过允许你用 MCP 特定元数据标记路由,并生成一个有效的 Ruby MCP 服务器(位于 tmp/mcp/server.rb),LLM 代理如 Goose 可以连接到。此外,它还提供了一种强大的方式来定义和管理控制器中的强参数,既是 MCP 服务器配置,也是 Rails 强参数强制执行。

这灵感来源于Gaggle的创作过程中。


特色

  • 标记路由:标签轨道路由带有mcp: true或特定操作(例如mcp: [:index, :create])以暴露给MCP服务器。
  • 自动MCP服务器生成:生成一个Ruby MCP服务器,位于tmp/mcp/server.rb,供LLM代理与你的应用交互。
  • 参数定义:在具有丰富元数据的控制器中定义允许的参数(例如:

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

MCP-Rails

Enhance Rails routing and parameter handling for LLM agents with MCP (Model Context Protocol) integration.

mcp-rails is a Ruby on Rails gem that builds on top of the mcp-rb library to seamlessly integrate MCP (Model Context Protocol) servers into your Rails application. It enhances Rails routes by allowing you to tag them with MCP-specific metadata and generates a valid Ruby MCP server (in tmp/mcp/server.rb) that LLM agents, such as Goose, can connect to. Additionally, it provides a powerful way to define and manage strong parameters in your controllers, which doubles as both MCP server configuration and Rails strong parameter enforcement.

This was inspired during the creation of Gaggle.


Features

  • Tagged Routes: Tag Rails routes with mcp: true or specific actions (e.g., mcp: [:index, :create]) to expose them to an MCP server.
  • Automatic MCP Server Generation: Generates a Ruby MCP server in tmp/mcp/server.rb for LLM agents to interact with your application.
  • Parameter Definition: Define permitted parameters in controllers with rich metadata (e.g., types, examples, required flags) that are used for both MCP server generation and Rails strong parameters.
  • HTTP Bridge for LLM Agents: Generates a ruby based MCP server to interact with your application through HTTP requests, ensuring LLM agents follow the same paths as human users.
  • Environment Variable Integration: Automatically includes specified environment variables in MCP tool calls.

Installation

Add this line to your application’s Gemfile:

gem 'mcp-rails'

Then run:

bundle install

Ensure you also have the mcp-rb gem installed, as mcp-rails depends on it:

gem 'mcp-rb'

Configuration

MCP-Rails can be configured in an initializer file. Create config/initializers/mcp_rails.rb:

MCP::Rails.configure do |config|
  # Server Configuration
  config.server_name = "my-app-server"      # Default: 'mcp-server'
  config.server_version = "2.0.0"           # Default: '1.0.0'
  
  # Output Configuration
  config.output_directory = Rails.root.join("tmp/mcp")  # Default: Rails.root.join('tmp', 'mcp')
  config.bypass_key_path = Rails.root.join("tmp/mcp/bypass_key.txt")  # Default: Rails.root.join('tmp', 'mcp', 'bypass_key.txt')
  
  # Environment Variables
  config.env_vars = ["API_KEY", "ORGANIZATION_ID"]  # Default: ['API_KEY', 'ORGANIZATION_ID']
  
  # Base URL Configuration
  config.base_url = "http://localhost:3000"  # Default: Uses action_mailer.default_url_options
end

If you are an engine developer, you can register your engine’s configuration with MCP-Rails:

MCP::Rails.configure do |config|
  config.register_engine(YourEngine, env_vars: ["YOUR_ENGINE_KEY"])
end

Environment Variables

在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →