Class: MCP::Server

Inherits:
Object
  • Object
show all
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
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. https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/completion#completion-results

100

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Returns a new instance of Server.



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
113
114
115
116
117
118
119
120
# File 'lib/mcp/server.rb', line 59

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
  @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::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::COMPLETION_COMPLETE => ->(_) { DEFAULT_COMPLETION_RESULT },
    Methods::LOGGING_SET_LEVEL => method(:configure_logging_level),

    # No op handlers for currently unsupported methods
    Methods::RESOURCES_SUBSCRIBE => ->(_) { {} },
    Methods::RESOURCES_UNSUBSCRIBE => ->(_) { {} },
    Methods::ELICITATION_CREATE => ->(_) {},
  }
  @transport = transport
end

Instance Attribute Details

#capabilitiesObject

Returns the value of attribute capabilities.



56
57
58
# File 'lib/mcp/server.rb', line 56

def capabilities
  @capabilities
end

#client_capabilitiesObject (readonly)

Returns the value of attribute client_capabilities.



57
58
59
# File 'lib/mcp/server.rb', line 57

def client_capabilities
  @client_capabilities
end

#configurationObject

Returns the value of attribute configuration.



56
57
58
# File 'lib/mcp/server.rb', line 56

def configuration
  @configuration
end

#descriptionObject

Returns the value of attribute description.



56
57
58
# File 'lib/mcp/server.rb', line 56

def description
  @description
end

#iconsObject

Returns the value of attribute icons.



56
57
58
# File 'lib/mcp/server.rb', line 56

def icons
  @icons
end

#instructionsObject

Returns the value of attribute instructions.



56
57
58
# File 'lib/mcp/server.rb', line 56

def instructions
  @instructions
end

#logging_message_notificationObject

Returns the value of attribute logging_message_notification.



56
57
58
# File 'lib/mcp/server.rb', line 56

def logging_message_notification
  @logging_message_notification
end

#nameObject

Returns the value of attribute name.



56
57
58
# File 'lib/mcp/server.rb', line 56

def name
  @name
end

#promptsObject

Returns the value of attribute prompts.



56
57
58
# File 'lib/mcp/server.rb', line 56

def prompts
  @prompts
end

#resourcesObject

Returns the value of attribute resources.



56
57
58
# File 'lib/mcp/server.rb', line 56

def resources
  @resources
end

#server_contextObject

Returns the value of attribute server_context.



56
57
58
# File 'lib/mcp/server.rb', line 56

def server_context
  @server_context
end

#titleObject

Returns the value of attribute title.



56
57
58
# File 'lib/mcp/server.rb', line 56

def title
  @title
end

#toolsObject

Returns the value of attribute tools.



56
57
58
# File 'lib/mcp/server.rb', line 56

def tools
  @tools
end

#transportObject

Returns the value of attribute transport.



56
57
58
# File 'lib/mcp/server.rb', line 56

def transport
  @transport
end

#versionObject

Returns the value of attribute version.



56
57
58
# File 'lib/mcp/server.rb', line 56

def version
  @version
end

#website_urlObject

Returns the value of attribute website_url.



56
57
58
# File 'lib/mcp/server.rb', line 56

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/mcp/server.rb', line 266

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: 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.

Yields:

  • (params)

    The request params containing :ref, :argument, and optionally :context.

Yield Returns:

  • (Hash)

    A hash with :completion key containing :values, optional :total, and :hasMore.



262
263
264
# File 'lib/mcp/server.rb', line 262

def completion_handler(&block)
  @handlers[Methods::COMPLETION_COMPLETE] = block
end

#create_sampling_message(messages:, max_tokens:, system_prompt: nil, model_preferences: nil, include_context: nil, temperature: nil, stop_sequences: nil, metadata: nil, tools: nil, tool_choice: nil, related_request_id: nil) ⇒ Object

Sends a sampling/createMessage request to the client. For single-client transports (e.g., StdioTransport). For multi-client transports (e.g., StreamableHTTPTransport), use ServerSession#create_sampling_message instead to ensure the request is routed to the correct client.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/mcp/server.rb', line 213

def create_sampling_message(
  messages:,
  max_tokens:,
  system_prompt: nil,
  model_preferences: nil,
  include_context: nil,
  temperature: nil,
  stop_sequences: nil,
  metadata: nil,
  tools: nil,
  tool_choice: nil,
  related_request_id: nil
)
  unless @transport
    raise "Cannot send sampling request without a transport."
  end

  params = build_sampling_params(
    @client_capabilities,
    messages: messages,
    max_tokens: max_tokens,
    system_prompt: system_prompt,
    model_preferences: model_preferences,
    include_context: include_context,
    temperature: temperature,
    stop_sequences: stop_sequences,
    metadata: ,
    tools: tools,
    tool_choice: tool_choice,
  )

  @transport.send_request(Methods::SAMPLING_CREATE_MESSAGE, params)
end

#define_custom_method(method_name:, &block) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/mcp/server.rb', line 165

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



158
159
160
161
162
163
# File 'lib/mcp/server.rb', line 158

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



148
149
150
151
152
153
154
155
156
# File 'lib/mcp/server.rb', line 148

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: 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.

Parameters:

  • request (Hash)

    A parsed JSON-RPC request.

  • session (ServerSession, nil) (defaults to: nil)

    Per-connection session. Passed by ServerSession#handle for session-scoped notification delivery. When nil, progress and logging notifications from tool handlers are silently skipped.

Returns:

  • (Hash, nil)

    The JSON-RPC response, or nil for notifications.



129
130
131
132
133
# File 'lib/mcp/server.rb', line 129

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.

Parameters:

  • request (String)

    A JSON-RPC request as a JSON string.

  • session (ServerSession, nil) (defaults to: nil)

    Per-connection session. Passed by ServerSession#handle_json for session-scoped notification delivery. When nil, progress and logging notifications from tool handlers are silently skipped.

Returns:

  • (String, nil)

    The JSON-RPC response as JSON, or nil for notifications.



142
143
144
145
146
# File 'lib/mcp/server.rb', line 142

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



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/mcp/server.rb', line 197

def notify_log_message(data:, level:, logger: nil)
  return unless @transport
  return unless logging_message_notification&.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_changedObject



181
182
183
184
185
186
187
# File 'lib/mcp/server.rb', line 181

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_changedObject



189
190
191
192
193
194
195
# File 'lib/mcp/server.rb', line 189

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_changedObject



173
174
175
176
177
178
179
# File 'lib/mcp/server.rb', line 173

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.

Yields:

  • (params)

    The request params containing :uri.

Yield Returns:

  • (Array<Hash>, Hash)

    Resource contents.



253
254
255
# File 'lib/mcp/server.rb', line 253

def resources_read_handler(&block)
  @handlers[Methods::RESOURCES_READ] = block
end