Merge pull request #306 from koic/support_customizing_faraday_client_in_mcp_client_http
Support customizing the Faraday client in `MCP::Client::HTTP`
This commit is contained in:
commit
a05d941edd
3 changed files with 41 additions and 1 deletions
12
README.md
12
README.md
|
|
@ -1401,6 +1401,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.
|
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
|
### Tool Objects
|
||||||
|
|
||||||
The client provides a wrapper class for tools returned by the server:
|
The client provides a wrapper class for tools returned by the server:
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,10 @@ module MCP
|
||||||
|
|
||||||
attr_reader :url
|
attr_reader :url
|
||||||
|
|
||||||
def initialize(url:, headers: {})
|
def initialize(url:, headers: {}, &block)
|
||||||
@url = url
|
@url = url
|
||||||
@headers = headers
|
@headers = headers
|
||||||
|
@faraday_customizer = block
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_request(request:)
|
def send_request(request:)
|
||||||
|
|
@ -78,6 +79,8 @@ module MCP
|
||||||
headers.each do |key, value|
|
headers.each do |key, value|
|
||||||
faraday.headers[key] = value
|
faraday.headers[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@faraday_customizer&.call(faraday)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -242,6 +242,31 @@ module MCP
|
||||||
assert_equal({ method: "tools/list", params: nil }, error.request)
|
assert_equal({ method: "tools/list", params: nil }, error.request)
|
||||||
end
|
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
|
def test_send_request_raises_error_for_non_json_response
|
||||||
request = {
|
request = {
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue