refactor mcp
This commit is contained in:
@@ -1,58 +1,44 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class McpController < ActionController::API
|
||||
before_action :authenticate_user!
|
||||
before_action :verify_mcp_enabled!
|
||||
class McpController < ActionController::Metal
|
||||
TOOL_CONTROLLERS = {
|
||||
'search_templates' => Mcp::SearchTemplatesController,
|
||||
'load_template' => Mcp::LoadTemplateController,
|
||||
'create_template' => Mcp::CreateTemplateController,
|
||||
'send_documents' => Mcp::SendDocumentsController,
|
||||
'search_documents' => Mcp::SearchDocumentsController
|
||||
}.freeze
|
||||
|
||||
before_action do
|
||||
authorize!(:manage, :mcp)
|
||||
end
|
||||
TOOLS = TOOL_CONTROLLERS.map { |_, controller| controller::SCHEMA }.freeze
|
||||
|
||||
def call
|
||||
return head :ok if request.raw_post.blank?
|
||||
return Mcp::ProtocolController.dispatch(:ok, request, response) if request.raw_post.blank?
|
||||
|
||||
body = JSON.parse(request.raw_post)
|
||||
body = nil unless body.is_a?(Hash)
|
||||
|
||||
result = Mcp::HandleRequest.call(body, current_user, current_ability)
|
||||
request.request_parameters = body || {}
|
||||
|
||||
if result
|
||||
render json: result
|
||||
else
|
||||
head :accepted
|
||||
end
|
||||
rescue CanCan::AccessDenied
|
||||
render json: { jsonrpc: '2.0', id: nil, error: { code: -32_603, message: 'Forbidden' } }, status: :forbidden
|
||||
action =
|
||||
case body&.dig('method')
|
||||
when 'initialize' then :initialize_request
|
||||
when 'notifications/initialized' then :initialized_notification
|
||||
when 'ping' then :ping
|
||||
when 'tools/list' then :tools_list
|
||||
when 'tools/call'
|
||||
tool = TOOL_CONTROLLERS[body.dig('params', 'name')]
|
||||
|
||||
return tool.dispatch(:call, request, response) if tool
|
||||
|
||||
:tool_not_found
|
||||
else
|
||||
:method_not_found
|
||||
end
|
||||
|
||||
Mcp::ProtocolController.dispatch(action, request, response)
|
||||
rescue JSON::ParserError
|
||||
render json: { jsonrpc: '2.0', id: nil, error: { code: -32_700, message: 'Parse error' } }, status: :bad_request
|
||||
end
|
||||
request.request_parameters = {}
|
||||
|
||||
private
|
||||
|
||||
def authenticate_user!
|
||||
render json: { error: 'Not authenticated' }, status: :unauthorized unless current_user
|
||||
end
|
||||
|
||||
def verify_mcp_enabled!
|
||||
return if Docuseal.multitenant?
|
||||
|
||||
return if AccountConfig.exists?(account_id: current_user.account_id,
|
||||
key: AccountConfig::ENABLE_MCP_KEY,
|
||||
value: true)
|
||||
|
||||
render json: { error: 'MCP is disabled' }, status: :forbidden
|
||||
end
|
||||
|
||||
def current_user
|
||||
@current_user ||= user_from_api_key
|
||||
end
|
||||
|
||||
def user_from_api_key
|
||||
token = request.headers['Authorization'].to_s[/\ABearer\s+(.+)\z/, 1]
|
||||
|
||||
return if token.blank?
|
||||
|
||||
sha256 = Digest::SHA256.hexdigest(token)
|
||||
|
||||
User.joins(:mcp_tokens).active.find_by(mcp_tokens: { sha256:, archived_at: nil })
|
||||
Mcp::ProtocolController.dispatch(:parse_error, request, response)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user