forked from maybe-finance/maybe
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Draft
jjmata
wants to merge
8
commits into
main
Choose a base branch
from
codex/add-model-control-protocol-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f4ebf2e
Add Model Context Protocol support
jjmata d6a577d
Lint noise
jjmata 6ac9292
Expects and array
jjmata ccf8eff
Move MCP.md to api/ folder
jjmata d15311f
Add CC implementation plan to branch
jjmata 6dd373d
Merge branch 'main' into codex/add-model-control-protocol-support
jjmata 6d674e3
Add a couple of simple tests to prove the concept
jjmata eaf2420
Linter noise
jjmata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class FetchAccountsBalanceTool < ApplicationTool | ||
def call(account_type: nil) | ||
family = Current.family | ||
return { error: "No family found" } unless family | ||
|
||
accounts = family.accounts | ||
accounts = accounts.where(accountable_type: account_type) if account_type.present? | ||
|
||
account_data = accounts.map do |account| | ||
{ | ||
id: account.id, | ||
name: account.name, | ||
type: account.accountable_type, | ||
balance: account.balance.to_f | ||
} | ||
end | ||
|
||
total_balance = accounts.sum(&:balance).to_f | ||
|
||
{ | ||
accounts: account_data, | ||
total_balance: total_balance | ||
} | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 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, | ||
allowed_origins: Rails.application.config.hosts.to_a, | ||
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) | ||
# alternatively, you can register tools and resources manually: | ||
# server.register_tool(MyTool) | ||
# server.register_resource(MyResource) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In the development environment, Rails lazy-loads classes, so
ApplicationTool.descendants
andApplicationResource.descendants
will likely be empty when this initializer runs. This will prevent your tools and resources from being registered. You should explicitly eager-load these directories to ensure all tool and resource classes are loaded before registration.