Class: RubyLLM::MCP::Auth::Flows::ClientCredentialsFlow

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb

Overview

Orchestrates OAuth 2.1 Client Credentials flow Used for application authentication without user interaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(discoverer:, client_registrar:, token_manager:, storage:, logger:) ⇒ ClientCredentialsFlow

Returns a new instance of ClientCredentialsFlow.



12
13
14
15
16
17
18
# File 'lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb', line 12

def initialize(discoverer:, client_registrar:, token_manager:, storage:, logger:)
  @discoverer = discoverer
  @client_registrar = client_registrar
  @token_manager = token_manager
  @storage = storage
  @logger = logger
end

Instance Attribute Details

#client_registrarObject (readonly)

Returns the value of attribute client_registrar.



10
11
12
# File 'lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb', line 10

def client_registrar
  @client_registrar
end

#discovererObject (readonly)

Returns the value of attribute discoverer.



10
11
12
# File 'lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb', line 10

def discoverer
  @discoverer
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb', line 10

def logger
  @logger
end

#storageObject (readonly)

Returns the value of attribute storage.



10
11
12
# File 'lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb', line 10

def storage
  @storage
end

#token_managerObject (readonly)

Returns the value of attribute token_manager.



10
11
12
# File 'lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb', line 10

def token_manager
  @token_manager
end

Instance Method Details

#execute(server_url, redirect_uri, scope, resource_metadata: nil) ⇒ Token

Perform client credentials flow

Parameters:

  • server_url (String)

    MCP server URL

  • redirect_uri (String)

    redirect URI (used for registration only)

  • scope (String, nil)

    requested scope

  • resource_metadata (String, nil) (defaults to: nil)

    explicit resource metadata URL hint

Returns:

  • (Token)

    access token

Raises:



26
27
28
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
61
62
# File 'lib/ruby_llm/mcp/auth/flows/client_credentials_flow.rb', line 26

def execute(server_url, redirect_uri, scope, resource_metadata: nil)
  logger.debug("Starting OAuth client credentials flow")

  # 1. Discover authorization server
   = discoverer.discover(server_url, resource_metadata_url: )
  raise Errors::TransportError.new(message: "OAuth server discovery failed") unless 

  # 2. Register client (or get cached client) with client credentials grant
  client_info = client_registrar.get_or_register(
    server_url,
    ,
    :client_credentials,
    redirect_uri,
    scope
  )

  # 3. Validate that we have a client secret
  unless client_info.client_secret
    raise Errors::TransportError.new(
      message: "Client credentials flow requires client_secret"
    )
  end

  # 4. Exchange client credentials for token
  token = token_manager.exchange_client_credentials(
    ,
    client_info,
    scope,
    server_url
  )

  # 5. Store token
  storage.set_token(server_url, token)

  logger.info("Client credentials authentication completed successfully")
  token
end