Class: Msf::MCP::Server

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(msf_client:, rate_limiter:) ⇒ Server

Initialize the MCP server with required dependencies

Parameters:



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

def initialize(msf_client:, rate_limiter:)
  @msf_client = msf_client

  # Create server context (passed to all tool calls)
  # Tools only need msf_client and rate_limiter
  @server_context = {
    msf_client: @msf_client,
    rate_limiter: rate_limiter
  }

  # 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::HostInfo,
      Tools::ServiceInfo,
      Tools::VulnerabilityInfo,
      Tools::NoteInfo,
      Tools::CredentialInfo,
      Tools::LootInfo
    ],
    server_context: @server_context,
    configuration: mcp_config
  )
end

Instance Method Details

#shutdownObject

Shutdown the MCP server and cleanup resources



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/msf/core/mcp/server.rb', line 89

def shutdown
  @puma_launcher&.stop
rescue StandardError => e
  elog({ message: 'Error stopping Puma', exception: e }, LOG_SOURCE, LOG_ERROR)
ensure
  @puma_log_io&.close
  @puma_launcher = nil
  @puma_log_io = nil
  @msf_client&.shutdown
  @mcp_server = nil
end

#start(transport: :stdio, host: 'localhost', port: 3000, min_threads: PUMA_MIN_THREADS, max_threads: PUMA_MAX_THREADS, workers: PUMA_WORKERS) ⇒ MCP::Server

Start the MCP server with specified transport

Parameters:

  • transport (Symbol) (defaults to: :stdio)

    Transport type (:stdio or :http)

  • host (String) (defaults to: 'localhost')

    Host address for HTTP transport (default: ‘localhost’)

  • port (Integer) (defaults to: 3000)

    Port number for HTTP transport (default: 3000)

  • min_threads (Integer) (defaults to: PUMA_MIN_THREADS)

    Minimum number of Puma threads (default: PUMA_MIN_THREADS)

  • max_threads (Integer) (defaults to: PUMA_MAX_THREADS)

    Maximum number of Puma threads (default: PUMA_MAX_THREADS)

  • workers (Integer) (defaults to: PUMA_WORKERS)

    Number of Puma worker processes (default: PUMA_WORKERS)

Returns:

  • (MCP::Server)

    The MCP server instance (for testing purposes)

Raises:

  • (ArgumentError)

    If an unknown transport is specified



75
76
77
78
79
80
81
82
83
84
# File 'lib/msf/core/mcp/server.rb', line 75

def start(transport: :stdio, host: 'localhost', port: 3000, 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, min_threads: min_threads, max_threads: max_threads, workers: workers)
  else
    raise ArgumentError, "Unknown transport: #{transport}. Use :stdio or :http"
  end
end