|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mcp |
| 4 | + class CreateTemplateController < McpBaseController |
| 5 | + SCHEMA = { |
| 6 | + name: 'create_template', |
| 7 | + title: 'Create Template', |
| 8 | + description: 'Create a document template. Provide a URL to upload a PDF/DOCX file, or provide only a name ' \ |
| 9 | + 'to create an empty template and receive an edit URL where the file can be uploaded via the UI.', |
| 10 | + inputSchema: { |
| 11 | + type: 'object', |
| 12 | + properties: { |
| 13 | + name: { |
| 14 | + type: 'string', |
| 15 | + description: 'Template name (used as the template name and required when url is not provided)' |
| 16 | + }, |
| 17 | + url: { |
| 18 | + type: 'string', |
| 19 | + description: 'Optional URL of a PDF or DOCX file to upload. If omitted, an empty template is ' \ |
| 20 | + 'created and the returned edit_url can be used to upload a file via the UI.' |
| 21 | + } |
| 22 | + }, |
| 23 | + required: %w[name] |
| 24 | + }, |
| 25 | + annotations: { |
| 26 | + readOnlyHint: false, |
| 27 | + destructiveHint: false, |
| 28 | + idempotentHint: false, |
| 29 | + openWorldHint: true |
| 30 | + } |
| 31 | + }.freeze |
| 32 | + |
| 33 | + # rubocop:disable Metrics/AbcSize, Metrics/MethodLength |
| 34 | + def call |
| 35 | + account = current_user.account |
| 36 | + |
| 37 | + @template = Template.new( |
| 38 | + account:, |
| 39 | + author: current_user, |
| 40 | + folder: account.default_template_folder, |
| 41 | + source: :mcp, |
| 42 | + name: mcp_params['name'].to_s.presence || 'New Template', |
| 43 | + fields: [], |
| 44 | + schema: [] |
| 45 | + ) |
| 46 | + |
| 47 | + authorize!(:create, @template) |
| 48 | + |
| 49 | + if mcp_params['url'].present? |
| 50 | + tempfile = Tempfile.new |
| 51 | + tempfile.binmode |
| 52 | + tempfile.write(DownloadUtils.call(mcp_params['url'], validate: true).body) |
| 53 | + tempfile.rewind |
| 54 | + |
| 55 | + filename = File.basename(URI.decode_www_form_component(mcp_params['url'])) |
| 56 | + |
| 57 | + file = ActionDispatch::Http::UploadedFile.new( |
| 58 | + tempfile:, |
| 59 | + filename:, |
| 60 | + type: Marcel::MimeType.for(tempfile) |
| 61 | + ) |
| 62 | + |
| 63 | + @template.name = mcp_params['name'].presence || File.basename(filename, '.*') |
| 64 | + @template.save! |
| 65 | + |
| 66 | + documents, = Templates::CreateAttachments.call(@template, { files: [file] }, extract_fields: true) |
| 67 | + schema = documents.map { |doc| { attachment_uuid: doc.uuid, name: doc.filename.base } } |
| 68 | + |
| 69 | + if @template.fields.blank? |
| 70 | + @template.fields = Templates::ProcessDocument.normalize_attachment_fields(@template, documents) |
| 71 | + end |
| 72 | + |
| 73 | + @template.update!(schema:) |
| 74 | + else |
| 75 | + @template.save! |
| 76 | + end |
| 77 | + |
| 78 | + WebhookUrls.enqueue_events(@template, 'template.created') |
| 79 | + |
| 80 | + SearchEntries.enqueue_reindex(@template) |
| 81 | + |
| 82 | + render_tool_result( |
| 83 | + id: @template.id, |
| 84 | + name: @template.name, |
| 85 | + edit_url: edit_template_url(@template) |
| 86 | + ) |
| 87 | + end |
| 88 | + # rubocop:enable Metrics/AbcSize, Metrics/MethodLength |
| 89 | + end |
| 90 | +end |
0 commit comments