Class: Msf::MCP::Server
- Inherits:
-
Object
- Object
- Msf::MCP::Server
- Defined in:
- lib/msf/core/mcp/server.rb
Overview
MCP Server Wrapper for Metasploit Framework
This class initializes and manages the MCP server with all registered tools. It provides a clean interface for starting/stopping the server and integrates with the Metasploit client and security layers.
The Server expects fully configured and authenticated dependencies to be provided during initialization. It does not handle configuration loading or client authentication - those are responsibilities of the calling code.
Constant Summary collapse
- PUMA_MIN_THREADS =
Puma thread pool configuration defaults
0- PUMA_MAX_THREADS =
5- PUMA_WORKERS =
0
Class Method Summary collapse
-
.generate_auth_token ⇒ String
Generate a random authentication token.
Instance Method Summary collapse
-
#initialize(msf_client:, rate_limiter:, dangerous_actions: false) ⇒ Server
constructor
Initialize the MCP server with required dependencies.
-
#shutdown ⇒ Object
Shutdown the MCP server and cleanup resources.
-
#start(transport: :stdio, host: 'localhost', port: 3000, auth_token: nil, min_threads: PUMA_MIN_THREADS, max_threads: PUMA_MAX_THREADS, workers: PUMA_WORKERS) ⇒ MCP::Server
Start the MCP server with specified transport.
Constructor Details
#initialize(msf_client:, rate_limiter:, dangerous_actions: false) ⇒ Server
Initialize the MCP server with required dependencies
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 65 66 67 68 69 70 |
# File 'lib/msf/core/mcp/server.rb', line 29 def initialize(msf_client:, rate_limiter:, dangerous_actions: false) @msf_client = msf_client # Create server context (passed to all tool calls) # Tools only need msf_client, rate_limiter, and the dangerous_actions gate. @server_context = { msf_client: @msf_client, rate_limiter: rate_limiter, dangerous_actions: dangerous_actions == true } # Create MCP configuration with request lifecycle callbacks mcp_config = ::MCP::Configuration.new mcp_config.around_request = create_around_request mcp_config.exception_reporter = create_exception_reporter # Initialize MCP server with all tools @mcp_server = ::MCP::Server.new( name: 'msfmcp', version: Msf::MCP::Application::VERSION, tools: [ Tools::SearchModules, Tools::ModuleInfo, Tools::ModuleExecute, Tools::ModuleCheck, Tools::ModuleResults, Tools::RunningStats, Tools::HostInfo, Tools::ServiceInfo, Tools::VulnerabilityInfo, Tools::NoteInfo, Tools::CredentialInfo, Tools::LootInfo, Tools::SessionList, Tools::SessionStop, Tools::SessionRead, Tools::SessionWrite ], server_context: @server_context, configuration: mcp_config ) end |
Class Method Details
.generate_auth_token ⇒ String
Generate a random authentication token
115 116 117 |
# File 'lib/msf/core/mcp/server.rb', line 115 def self.generate_auth_token SecureRandom.hex(32) end |
Instance Method Details
#shutdown ⇒ Object
Shutdown the MCP server and cleanup resources
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/msf/core/mcp/server.rb', line 99 def shutdown @puma_server&.stop(true) @puma_launcher&.stop rescue StandardError => e elog({ message: 'Error stopping Puma', exception: e }, LOG_SOURCE, LOG_ERROR) ensure @puma_log_io&.close @puma_server = nil @puma_launcher = nil @puma_log_io = nil @msf_client&.shutdown @mcp_server = nil end |
#start(transport: :stdio, host: 'localhost', port: 3000, auth_token: nil, min_threads: PUMA_MIN_THREADS, max_threads: PUMA_MAX_THREADS, workers: PUMA_WORKERS) ⇒ MCP::Server
Start the MCP server with specified transport
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/msf/core/mcp/server.rb', line 85 def start(transport: :stdio, host: 'localhost', port: 3000, auth_token: nil, min_threads: PUMA_MIN_THREADS, max_threads: PUMA_MAX_THREADS, workers: PUMA_WORKERS) case transport when :stdio start_stdio when :http start_http(host, port, auth_token, min_threads: min_threads, max_threads: max_threads, workers: workers) else raise ArgumentError, "Unknown transport: #{transport}. Use :stdio or :http" end end |