Class: Toolchest::RackApp
- Inherits:
-
Object
- Object
- Toolchest::RackApp
- Defined in:
- lib/toolchest/rack_app.rb
Instance Attribute Summary collapse
-
#mount_key ⇒ Object
readonly
Returns the value of attribute mount_key.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(mount_key: :default) ⇒ RackApp
constructor
A new instance of RackApp.
Constructor Details
#initialize(mount_key: :default) ⇒ RackApp
Returns a new instance of RackApp.
5 6 7 8 9 10 11 |
# File 'lib/toolchest/rack_app.rb', line 5 def initialize(mount_key: :default) @mount_key = mount_key.to_sym @server = build_mcp_server @transport = MCP::Server::Transports::StreamableHTTPTransport.new(@server) @server.transport = @transport install_handlers! end |
Instance Attribute Details
#mount_key ⇒ Object (readonly)
Returns the value of attribute mount_key.
3 4 5 |
# File 'lib/toolchest/rack_app.rb', line 3 def mount_key @mount_key end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/toolchest/rack_app.rb', line 13 def call(env) request = Rack::Request.new(env) env["toolchest.mount_key"] ||= @mount_key.to_s auth = authenticate(request) if auth.nil? && config.auth != :none mount_path = config.mount_path || "/mcp" = "#{request.base_url}/.well-known/oauth-protected-resource#{mount_path}" return [401, { "WWW-Authenticate" => %(Bearer resource_metadata="#{}"), "Content-Type" => "application/json" }, ['{"error":"unauthorized"}']] end Toolchest::Current.set(auth: auth, mount_key: @mount_key.to_s) do status, headers, body = @transport.handle_request(request) # The transport may return a streaming body (proc) that executes after # Current.set unwinds. Wrap it to restore Current for the duration. wrapped_body = if body.respond_to?(:call) captured_auth = auth captured_mount = @mount_key.to_s proc { |stream| Toolchest::Current.set(auth: captured_auth, mount_key: captured_mount) do body.call(stream) end } else body end [status, headers.dup, wrapped_body] end end |