Class: McpToolkit::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp_toolkit/registry.rb

Overview

Central registry of read-only resources exposed via the MCP server. Resources are registered at boot (in a to_prepare initializer) and consumed by the generic resources / resource_schema / get / list tools.

Instances are addressable so tests (and, in principle, multiple mounted servers) don't collide; the app-facing convenience is McpToolkit.registry, which returns the process-wide instance.

Defined Under Namespace

Classes: UnknownResource

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



13
14
15
16
# File 'lib/mcp_toolkit/registry.rb', line 13

def initialize
  @resources = {}
  @default_required_permissions_scope = nil
end

Instance Method Details

#default_required_permissions_scope(scope = nil) ⇒ Object

Registry-wide DEFAULT required scope, so a satellite declares its scope ONCE for every resource instead of repeating it per resource:

McpToolkit.registry.default_required_permissions_scope "notifications__read"

A resource's own required_permissions_scope overrides this. Default nil = no scope required unless a resource declares its own. Read with no arg.

Declared in the satellite's configure block (NOT inside to_prepare), so it survives reset! and stays set across dev reloads.



28
29
30
31
# File 'lib/mcp_toolkit/registry.rb', line 28

def default_required_permissions_scope(scope = nil)
  @default_required_permissions_scope = scope if scope
  @default_required_permissions_scope
end

#fetch(name) ⇒ Object



45
46
47
# File 'lib/mcp_toolkit/registry.rb', line 45

def fetch(name)
  find(name) or raise(UnknownResource, McpToolkit::UnknownResourceMessage.new(name, resource_names).build)
end

#find(name) ⇒ Object



49
50
51
# File 'lib/mcp_toolkit/registry.rb', line 49

def find(name)
  @resources[name.to_s]
end

#register(name) ⇒ Object



33
34
35
36
37
# File 'lib/mcp_toolkit/registry.rb', line 33

def register(name, &)
  resource = McpToolkit::Resource.new(name)
  resource.instance_eval(&)
  @resources[name.to_s] = resource
end

#required_scope_for(resource) ⇒ Object

The scope a token must carry to reach resource via the generic tools: the resource's own declared scope, else the registry default, else nil (no check).



41
42
43
# File 'lib/mcp_toolkit/registry.rb', line 41

def required_scope_for(resource)
  resource.effective_required_permissions_scope(@default_required_permissions_scope)
end

#reset!Object

Clears registered resources for a dev reload (the satellite re-declares them in to_prepare). The default_required_permissions_scope is PRESERVED, since it's declared once in configure rather than per-reload.



64
65
66
# File 'lib/mcp_toolkit/registry.rb', line 64

def reset!
  @resources = {}
end

#resource_namesObject



57
58
59
# File 'lib/mcp_toolkit/registry.rb', line 57

def resource_names
  @resources.keys
end

#resourcesObject



53
54
55
# File 'lib/mcp_toolkit/registry.rb', line 53

def resources
  @resources.values
end