Support customizing the Faraday client in MCP::Client::HTTP

## Motivation and Context

`MCP::Client::HTTP` builds its Faraday connection internally in a private `client` method
with no way to customize the middleware stack or adapter.
Users who need observability (request/response recording, failure logging) or
a different HTTP adapter must override a private method, coupling to an internal API.

This accepts an optional block in `MCP::Client::HTTP.new` that yields the Faraday builder
after default middleware is configured, allowing users to add custom middleware or swap
the HTTP adapter.

## How Has This Been Tested?

Added a test that verifies custom headers set via the block are included in requests.
All existing tests continue to pass.

## Breaking Changes

None. The block argument is optional, so existing code is unaffected.

Resolves #303
This commit is contained in:
Koichi ITO 2026-04-09 04:52:32 +09:00
commit f7952e8aaf

View file

@ -7,9 +7,10 @@ module MCP
attr_reader :url
def initialize(url:, headers: {})
def initialize(url:, headers: {}, &block)
@url = url
@headers = headers
@faraday_customizer = block
end
def send_request(request:)
@ -78,6 +79,8 @@ module MCP
headers.each do |key, value|
faraday.headers[key] = value
end
@faraday_customizer&.call(faraday)
end
end