Class: Msf::MCP::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/mcp/application.rb

Overview

Main application class that orchestrates the MCP server startup and lifecycle

Constant Summary collapse

VERSION =
'0.1.0'
<<~BANNER
  MSF MCP Server v#{VERSION}
  Model Context Protocol server for Metasploit Framework
BANNER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV, output: $stderr) ⇒ Application

Initialize the application with command-line arguments

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    Command-line arguments

  • output (IO) (defaults to: $stderr)

    Output stream for messages (default: $stderr)



22
23
24
25
26
27
28
29
30
31
# File 'lib/msf/core/mcp/application.rb', line 22

def initialize(argv = ARGV, output: $stderr)
  @argv = argv.dup
  @output = output
  @options = {}
  @config = nil
  @msf_client = nil
  @mcp_server = nil
  @rate_limiter = nil
  @rpc_manager = nil
end

Instance Attribute Details

#configObject (readonly)

For testing purposes:



16
17
18
# File 'lib/msf/core/mcp/application.rb', line 16

def config
  @config
end

#mcp_serverObject (readonly)

For testing purposes:



16
17
18
# File 'lib/msf/core/mcp/application.rb', line 16

def mcp_server
  @mcp_server
end

#msf_clientObject (readonly)

For testing purposes:



16
17
18
# File 'lib/msf/core/mcp/application.rb', line 16

def msf_client
  @msf_client
end

#optionsObject (readonly)

For testing purposes:



16
17
18
# File 'lib/msf/core/mcp/application.rb', line 16

def options
  @options
end

#rate_limiterObject (readonly)

For testing purposes:



16
17
18
# File 'lib/msf/core/mcp/application.rb', line 16

def rate_limiter
  @rate_limiter
end

#rpc_managerObject (readonly)

For testing purposes:



16
17
18
# File 'lib/msf/core/mcp/application.rb', line 16

def rpc_manager
  @rpc_manager
end

Instance Method Details

#runvoid

This method returns an undefined value.

Run the application



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
# File 'lib/msf/core/mcp/application.rb', line 36

def run
  parse_arguments
  install_signal_handlers
  load_configuration
  validate_configuration
  initialize_logger
  initialize_rate_limiter
  ensure_rpc_server
  initialize_metasploit_client
  authenticate_metasploit
  initialize_mcp_server
  start_mcp_server
rescue Msf::MCP::Config::ValidationError, Msf::MCP::Config::ConfigurationError => e
  handle_configuration_error(e)
rescue Msf::MCP::Metasploit::ConnectionError => e
  handle_connection_error(e)
rescue Msf::MCP::Metasploit::APIError => e
  handle_api_error(e)
rescue Msf::MCP::Metasploit::AuthenticationError => e
  handle_authentication_error(e)
rescue Msf::MCP::Metasploit::RpcStartupError => e
  handle_rpc_startup_error(e)
rescue StandardError => e
  handle_fatal_error(e)
end

#shutdown(signal = 'INT') ⇒ void

This method returns an undefined value.

Shutdown the application gracefully

Performs cleanup operations before process termination:

  • Logs shutdown event via Rex

  • Closes MCP server and Metasploit client connections

  • Cleans up resources

Parameters:

  • signal (String) (defaults to: 'INT')

    Signal name (e.g., ‘INT’, ‘TERM’)



71
72
73
74
75
76
77
78
79
# File 'lib/msf/core/mcp/application.rb', line 71

def shutdown(signal = 'INT')
  ilog({
    message: 'Shutting down',
    context: { signal: "SIG#{signal}" }
  }, LOG_SOURCE, LOG_INFO)
  @mcp_server&.shutdown
  @rpc_manager&.stop_rpc_server
  @output.puts "\nShutdown complete"
end