Class: Iron::Mcp::ToolContext

Inherits:
Object
  • Object
show all
Defined in:
app/mcp/iron/mcp/tool_context.rb

Instance Method Summary collapse

Instance Method Details

#create_entry(handle:, content:, route: nil, locale: nil) ⇒ Object



43
44
45
46
# File 'app/mcp/iron/mcp/tool_context.rb', line 43

def create_entry(handle:, content:, route: nil, locale: nil)
  authorize_write!
  Iron::Content.create(handle, content, route:, locale: effective_locale(locale), actor: Current.user, allow_local_files: false)
end

#delete_entry(handle:, id: nil, route: nil) ⇒ Object



53
54
55
56
# File 'app/mcp/iron/mcp/tool_context.rb', line 53

def delete_entry(handle:, id: nil, route: nil)
  authorize_write!
  Iron::Content.delete(handle, id:, route:, actor: Current.user)
end

#describe_schemaObject



17
18
19
20
21
# File 'app/mcp/iron/mcp/tool_context.rb', line 17

def describe_schema
  Rails.cache.fetch(Iron::Api::OpenapiSpec.cache_key, expires_in: 1.hour) do
    Iron::Api::OpenapiSpec.new.to_h
  end
end

#get_entry(handle:, id: nil, route: nil, locale: nil) ⇒ Object



30
31
32
# File 'app/mcp/iron/mcp/tool_context.rb', line 30

def get_entry(handle:, id: nil, route: nil, locale: nil)
  Iron::Content.get(handle, id:, route:, locale: effective_locale(locale))
end

#list_content_typesObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/mcp/iron/mcp/tool_context.rb', line 4

def list_content_types
  ContentType.order(:handle).map do |content_type|
    {
      "handle" => content_type.handle,
      "name" => content_type.name,
      "kind" => content_type.single? ? "single" : "collection",
      "web_published" => content_type.web_publishing_enabled?,
      "base_path" => content_type.base_path.presence,
      "title_field" => content_type.title_field_definition&.handle
    }.compact
  end
end

#list_entries(handle:, route: nil, after: nil, per_page: nil, locale: nil) ⇒ Object



23
24
25
26
27
28
# File 'app/mcp/iron/mcp/tool_context.rb', line 23

def list_entries(handle:, route: nil, after: nil, per_page: nil, locale: nil)
  return Iron::Content.get(handle, route:, locale: effective_locale(locale)) if route.present?

  content_type = ContentType.find_by!(handle:)
  in_locale(locale) { serialize_collection(Paginator.new(after:, per_page:).call(content_type.entries)) }
end

#search(query:, after: nil, per_page: nil, locale: nil) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
# File 'app/mcp/iron/mcp/tool_context.rb', line 34

def search(query:, after: nil, per_page: nil, locale: nil)
  raise InvalidRequest, "query is required" if query.blank?

  in_locale(locale) do
    scope = Entry.search(query, locale: Current.locale)
    serialize_collection(Paginator.new(after:, per_page:).call(scope))
  end
end

#update_entry(handle:, content:, id: nil, route: nil, locale: nil) ⇒ Object



48
49
50
51
# File 'app/mcp/iron/mcp/tool_context.rb', line 48

def update_entry(handle:, content:, id: nil, route: nil, locale: nil)
  authorize_write!
  Iron::Content.update(handle, content, id:, route:, locale: effective_locale(locale), actor: Current.user, allow_local_files: false)
end

#upload_asset(url: nil, data: nil, filename: nil, content_type: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'app/mcp/iron/mcp/tool_context.rb', line 58

def upload_asset(url: nil, data: nil, filename: nil, content_type: nil)
  authorize_write!
  blob = build_blob(url:, data:, filename:, content_type:)

  {
    "signed_id" => blob.signed_id,
    "filename" => blob.filename.to_s,
    "content_type" => blob.content_type,
    "byte_size" => blob.byte_size
  }
end