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.

Instance Method Summary collapse

Constructor Details

#initialize(msf_client:, rate_limiter:) ⇒ Server

Initialize the MCP server with required dependencies

Parameters:



23
24
25
26
27
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
# File 'lib/msf/core/mcp/server.rb', line 23

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



81
82
83
84
# File 'lib/msf/core/mcp/server.rb', line 81

def shutdown
  @msf_client&.shutdown
  @mcp_server = nil
end

#start(transport: :stdio, host: 'localhost', port: 3000) ⇒ 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)

Returns:

  • (MCP::Server)

    The MCP server instance (for testing purposes)

Raises:

  • (ArgumentError)

    If an unknown transport is specified



67
68
69
70
71
72
73
74
75
76
# File 'lib/msf/core/mcp/server.rb', line 67

def start(transport: :stdio, host: 'localhost', port: 3000)
  case transport
  when :stdio
    start_stdio
  when :http
    start_http(host, port)
  else
    raise ArgumentError, "Unknown transport: #{transport}. Use :stdio or :http"
  end
end