Class: ActionMCP::GatewayIdentifiers::ApiKeyIdentifier

Inherits:
ActionMCP::GatewayIdentifier show all
Defined in:
lib/action_mcp/gateway_identifiers/api_key_identifier.rb

Overview

Example Gateway identifier for API key-based authentication.

This identifier looks for API keys in various locations:

  • Authorization header (Bearer token)

  • Custom X-API-Key header

  • Query parameters

Examples:

Usage in ApplicationGateway

class ApplicationGateway < ActionMCP::Gateway
  identified_by ActionMCP::GatewayIdentifiers::ApiKeyIdentifier
end

Configuration

# config/mcp.yml
authentication_methods: ["api_key"]

API Key usage

# Via Authorization header:
# Authorization: Bearer your-api-key-here
#
# Via custom header:
# X-API-Key: your-api-key-here
#
# Via query parameter:
# ?api_key=your-api-key-here

Instance Method Summary collapse

Methods inherited from ActionMCP::GatewayIdentifier

authenticates, identifier, #initialize

Constructor Details

This class inherits a constructor from ActionMCP::GatewayIdentifier

Instance Method Details

#resolveObject

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_mcp/gateway_identifiers/api_key_identifier.rb', line 34

def resolve
  api_key = extract_api_key
  raise Unauthorized, "Missing API key" unless api_key

  # Look up user by API key
  # Assumes you have an api_key or api_token field on your User model
  user = User.find_by(api_key: api_key) || User.find_by(api_token: api_key)
  raise Unauthorized, "Invalid API key" unless user

  # Optional: Check if API key is still valid (not expired, user active, etc.)
  if user.respond_to?(:api_key_expired?) && user.api_key_expired?
    raise Unauthorized, "API key expired"
  end

  if user.respond_to?(:active?) && !user.active?
    raise Unauthorized, "User account inactive"
  end

  user
end