Files
docuseal/app/controllers/mcp_controller.rb
T

45 lines
1.3 KiB
Ruby
Raw Normal View History

2026-03-05 12:17:14 +02:00
# frozen_string_literal: true
2026-07-22 15:53:59 +03:00
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
2026-03-05 12:17:14 +02:00
2026-07-22 15:53:59 +03:00
TOOLS = TOOL_CONTROLLERS.map { |_, controller| controller::SCHEMA }.freeze
2026-03-05 12:17:14 +02:00
def call
2026-07-22 15:53:59 +03:00
return Mcp::ProtocolController.dispatch(:ok, request, response) if request.raw_post.blank?
2026-03-05 12:17:14 +02:00
body = JSON.parse(request.raw_post)
2026-07-22 15:53:59 +03:00
body = nil unless body.is_a?(Hash)
2026-03-05 12:17:14 +02:00
2026-07-22 15:53:59 +03:00
request.request_parameters = body || {}
2026-03-05 12:17:14 +02:00
2026-07-22 15:53:59 +03:00
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')]
2026-03-05 12:17:14 +02:00
2026-07-22 15:53:59 +03:00
return tool.dispatch(:call, request, response) if tool
2026-03-05 12:17:14 +02:00
2026-07-22 15:53:59 +03:00
:tool_not_found
else
:method_not_found
end
2026-03-05 12:17:14 +02:00
2026-07-22 15:53:59 +03:00
Mcp::ProtocolController.dispatch(action, request, response)
rescue JSON::ParserError
request.request_parameters = {}
2026-03-05 12:17:14 +02:00
2026-07-22 15:53:59 +03:00
Mcp::ProtocolController.dispatch(:parse_error, request, response)
2026-03-05 12:17:14 +02:00
end
end