-
Notifications
You must be signed in to change notification settings - Fork 97
Add Model Context Protocol support with fast-mcp #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
f4ebf2e
d6a577d
6ac9292
ccf8eff
d15311f
6dd373d
6d674e3
eaf2420
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ApplicationResource < ActionResource::Base | ||
| # write your custom logic to be shared across all resources here | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ApplicationTool < ActionTool::Base | ||
| # write your custom logic to be shared across all tools here | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # FastMcp - Model Context Protocol for Rails | ||
| # This initializer sets up the MCP middleware in your Rails application. | ||
| # | ||
| # In Rails applications, you can use: | ||
| # - ActionTool::Base as an alias for FastMcp::Tool | ||
| # - ActionResource::Base as an alias for FastMcp::Resource | ||
| # | ||
| # All your tools should inherit from ApplicationTool which already uses ActionTool::Base, | ||
| # and all your resources should inherit from ApplicationResource which uses ActionResource::Base. | ||
|
|
||
| # Mount the MCP middleware in your Rails application | ||
| # You can customize the options below to fit your needs. | ||
| require 'fast_mcp' | ||
|
|
||
| FastMcp.mount_in_rails( | ||
| Rails.application, | ||
| name: Rails.application.class.module_parent_name.underscore.dasherize, | ||
| version: '1.0.0', | ||
| path_prefix: '/mcp', # This is the default path prefix | ||
| messages_route: 'messages', # This is the default route for the messages endpoint | ||
| sse_route: 'sse' # This is the default route for the SSE endpoint | ||
| # Add allowed origins below, it defaults to Rails.application.config.hosts | ||
| # allowed_origins: ['localhost', '127.0.0.1', '[::1]', 'example.com', /.*\.example\.com/], | ||
| # localhost_only: true, # Set to false to allow connections from other hosts | ||
| # whitelist specific ips to if you want to run on localhost and allow connections from other IPs | ||
| # allowed_ips: ['127.0.0.1', '::1'] | ||
| # authenticate: true, # Uncomment to enable authentication | ||
| # auth_token: 'your-token', # Required if authenticate: true | ||
| ) do |server| | ||
| Rails.application.config.after_initialize do | ||
| # FastMcp will automatically discover and register: | ||
| # - All classes that inherit from ApplicationTool (which uses ActionTool::Base) | ||
| # - All classes that inherit from ApplicationResource (which uses ActionResource::Base) | ||
| server.register_tools(*ApplicationTool.descendants) | ||
| server.register_resources(*ApplicationResource.descendants) | ||
|
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the development environment, Rails lazy-loads classes, so # Eager-load tools and resources to ensure they are discovered, especially in development.
Rails.autoloaders.main.eager_load_dir(Rails.root.join("app/tools"))
Rails.autoloaders.main.eager_load_dir(Rails.root.join("app/resources"))
server.register_tools(*ApplicationTool.descendants)
server.register_resources(*ApplicationResource.descendants) |
||
| # alternatively, you can register tools and resources manually: | ||
| # server.register_tool(MyTool) | ||
| # server.register_resource(MyResource) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Model Context Protocol (MCP) | ||
|
|
||
| This application ships with [fast-mcp](https://github.com/yjacquin/fast-mcp), a Ruby implementation of the Model Context Protocol. The middleware is mounted at `/mcp` and exposes two endpoints: | ||
|
|
||
| - `POST /mcp/messages` – JSON‑RPC endpoint for MCP requests | ||
| - `GET /mcp/sse` – Server‑sent events stream | ||
|
|
||
| ## Defining tools and resources | ||
|
|
||
| Add tools under `app/tools/` and resources under `app/resources/`. Tools inherit from `ApplicationTool` and resources from `ApplicationResource`. | ||
|
|
||
| Any subclasses of these base classes are automatically registered with the MCP server on boot. | ||
|
|
||
| ## Example request | ||
|
|
||
| Once the Rails server is running, an MCP client can send a JSON‑RPC message to invoke a tool: | ||
|
|
||
| ```bash | ||
| curl -X POST http://localhost:3000/mcp/messages \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"jsonrpc":"2.0","id":"1","method":"tools/execute","params":{"name":"your_tool","arguments":{}}}' | ||
| ``` | ||
|
|
||
| Replace `your_tool` with the name of a tool you defined. | ||
|
|
||
| Use the SSE endpoint to subscribe to resource updates or other MCP events: | ||
|
|
||
| ```bash | ||
| curl http://localhost:3000/mcp/sse | ||
| ``` | ||
|
|
||
| Consult the [fast-mcp documentation](https://github.com/yjacquin/fast-mcp) for full protocol details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve maintainability and avoid using a 'magic string', it's a good practice to define the version as a constant. This makes it easier to manage and update the version number, especially if it's referenced elsewhere in the application.