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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msf_client:, rate_limiter:, dangerous_actions: false) ⇒ Server

Initialize the MCP server with required dependencies

Parameters:

  • msf_client (Metasploit::Client)

    Configured and authenticated Metasploit client

  • rate_limiter (Security::RateLimiter)

    Configured rate limiter

  • dangerous_actions (Boolean) (defaults to: false)

    Whether dangerous (destructive) tools are permitted



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_tokenString

Generate a random authentication token

Returns:

  • (String)


115
116
117
# File 'lib/msf/core/mcp/server.rb', line 115

def self.generate_auth_token
  SecureRandom.hex(32)
end

Instance Method Details

#shutdownObject

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

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



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