Class: MCP::Client::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/client/http.rb

Constant Summary collapse

ACCEPT_HEADER =
"application/json, text/event-stream"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, headers: {}) ⇒ HTTP

Returns a new instance of HTTP.



10
11
12
13
# File 'lib/mcp/client/http.rb', line 10

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

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/mcp/client/http.rb', line 8

def url
  @url
end

Instance Method Details

#send_request(request:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mcp/client/http.rb', line 15

def send_request(request:)
  method = request[:method] || request["method"]
  params = request[:params] || request["params"]

  response = client.post("", request)
  validate_response_content_type!(response, method, params)
  response.body
rescue Faraday::BadRequestError => e
  raise RequestHandlerError.new(
    "The #{method} request is invalid",
    { method: method, params: params },
    error_type: :bad_request,
    original_error: e,
  )
rescue Faraday::UnauthorizedError => e
  raise RequestHandlerError.new(
    "You are unauthorized to make #{method} requests",
    { method: method, params: params },
    error_type: :unauthorized,
    original_error: e,
  )
rescue Faraday::ForbiddenError => e
  raise RequestHandlerError.new(
    "You are forbidden to make #{method} requests",
    { method: method, params: params },
    error_type: :forbidden,
    original_error: e,
  )
rescue Faraday::ResourceNotFound => e
  raise RequestHandlerError.new(
    "The #{method} request is not found",
    { method: method, params: params },
    error_type: :not_found,
    original_error: e,
  )
rescue Faraday::UnprocessableEntityError => e
  raise RequestHandlerError.new(
    "The #{method} request is unprocessable",
    { method: method, params: params },
    error_type: :unprocessable_entity,
    original_error: e,
  )
rescue Faraday::Error => e # Catch-all
  raise RequestHandlerError.new(
    "Internal error handling #{method} request",
    { method: method, params: params },
    error_type: :internal_error,
    original_error: e,
  )
end