Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,18 @@ client.tools # will make the call using Bearer auth

You can add any custom headers needed for your authentication scheme, or for any other purpose. The client will include these headers on every request.

#### Customizing the Faraday Connection

You can pass a block to `MCP::Client::HTTP.new` to customize the underlying Faraday connection.
The block is called after the default middleware is configured, so you can add middleware or swap the HTTP adapter:

```ruby
http_transport = MCP::Client::HTTP.new(url: "https://api.example.com/mcp") do |faraday|
faraday.use MyApp::Middleware::HttpRecorder
faraday.adapter :typhoeus
end
```

### Tool Objects

The client provides a wrapper class for tools returned by the server:
Expand Down
5 changes: 4 additions & 1 deletion lib/mcp/client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class HTTP

attr_reader :url

def initialize(url:, headers: {})
def initialize(url:, headers: {}, &block)
@url = url
@headers = headers
@faraday_customizer = block
end

def send_request(request:)
Expand Down Expand Up @@ -78,6 +79,8 @@ def client
headers.each do |key, value|
faraday.headers[key] = value
end

@faraday_customizer&.call(faraday)
end
end

Expand Down
25 changes: 25 additions & 0 deletions test/mcp/client/http_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,31 @@ def test_send_request_raises_internal_error
assert_equal({ method: "tools/list", params: nil }, error.request)
end

def test_block_customizes_faraday_connection
custom_client = HTTP.new(url: url) do |faraday|
faraday.headers["X-Custom"] = "test-value"
end

request = {
jsonrpc: "2.0",
id: "test_id",
method: "tools/list",
}

stub_request(:post, url).with(
headers: {
"X-Custom" => "test-value",
"Accept" => "application/json, text/event-stream",
},
).to_return(
status: 200,
headers: { "Content-Type" => "application/json" },
body: { result: { tools: [] } }.to_json,
)

custom_client.send_request(request: request)
end

def test_send_request_raises_error_for_non_json_response
request = {
jsonrpc: "2.0",
Expand Down