Module: RubyLLM::MCP::Auth::TransportOauthHelper

Defined in:
lib/ruby_llm/mcp/auth/transport_oauth_helper.rb

Overview

Helper module for preparing OAuth providers for transports This keeps OAuth logic out of the Native module while making it reusable

Class Method Summary collapse

Class Method Details

.create_oauth_provider(config) ⇒ OAuthProvider, ...

Create OAuth provider from configuration Accepts either a provider instance or a configuration hash

Parameters:

  • config (Hash)

    transport configuration hash (will be modified)

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_llm/mcp/auth/transport_oauth_helper.rb', line 29

def create_oauth_provider(config)
  oauth_config = config.delete(:oauth) || config.delete("oauth")
  return nil unless oauth_config

  # If provider key exists with an instance, use it
  if oauth_config.is_a?(Hash) && (oauth_config[:provider] || oauth_config["provider"])
    return oauth_config[:provider] || oauth_config["provider"]
  end

  # If oauth_config itself is a provider instance, use it directly
  if oauth_config.respond_to?(:access_token) && oauth_config.respond_to?(:start_authorization_flow)
    return oauth_config
  end

  # Otherwise create new provider from config hash
  server_url = determine_server_url(config)
  return nil unless server_url

  redirect_uri = oauth_config[:redirect_uri] || oauth_config["redirect_uri"] || "http://localhost:8080/callback"
  scope = oauth_config[:scope] || oauth_config["scope"]
  storage = oauth_config[:storage] || oauth_config["storage"]
  grant_type = oauth_config[:grant_type] || oauth_config["grant_type"] || :authorization_code

  RubyLLM::MCP::Auth::OAuthProvider.new(
    server_url: server_url,
    redirect_uri: redirect_uri,
    scope: scope,
    logger: MCP.logger,
    storage: storage,
    grant_type: grant_type
  )
end

.determine_server_url(config) ⇒ String?

Determine server URL from transport config

Parameters:

  • config (Hash)

    transport configuration hash

Returns:

  • (String, nil)

    server URL or nil



65
66
67
# File 'lib/ruby_llm/mcp/auth/transport_oauth_helper.rb', line 65

def determine_server_url(config)
  config[:url] || config["url"]
end

.oauth_config_present?(config) ⇒ Boolean

Check if OAuth configuration is present

Parameters:

  • config (Hash)

    transport configuration hash

Returns:

  • (Boolean)

    true if OAuth config is present



14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby_llm/mcp/auth/transport_oauth_helper.rb', line 14

def oauth_config_present?(config)
  oauth_config = config[:oauth] || config["oauth"]
  return false if oauth_config.nil?

  # If it's an OAuth provider instance, it's present
  return true if oauth_config.respond_to?(:access_token)

  # If it's a hash, check if it's not empty
  !oauth_config.empty?
end

.prepare_http_transport_config(config, oauth_provider) ⇒ Hash

Prepare HTTP transport configuration with OAuth provider

Parameters:

  • config (Hash)

    transport configuration hash (will be modified)

  • oauth_provider (OAuthProvider, nil)

    OAuth provider instance

Returns:

  • (Hash)

    prepared configuration



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_llm/mcp/auth/transport_oauth_helper.rb', line 73

def prepare_http_transport_config(config, oauth_provider)
  options = {
    version: config.delete(:version) || config.delete("version"),
    headers: config.delete(:headers) || config.delete("headers"),
    oauth_provider: oauth_provider,
    reconnection: config.delete(:reconnection) || config.delete("reconnection"),
    reconnection_options: config.delete(:reconnection_options) || config.delete("reconnection_options"),
    rate_limit: config.delete(:rate_limit) || config.delete("rate_limit"),
    session_id: config.delete(:session_id) || config.delete("session_id")
  }.compact

  config[:options] = options
  config
end

.prepare_stdio_transport_config(config) ⇒ Hash

Prepare stdio transport configuration

Parameters:

  • config (Hash)

    transport configuration hash (will be modified)

Returns:

  • (Hash)

    prepared configuration



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruby_llm/mcp/auth/transport_oauth_helper.rb', line 91

def prepare_stdio_transport_config(config)
  # Remove OAuth config from stdio transport (not supported)
  config.delete(:oauth)
  config.delete("oauth")

  options = {
    args: config.delete(:args) || config.delete("args"),
    env: config.delete(:env) || config.delete("env")
  }.compact

  config[:options] = options unless options.empty?
  config
end