Skip to content

Commit 72e07aa

Browse files
committed
make faraday optional
1 parent f43c340 commit 72e07aa

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ gem "sorbet-static-and-runtime"
2525

2626
group :test do
2727
gem "webmock"
28+
gem "faraday", ">= 2.0"
2829
end

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ Add this line to your application's Gemfile:
1010
gem 'mcp'
1111
```
1212

13+
### Optional Dependencies
14+
15+
The MCP gem has different dependency requirements depending on your use case:
16+
17+
**For Server-only usage:**
18+
```ruby
19+
gem 'mcp'
20+
```
21+
22+
**For client HTTP transport usage:**
23+
```ruby
24+
gem 'mcp'
25+
gem 'faraday', '>= 2.0'
26+
```
27+
1328
And then execute:
1429

1530
```console
@@ -220,6 +235,8 @@ $ ruby examples/stdio_server.rb
220235

221236
The `ModelContextProtocol::Client` module provides client implementations for interacting with MCP servers. Currently, it supports HTTP transport for making JSON-RPC requests to MCP servers.
222237

238+
**Note:** The client HTTP transport requires the `faraday` gem. Add `gem 'faraday', '>= 2.0'` to your Gemfile if you plan to use the client HTTP transport functionality.
239+
223240
### HTTP Client
224241

225242
The `ModelContextProtocol::Client::Http` class provides a simple HTTP client for interacting with MCP servers:

lib/mcp/client/http.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def call_tool(tool:, input:)
3333
attr_reader :headers
3434

3535
def client
36+
require_faraday!
3637
@client ||= Faraday.new(url) do |faraday|
3738
faraday.request(:json)
3839
faraday.response(:json)
@@ -44,6 +45,13 @@ def client
4445
end
4546
end
4647

48+
def require_faraday!
49+
require "faraday"
50+
rescue LoadError
51+
raise LoadError, "The 'faraday' gem is required to use the MCP client HTTP transport. " \
52+
"Add it to your Gemfile: gem 'faraday', '>= 2.0'"
53+
end
54+
4755
def make_request(method:, params: nil)
4856
client.post(
4957
"",

mcp.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Gem::Specification.new do |spec|
2727
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2828
spec.require_paths = ["lib"]
2929

30-
spec.add_dependency("faraday", ">= 2.0")
3130
spec.add_dependency("json_rpc_handler", "~> 0.1")
3231
spec.add_dependency("json-schema", ">= 4.1")
32+
33+
# Faraday is required for the client HTTP transport layer
3334
end

test/mcp/client/http_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ def test_initialization_with_custom_version
2323
assert_equal(custom_version, client.version)
2424
end
2525

26+
def test_raises_load_error_when_faraday_not_available
27+
client = Http.new(url: url)
28+
29+
# simulate Faraday not being available
30+
Http.any_instance.stubs(:require).with("faraday").raises(LoadError, "cannot load such file -- faraday")
31+
32+
error = assert_raises(LoadError) do
33+
client.send(:client) # Call the private method that triggers require_faraday!
34+
end
35+
36+
assert_includes(error.message, "The 'faraday' gem is required to use the MCP client HTTP transport")
37+
assert_includes(error.message, "Add it to your Gemfile: gem 'faraday', '>= 2.0'")
38+
end
39+
2640
def test_headers_are_added_to_the_request
2741
headers = { "Authorization" => "Bearer token" }
2842
client = Http.new(url: url, headers: headers)

0 commit comments

Comments
 (0)