Class: SwaggerMCPTool::SwaggerClient

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_mcp_tool/swagger_client.rb

Overview

The SwaggerClient class is responsible for fetching and parsing Swagger/OpenAPI specifications from a given URL. It supports both Swagger 2.0 and OpenAPI 3.x formats, handling JSON and YAML content types. The class provides methods to extract the base URL from the specification and manages HTTP(S) requests with proper SSL configuration and error handling.

Example usage:

client = SwaggerMCPTool::SwaggerClient.new('https://example.com/swagger.json')
spec = client.swagger_spec
base_url = client.base_url

Attributes:

@swagger_spec [Hash] The parsed Swagger/OpenAPI specification.
@base_url [String] The base URL extracted from the specification.

Methods:

- initialize(url): Initializes the client and fetches the specification.
- fetch_swagger_spec(url): Fetches and parses the Swagger/OpenAPI spec from the URL.
- parse_url_content(url, content): Parses the content as JSON or YAML.
- get_base_url(swagger_spec): Determines the base URL from the spec.
- get_base_for_swagger_v2(swagger_spec): Extracts base URL for Swagger 2.0.
- get_base_for_swagger_v3(swagger_spec): Extracts base URL for OpenAPI 3.x.
- make_http_request(url): Performs the HTTP GET request.
- create_http_client(uri): Creates and configures the HTTP client.
- configure_ssl(http, uri): Configures SSL for HTTPS requests.
- validate_response!(response): Validates the HTTP response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ SwaggerClient

Returns a new instance of SwaggerClient.



40
41
42
43
44
45
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 40

def initialize(url)
  @config = Config.instance
  @swagger_url = url
  @swagger_spec = fetch_swagger_spec(url)
  @base_url = get_base_url(@swagger_spec)
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



38
39
40
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 38

def base_url
  @base_url
end

#swagger_specObject (readonly)

Returns the value of attribute swagger_spec.



38
39
40
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 38

def swagger_spec
  @swagger_spec
end

Instance Method Details

#configure_ssl(http, uri) ⇒ Object



103
104
105
106
107
108
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 103

def configure_ssl(http, uri)
  return unless uri.scheme == 'https'

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end

#create_http_client(uri) ⇒ Object



97
98
99
100
101
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 97

def create_http_client(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  configure_ssl(http, uri)
  http
end

#fetch_swagger_spec(url) ⇒ Object

Fetch Swagger/OpenAPI specification from URL



48
49
50
51
52
53
54
55
56
57
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 48

def fetch_swagger_spec(url)
  @config.logger.info "Fetching Swagger specification from: #{url}"

  # Use HTTPS if specified in the URL
  http_response = make_http_request(url)
  validate_response!(http_response)

  # Parse JSON or YAML based on content
  parse_url_content(url, http_response.body)
end

#get_base_for_swagger_v2(swagger_spec) ⇒ Object



78
79
80
81
82
83
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 78

def get_base_for_swagger_v2(swagger_spec)
  base_path = swagger_spec['basePath'] || ''
  host = swagger_spec['host'] || ''
  schemes = swagger_spec['schemes'] || ['https']
  "#{schemes.first}://#{host}#{base_path}"
end

#get_base_for_swagger_v3(swagger_spec) ⇒ Object



85
86
87
88
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 85

def get_base_for_swagger_v3(swagger_spec)
  servers = swagger_spec['servers'] || [{ 'url' => '' }]
  servers.first['url']
end

#get_base_url(swagger_spec) ⇒ Object

Get base URL from Swagger spec



68
69
70
71
72
73
74
75
76
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 68

def get_base_url(swagger_spec)
  if swagger_spec['swagger'] == '2.0'
    get_base_for_swagger_v2(swagger_spec)
  elsif swagger_spec['openapi']&.start_with?('3.')
    get_base_for_swagger_v3(swagger_spec)
  else
    ''
  end
end

#make_http_request(url) ⇒ Object



90
91
92
93
94
95
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 90

def make_http_request(url)
  uri = URI(url)
  http = create_http_client(uri)
  request = Net::HTTP::Get.new(uri.request_uri)
  http.request(request)
end

#parse_url_content(url, content) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 59

def parse_url_content(url, content)
  if url.end_with?('.json') || content.strip.start_with?('{')
    JSON.parse(content)
  else
    YAML.safe_load(content)
  end
end

#validate_response!(response) ⇒ Object



110
111
112
113
114
# File 'lib/swagger_mcp_tool/swagger_client.rb', line 110

def validate_response!(response)
  return if response.is_a?(Net::HTTPSuccess)

  raise "Failed to fetch Swagger specification: #{response.code} #{response.message}"
end