Class: RubyLLM::MCP::Auth::Discoverer

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

Overview

Service for discovering OAuth authorization servers Implements RFC 8414 (Server Metadata) and RFC 9728 (Protected Resource Metadata)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_client, storage, logger) ⇒ Discoverer

Returns a new instance of Discoverer.



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

def initialize(http_client, storage, logger)
  @http_client = http_client
  @storage = storage
  @logger = logger
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



9
10
11
# File 'lib/ruby_llm/mcp/auth/discoverer.rb', line 9

def http_client
  @http_client
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/ruby_llm/mcp/auth/discoverer.rb', line 9

def logger
  @logger
end

#storageObject (readonly)

Returns the value of attribute storage.



9
10
11
# File 'lib/ruby_llm/mcp/auth/discoverer.rb', line 9

def storage
  @storage
end

Instance Method Details

#discover(server_url, resource_metadata_url: nil) ⇒ ServerMetadata?

Discover OAuth authorization server Tries two patterns: server as own auth server, or delegated auth server

Parameters:

  • server_url (String)

    MCP server URL

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

    explicit resource metadata URL from WWW-Authenticate

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_llm/mcp/auth/discoverer.rb', line 22

def discover(server_url, resource_metadata_url: nil)
  logger.debug("Discovering OAuth authorization server for #{server_url}")

  cached = storage.(server_url)
  return cached if cached && .nil?

  # Prefer protected resource metadata discovery to follow MCP authorization rules,
  # then fall back to direct auth server metadata discovery for compatibility.
   = try_protected_resource_discovery(server_url, resource_metadata_url: )
   ||= try_authorization_server_discovery(server_url)
   ||= try_legacy_authorization_server_discovery(server_url)
   ||= cached
   ||= (server_url)

  # Cache and return
  storage.(server_url, ) if 
  
end