@@ -27,6 +27,44 @@ def initialize(transport:, client_info: nil, protocol_version: "2025-06-18")
2727 # So keeping it public
2828 attr_reader :transport , :client_info , :protocol_version , :session_info
2929
30+ # Initializes the MCP session with the server.
31+ # This method performs the MCP handshake: sends an initialize request,
32+ # receives session information, and sends the initialized notification.
33+ #
34+ # @return [Hash] The session information including server_info
35+ #
36+ # @example
37+ # client.init
38+ # # => { server_info: { name: "my-server", version: "1.0.0" }, ... }
39+ def init
40+ return @session_info if @initialized
41+
42+ response = transport . send_request ( request : {
43+ jsonrpc : JsonRpcHandler ::Version ::V2_0 ,
44+ id : request_id ,
45+ method : "initialize" ,
46+ params : {
47+ protocolVersion : protocol_version ,
48+ capabilities : { } ,
49+ clientInfo : {
50+ name : client_info [ :name ] || client_info [ "name" ] ,
51+ version : client_info [ :version ] || client_info [ "version" ] ,
52+ } ,
53+ } ,
54+ } )
55+
56+ @session_info = response . dig ( "result" )
57+ @initialized = true
58+
59+ # Send the initialized notification
60+ transport . send_notification ( notification : {
61+ jsonrpc : JsonRpcHandler ::Version ::V2_0 ,
62+ method : "notifications/initialized" ,
63+ } )
64+
65+ @session_info
66+ end
67+
3068 # Returns the list of tools available from the server.
3169 # Each call will make a new request – the result is not cached.
3270 #
@@ -38,6 +76,8 @@ def initialize(transport:, client_info: nil, protocol_version: "2025-06-18")
3876 # puts tool.name
3977 # end
4078 def tools
79+ init unless @initialized
80+
4181 response = transport . send_request ( request : {
4282 jsonrpc : JsonRpcHandler ::Version ::V2_0 ,
4383 id : request_id ,
@@ -58,6 +98,8 @@ def tools
5898 #
5999 # @return [Array<Hash>] An array of available resources.
60100 def resources
101+ init unless @initialized
102+
61103 response = transport . send_request ( request : {
62104 jsonrpc : JsonRpcHandler ::Version ::V2_0 ,
63105 id : request_id ,
@@ -82,6 +124,8 @@ def resources
82124 # The exact requirements for `arguments` are determined by the transport layer in use.
83125 # Consult the documentation for your transport (e.g., MCP::Client::HTTP) for details.
84126 def call_tool ( tool :, arguments : nil )
127+ init unless @initialized
128+
85129 transport . send_request ( request : {
86130 jsonrpc : JsonRpcHandler ::Version ::V2_0 ,
87131 id : request_id ,
@@ -95,6 +139,8 @@ def call_tool(tool:, arguments: nil)
95139 # @param uri [String] The URI of the resource to read.
96140 # @return [Array<Hash>] An array of resource contents (text or blob).
97141 def read_resource ( uri :)
142+ init unless @initialized
143+
98144 response = transport . send_request ( request : {
99145 jsonrpc : JsonRpcHandler ::Version ::V2_0 ,
100146 id : request_id ,
0 commit comments