### Running Generated MCP Server with Goose (Shell) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Provides a shell command example demonstrating how to start a Goose session and load the `mcp-rails` generated Ruby server file (`tmp/mcp/server.rb`) as an extension, enabling Goose to interact with the application via the MCP protocol. ```shell goose session --with-extension "ruby path_to/tmp/mcp/server.rb" ``` -------------------------------- ### Install Gems with Bundle (Bash) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Run this command in your terminal after updating the Gemfile to download and install the specified gems. ```Bash bundle install ``` -------------------------------- ### Example MCP Action Parameters (JSON) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Shows the expected JSON structure of parameters that the LLM agent will provide based on the `permitted_params_for` definition, demonstrating nested resources and array types. ```json { "channel": { "name": "Channel Name", "user_ids": ["1", "2"] } } ``` -------------------------------- ### Standardized MCP Response Format (JSON) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Provides an example of the standardized JSON structure used for all MCP responses. It includes a status code, a data wrapper, and demonstrates automatic camelization of keys (e.g., `user_ids` becomes `userIds`). ```json { "status": 200, "data": { "name": "General", "userIds": ["1", "2"] # Note: automatically camelized } } ``` -------------------------------- ### Defining Permitted Parameters for MCP in Rails Controller (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Illustrates how to use the `permitted_params_for` block within a Rails controller to define the structure, types, and examples for parameters expected by an action, which `mcp-rails` uses for API generation and strong parameters. ```ruby # app/controllers/channels_controller.rb class ChannelsController < ApplicationController permitted_params_for :create do param :channel, required: true do param :name, type: :string, example: "General Chat", required: true param :goose_ids, type: :array, example: ["goose-123", "goose-456"] end end def index @channels = Channel.all render json: @channels end def create @channel = Channel.new(resource_params) if @channel.save render json: @channel, status: :created else render json: @channel.errors, status: :unprocessable_entity end end end ``` -------------------------------- ### Configure MCP-Rails Initializer (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Create this initializer file to customize the behavior of MCP-Rails, including server metadata, output locations, environment variables to include in tool calls, and the base URL for API requests. ```Ruby 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 ``` -------------------------------- ### Including MCP-Rails Test Helper (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Demonstrates how to include the `MCP::Rails::TestHelper` module in an `ActionDispatch::IntegrationTest` class. This helper simplifies testing MCP server responses by handling server generation, initialization, and cleanup automatically. ```ruby require "mcp/rails/test_helper" class MCPChannelTest < ActionDispatch::IntegrationTest include MCP::Rails::TestHelper end ``` -------------------------------- ### Running MCP Server with Goose Agent (Bash) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Shows how to run the generated MCP server as an extension when using an LLM agent like Goose, allowing the agent to interact with the Rails application's defined MCP endpoints. ```bash goose session --with-extension "path_to/tmp/mcp/server.rb" ``` -------------------------------- ### Tagging Routes for MCP Exposure in Rails (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Shows how to use the `mcp` option in `config/routes.rb` to specify which routes or actions should be exposed to the generated MCP server for LLM agent interaction. ```ruby # config/routes.rb Rails.application.routes.draw do resources :channels, only: [:index, :create], mcp: true resources :posts, mcp: [:create] end ``` -------------------------------- ### Generating the MCP Server (Bash) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Provides the command-line instruction to generate the executable MCP server file (`tmp/mcp/server.rb`) based on the configured routes, parameters, and tool descriptions in the Rails application. ```bash bin/rails mcp:rails:generate_server ``` -------------------------------- ### Customizing Tool Descriptions for MCP Actions (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Shows how to use the `tool_description_for` method in a Rails controller to provide custom, more informative descriptions for specific actions. These descriptions are used when generating the MCP server definition for LLM agents. ```ruby class ChannelsController < ApplicationController tool_description_for :create, "Create a new channel with the specified name and members" tool_description_for :index, "List all available channels" tool_description_for :show, "Get detailed information about a specific channel" # ... rest of controller code end ``` -------------------------------- ### Register Engine Configuration with MCP-Rails (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Use this configuration option within the initializer to register an engine's specific settings, such as additional environment variables, with MCP-Rails. ```Ruby MCP::Rails.configure do |config| config.register_engine(YourEngine, env_vars: ["YOUR_ENGINE_KEY"]) end ``` -------------------------------- ### Creating MCP-Specific View Template (Jbuilder Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Illustrates how to create a view template specifically for MCP responses using the `.mcp.jbuilder` extension. This allows for custom data formatting or selection tailored for the MCP protocol. ```ruby # app/views/channels/show.mcp.jbuilder json.name @channel.name json.user_ids @channel.user_ids ``` -------------------------------- ### Defining Parameters for MCP Actions in Rails Controller (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Demonstrates how to use the `permitted_params_for` DSL in a Rails controller to define parameters for specific MCP actions. These definitions are used for both MCP server configuration and automatic strong parameter enforcement via `resource_params`. ```ruby class ChannelsController < ApplicationController # Define parameters for the :create action permitted_params_for :create do param :channel, required: true do param :name, type: :string, example: "Channel Name", required: true param :user_ids, type: :array, item_type: :string, example: ["1", "2"] end end def create @channel = Channel.new(resource_params) # Automatically uses the defined params if @channel.save render json: @channel, status: :created else render json: @channel.errors, status: :unprocessable_entity end end end ``` -------------------------------- ### Add MCP-Rails and MCP-RB Gems to Gemfile (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Add these lines to your application's Gemfile to include the necessary gems for MCP integration. `mcp-rails` builds on and depends on `mcp-rb`. ```Ruby gem 'mcp-rails' ``` ```Ruby gem 'mcp-rb' ``` -------------------------------- ### Explicitly Rendering MCP Responses in Rails Controller (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Demonstrates how to use `respond_to` in a controller action to handle both standard JSON and MCP-specific requests. Rendering with `render mcp:` automatically applies MCP formatting, including key camelization. ```ruby class ChannelsController < ApplicationController def show @channel = Channel.find(params[:id]) respond_to do |format| format.json { render json: @channel } format.mcp { render mcp: @channel } # Keys will be automatically camelized end end end ``` -------------------------------- ### Tag Rails Routes for MCP Exposure (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Modify your `config/routes.rb` file to tag resources or specific actions using the `mcp:` option. This determines which routes will be included in the generated MCP server definition. ```Ruby Rails.application.routes.draw do resources :channels, mcp: true # Exposes all RESTful actions to MCP # OR resources :channels, mcp: [:index, :create] # Exposes only specified actions end ``` -------------------------------- ### Testing MCP Tool Call in Rails Integration Test (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Shows how to use `mcp-rails` test helpers (`mcp_server`, `mcp_tool_call`, `mcp_response_body`) within a Rails integration test to simulate an MCP tool call and assert the response. ```ruby class MCPChannelTest < ActionDispatch::IntegrationTest include MCP::Rails::TestHelper test "creates a channel via MCP" do server = mcp_server mcp_tool_call( server, "create_channel", channel: { name: "General", user_ids: ["1", "2"] } ) assert_equal false, mcp_response_body.dig(:result, :isError) assert_equal "Channel created successfully", mcp_response_body.dig(:result, :message) end end ``` -------------------------------- ### Fallback JSON View Template (Jbuilder Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Shows a standard `.json.jbuilder` view template. MCP-Rails will automatically fall back to this template if a corresponding `.mcp.jbuilder` view is not found for a given action. ```ruby # app/views/channels/show.json.jbuilder json.name @channel.name json.user_ids @channel.user_ids ``` -------------------------------- ### Bypassing CSRF for MCP Requests in Rails (Ruby) Source: https://github.com/tonksthebear/mcp-rails/blob/main/README.md Demonstrates how to conditionally skip CSRF verification in the `ApplicationController` for requests originating from the MCP server by checking for the `mcp_invocation?` helper. ```ruby # app/controllers/application_controller.rb class ApplicationController < ActionController::Base skip_before_action :verify_authenticity_token, if: :mcp_invocation? end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.