Class: MCP::Server
- Inherits:
-
Object
- Object
- MCP::Server
- Includes:
- Instrumentation
- Defined in:
- lib/mcp/server.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: Transports Classes: Capabilities, MethodAlreadyDefinedError, RequestHandlerError
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
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
Returns the value of attribute 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.
-
#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
- #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, annotations: nil, meta: nil, &block) ⇒ Object
- #handle(request) ⇒ Object
- #handle_json(request) ⇒ Object
-
#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, transport: nil) ⇒ Server
constructor
A new instance of Server.
- #notify_log_message(data:, level:, logger: nil) ⇒ Object
- #notify_progress(progress_token:, progress:, total: nil, message: nil) ⇒ Object
- #notify_prompts_list_changed ⇒ Object
- #notify_resources_list_changed ⇒ Object
- #notify_tools_list_changed ⇒ Object
- #prompts_get_handler(&block) ⇒ Object
- #prompts_list_handler(&block) ⇒ Object
- #resources_list_handler(&block) ⇒ Object
- #resources_read_handler(&block) ⇒ Object
- #resources_templates_list_handler(&block) ⇒ Object
- #tools_call_handler(&block) ⇒ Object
- #tools_list_handler(&block) ⇒ Object
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, transport: nil) ⇒ Server
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/mcp/server.rb', line 52 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, 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 @configuration = MCP.configuration.merge(configuration) @client = nil validate! @capabilities = capabilities || default_capabilities = 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::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::LOGGING_SET_LEVEL => method(:configure_logging_level), # No op handlers for currently unsupported methods Methods::RESOURCES_SUBSCRIBE => ->(_) { {} }, Methods::RESOURCES_UNSUBSCRIBE => ->(_) { {} }, Methods::COMPLETION_COMPLETE => ->(_) { { completion: { values: [], hasMore: false } } }, Methods::ELICITATION_CREATE => ->(_) {}, } @transport = transport end |
Instance Attribute Details
#capabilities ⇒ Object
Returns the value of attribute capabilities.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def capabilities @capabilities end |
#configuration ⇒ Object
Returns the value of attribute configuration.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def configuration @configuration end |
#description ⇒ Object
Returns the value of attribute description.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def description @description end |
#icons ⇒ Object
Returns the value of attribute icons.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def icons @icons end |
#instructions ⇒ Object
Returns the value of attribute instructions.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def instructions @instructions end |
#logging_message_notification ⇒ Object
Returns the value of attribute logging_message_notification.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def end |
#name ⇒ Object
Returns the value of attribute name.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def name @name end |
#prompts ⇒ Object
Returns the value of attribute prompts.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def prompts @prompts end |
#resources ⇒ Object
Returns the value of attribute resources.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def resources @resources end |
#server_context ⇒ Object
Returns the value of attribute server_context.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def server_context @server_context end |
#title ⇒ Object
Returns the value of attribute title.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def title @title end |
#tools ⇒ Object
Returns the value of attribute tools.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def tools @tools end |
#transport ⇒ Object
Returns the value of attribute transport.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def transport @transport end |
#version ⇒ Object
Returns the value of attribute version.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def version @version end |
#website_url ⇒ Object
Returns the value of attribute website_url.
50 51 52 |
# File 'lib/mcp/server.rb', line 50 def website_url @website_url end |
Instance Method Details
#define_custom_method(method_name:, &block) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/mcp/server.rb', line 143 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
136 137 138 139 140 141 |
# File 'lib/mcp/server.rb', line 136 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, annotations: nil, meta: nil, &block) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/mcp/server.rb', line 126 def define_tool(name: nil, title: nil, description: nil, input_schema: nil, annotations: nil, meta: nil, &block) tool = Tool.define(name: name, title: title, description: description, input_schema: input_schema, annotations: annotations, meta: , &block) tool_name = tool.name_value @tool_names << tool_name @tools[tool_name] = tool validate! end |
#handle(request) ⇒ Object
114 115 116 117 118 |
# File 'lib/mcp/server.rb', line 114 def handle(request) JsonRpcHandler.handle(request) do |method| handle_request(request, method) end end |
#handle_json(request) ⇒ Object
120 121 122 123 124 |
# File 'lib/mcp/server.rb', line 120 def handle_json(request) JsonRpcHandler.handle_json(request) do |method| handle_request(request, method) end end |
#notify_log_message(data:, level:, logger: nil) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/mcp/server.rb', line 190 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_progress(progress_token:, progress:, total: nil, message: nil) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mcp/server.rb', line 175 def notify_progress(progress_token:, progress:, total: nil, message: nil) return unless @transport params = { "progressToken" => progress_token, "progress" => progress, "total" => total, "message" => , }.compact @transport.send_notification(Methods::NOTIFICATIONS_PROGRESS, params) rescue => e report_exception(e, notification: "progress") end |
#notify_prompts_list_changed ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/mcp/server.rb', line 159 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
167 168 169 170 171 172 173 |
# File 'lib/mcp/server.rb', line 167 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
151 152 153 154 155 156 157 |
# File 'lib/mcp/server.rb', line 151 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 |
#prompts_get_handler(&block) ⇒ Object
226 227 228 |
# File 'lib/mcp/server.rb', line 226 def prompts_get_handler(&block) @handlers[Methods::PROMPTS_GET] = block end |
#prompts_list_handler(&block) ⇒ Object
222 223 224 |
# File 'lib/mcp/server.rb', line 222 def prompts_list_handler(&block) @handlers[Methods::PROMPTS_LIST] = block end |
#resources_list_handler(&block) ⇒ Object
202 203 204 |
# File 'lib/mcp/server.rb', line 202 def resources_list_handler(&block) @handlers[Methods::RESOURCES_LIST] = block end |
#resources_read_handler(&block) ⇒ Object
206 207 208 |
# File 'lib/mcp/server.rb', line 206 def resources_read_handler(&block) @handlers[Methods::RESOURCES_READ] = block end |
#resources_templates_list_handler(&block) ⇒ Object
210 211 212 |
# File 'lib/mcp/server.rb', line 210 def resources_templates_list_handler(&block) @handlers[Methods::RESOURCES_TEMPLATES_LIST] = block end |
#tools_call_handler(&block) ⇒ Object
218 219 220 |
# File 'lib/mcp/server.rb', line 218 def tools_call_handler(&block) @handlers[Methods::TOOLS_CALL] = block end |
#tools_list_handler(&block) ⇒ Object
214 215 216 |
# File 'lib/mcp/server.rb', line 214 def tools_list_handler(&block) @handlers[Methods::TOOLS_LIST] = block end |