Module: VectorMCP::Server::MessageHandling

Included in:
VectorMCP::Server
Defined in:
lib/vector_mcp/server/message_handling.rb

Overview

Handles message processing and request/notification dispatching

Instance Method Summary collapse

Instance Method Details

#handle_message(message, session, session_id) ⇒ Object?

Handles an incoming JSON-RPC message (request or notification). This is the main dispatch point for messages received by a transport.

Parameters:

  • message (Hash)

    The parsed JSON-RPC message object.

  • session (VectorMCP::Session)

    The client session associated with this message.

  • session_id (String)

    A unique identifier for the underlying transport connection.

Returns:

  • (Object, nil)

    For requests, returns the result data to be sent in the JSON-RPC response. For notifications, returns nil.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vector_mcp/server/message_handling.rb', line 18

def handle_message(message, session, session_id)
  id = message["id"]
  method = message["method"]
  params = message["params"] || {}

  if id && method # Request
    logger.info("[#{session_id}] Request [#{id}]: #{method} with params: #{params.inspect}")
    handle_request(id, method, params, session)
  elsif method # Notification
    logger.info("[#{session_id}] Notification: #{method} with params: #{params.inspect}")
    handle_notification(method, params, session)
    nil # Notifications do not have a return value to send back to client
  elsif id # Invalid: Has ID but no method
    logger.warn("[#{session_id}] Invalid message: Has ID [#{id}] but no method. #{message.inspect}")
    raise VectorMCP::InvalidRequestError.new("Request object must include a 'method' member.", request_id: id)
  else # Invalid: No ID and no method
    logger.warn("[#{session_id}] Invalid message: Missing both 'id' and 'method'. #{message.inspect}")
    raise VectorMCP::InvalidRequestError.new("Invalid message format", request_id: nil)
  end
end

#on_notification(method) {|params, session, server| ... } ⇒ self

Registers a handler for a specific JSON-RPC notification method.

Parameters:

  • method (String, Symbol)

    The method name (e.g., "my/customNotification").

Yields:

  • (params, session, server)

    A block to handle the notification.

Returns:

  • (self)

    The server instance.



56
57
58
59
# File 'lib/vector_mcp/server/message_handling.rb', line 56

def on_notification(method, &handler)
  @notification_handlers[method.to_s] = handler
  self
end

#on_request(method) {|params, session, server| ... } ⇒ self

Registers a handler for a specific JSON-RPC request method.

Parameters:

  • method (String, Symbol)

    The method name (e.g., "my/customMethod").

Yields:

  • (params, session, server)

    A block to handle the request.

Returns:

  • (self)

    The server instance.



46
47
48
49
# File 'lib/vector_mcp/server/message_handling.rb', line 46

def on_request(method, &handler)
  @request_handlers[method.to_s] = handler
  self
end