11# frozen_string_literal: true
22
3- require "net/http"
3+ $LOAD_PATH. unshift ( File . expand_path ( "../lib" , __dir__ ) )
4+ require "mcp"
45require "json"
5- require "uri"
66
7- # Simple HTTP client example for interacting with the MCP HTTP server
8- class MCPHTTPClient
7+ # Example MCP HTTP client using the improved MCP::Client library
8+ # This demonstrates how to use the streamable HTTP transport with session management
9+ class MCPHTTPClientExample
910 def initialize ( base_url = "http://localhost:9292" )
10- @base_url = base_url
11- @session_id = nil
12- end
13-
14- def send_request ( method , params = nil , id = nil )
15- uri = URI ( @base_url )
16- http = Net ::HTTP . new ( uri . host , uri . port )
17-
18- request = Net ::HTTP ::Post . new ( uri . path . empty? ? "/" : uri . path )
19- request [ "Content-Type" ] = "application/json"
20- request [ "Mcp-Session-Id" ] = @session_id if @session_id
21-
22- body = {
23- jsonrpc : "2.0" ,
24- method : method ,
25- params : params ,
26- id : id || rand ( 10000 ) ,
27- } . compact
28-
29- request . body = body . to_json
30-
31- response = http . request ( request )
32-
33- # Store session ID if provided
34- if response [ "Mcp-Session-Id" ]
35- @session_id = response [ "Mcp-Session-Id" ]
36- puts "Session ID: #{ @session_id } "
37- end
38-
39- JSON . parse ( response . body )
11+ # Create the HTTP transport
12+ transport = MCP ::Client ::HTTP . new ( url : base_url )
13+
14+ # Create the MCP client with custom client info and protocol version
15+ @client = MCP ::Client . new (
16+ transport : transport ,
17+ client_info : { name : "example_client" , version : "1.0" } ,
18+ protocol_version : "2024-11-05" ,
19+ )
4020 end
4121
4222 def initialize_session
4323 puts "=== Initializing session ==="
44- result = send_request ( "initialize" , {
45- protocolVersion : "2024-11-05" ,
46- capabilities : { } ,
47- clientInfo : {
48- name : "example_client" ,
49- version : "1.0" ,
50- } ,
51- } )
52- puts "Response: #{ JSON . pretty_generate ( result ) } "
53-
54- result
55- end
56-
57- def ping
58- puts "=== Sending ping ==="
59- result = send_request ( "ping" )
24+ result = @client . init
6025 puts "Response: #{ JSON . pretty_generate ( result ) } "
26+ puts "Session ID: #{ @client . transport . session_id } " if @client . transport . session_id
6127
6228 result
6329 end
6430
6531 def list_tools
6632 puts "=== Listing tools ==="
67- result = send_request ( "tools/list" )
33+ tools = @client . tools
34+ result = { tools : tools . map { |t | { name : t . name , description : t . description } } }
6835 puts "Response: #{ JSON . pretty_generate ( result ) } "
6936
70- result
37+ tools
7138 end
7239
7340 def call_tool ( name , arguments )
7441 puts "=== Calling tool: #{ name } ==="
75- result = send_request ( "tools/call" , {
76- name : name ,
77- arguments : arguments ,
78- } )
42+ tool = @client . tools . find { |t | t . name == name }
43+
44+ unless tool
45+ puts "Error: Tool '#{ name } ' not found"
46+ return nil
47+ end
48+
49+ result = @client . call_tool ( tool : tool , arguments : arguments )
7950 puts "Response: #{ JSON . pretty_generate ( result ) } "
8051
8152 result
8253 end
8354
8455 def list_prompts
8556 puts "=== Listing prompts ==="
86- result = send_request ( " prompts/list" )
57+ result = @client . prompts
8758 puts "Response: #{ JSON . pretty_generate ( result ) } "
8859
8960 result
9061 end
9162
9263 def get_prompt ( name , arguments )
9364 puts "=== Getting prompt: #{ name } ==="
94- result = send_request ( "prompts/get" , {
95- name : name ,
96- arguments : arguments ,
97- } )
65+ result = @client . get_prompt ( name : name , arguments : arguments )
9866 puts "Response: #{ JSON . pretty_generate ( result ) } "
9967
10068 result
10169 end
10270
10371 def list_resources
10472 puts "=== Listing resources ==="
105- result = send_request ( " resources/list" )
73+ result = @client . resources
10674 puts "Response: #{ JSON . pretty_generate ( result ) } "
10775
10876 result
10977 end
11078
11179 def read_resource ( uri )
11280 puts "=== Reading resource: #{ uri } ==="
113- result = send_request ( "resources/read" , {
114- uri : uri ,
115- } )
81+ result = @client . read_resource ( uri : uri )
11682 puts "Response: #{ JSON . pretty_generate ( result ) } "
11783
11884 result
11985 end
120-
121- def close_session
122- return unless @session_id
123-
124- puts "=== Closing session ==="
125- uri = URI ( @base_url )
126- http = Net ::HTTP . new ( uri . host , uri . port )
127-
128- request = Net ::HTTP ::Delete . new ( uri . path . empty? ? "/" : uri . path )
129- request [ "Mcp-Session-Id" ] = @session_id
130-
131- response = http . request ( request )
132- result = JSON . parse ( response . body )
133- puts "Response: #{ JSON . pretty_generate ( result ) } "
134-
135- @session_id = nil
136- result
137- end
13886end
13987
14088# Main script
14189if __FILE__ == $PROGRAM_NAME
14290 puts <<~MESSAGE
143- MCP HTTP Client Example
91+ MCP HTTP Client Example (Using MCP::Client Library)
14492 Make sure the HTTP server is running (ruby examples/http_server.rb)
14593 #{ "=" * 50 }
14694 MESSAGE
14795
148- client = MCPHTTPClient . new
96+ client = MCPHTTPClientExample . new
14997
15098 begin
151- # Initialize session
99+ # Initialize session (automatically called on first request if not explicit)
152100 client . initialize_session
153101
154- # Test ping
155- client . ping
156-
157102 # List available tools
158103 client . list_tools
159104
@@ -177,8 +122,5 @@ def close_session
177122 rescue => e
178123 puts "Error: #{ e . message } "
179124 puts e . backtrace
180- ensure
181- # Clean up session
182- client . close_session
183125 end
184126end
0 commit comments