Class: MCPClient::OAuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp_client/oauth_client.rb

Overview

Utility class for creating OAuth-enabled MCP clients

Class Method Summary collapse

Class Method Details

.complete_oauth_flow(server, code, state) ⇒ Auth::Token

Complete OAuth authorization flow with authorization code

Raises:

  • (ArgumentError)

    if server doesn’t have OAuth provider



94
95
96
97
98
99
# File 'lib/mcp_client/oauth_client.rb', line 94

def self.complete_oauth_flow(server, code, state)
  oauth_provider = server.instance_variable_get(:@oauth_provider)
  raise ArgumentError, 'Server does not have OAuth provider configured' unless oauth_provider

  oauth_provider.complete_authorization_flow(code, state)
end

.create_http_server(server_url:, **options) ⇒ ServerHTTP

Create an OAuth-enabled HTTP server

Options Hash (**options):

  • :redirect_uri (String)

    OAuth redirect URI (default: ‘localhost:8080/callback’)

  • :scope (String, nil)

    OAuth scope

  • :endpoint (String)

    JSON-RPC endpoint path (default: ‘/rpc’)

  • :headers (Hash)

    Additional headers to include in requests

  • :read_timeout (Integer)

    Read timeout in seconds (default: 30)

  • :retries (Integer)

    Retry attempts on transient errors (default: 3)

  • :retry_backoff (Numeric)

    Base delay for exponential backoff (default: 1)

  • :name (String, nil)

    Optional name for this server

  • :logger (Logger, nil)

    Optional logger

  • :storage (Object, nil)

    Storage backend for OAuth tokens and client info



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mcp_client/oauth_client.rb', line 24

def self.create_http_server(server_url:, **options)
  opts = default_server_options.merge(options)

  oauth_provider = Auth::OAuthProvider.new(
    server_url: server_url,
    redirect_uri: opts[:redirect_uri],
    scope: opts[:scope],
    logger: opts[:logger],
    storage: opts[:storage]
  )

  ServerHTTP.new(
    base_url: server_url,
    endpoint: opts[:endpoint],
    headers: opts[:headers],
    read_timeout: opts[:read_timeout],
    retries: opts[:retries],
    retry_backoff: opts[:retry_backoff],
    name: opts[:name],
    logger: opts[:logger],
    oauth_provider: oauth_provider
  )
end

.create_streamable_http_server(server_url:, **options) ⇒ ServerStreamableHTTP

Create an OAuth-enabled Streamable HTTP server



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mcp_client/oauth_client.rb', line 52

def self.create_streamable_http_server(server_url:, **options)
  opts = default_server_options.merge(options)

  oauth_provider = Auth::OAuthProvider.new(
    server_url: server_url,
    redirect_uri: opts[:redirect_uri],
    scope: opts[:scope],
    logger: opts[:logger],
    storage: opts[:storage]
  )

  ServerStreamableHTTP.new(
    base_url: server_url,
    endpoint: opts[:endpoint],
    headers: opts[:headers],
    read_timeout: opts[:read_timeout],
    retries: opts[:retries],
    retry_backoff: opts[:retry_backoff],
    name: opts[:name],
    logger: opts[:logger],
    oauth_provider: oauth_provider
  )
end

.start_oauth_flow(server) ⇒ String

Perform OAuth authorization flow for a server This is a helper method that can be used to manually perform the OAuth flow

Raises:

  • (ArgumentError)

    if server doesn’t have OAuth provider



81
82
83
84
85
86
# File 'lib/mcp_client/oauth_client.rb', line 81

def self.start_oauth_flow(server)
  oauth_provider = server.instance_variable_get(:@oauth_provider)
  raise ArgumentError, 'Server does not have OAuth provider configured' unless oauth_provider

  oauth_provider.start_authorization_flow
end

.valid_token?(server) ⇒ Boolean

Check if server has a valid OAuth access token



104
105
106
107
108
109
110
# File 'lib/mcp_client/oauth_client.rb', line 104

def self.valid_token?(server)
  oauth_provider = server.instance_variable_get(:@oauth_provider)
  return false unless oauth_provider

  token = oauth_provider.access_token
  !!(token && !token.expired?)
end