Class: Toolchest::Router
- Inherits:
-
Object
- Object
- Toolchest::Router
- Defined in:
- lib/toolchest/router.rb
Instance Attribute Summary collapse
-
#mcp_server ⇒ Object
Returns the value of attribute mcp_server.
-
#rack_app ⇒ Object
Returns the value of attribute rack_app.
Instance Method Summary collapse
- #completion_values(argument_name) ⇒ Object
- #dispatch(tool_name, arguments = {}) ⇒ Object
- #dispatch_response(params) ⇒ Object
-
#initialize(mount_key: :default) ⇒ Router
constructor
A new instance of Router.
- #notify_log(level:, message:) ⇒ Object
- #prompts_for_handler ⇒ Object
- #prompts_get(name, arguments = {}) ⇒ Object
- #prompts_get_response(params) ⇒ Object
- #prompts_list ⇒ Object
- #register(toolbox_class) ⇒ Object
- #resource_templates_for_handler ⇒ Object
- #resources_for_handler ⇒ Object
- #resources_list ⇒ Object
- #resources_read(uri) ⇒ Object
- #resources_read_response(params) ⇒ Object
- #toolbox_classes ⇒ Object
-
#tools_for_handler ⇒ Object
For the MCP SDK handler — returns array (SDK wraps it).
- #tools_list ⇒ Object
Constructor Details
#initialize(mount_key: :default) ⇒ Router
Returns a new instance of Router.
5 6 7 8 9 10 11 |
# File 'lib/toolchest/router.rb', line 5 def initialize(mount_key: :default) @mount_key = mount_key.to_sym @tool_map = {} @toolbox_classes = [] @mcp_server = nil @rack_app = nil end |
Instance Attribute Details
#mcp_server ⇒ Object
Returns the value of attribute mcp_server.
3 4 5 |
# File 'lib/toolchest/router.rb', line 3 def mcp_server @mcp_server end |
#rack_app ⇒ Object
Returns the value of attribute rack_app.
3 4 5 |
# File 'lib/toolchest/router.rb', line 3 def rack_app @rack_app end |
Instance Method Details
#completion_values(argument_name) ⇒ Object
164 165 166 167 168 169 |
# File 'lib/toolchest/router.rb', line 164 def completion_values(argument_name) tool_definitions.flat_map(&:params) .select { |p| p.name.to_s == argument_name.to_s && p.enum } .flat_map(&:enum) .uniq end |
#dispatch(tool_name, arguments = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/toolchest/router.rb', line 48 def dispatch(tool_name, arguments = {}) definition = find_tool(tool_name) unless definition return { content: [{ type: "text", text: "Unknown tool: #{tool_name}" }], isError: true } end config = Toolchest.configuration(@mount_key) if config.filter_tools_by_scope auth = Toolchest::Current.auth scopes = auth ? extract_scopes(auth) : nil if config.auth != :none && (!scopes || !tool_allowed_by_scopes?(definition, scopes)) return { content: [{ type: "text", text: "Forbidden: insufficient scope" }], isError: true } end end start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) auth = Toolchest::Current.auth token_hint = extract_token_hint(auth) log_request_start(definition, arguments, token_hint) toolbox = definition.toolbox_class.new( params: arguments, tool_definition: definition ) response = toolbox.dispatch(definition.method_name) duration = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(1) log_request_complete(definition, response, duration) response end |
#dispatch_response(params) ⇒ Object
81 82 83 84 85 |
# File 'lib/toolchest/router.rb', line 81 def dispatch_response(params) name = params[:name] || params["name"] arguments = params[:arguments] || params["arguments"] || {} dispatch(name, arguments) end |
#notify_log(level:, message:) ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'lib/toolchest/router.rb', line 171 def notify_log(level:, message:) return unless @mcp_server @mcp_server.( data: , level: level, logger: "Toolchest" ) end |
#prompts_for_handler ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/toolchest/router.rb', line 134 def prompts_for_handler prompts_list.map { |p| prompt = { name: p[:name], description: p[:description] }.compact if p[:arguments].any? prompt[:arguments] = p[:arguments].map { |name, opts| arg = { name: name.to_s } arg[:description] = opts[:description] if opts[:description] arg[:required] = opts[:required] if opts.key?(:required) arg } end prompt } end |
#prompts_get(name, arguments = {}) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/toolchest/router.rb', line 149 def prompts_get(name, arguments = {}) prompt = prompts_list.find { |p| p[:name] == name } return { messages: [] } unless prompt kwargs = arguments.transform_keys(&:to_sym) = prompt[:block].call(**kwargs) { messages: } end |
#prompts_get_response(params) ⇒ Object
158 159 160 161 162 |
# File 'lib/toolchest/router.rb', line 158 def prompts_get_response(params) name = params[:name] || params["name"] arguments = params[:arguments] || params["arguments"] || {} prompts_get(name, arguments) end |
#prompts_list ⇒ Object
132 |
# File 'lib/toolchest/router.rb', line 132 def prompts_list = @toolbox_classes.flat_map(&:prompts) |
#register(toolbox_class) ⇒ Object
13 14 15 16 |
# File 'lib/toolchest/router.rb', line 13 def register(toolbox_class) @toolbox_classes << toolbox_class unless @toolbox_classes.include?(toolbox_class) rebuild_tool_map! end |
#resource_templates_for_handler ⇒ Object
95 96 97 98 99 |
# File 'lib/toolchest/router.rb', line 95 def resource_templates_for_handler @toolbox_classes.flat_map(&:resources).select { |r| r[:template] }.map { |r| { uriTemplate: r[:uri], name: r[:name], description: r[:description] }.compact } end |
#resources_for_handler ⇒ Object
89 90 91 92 93 |
# File 'lib/toolchest/router.rb', line 89 def resources_for_handler resources_list.map { |r| { uri: r[:uri], name: r[:name], description: r[:description] }.compact } end |
#resources_list ⇒ Object
87 |
# File 'lib/toolchest/router.rb', line 87 def resources_list = @toolbox_classes.flat_map(&:resources).reject { |r| r[:template] } |
#resources_read(uri) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/toolchest/router.rb', line 101 def resources_read(uri) resource = @toolbox_classes.flat_map(&:resources).find { |r| if r[:template] pattern = r[:uri].gsub(/\{[^}]+\}/, "([^/]+)") uri.match?(Regexp.new("^#{pattern}$")) else r[:uri] == uri end } unless resource return [{ uri: uri, mimeType: "text/plain", text: "Resource not found: #{uri}" }] end result = if resource[:template] pattern = resource[:uri].gsub(/\{([^}]+)\}/, '(?<\1>[^/]+)') match = uri.match(Regexp.new("^#{pattern}$")) kwargs = match.named_captures.transform_keys(&:to_sym) resource[:block].call(**kwargs) else resource[:block].call end [{ uri: uri, mimeType: "application/json", text: result.to_json }] end |
#resources_read_response(params) ⇒ Object
127 128 129 130 |
# File 'lib/toolchest/router.rb', line 127 def resources_read_response(params) uri = params[:uri] || params["uri"] resources_read(uri) end |
#toolbox_classes ⇒ Object
18 |
# File 'lib/toolchest/router.rb', line 18 def toolbox_classes = @toolbox_classes |
#tools_for_handler ⇒ Object
For the MCP SDK handler — returns array (SDK wraps it)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/toolchest/router.rb', line 23 def tools_for_handler config = Toolchest.configuration(@mount_key) unless config.filter_tools_by_scope return tools_list end auth = Toolchest::Current.auth # No auth: show all tools for :none, show nothing otherwise unless auth return config.auth == :none ? tools_list : [] end scopes = extract_scopes(auth) # Auth present but no scopes extractable: fail closed unless scopes return [] end tool_definitions.select { |td| tool_allowed_by_scopes?(td, scopes) } .map { |td| td.to_mcp_schema } end |
#tools_list ⇒ Object
20 |
# File 'lib/toolchest/router.rb', line 20 def tools_list = tool_definitions.map { |td| td.to_mcp_schema } |