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
DEFAULT_PROTOCOL_VERSION =
"2024-11-05"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

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
  @exclude_tools = []
end

Instance Attribute Details

#auth_header_nameObject

Returns the value of attribute auth_header_name.



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

def auth_header_name
  @auth_header_name
end

#auth_typeObject

Returns the value of attribute auth_type.



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

def auth_type
  @auth_type
end

#default_tokenObject

Returns the value of attribute default_token.



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

def default_token
  @default_token
end

#exclude_toolsObject

Returns the value of attribute exclude_tools.



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

def exclude_tools
  @exclude_tools
end

#log_fileObject

Returns the value of attribute log_file.



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

def log_file
  @log_file
end

#log_levelObject

Returns the value of attribute log_level.



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

def log_level
  @log_level
end

#mcp_description_for_humanObject

Returns the value of attribute mcp_description_for_human.



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

def mcp_description_for_human
  @mcp_description_for_human
end

#mcp_description_for_modelObject

Returns the value of attribute mcp_description_for_model.



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

def mcp_description_for_model
  @mcp_description_for_model
end

#mcp_nameObject

Returns the value of attribute mcp_name.



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

def mcp_name
  @mcp_name
end

#mcp_name_for_humanObject

Returns the value of attribute mcp_name_for_human.



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

def mcp_name_for_human
  @mcp_name_for_human
end

#promptsObject

Returns the value of attribute prompts.



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

def prompts
  @prompts
end

#protocol_versionObject

Returns the value of attribute protocol_version.



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

def protocol_version
  @protocol_version
end

#resourcesObject

Returns the value of attribute resources.



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

def resources
  @resources
end

#server_bindObject

Returns the value of attribute server_bind.



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

def server_bind
  @server_bind
end

#server_portObject

Returns the value of attribute server_port.



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

def server_port
  @server_port
end

#server_typeObject

Returns the value of attribute server_type.



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

def server_type
  @server_type
end

#swagger_urlObject

Returns the value of attribute swagger_url.



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

def swagger_url
  @swagger_url
end

#toolsObject

Returns the value of attribute tools.



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

def tools
  @tools
end

Class Method Details

.configureObject

Configure the instance



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/swagger_mcp_tool/config.rb', line 111

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/swagger_mcp_tool/config.rb', line 94

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



85
86
87
88
89
90
91
92
# File 'lib/swagger_mcp_tool/config.rb', line 85

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



81
82
83
# File 'lib/swagger_mcp_tool/config.rb', line 81

def logger
  @logger ||= create_logger
end

#setup_auth_defaultsObject



75
76
77
78
# File 'lib/swagger_mcp_tool/config.rb', line 75

def setup_auth_defaults
  @auth_type = DEFAULT_AUTH_TYPE
  @default_token = nil
end

#setup_mcp_defaultsObject



67
68
69
70
71
72
73
# File 'lib/swagger_mcp_tool/config.rb', line 67

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
  @protocol_version = DEFAULT_PROTOCOL_VERSION
end

#setup_server_defaultsObject



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

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



63
64
65
# File 'lib/swagger_mcp_tool/config.rb', line 63

def setup_swagger_defaults
  @swagger_url = DEFAULT_SWAGGER_URL
end

#to_context(request) ⇒ Object

Get server context



124
125
126
127
128
129
130
131
132
# File 'lib/swagger_mcp_tool/config.rb', line 124

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