Class: KairosMcp::ResourceRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/kairos_mcp/resource_registry.rb

Overview

ResourceRegistry: Unified resource access layer for all layers (L0/L1/L2)

Provides URI-based access to all KairosChain resources:

  • l0://kairos.md, l0://kairos.rb (L0 skills)

  • knowledge://name, knowledge://name/scripts/file (L1)

  • context://session/name, context://session/name/scripts/file (L2)

Constant Summary collapse

MIME_TYPES =

MIME type mappings

{
  '.md' => 'text/markdown',
  '.rb' => 'text/x-ruby',
  '.py' => 'text/x-python',
  '.sh' => 'application/x-sh',
  '.bash' => 'application/x-sh',
  '.js' => 'text/javascript',
  '.ts' => 'text/typescript',
  '.json' => 'application/json',
  '.yaml' => 'application/yaml',
  '.yml' => 'application/yaml',
  '.html' => 'text/html',
  '.css' => 'text/css',
  '.txt' => 'text/plain',
  '.png' => 'image/png',
  '.jpg' => 'image/jpeg',
  '.jpeg' => 'image/jpeg',
  '.gif' => 'image/gif',
  '.svg' => 'image/svg+xml',
  '.pdf' => 'application/pdf'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(user_context: nil) ⇒ ResourceRegistry

Returns a new instance of ResourceRegistry.



42
43
44
45
46
47
48
# File 'lib/kairos_mcp/resource_registry.rb', line 42

def initialize(user_context: nil)
  @skills_dir = KairosMcp.skills_dir
  @knowledge_dir = KairosMcp.knowledge_dir(user_context: user_context)
  @context_dir = KairosMcp.context_dir(user_context: user_context)
  @knowledge_provider = KnowledgeProvider.new(@knowledge_dir, vector_search_enabled: false, user_context: user_context)
  @context_manager = ContextManager.new(@context_dir, user_context: user_context)
end

Instance Method Details

#list(filter: nil, type: 'all', layer: 'all') ⇒ Array<Hash>

List all resources with optional filtering

Parameters:

  • filter (String, nil) (defaults to: nil)

    Filter by layer or name (e.g., “l0”, “knowledge”, “context”, “example_knowledge”)

  • type (String) (defaults to: 'all')

    Resource type filter: “all”, “md”, “scripts”, “assets”, “references”

  • layer (String) (defaults to: 'all')

    Layer filter: “all”, “l0”, “l1”, “l2”

Returns:

  • (Array<Hash>)

    List of resource info



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kairos_mcp/resource_registry.rb', line 56

def list(filter: nil, type: 'all', layer: 'all')
  resources = []

  # L0 resources
  if include_layer?(layer, 'l0') && include_filter?(filter, 'l0')
    resources.concat(list_l0_resources(type))
  end

  # L1 (knowledge) resources
  if include_layer?(layer, 'l1') && (filter.nil? || filter == 'knowledge' || knowledge_name?(filter))
    resources.concat(list_l1_resources(filter, type))
  end

  # L2 (context) resources
  if include_layer?(layer, 'l2') && (filter.nil? || filter == 'context' || context_filter?(filter))
    resources.concat(list_l2_resources(filter, type))
  end

  resources
end

#parse_uri(uri) ⇒ Hash?

Parse a URI into components

Parameters:

  • uri (String)

    Resource URI

Returns:

  • (Hash, nil)

    Parsed components or nil if invalid



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/kairos_mcp/resource_registry.rb', line 101

def parse_uri(uri)
  # Match: scheme://path
  match = uri.match(%r{\A(\w+)://(.+)\z})
  return nil unless match

  scheme = match[1]
  path = match[2]

  case scheme
  when 'l0'
    { scheme: scheme, file: path }
  when 'knowledge'
    parse_knowledge_uri(path)
  when 'context'
    parse_context_uri(path)
  else
    nil
  end
end

#read(uri) ⇒ Hash?

Read a resource by URI

Parameters:

  • uri (String)

    Resource URI (e.g., “knowledge://example/scripts/test.sh”)

Returns:

  • (Hash, nil)

    Resource content and metadata, or nil if not found



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kairos_mcp/resource_registry.rb', line 81

def read(uri)
  parsed = parse_uri(uri)
  return nil unless parsed

  case parsed[:scheme]
  when 'l0'
    read_l0_resource(parsed)
  when 'knowledge'
    read_l1_resource(parsed)
  when 'context'
    read_l2_resource(parsed)
  else
    nil
  end
end