> ## Documentation Index
> Fetch the complete documentation index at: https://mcp-b-sync-npm-packages-docs-bf03420.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Rails Integration

> Integrate WebMCP tools with Ruby on Rails using Stimulus controllers.

<Card title="Full Example" icon="github" href="https://github.com/WebMCP-org/examples/tree/main/rails">
  Production-ready Rails example with Stimulus controllers
</Card>

## Quick start

```bash theme={null}
git clone https://github.com/WebMCP-org/examples.git
cd examples/rails && bundle install && bin/rails server
```

## Installation

**With importmap (Rails 7+):**

```bash theme={null}
bin/importmap pin @mcp-b/global
```

**With npm/yarn:**

```bash theme={null}
npm install @mcp-b/global
```

## The pattern

Use Stimulus `connect`/`disconnect` for lifecycle management:

```javascript "app/javascript/controllers/webmcp_controller.js" theme={null}
import { Controller } from "@hotwired/stimulus"
import "@mcp-b/global"

export default class extends Controller {
  static values = { name: String, description: String }
  #registration = null

  connect() {
    if (!('modelContext' in navigator)) return

    this.#registration = navigator.modelContext.registerTool({
      name: this.nameValue,
      description: this.descriptionValue,
      inputSchema: { type: 'object', properties: {} },
      execute: async () => {
        return { content: [{ type: 'text', text: 'Done' }] }
      }
    })
  }

  disconnect() {
    this.#registration?.unregister()
  }
}
```

```erb theme={null}
<div data-controller="webmcp"
     data-webmcp-name-value="my_tool"
     data-webmcp-description-value="My tool description">
  Content
</div>
```

## Turbo persistence

Attach controllers to `<body>` for persistence across Turbo navigations:

```erb theme={null}
<%# app/views/layouts/application.html.erb %>
<body data-controller="global-tools">
  <%= yield %>
</body>
```

## Common issues

<AccordionGroup>
  <Accordion title="CSRF token issues">
    Include the CSRF token in fetch requests:

    ```javascript theme={null}
    headers: {
      'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
    }
    ```
  </Accordion>

  <Accordion title="Tools not registering after Turbo navigation">
    Place controllers on `<body>` so they persist, or handle `turbo:load` event.
  </Accordion>

  <Accordion title="importmap not finding @mcp-b/global">
    Pin the package: `bin/importmap pin @mcp-b/global`
  </Accordion>
</AccordionGroup>

## Development

Use [Chrome DevTools MCP](/packages/chrome-devtools-mcp) for AI-driven development - your AI can write, discover, and test tools in real-time.
