Skip to content

Commit ded9106

Browse files
committed
Add Ruby MCP client example
## Motivation and Context Add an LLM-powered chatbot MCP client example using the MCP Ruby SDK and the Anthropic Ruby SDK, following the same pattern as other language examples. https://github.com/modelcontextprotocol/ruby-sdk The Gemfile specifies mcp version 0.9.1 or higher, which supports the stdio client. https://github.com/modelcontextprotocol/ruby-sdk/releases/tag/v0.9.1 ## Additional context Once anthropics/anthropic-sdk-ruby#161 is merged and released, `gem "base64"` will no longer be needed in the Gemfile.
1 parent 494cfaa commit ded9106

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

mcp-client-ruby/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ANTHROPIC_API_KEY=

mcp-client-ruby/Gemfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "anthropic"
6+
gem "base64"
7+
gem "dotenv"
8+
gem "mcp", '>= 0.9.1'

mcp-client-ruby/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# An LLM-Powered Chatbot MCP Client written in Ruby
2+
3+
See the [Building MCP clients](https://modelcontextprotocol.io/tutorials/building-a-client) tutorial for more information.

mcp-client-ruby/client.rb

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# frozen_string_literal: true
2+
3+
require "anthropic"
4+
require "dotenv/load"
5+
require "json"
6+
require "mcp"
7+
8+
class MCPClient
9+
ANTHROPIC_MODEL = "claude-sonnet-4-5"
10+
11+
def initialize
12+
@mcp_client = nil
13+
@transport = nil
14+
@anthropic_client = nil
15+
end
16+
17+
def connect_to_server(server_script_path)
18+
command = case File.extname(server_script_path)
19+
when ".rb"
20+
"ruby"
21+
when ".py"
22+
"python3"
23+
when ".js"
24+
"node"
25+
else
26+
raise ArgumentError, "Server script must be a .rb, .py, or .js file."
27+
end
28+
29+
@transport = MCP::Client::Stdio.new(command: command, args: [server_script_path])
30+
@mcp_client = MCP::Client.new(transport: @transport)
31+
32+
tool_names = @mcp_client.tools.map(&:name)
33+
puts "\nConnected to server with tools: #{tool_names}"
34+
end
35+
36+
def chat_loop
37+
puts <<~MESSAGE
38+
MCP Client Started!
39+
Type your queries or 'quit' to exit.
40+
MESSAGE
41+
42+
loop do
43+
print "\nQuery: "
44+
line = $stdin.gets
45+
break if line.nil?
46+
47+
query = line.chomp.strip
48+
break if query.downcase == "quit"
49+
next if query.empty?
50+
51+
begin
52+
response = process_query(query)
53+
puts "\n#{response}"
54+
rescue => e
55+
puts "\nError: #{e.message}"
56+
end
57+
end
58+
end
59+
60+
def cleanup
61+
@transport&.close
62+
end
63+
64+
private
65+
66+
def process_query(query)
67+
messages = [{ role: "user", content: query }]
68+
69+
available_tools = @mcp_client.tools.map do |tool|
70+
{ name: tool.name, description: tool.description, input_schema: tool.input_schema }
71+
end
72+
73+
# Initial Claude API call.
74+
response = chat(messages, tools: available_tools)
75+
76+
# Process response and handle tool calls.
77+
if response.content.any?(Anthropic::Models::ToolUseBlock)
78+
assistant_content = response.content.filter_map do |content_block|
79+
case content_block
80+
when Anthropic::Models::TextBlock
81+
{ type: "text", text: content_block.text }
82+
when Anthropic::Models::ToolUseBlock
83+
{ type: "tool_use", id: content_block.id, name: content_block.name, input: content_block.input }
84+
end
85+
end
86+
messages << { role: "assistant", content: assistant_content }
87+
end
88+
89+
response.content.each_with_object([]) do |content, response_parts|
90+
case content
91+
when Anthropic::Models::TextBlock
92+
response_parts << content.text
93+
when Anthropic::Models::ToolUseBlock
94+
# Execute tool call via MCP.
95+
result = @mcp_client.call_tool(name: content.name, arguments: content.input)
96+
response_parts << "[Calling tool #{content.name} with args #{content.input.to_json}]"
97+
98+
tool_result_content = result.dig("result", "content")
99+
result_text = if tool_result_content.is_a?(Array)
100+
tool_result_content.filter_map { |content_item| content_item["text"] }.join("\n")
101+
else
102+
tool_result_content.to_s
103+
end
104+
105+
messages << {
106+
role: "user",
107+
content: [{
108+
type: "tool_result",
109+
tool_use_id: content.id,
110+
content: result_text
111+
}]
112+
}
113+
114+
# Get next response from Claude.
115+
response = chat(messages)
116+
117+
response.content.each do |content_block|
118+
response_parts << content_block.text if content_block.is_a?(Anthropic::Models::TextBlock)
119+
end
120+
end
121+
end.join("\n")
122+
end
123+
124+
def chat(messages, tools: nil)
125+
params = { model: ANTHROPIC_MODEL, max_tokens: 1000, messages: messages }
126+
params[:tools] = tools if tools
127+
128+
anthropic_client.messages.create(**params)
129+
end
130+
131+
def anthropic_client
132+
@anthropic_client ||= Anthropic::Client.new(api_key: ENV["ANTHROPIC_API_KEY"])
133+
end
134+
end
135+
136+
if ARGV.empty?
137+
puts "Usage: ruby client.rb <path_to_server_script>"
138+
exit 1
139+
end
140+
141+
client = MCPClient.new
142+
143+
begin
144+
client.connect_to_server(ARGV[0])
145+
146+
api_key = ENV["ANTHROPIC_API_KEY"]
147+
if api_key.nil? || api_key.empty?
148+
puts <<~MESSAGE
149+
No ANTHROPIC_API_KEY found. To query these tools with Claude, set your API key:
150+
export ANTHROPIC_API_KEY=your-api-key-here
151+
MESSAGE
152+
exit
153+
end
154+
155+
client.chat_loop
156+
rescue => e
157+
puts "Error: #{e.message}"
158+
exit 1
159+
ensure
160+
client.cleanup
161+
end

0 commit comments

Comments
 (0)