Class: MCPClient::OAuthClient
- Inherits:
-
Object
- Object
- MCPClient::OAuthClient
- Defined in:
- lib/mcp_client/oauth_client.rb
Overview
Utility class for creating OAuth-enabled MCP clients
Class Method Summary collapse
-
.complete_oauth_flow(server, code, state, iss: nil) ⇒ Auth::Token
Complete OAuth authorization flow with authorization code.
-
.create_http_server(server_url:, **options) ⇒ ServerHTTP
Create an OAuth-enabled HTTP server.
-
.create_streamable_http_server(server_url:, **options) ⇒ ServerStreamableHTTP
Create an OAuth-enabled Streamable HTTP server.
-
.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.
-
.valid_token?(server) ⇒ Boolean
Check if server has a valid OAuth access token.
Class Method Details
.complete_oauth_flow(server, code, state, iss: nil) ⇒ Auth::Token
Complete OAuth authorization flow with authorization code
104 105 106 107 108 109 110 111 |
# File 'lib/mcp_client/oauth_client.rb', line 104 def self.complete_oauth_flow(server, code, state, iss: nil) oauth_provider = server.instance_variable_get(:@oauth_provider) raise ArgumentError, 'Server does not have OAuth provider configured' unless oauth_provider return oauth_provider.(code, state) if iss.nil? oauth_provider.(code, state, iss: iss) end |
.create_http_server(server_url:, **options) ⇒ ServerHTTP
Create an OAuth-enabled HTTP server
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 |
# File 'lib/mcp_client/oauth_client.rb', line 28 def self.create_http_server(server_url:, **) opts = .merge() oauth_provider = Auth::OAuthProvider.new( server_url: server_url, redirect_uri: opts[:redirect_uri], scope: opts[:scope], logger: opts[:logger], storage: opts[:storage], client_id_metadata_url: opts[:client_id_metadata_url], application_type: opts[:application_type] ) 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mcp_client/oauth_client.rb', line 58 def self.create_streamable_http_server(server_url:, **) opts = .merge() oauth_provider = Auth::OAuthProvider.new( server_url: server_url, redirect_uri: opts[:redirect_uri], scope: opts[:scope], logger: opts[:logger], storage: opts[:storage], client_id_metadata_url: opts[:client_id_metadata_url], application_type: opts[:application_type] ) 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
89 90 91 92 93 94 |
# File 'lib/mcp_client/oauth_client.rb', line 89 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. end |
.valid_token?(server) ⇒ Boolean
Check if server has a valid OAuth access token
116 117 118 119 120 121 122 |
# File 'lib/mcp_client/oauth_client.rb', line 116 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 |