Class: ModelContextProtocol::Server::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/model_context_protocol/server/configuration.rb

Defined Under Namespace

Classes: InvalidLogLevelError, InvalidPaginationError, InvalidRegistryError, InvalidServerInstructionsError, InvalidServerNameError, InvalidServerTitleError, InvalidServerVersionError, InvalidTransportError, MissingRequiredEnvironmentVariable

Constant Summary collapse

VALID_LOG_LEVELS =

Valid MCP log levels per the specification

%w[debug info notice warning error critical alert emergency].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



38
39
40
41
42
43
44
45
46
# File 'lib/model_context_protocol/server/configuration.rb', line 38

def initialize
  @logging_enabled = true
  @default_log_level = "info"
  @logger = ModelContextProtocol::Server::MCPLogger.new(
    logger_name: "server",
    level: @default_log_level,
    enabled: @logging_enabled
  )
end

Instance Attribute Details

#instructionsObject

Returns the value of attribute instructions.



35
36
37
# File 'lib/model_context_protocol/server/configuration.rb', line 35

def instructions
  @instructions
end

#loggerObject (readonly)

Returns the value of attribute logger.



36
37
38
# File 'lib/model_context_protocol/server/configuration.rb', line 36

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



35
36
37
# File 'lib/model_context_protocol/server/configuration.rb', line 35

def name
  @name
end

#paginationObject

Returns the value of attribute pagination.



35
36
37
# File 'lib/model_context_protocol/server/configuration.rb', line 35

def pagination
  @pagination
end

#registryObject

Returns the value of attribute registry.



35
36
37
# File 'lib/model_context_protocol/server/configuration.rb', line 35

def registry
  @registry
end

#titleObject

Returns the value of attribute title.



35
36
37
# File 'lib/model_context_protocol/server/configuration.rb', line 35

def title
  @title
end

#transportObject

Returns the value of attribute transport.



35
36
37
# File 'lib/model_context_protocol/server/configuration.rb', line 35

def transport
  @transport
end

#versionObject

Returns the value of attribute version.



35
36
37
# File 'lib/model_context_protocol/server/configuration.rb', line 35

def version
  @version
end

Instance Method Details

#contextObject



159
160
161
# File 'lib/model_context_protocol/server/configuration.rb', line 159

def context
  @context ||= {}
end

#context=(context_hash = {}) ⇒ Object



163
164
165
# File 'lib/model_context_protocol/server/configuration.rb', line 163

def context=(context_hash = {})
  @context = context_hash
end

#default_log_level=(level) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/model_context_protocol/server/configuration.rb', line 61

def default_log_level=(level)
  unless VALID_LOG_LEVELS.include?(level.to_s)
    raise InvalidLogLevelError, "Invalid log level: #{level}. Valid levels are: #{VALID_LOG_LEVELS.join(", ")}"
  end

  @default_log_level = level.to_s
  @logger.set_mcp_level(@default_log_level)
end

#environment_variable(key) ⇒ Object



138
139
140
# File 'lib/model_context_protocol/server/configuration.rb', line 138

def environment_variable(key)
  environment_variables[key.to_s.upcase] || ENV[key.to_s.upcase] || nil
end

#environment_variablesObject



134
135
136
# File 'lib/model_context_protocol/server/configuration.rb', line 134

def environment_variables
  @environment_variables ||= {}
end

#logging_enabled=(value) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/model_context_protocol/server/configuration.rb', line 52

def logging_enabled=(value)
  @logging_enabled = value
  @logger = ModelContextProtocol::Server::MCPLogger.new(
    logger_name: "server",
    level: @default_log_level,
    enabled: value
  )
end

#logging_enabled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/model_context_protocol/server/configuration.rb', line 48

def logging_enabled?
  @logging_enabled
end

#pagination_enabled?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/model_context_protocol/server/configuration.rb', line 88

def pagination_enabled?
  return true if pagination.nil?

  case pagination
  when Hash
    pagination[:enabled] != false
  when false
    false
  else
    true
  end
end

#pagination_optionsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/model_context_protocol/server/configuration.rb', line 101

def pagination_options
  case pagination
  when Hash
    {
      enabled: pagination[:enabled] != false,
      default_page_size: pagination[:default_page_size] || 100,
      max_page_size: pagination[:max_page_size] || 1000,
      cursor_ttl: pagination[:cursor_ttl] || 3600
    }
  when false
    {enabled: false}
  else
    {
      enabled: true,
      default_page_size: 100,
      max_page_size: 1000,
      cursor_ttl: 3600
    }
  end
end

#require_environment_variable(key) ⇒ Object



142
143
144
# File 'lib/model_context_protocol/server/configuration.rb', line 142

def require_environment_variable(key)
  required_environment_variables << key.to_s.upcase
end

#set_environment_variable(key, value) ⇒ Object

Programatically set an environment variable - useful if an alternative to environment variables is used for security purposes. Despite being more like ‘configuration variables’, these are called environment variables to align with the Model Context Protocol terminology.

see: modelcontextprotocol.io/docs/tools/debugging#environment-variables

Parameters:

  • key (String)

    The key to set the environment variable for

  • value (String)

    The value to set the environment variable to



155
156
157
# File 'lib/model_context_protocol/server/configuration.rb', line 155

def set_environment_variable(key, value)
  environment_variables[key.to_s.upcase] = value
end

#transport_optionsObject



79
80
81
82
83
84
85
86
# File 'lib/model_context_protocol/server/configuration.rb', line 79

def transport_options
  case transport
  when Hash
    transport.except(:type, "type").transform_keys(&:to_sym)
  else
    {}
  end
end

#transport_typeObject



70
71
72
73
74
75
76
77
# File 'lib/model_context_protocol/server/configuration.rb', line 70

def transport_type
  case transport
  when Hash
    transport[:type] || transport["type"]
  when Symbol, String
    transport.to_sym
  end
end

#validate!Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/model_context_protocol/server/configuration.rb', line 122

def validate!
  raise InvalidServerNameError unless valid_name?
  raise InvalidRegistryError unless valid_registry?
  raise InvalidServerVersionError unless valid_version?
  validate_transport!
  validate_pagination!
  validate_title!
  validate_instructions!

  validate_environment_variables!
end