Module: LLM::MCP::RPC

Included in:
LLM::MCP
Defined in:
lib/llm/mcp/rpc.rb

Overview

The RPC module provides the JSON-RPC interface used by LLM::MCP. MCP uses JSON-RPC to exchange messages between a client and a server. A client sends a method name and its parameters as a request, and the server replies with either a result or an error.

This module is responsible for composing those requests, applying the defaults needed by built-in MCP methods such as initialize, and reading responses for request methods. Notifications are sent without waiting for a response, and errors are raised as Error.

Instance Method Summary collapse

Instance Method Details

#call(transport, method, params = {}) ⇒ Object?

Sends a method over the transport.

Parameters:

  • transport (LLM::MCP::Transport)

    The transport to write to

  • method (String)

    The method name to call

  • params (Hash) (defaults to: {})

    The parameters to send with the method call

Returns:

  • (Object, nil)

    The result of the method call, or nil if it’s a notification



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/llm/mcp/rpc.rb', line 27

def call(transport, method, params = {})
  message = {jsonrpc: "2.0", method:, params: default_params(method).merge(params)}
  if notification?(method)
    router.write(transport, message)
    return nil
  end
  id, mailbox = router.register
  begin
    router.write(transport, message.merge(id:))
    recv(transport, id, mailbox)
  ensure
    router.clear(id)
  end
end