Class: ModelContextProtocol::Server::Configuration
- Inherits:
-
Object
- Object
- ModelContextProtocol::Server::Configuration
- Defined in:
- lib/model_context_protocol/server/configuration.rb
Overview
Base settings container for MCP servers with two concrete subclasses:
- StdioConfiguration for standalone scripts using stdin/stdout
- StreamableHttpConfiguration for Rack applications using streamable HTTP with Redis
Server.rb factory methods (with_stdio_transport, with_streamable_http_transport) instantiate the appropriate subclass, yield it to a block for population, validate it, then pass it to Router.new. Router reads pagination settings via pagination_options and queries transport capabilities via supports_list_changed? and apply_environment_variables?.
The base class provides shared attributes (name, version, registry, pagination) and validation logic, while subclasses override transport_type and validate_transport! to enforce transport-specific constraints.
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidPaginationError, InvalidRegistryError, InvalidServerInstructionsError, InvalidServerNameError, InvalidServerTitleError, InvalidServerVersionError, InvalidTransportError, MissingRequiredEnvironmentVariable
Instance Attribute Summary collapse
-
#client_logger ⇒ Object
readonly
Returns the value of attribute client_logger.
-
#instructions ⇒ String?
Optional guidance for LLMs on how to use the server (sent in initialize response instructions field if present).
-
#name ⇒ String
The server's identifying name (sent in initialize response serverInfo.name).
-
#pagination ⇒ Hash, ...
Pagination settings or false to disable; Router calls pagination_options to extract default_page_size, max_page_size, and cursor_ttl when handling list requests (resources/list, prompts/list, tools/list).
-
#title ⇒ String?
Optional human-readable display title for Claude Desktop UI (sent in initialize response serverInfo.title if present).
-
#version ⇒ String
The server's version string (sent in initialize response serverInfo.version).
Instance Method Summary collapse
-
#apply_environment_variables? ⇒ Boolean
Determine whether Router should modify ENV before executing handlers.
-
#context ⇒ Hash
Access server-wide key-value storage merged with per-request session_context by Router.
-
#context=(context_hash = {}) ⇒ Hash
Replace the server-wide context with a new hash.
-
#initialize ⇒ Configuration
constructor
Initialize shared attributes and loggers for any configuration subclass.
-
#pagination_enabled? ⇒ Boolean
Check whether pagination is active for list responses (resources/list, prompts/list, tools/list).
-
#pagination_options ⇒ Hash
Extract normalized pagination settings for Router to pass to Pagination.extract_pagination_params.
-
#registry(&block) ⇒ ModelContextProtocol::Server::Registry
Create and store a Registry from a block defining prompts, resources, and tools.
-
#server_logger ⇒ ServerLogger
Lazily-built Ruby Logger for server-side diagnostics (not sent to clients).
-
#supports_list_changed? ⇒ Boolean
Determine whether the transport supports notifications/resources/list_changed and notifications/tools/list_changed.
-
#transport_type ⇒ Symbol?
Identify the transport layer for this configuration.
-
#validate! ⇒ void
Verify all required attributes and transport-specific constraints.
Constructor Details
#initialize ⇒ Configuration
Initialize shared attributes and loggers for any configuration subclass. ClientLogger queues messages until a transport connects; ServerLogger is built lazily on first access via #server_logger.
97 98 99 100 101 102 |
# File 'lib/model_context_protocol/server/configuration.rb', line 97 def initialize @client_logger = ModelContextProtocol::Server::ClientLogger.new( logger_name: "server", level: "info" ) end |
Instance Attribute Details
#client_logger ⇒ Object (readonly)
Returns the value of attribute client_logger.
76 77 78 |
# File 'lib/model_context_protocol/server/configuration.rb', line 76 def client_logger @client_logger end |
#instructions ⇒ String?
71 72 73 |
# File 'lib/model_context_protocol/server/configuration.rb', line 71 def instructions @instructions end |
#name ⇒ String
50 51 52 |
# File 'lib/model_context_protocol/server/configuration.rb', line 50 def name @name end |
#pagination ⇒ Hash, ...
61 62 63 |
# File 'lib/model_context_protocol/server/configuration.rb', line 61 def pagination @pagination end |
#title ⇒ String?
66 67 68 |
# File 'lib/model_context_protocol/server/configuration.rb', line 66 def title @title end |
#version ⇒ String
54 55 56 |
# File 'lib/model_context_protocol/server/configuration.rb', line 54 def version @version end |
Instance Method Details
#apply_environment_variables? ⇒ Boolean
Determine whether Router should modify ENV before executing handlers. StdioConfiguration returns true because stdin/stdout scripts run single-threaded and ENV mutation is safe. StreamableHttpConfiguration returns false because ENV is global and modifying it in a multi-threaded Rack server creates race conditions.
140 |
# File 'lib/model_context_protocol/server/configuration.rb', line 140 def apply_environment_variables? = false |
#context ⇒ Hash
Access server-wide key-value storage merged with per-request session_context by Router. Router.effective_context merges this with Thread.current[:session_context], then passes the result to prompts, resources, and tools so they can access both server-level (shared across all requests) and session-level (specific to HTTP session) data.
217 218 219 |
# File 'lib/model_context_protocol/server/configuration.rb', line 217 def context @context ||= {} end |
#context=(context_hash = {}) ⇒ Hash
Replace the server-wide context with a new hash. Router reads this via effective_context, merging it with session-level data before passing to handler implementations.
227 228 229 |
# File 'lib/model_context_protocol/server/configuration.rb', line 227 def context=(context_hash = {}) @context = context_hash end |
#pagination_enabled? ⇒ Boolean
Check whether pagination is active for list responses (resources/list, prompts/list, tools/list). Router calls this before extracting pagination params; if false, it returns unpaginated results. Enabled by default (nil or true), or when pagination Hash has enabled != false.
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/model_context_protocol/server/configuration.rb', line 147 def pagination_enabled? return true if pagination.nil? case pagination when Hash pagination[:enabled] != false when false false else true end end |
#pagination_options ⇒ Hash
Extract normalized pagination settings for Router to pass to Pagination.extract_pagination_params. Router uses default_page_size and max_page_size to validate cursor and page size params from the client, and cursor_ttl to configure how long the Pagination module stores cursor state in memory.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/model_context_protocol/server/configuration.rb', line 166 def 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 |
#registry(&block) ⇒ ModelContextProtocol::Server::Registry
Create and store a Registry from a block defining prompts, resources, and tools. Router queries the resulting registry to handle resources/list, tools/call, etc.
113 114 115 116 117 |
# File 'lib/model_context_protocol/server/configuration.rb', line 113 def registry(&block) return @registry unless block @registry = ModelContextProtocol::Server::Registry.new(&block) end |
#server_logger ⇒ ServerLogger
Lazily-built Ruby Logger for server-side diagnostics (not sent to clients). Reads from GlobalConfig::ServerLogging on first access so that Server.configure_server_logging can be called before or after the factory method.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/model_context_protocol/server/configuration.rb', line 83 def server_logger @server_logger ||= begin params = if ModelContextProtocol::Server::GlobalConfig::ServerLogging.configured? ModelContextProtocol::Server::GlobalConfig::ServerLogging.logger_params else {} end ModelContextProtocol::Server::ServerLogger.new(**params) end end |
#supports_list_changed? ⇒ Boolean
Determine whether the transport supports notifications/resources/list_changed and notifications/tools/list_changed. Router queries this when building the initialize response capabilities hash (adding listChanged: true to prompts/resources/tools). Only HTTP transport returns true (stdio can't push unsolicited notifications).
132 |
# File 'lib/model_context_protocol/server/configuration.rb', line 132 def supports_list_changed? = false |
#transport_type ⇒ Symbol?
Identify the transport layer for this configuration. Subclasses return :stdio or :streamable_http; Server.start uses this to instantiate the correct Transport class (StdioTransport or StreamableHttpTransport).
124 |
# File 'lib/model_context_protocol/server/configuration.rb', line 124 def transport_type = nil |
#validate! ⇒ void
This method returns an undefined value.
Verify all required attributes and transport-specific constraints. Called by Server.build_server (the factory method's internal logic) after yielding the configuration block but before constructing the Router. Ensures the configuration is complete and internally consistent.
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/model_context_protocol/server/configuration.rb', line 200 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! end |