Class: SwaggerMCPTool::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/swagger_mcp_tool/config.rb

Overview

Configuration management for SwaggerMCPTool

This class uses the Singleton pattern to ensure a single configuration instance throughout the application lifecycle.

Constant Summary collapse

DEFAULT_SERVER_PORT =
3001
DEFAULT_SERVER_BIND =
'0.0.0.0'
DEFAULT_SERVER_TYPE =
:puma
DEFAULT_LOG_LEVEL =
Logger::INFO
DEFAULT_AUTH_HEADER_NAME =
'Token'
DEFAULT_SWAGGER_URL =
'https://petstore.swagger.io/v2/swagger.json'
DEFAULT_MCP_NAME =
'swagger_api_tools'
DEFAULT_MCP_NAME_FOR_HUMAN =
'Swagger API Tools'
DEFAULT_MCP_DESCRIPTION_FOR_HUMAN =
'Tools for interacting with APIs via Swagger/OpenAPI specifications'
DEFAULT_MCP_DESCRIPTION_FOR_MODEL =
'This MCP server provides tools for interacting with APIs via Swagger/OpenAPI specifications.'
DEFAULT_AUTH_TYPE =
'none'
DEFAULT_LOG_FILE =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/swagger_mcp_tool/config.rb', line 33

def initialize
  # Server settings
  setup_server_defaults

  # Swagger settings
  setup_swagger_defaults

  # MCP settings
  setup_mcp_defaults

  # Auth settings
  setup_auth_defaults

  # Tools, Prompts and resources
  @tools = []
  @resources = []
  @prompts = [] # Will be generated lazily
end

Instance Attribute Details

#auth_header_nameObject

Returns the value of attribute auth_header_name.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def auth_header_name
  @auth_header_name
end

#auth_typeObject

Returns the value of attribute auth_type.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def auth_type
  @auth_type
end

#default_tokenObject

Returns the value of attribute default_token.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def default_token
  @default_token
end

#log_fileObject

Returns the value of attribute log_file.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def log_file
  @log_file
end

#log_levelObject

Returns the value of attribute log_level.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def log_level
  @log_level
end

#mcp_description_for_humanObject

Returns the value of attribute mcp_description_for_human.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def mcp_description_for_human
  @mcp_description_for_human
end

#mcp_description_for_modelObject

Returns the value of attribute mcp_description_for_model.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def mcp_description_for_model
  @mcp_description_for_model
end

#mcp_nameObject

Returns the value of attribute mcp_name.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def mcp_name
  @mcp_name
end

#mcp_name_for_humanObject

Returns the value of attribute mcp_name_for_human.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def mcp_name_for_human
  @mcp_name_for_human
end

#promptsObject

Returns the value of attribute prompts.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def prompts
  @prompts
end

#resourcesObject

Returns the value of attribute resources.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def resources
  @resources
end

#server_bindObject

Returns the value of attribute server_bind.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def server_bind
  @server_bind
end

#server_portObject

Returns the value of attribute server_port.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def server_port
  @server_port
end

#server_typeObject

Returns the value of attribute server_type.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def server_type
  @server_type
end

#swagger_urlObject

Returns the value of attribute swagger_url.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def swagger_url
  @swagger_url
end

#toolsObject

Returns the value of attribute tools.



14
15
16
# File 'lib/swagger_mcp_tool/config.rb', line 14

def tools
  @tools
end

Class Method Details

.configureObject

Configure the instance



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/swagger_mcp_tool/config.rb', line 108

def self.configure
  return unless block_given?

  config_instance = instance
  yield config_instance
  log_configuration_details(config_instance)
  config_instance
rescue StandardError => e
  config_instance.logger.error "Configuration failed: #{e.message}"
  raise e
end

Instance Method Details

#create_file_loggerObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/swagger_mcp_tool/config.rb', line 91

def create_file_logger
  target = case @log_file
           when $stdout, $stdout, 'STDOUT', 'stdout'
             $stdout
           when $stderr, $stderr, 'STDERR', 'stderr'
             $stderr
           when nil, ''
             $stderr
           else
             @log_file
           end
  logger = Logger.new(target)
  logger.level = @log_level
  logger
end

#create_loggerObject



82
83
84
85
86
87
88
89
# File 'lib/swagger_mcp_tool/config.rb', line 82

def create_logger
  create_file_logger
rescue StandardError => e
  logger = Logger.new($stdout)
  logger.level = @log_level
  logger.warn "Could not create log file #{@log_file} (#{e.message}), using stderr for logging"
  logger
end

#loggerObject

Get the logger



78
79
80
# File 'lib/swagger_mcp_tool/config.rb', line 78

def logger
  @logger ||= create_logger
end

#setup_auth_defaultsObject



72
73
74
75
# File 'lib/swagger_mcp_tool/config.rb', line 72

def setup_auth_defaults
  @auth_type = DEFAULT_AUTH_TYPE
  @default_token = nil
end

#setup_mcp_defaultsObject



65
66
67
68
69
70
# File 'lib/swagger_mcp_tool/config.rb', line 65

def setup_mcp_defaults
  @mcp_name = DEFAULT_MCP_NAME
  @mcp_name_for_human = DEFAULT_MCP_NAME_FOR_HUMAN
  @mcp_description_for_human = DEFAULT_MCP_DESCRIPTION_FOR_HUMAN
  @mcp_description_for_model = DEFAULT_MCP_DESCRIPTION_FOR_MODEL
end

#setup_server_defaultsObject



52
53
54
55
56
57
58
59
# File 'lib/swagger_mcp_tool/config.rb', line 52

def setup_server_defaults
  @server_port = DEFAULT_SERVER_PORT
  @server_bind = DEFAULT_SERVER_BIND
  @server_type = DEFAULT_SERVER_TYPE
  @log_level = DEFAULT_LOG_LEVEL
  @log_file = nil
  @auth_header_name = DEFAULT_AUTH_HEADER_NAME
end

#setup_swagger_defaultsObject



61
62
63
# File 'lib/swagger_mcp_tool/config.rb', line 61

def setup_swagger_defaults
  @swagger_url = DEFAULT_SWAGGER_URL
end

#to_context(request) ⇒ Object

Get server context



121
122
123
124
125
126
127
128
129
# File 'lib/swagger_mcp_tool/config.rb', line 121

def to_context(request)
  auth_header_name = self.auth_header_name || 'Authorization'
  {
    user_id: request.env['HTTP_X_USER_ID'] || '1',
    auth_header_name.to_sym => request.env['HTTP_X_AUTH_TOKEN'],
    username: request.env['HTTP_X_USERNAME'],
    email: request.env['HTTP_X_EMAIL']
  }
end