-
Notifications
You must be signed in to change notification settings - Fork 1
feat(mcp): Add MCP server routes & dedicated customization #232
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
Open
EnkiP
wants to merge
7
commits into
main
Choose a base branch
from
feat/mcp-server
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.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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
76 changes: 76 additions & 0 deletions
76
packages/forest_admin_agent/lib/forest_admin_agent/mcp/activity_log_creator.rb
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,76 @@ | ||
| require 'faraday' | ||
| require 'json' | ||
|
|
||
| module ForestAdminAgent | ||
| module Mcp | ||
| class ActivityLogCreator | ||
| ACTION_TO_TYPE = { | ||
| 'index' => 'read', | ||
| 'search' => 'read', | ||
| 'filter' => 'read', | ||
| 'listHasMany' => 'read', | ||
| 'actionForm' => 'read', | ||
| 'action' => 'write', | ||
| 'create' => 'write', | ||
| 'update' => 'write', | ||
| 'delete' => 'write', | ||
| 'availableActions' => 'read', | ||
| 'availableCollections' => 'read' | ||
| }.freeze | ||
|
|
||
| def self.create(forest_server_url, auth_info, action, extra = {}) | ||
| type = ACTION_TO_TYPE[action] | ||
| raise "Unknown action type: #{action}" unless type | ||
|
|
||
| forest_server_token = auth_info.dig(:extra, :forest_server_token) | ||
| rendering_id = auth_info.dig(:extra, :rendering_id) | ||
|
|
||
| client = Faraday.new(forest_server_url) do |conn| | ||
| conn.headers['Content-Type'] = 'application/json' | ||
| conn.headers['Forest-Application-Source'] = 'MCP' | ||
| conn.headers['Authorization'] = "Bearer #{forest_server_token}" | ||
| end | ||
|
|
||
| records = extra[:record_ids] || (extra[:record_id] ? [extra[:record_id]] : []) | ||
|
|
||
| payload = { | ||
| data: { | ||
| id: 1, | ||
| type: 'activity-logs-requests', | ||
| attributes: { | ||
| type: type, | ||
| action: action, | ||
| label: extra[:label], | ||
| records: records.map(&:to_s) | ||
| }.compact, | ||
| relationships: { | ||
| rendering: { | ||
| data: { | ||
| id: rendering_id.to_s, | ||
| type: 'renderings' | ||
| } | ||
| }, | ||
| collection: { | ||
| data: if extra[:collection_name] | ||
| { | ||
| id: extra[:collection_name], | ||
| type: 'collections' | ||
| } | ||
| end | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| response = client.post('/api/activity-logs-requests', payload.to_json) | ||
|
|
||
| return if response.success? | ||
|
|
||
| Facades::Container.logger.log( | ||
| 'Warn', | ||
| "[MCP] Failed to create activity log: #{response.body}" | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
82 changes: 82 additions & 0 deletions
82
packages/forest_admin_agent/lib/forest_admin_agent/mcp/agent_caller.rb
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,82 @@ | ||
| require 'faraday' | ||
| require 'json' | ||
|
|
||
| module ForestAdminAgent | ||
| module Mcp | ||
| class AgentCaller | ||
| def initialize(auth_info) | ||
| @token = auth_info[:token] | ||
| @forest_server_token = auth_info.dig(:extra, :forest_server_token) | ||
| @api_endpoint = auth_info.dig(:extra, :environment_api_endpoint) | ||
| end | ||
|
|
||
| def collection(name) | ||
| CollectionClient.new(name, @forest_server_token, @api_endpoint) | ||
| end | ||
|
|
||
| class CollectionClient | ||
| def initialize(name, token, api_endpoint) | ||
| @name = name | ||
| @token = token | ||
| @api_endpoint = api_endpoint | ||
| end | ||
|
|
||
| def list(params = {}) | ||
| payload = build_list_payload(params) | ||
|
|
||
| response = http_client.post("/forest/rpc/#{@name}/list", payload.to_json) | ||
|
|
||
| handle_response(response) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_list_payload(params) | ||
| payload = {} | ||
|
|
||
| payload[:filters] = params[:filters] if params[:filters] | ||
|
|
||
| payload[:search] = params[:search] if params[:search] | ||
|
|
||
| if params[:sort] | ||
| payload[:sort] = [ | ||
| { | ||
| field: params[:sort][:field], | ||
| ascending: params[:sort][:ascending] | ||
| } | ||
| ] | ||
| end | ||
|
|
||
| payload | ||
| end | ||
|
|
||
| def handle_response(response) | ||
| unless response.success? | ||
| error_body = parse_error_body(response) | ||
| raise ForestAdminAgent::Http::Exceptions::BadRequestError, error_body | ||
| end | ||
|
|
||
| JSON.parse(response.body) | ||
| end | ||
|
|
||
| def parse_error_body(response) | ||
| body = response.body | ||
|
|
||
| return body if body.is_a?(String) && !body.empty? | ||
|
|
||
| return body['error'] || body['message'] || body.to_json if body.is_a?(Hash) | ||
|
|
||
| "Request failed with status #{response.status}" | ||
| end | ||
|
|
||
| def http_client | ||
| @http_client ||= Faraday.new(@api_endpoint) do |conn| | ||
| conn.headers['Content-Type'] = 'application/json' | ||
| conn.headers['Authorization'] = "Bearer #{@token}" | ||
| conn.ssl.verify = !ForestAdminAgent::Facades::Container.cache(:debug) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
37 changes: 37 additions & 0 deletions
37
packages/forest_admin_agent/lib/forest_admin_agent/mcp/error_parser.rb
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,37 @@ | ||
| module ForestAdminAgent | ||
| module Mcp | ||
| class ErrorParser | ||
| def self.parse(error) | ||
| return error.message if error.is_a?(StandardError) | ||
|
|
||
| parse_from_string(error.to_s) | ||
| end | ||
|
|
||
| def self.parse_from_string(error_string) | ||
| return nil if error_string.nil? || error_string.empty? | ||
|
|
||
| # Try to parse as JSON | ||
| begin | ||
| parsed = JSON.parse(error_string) | ||
| extract_from_json(parsed) | ||
| rescue JSON::ParserError | ||
| # Not JSON, return as-is | ||
| error_string | ||
| end | ||
| end | ||
|
|
||
| def self.extract_from_json(parsed) | ||
| # Handle JSON:API error format | ||
| if parsed.is_a?(Hash) && parsed['errors'].is_a?(Array) | ||
| errors = parsed['errors'] | ||
| return errors.filter_map { |e| e['detail'] || e['title'] || e['name'] }.join(', ') | ||
| end | ||
|
|
||
| # Handle simple error object | ||
| return parsed['detail'] || parsed['message'] || parsed['error'] || parsed.to_s if parsed.is_a?(Hash) | ||
|
|
||
| parsed.to_s | ||
| end | ||
| end | ||
| end | ||
| end |
8 changes: 8 additions & 0 deletions
8
packages/forest_admin_agent/lib/forest_admin_agent/mcp/errors.rb
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,8 @@ | ||
| module ForestAdminAgent | ||
| module Mcp | ||
| class InvalidTokenError < StandardError; end | ||
| class InvalidClientError < StandardError; end | ||
| class InvalidRequestError < StandardError; end | ||
| class UnsupportedTokenTypeError < StandardError; end | ||
| end | ||
| end |
89 changes: 89 additions & 0 deletions
89
packages/forest_admin_agent/lib/forest_admin_agent/mcp/filter_schema.rb
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,89 @@ | ||
| module ForestAdminAgent | ||
| module Mcp | ||
| class FilterSchema | ||
| OPERATORS = %w[ | ||
| Equal | ||
| NotEqual | ||
| LessThan | ||
| GreaterThan | ||
| LessThanOrEqual | ||
| GreaterThanOrEqual | ||
| Match | ||
| NotContains | ||
| NotIContains | ||
| LongerThan | ||
| ShorterThan | ||
| IncludesAll | ||
| IncludesNone | ||
| Today | ||
| Yesterday | ||
| PreviousMonth | ||
| PreviousQuarter | ||
| PreviousWeek | ||
| PreviousYear | ||
| PreviousMonthToDate | ||
| PreviousQuarterToDate | ||
| PreviousWeekToDate | ||
| PreviousXDaysToDate | ||
| PreviousXDays | ||
| PreviousYearToDate | ||
| Present | ||
| Blank | ||
| Missing | ||
| In | ||
| NotIn | ||
| StartsWith | ||
| EndsWith | ||
| Contains | ||
| IStartsWith | ||
| IEndsWith | ||
| IContains | ||
| Like | ||
| ILike | ||
| Before | ||
| After | ||
| AfterXHoursAgo | ||
| BeforeXHoursAgo | ||
| Future | ||
| Past | ||
| ].freeze | ||
|
|
||
| AGGREGATORS = %w[And Or].freeze | ||
|
|
||
| def self.json_schema | ||
| { | ||
| oneOf: [ | ||
| leaf_schema, | ||
| branch_schema | ||
| ] | ||
| } | ||
| end | ||
|
|
||
| def self.leaf_schema | ||
| { | ||
| type: 'object', | ||
| properties: { | ||
| field: { type: 'string' }, | ||
| operator: { type: 'string', enum: OPERATORS }, | ||
| value: {} | ||
| }, | ||
| required: %w[field operator] | ||
| } | ||
| end | ||
|
|
||
| def self.branch_schema | ||
| { | ||
| type: 'object', | ||
| properties: { | ||
| aggregator: { type: 'string', enum: AGGREGATORS }, | ||
| conditions: { | ||
| type: 'array', | ||
| items: { '$ref': '#' } | ||
| } | ||
| }, | ||
| required: %w[aggregator conditions] | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.
Found 2 issues:
1. Function with many parameters (count = 4): create [qlty:function-parameters]
2. Function with high complexity (count = 5): create [qlty:function-complexity]