Class: MCP::Server
- Inherits:
-
Object
- Object
- MCP::Server
- Includes:
- Instrumentation, Pagination
- Defined in:
- lib/mcp/server.rb,
lib/mcp/server/pagination.rb,
lib/mcp/server/transports.rb,
lib/mcp/server/capabilities.rb,
lib/mcp/server/transports/stdio_transport.rb,
lib/mcp/server/transports/streamable_http_transport.rb
Defined Under Namespace
Modules: Pagination, Transports Classes: Capabilities, MethodAlreadyDefinedError, RequestHandlerError, ResourceNotFoundError, URLElicitationRequiredError, ValidationError
Constant Summary collapse
- DEFAULT_VERSION =
"0.1.0"- UNSUPPORTED_PROPERTIES_UNTIL_2025_06_18 =
[:description, :icons].freeze
- UNSUPPORTED_PROPERTIES_UNTIL_2025_03_26 =
[:title, :websiteUrl].freeze
- DEFAULT_COMPLETION_RESULT =
{ completion: { values: [], hasMore: false } }.freeze
- MAX_COMPLETION_VALUES =
Servers return an array of completion values ranked by relevance, with maximum 100 items per response. modelcontextprotocol.io/specification/2025-11-25/server/utilities/completion#completion-results
100
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
Returns the value of attribute capabilities.
-
#client_capabilities ⇒ Object
readonly
Returns the value of attribute client_capabilities.
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#description ⇒ Object
Returns the value of attribute description.
-
#icons ⇒ Object
Returns the value of attribute icons.
-
#instructions ⇒ Object
Returns the value of attribute instructions.
-
#logging_message_notification ⇒ Object
Returns the value of attribute logging_message_notification.
-
#name ⇒ Object
Returns the value of attribute name.
-
#page_size ⇒ Object
Returns the value of attribute page_size.
-
#prompts ⇒ Object
Returns the value of attribute prompts.
-
#resources ⇒ Object
Returns the value of attribute resources.
-
#server_context ⇒ Object
Returns the value of attribute server_context.
-
#title ⇒ Object
Returns the value of attribute title.
-
#tools ⇒ Object
Returns the value of attribute tools.
-
#transport ⇒ Object
Returns the value of attribute transport.
-
#version ⇒ Object
Returns the value of attribute version.
-
#website_url ⇒ Object
Returns the value of attribute website_url.
Instance Method Summary collapse
- #build_sampling_params(capabilities, messages:, max_tokens:, system_prompt: nil, model_preferences: nil, include_context: nil, temperature: nil, stop_sequences: nil, metadata: nil, tools: nil, tool_choice: nil) ⇒ Object
-
#completion_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘completion/complete` requests.
- #define_custom_method(method_name:, &block) ⇒ Object
- #define_prompt(name: nil, title: nil, description: nil, arguments: [], &block) ⇒ Object
- #define_tool(name: nil, title: nil, description: nil, input_schema: nil, output_schema: nil, annotations: nil, meta: nil, &block) ⇒ Object
-
#handle(request, session: nil) ⇒ Hash?
Processes a parsed JSON-RPC request and returns the response as a Hash.
-
#handle_json(request, session: nil) ⇒ String?
Processes a JSON-RPC request string and returns the response as a JSON string.
-
#initialize(description: nil, icons: [], name: "model_context_protocol", title: nil, version: DEFAULT_VERSION, website_url: nil, instructions: nil, tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: nil, page_size: nil, transport: nil) ⇒ Server
constructor
A new instance of Server.
- #notify_log_message(data:, level:, logger: nil) ⇒ Object
- #notify_prompts_list_changed ⇒ Object
- #notify_resources_list_changed ⇒ Object
- #notify_tools_list_changed ⇒ Object
-
#resources_read_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘resources/read` requests.
-
#resources_subscribe_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘resources/subscribe` requests.
-
#resources_unsubscribe_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘resources/unsubscribe` requests.
-
#roots_list_changed_handler {|params| ... } ⇒ Object
Sets a handler for ‘notifications/roots/list_changed` notifications.
Methods included from Instrumentation
#add_instrumentation_data, #instrument_call
Constructor Details
#initialize(description: nil, icons: [], name: "model_context_protocol", title: nil, version: DEFAULT_VERSION, website_url: nil, instructions: nil, tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: nil, page_size: nil, transport: nil) ⇒ Server
Returns a new instance of Server.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/mcp/server.rb', line 107 def initialize( description: nil, icons: [], name: "model_context_protocol", title: nil, version: DEFAULT_VERSION, website_url: nil, instructions: nil, tools: [], prompts: [], resources: [], resource_templates: [], server_context: nil, configuration: nil, capabilities: nil, page_size: nil, transport: nil ) @description = description @icons = icons @name = name @title = title @version = version @website_url = website_url @instructions = instructions @tool_names = tools.map(&:name_value) @tools = tools.to_h { |t| [t.name_value, t] } @prompts = prompts.to_h { |p| [p.name_value, p] } @resources = resources @resource_templates = resource_templates @resource_index = index_resources_by_uri(resources) @server_context = server_context self.page_size = page_size @configuration = MCP.configuration.merge(configuration) @client = nil validate! @capabilities = capabilities || default_capabilities @client_capabilities = nil @logging_message_notification = nil @handlers = { Methods::RESOURCES_LIST => method(:list_resources), Methods::RESOURCES_READ => method(:read_resource_no_content), Methods::RESOURCES_TEMPLATES_LIST => method(:list_resource_templates), Methods::RESOURCES_SUBSCRIBE => ->(_) { {} }, Methods::RESOURCES_UNSUBSCRIBE => ->(_) { {} }, Methods::TOOLS_LIST => method(:list_tools), Methods::TOOLS_CALL => method(:call_tool), Methods::PROMPTS_LIST => method(:list_prompts), Methods::PROMPTS_GET => method(:get_prompt), Methods::INITIALIZE => method(:init), Methods::PING => ->(_) { {} }, Methods::NOTIFICATIONS_INITIALIZED => ->(_) {}, Methods::NOTIFICATIONS_PROGRESS => ->(_) {}, Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED => ->(_) {}, Methods::COMPLETION_COMPLETE => ->(_) { DEFAULT_COMPLETION_RESULT }, Methods::LOGGING_SET_LEVEL => method(:configure_logging_level), } @transport = transport end |
Instance Attribute Details
#capabilities ⇒ Object
Returns the value of attribute capabilities.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def capabilities @capabilities end |
#client_capabilities ⇒ Object (readonly)
Returns the value of attribute client_capabilities.
105 106 107 |
# File 'lib/mcp/server.rb', line 105 def client_capabilities @client_capabilities end |
#configuration ⇒ Object
Returns the value of attribute configuration.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def configuration @configuration end |
#description ⇒ Object
Returns the value of attribute description.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def description @description end |
#icons ⇒ Object
Returns the value of attribute icons.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def icons @icons end |
#instructions ⇒ Object
Returns the value of attribute instructions.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def instructions @instructions end |
#logging_message_notification ⇒ Object
Returns the value of attribute logging_message_notification.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def @logging_message_notification end |
#name ⇒ Object
Returns the value of attribute name.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def name @name end |
#page_size ⇒ Object
Returns the value of attribute page_size.
105 106 107 |
# File 'lib/mcp/server.rb', line 105 def page_size @page_size end |
#prompts ⇒ Object
Returns the value of attribute prompts.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def prompts @prompts end |
#resources ⇒ Object
Returns the value of attribute resources.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def resources @resources end |
#server_context ⇒ Object
Returns the value of attribute server_context.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def server_context @server_context end |
#title ⇒ Object
Returns the value of attribute title.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def title @title end |
#tools ⇒ Object
Returns the value of attribute tools.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def tools @tools end |
#transport ⇒ Object
Returns the value of attribute transport.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def transport @transport end |
#version ⇒ Object
Returns the value of attribute version.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def version @version end |
#website_url ⇒ Object
Returns the value of attribute website_url.
104 105 106 |
# File 'lib/mcp/server.rb', line 104 def website_url @website_url end |
Instance Method Details
#build_sampling_params(capabilities, messages:, max_tokens:, system_prompt: nil, model_preferences: nil, include_context: nil, temperature: nil, stop_sequences: nil, metadata: nil, tools: nil, tool_choice: nil) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/mcp/server.rb', line 310 def build_sampling_params( capabilities, messages:, max_tokens:, system_prompt: nil, model_preferences: nil, include_context: nil, temperature: nil, stop_sequences: nil, metadata: nil, tools: nil, tool_choice: nil ) unless capabilities&.dig(:sampling) raise "Client does not support sampling." end if tools && !capabilities.dig(:sampling, :tools) raise "Client does not support sampling with tools." end if tool_choice && !capabilities.dig(:sampling, :tools) raise "Client does not support sampling with tool_choice." end { messages: , maxTokens: max_tokens, systemPrompt: system_prompt, modelPreferences: model_preferences, includeContext: include_context, temperature: temperature, stopSequences: stop_sequences, metadata: , tools: tools, toolChoice: tool_choice, }.compact end |
#completion_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘completion/complete` requests. The block receives the parsed request params and should return completion values.
288 289 290 |
# File 'lib/mcp/server.rb', line 288 def completion_handler(&block) @handlers[Methods::COMPLETION_COMPLETE] = block end |
#define_custom_method(method_name:, &block) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/mcp/server.rb', line 213 def define_custom_method(method_name:, &block) if @handlers.key?(method_name) raise MethodAlreadyDefinedError, method_name end @handlers[method_name] = block end |
#define_prompt(name: nil, title: nil, description: nil, arguments: [], &block) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/mcp/server.rb', line 206 def define_prompt(name: nil, title: nil, description: nil, arguments: [], &block) prompt = Prompt.define(name: name, title: title, description: description, arguments: arguments, &block) @prompts[prompt.name_value] = prompt validate! end |
#define_tool(name: nil, title: nil, description: nil, input_schema: nil, output_schema: nil, annotations: nil, meta: nil, &block) ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/mcp/server.rb', line 196 def define_tool(name: nil, title: nil, description: nil, input_schema: nil, output_schema: nil, annotations: nil, meta: nil, &block) tool = Tool.define(name: name, title: title, description: description, input_schema: input_schema, output_schema: output_schema, annotations: annotations, meta: , &block) tool_name = tool.name_value @tool_names << tool_name @tools[tool_name] = tool validate! end |
#handle(request, session: nil) ⇒ Hash?
Processes a parsed JSON-RPC request and returns the response as a Hash.
177 178 179 180 181 |
# File 'lib/mcp/server.rb', line 177 def handle(request, session: nil) JsonRpcHandler.handle(request) do |method, request_id| handle_request(request, method, session: session, related_request_id: request_id) end end |
#handle_json(request, session: nil) ⇒ String?
Processes a JSON-RPC request string and returns the response as a JSON string.
190 191 192 193 194 |
# File 'lib/mcp/server.rb', line 190 def handle_json(request, session: nil) JsonRpcHandler.handle_json(request) do |method, request_id| handle_request(request, method, session: session, related_request_id: request_id) end end |
#notify_log_message(data:, level:, logger: nil) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/mcp/server.rb', line 253 def (data:, level:, logger: nil) return unless @transport return unless &.should_notify?(level) params = { "data" => data, "level" => level } params["logger"] = logger if logger @transport.send_notification(Methods::NOTIFICATIONS_MESSAGE, params) rescue => e report_exception(e, { notification: "log_message" }) end |
#notify_prompts_list_changed ⇒ Object
237 238 239 240 241 242 243 |
# File 'lib/mcp/server.rb', line 237 def notify_prompts_list_changed return unless @transport @transport.send_notification(Methods::NOTIFICATIONS_PROMPTS_LIST_CHANGED) rescue => e report_exception(e, { notification: "prompts_list_changed" }) end |
#notify_resources_list_changed ⇒ Object
245 246 247 248 249 250 251 |
# File 'lib/mcp/server.rb', line 245 def notify_resources_list_changed return unless @transport @transport.send_notification(Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED) rescue => e report_exception(e, { notification: "resources_list_changed" }) end |
#notify_tools_list_changed ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/mcp/server.rb', line 229 def notify_tools_list_changed return unless @transport @transport.send_notification(Methods::NOTIFICATIONS_TOOLS_LIST_CHANGED) rescue => e report_exception(e, { notification: "tools_list_changed" }) end |
#resources_read_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘resources/read` requests. The block receives the parsed request params and should return resource contents. The return value is set as the `contents` field of the response.
279 280 281 |
# File 'lib/mcp/server.rb', line 279 def resources_read_handler(&block) @handlers[Methods::RESOURCES_READ] = block end |
#resources_subscribe_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘resources/subscribe` requests. The block receives the parsed request params. The return value is ignored; the response is always an empty result `{}` per the MCP specification.
297 298 299 |
# File 'lib/mcp/server.rb', line 297 def resources_subscribe_handler(&block) @handlers[Methods::RESOURCES_SUBSCRIBE] = block end |
#resources_unsubscribe_handler {|params| ... } ⇒ Object
Sets a custom handler for ‘resources/unsubscribe` requests. The block receives the parsed request params. The return value is ignored; the response is always an empty result `{}` per the MCP specification.
306 307 308 |
# File 'lib/mcp/server.rb', line 306 def resources_unsubscribe_handler(&block) @handlers[Methods::RESOURCES_UNSUBSCRIBE] = block end |
#roots_list_changed_handler {|params| ... } ⇒ Object
Sets a handler for ‘notifications/roots/list_changed` notifications. Called when a client notifies the server that its filesystem roots have changed.
269 270 271 |
# File 'lib/mcp/server.rb', line 269 def roots_list_changed_handler(&block) @handlers[Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED] = block end |