Class: SwaggerMCPTool::Server

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Helpers::ToolRegister, Logging
Defined in:
lib/swagger_mcp_tool/server.rb

Overview

The Server class is the main Sinatra-based HTTP server for the SwaggerMCPTool. It provides endpoints for manifest retrieval, tool generation, authentication token management, and health checks. The server dynamically generates tools from a Swagger/OpenAPI specification, manages user authentication, and integrates with the MCP (Model Control Protocol) server.

Key responsibilities:

  • Initializes configuration and tool registry on startup.

  • Exposes RESTful endpoints for manifest, MCP requests, tool generation, and health checks.

  • Handles CORS and server configuration.

  • Dynamically generates and registers tools from a Swagger URL.

  • Manages user authentication tokens and server context.

Endpoints:

  • GET /mcp/manifest : Returns the MCP manifest for integration.

  • POST /mcp : Handles MCP requests with dynamic tools.

  • POST /set_auth_token : Sets authentication tokens for users.

  • POST /generate_tools : Generates tools from a provided Swagger URL.

  • GET /logo.png : Serves the logo image.

  • GET /health : Health check and server status.

Usage:

SwaggerMCPTool::Server.start

Dependencies:

  • Sinatra::Base

  • SwaggerMCPTool::Config

  • SwaggerMCPTool::ToolRegistry

  • SwaggerClient

  • ToolGenerator

  • MCP::Server

Configuration is managed via the Config singleton, and tools are registered dynamically via the ToolRegistry singleton.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ToolRegister

#generate_tools_from_swagger_url, #initialize_tools, #setup_tool_registry, #tool_registry

Methods included from Logging

#log_and_raise_error, #log_message, #log_request_details, #log_request_execution, #log_server_initialization

Constructor Details

#initialize(app = nil) ⇒ Server

Initialize with configuration



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/swagger_mcp_tool/server.rb', line 55

def initialize(app = nil)
  super(app)
  @config = Config.instance

  # Debug configuration values
  log_server_initialization
  # Generate tools on startup
  setup_tool_registry
  initialize_tools
  # Configure server settings
  configure_server
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



49
50
51
# File 'lib/swagger_mcp_tool/server.rb', line 49

def config
  @config
end

#dynamic_toolsObject (readonly)

Returns the value of attribute dynamic_tools.



49
50
51
# File 'lib/swagger_mcp_tool/server.rb', line 49

def dynamic_tools
  @dynamic_tools
end

Class Method Details

.startObject

Start the server



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

def self.start
  # self.define_routes
  new
  run!
end

Instance Method Details

#add_optionsObject



110
111
112
113
114
115
116
117
# File 'lib/swagger_mcp_tool/server.rb', line 110

def add_options
  self.class.options '*' do
    response.headers['Allow'] = 'GET, POST, OPTIONS'
    response.headers['Access-Control-Allow-Headers'] =
      'Authorization, Content-Type, Accept, X-User-ID, Auth-Token, X-Username, X-Email'
    200
  end
end

#configure_serverObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/swagger_mcp_tool/server.rb', line 90

def configure_server
  self.class.set :server, @config.server_type
  self.class.set :bind, @config.server_bind
  self.class.set :port, @config.server_port

  # Enable CORS
  enable_cors

  # Add options headers
  add_options
end

#enable_corsObject



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

def enable_cors
  self.class.before do
    headers 'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
            'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-User-ID, Auth-Token, X-Username, X-Email'
  end
end