Class: RubyLLM::MCP::Auth::Browser::CallbackHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/mcp/auth/browser/callback_handler.rb

Overview

Handles OAuth callback request processing Extracts and validates OAuth parameters from callback requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback_path:, logger: nil) ⇒ CallbackHandler

Returns a new instance of CallbackHandler.



12
13
14
15
# File 'lib/ruby_llm/mcp/auth/browser/callback_handler.rb', line 12

def initialize(callback_path:, logger: nil)
  @callback_path = callback_path
  @logger = logger || MCP.logger
end

Instance Attribute Details

#callback_pathObject (readonly)

Returns the value of attribute callback_path.



10
11
12
# File 'lib/ruby_llm/mcp/auth/browser/callback_handler.rb', line 10

def callback_path
  @callback_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/ruby_llm/mcp/auth/browser/callback_handler.rb', line 10

def logger
  @logger
end

Instance Method Details

#extract_oauth_params(params) ⇒ Hash

Extract OAuth parameters from parsed params

Parameters:

  • params (Hash)

    parsed query parameters

Returns:

  • (Hash)

    OAuth parameters (code, state, error, error_description)



39
40
41
42
43
44
45
46
# File 'lib/ruby_llm/mcp/auth/browser/callback_handler.rb', line 39

def extract_oauth_params(params)
  {
    code: params["code"],
    state: params["state"],
    error: params["error"],
    error_description: params["error_description"]
  }
end

#parse_callback_params(path, http_server) ⇒ Hash

Parse callback parameters from path

Parameters:

  • path (String)

    full request path with query string

  • http_server (HttpServer)

    HTTP server instance for parsing

Returns:

  • (Hash)

    parsed parameters



29
30
31
32
33
34
# File 'lib/ruby_llm/mcp/auth/browser/callback_handler.rb', line 29

def parse_callback_params(path, http_server)
  _, query_string = path.split("?", 2)
  params = http_server.parse_query_params(query_string || "")
  @logger.debug("Callback params: #{params.keys.join(', ')}")
  params
end

#update_result_with_oauth_params(oauth_params, result, mutex, condition) ⇒ Object

Update result hash with OAuth parameters (thread-safe)

Parameters:

  • oauth_params (Hash)

    OAuth parameters

  • result (Hash)

    result container

  • mutex (Mutex)

    synchronization mutex

  • condition (ConditionVariable)

    wait condition



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_llm/mcp/auth/browser/callback_handler.rb', line 53

def update_result_with_oauth_params(oauth_params, result, mutex, condition)
  mutex.synchronize do
    if oauth_params[:error]
      result[:error] = oauth_params[:error_description] || oauth_params[:error]
    elsif oauth_params[:code] && oauth_params[:state]
      result[:code] = oauth_params[:code]
      result[:state] = oauth_params[:state]
    else
      result[:error] = "Invalid callback: missing code or state parameter"
    end
    result[:completed] = true
    condition.signal
  end
end

#valid_callback_path?(path) ⇒ Boolean

Validate that the request path matches the expected callback path

Parameters:

  • path (String)

    request path

Returns:

  • (Boolean)

    true if path is valid



20
21
22
23
# File 'lib/ruby_llm/mcp/auth/browser/callback_handler.rb', line 20

def valid_callback_path?(path)
  uri_path, = path.split("?", 2)
  uri_path == @callback_path
end