Class: Msf::MCP::Tools::HostInfo

Inherits:
MCP::Tool
  • Object
show all
Extended by:
ToolHelper
Defined in:
lib/msf/core/mcp/tools/host_info.rb

Overview

MCP Tool: Query Metasploit Database Hosts

Retrieves host information from the Metasploit database including IP addresses, operating systems, and discovery metadata.

Constant Summary

Constants included from ToolHelper

ToolHelper::DANGEROUS_MODE_DISABLED_MESSAGE

Class Method Summary collapse

Methods included from ToolHelper

dangerous_mode_required!, tool_error_response, with_tool_context

Class Method Details

.call(workspace: 'default', addresses: nil, only_up: false, limit: Msf::MCP::Security::InputValidator::LIMIT_DEFAULT, offset: 0, server_context:) ⇒ MCP::Tool::Response

Execute host query

Parameters:

  • workspace (String) (defaults to: 'default')

    Workspace name (default: ‘default’)

  • addresses (String, nil) (defaults to: nil)

    IP address or CIDR range to filter

  • only_up (Boolean) (defaults to: false)

    Filter to only return hosts that are up

  • limit (Integer) (defaults to: Msf::MCP::Security::InputValidator::LIMIT_DEFAULT)

    Maximum results (default: 100)

  • offset (Integer) (defaults to: 0)

    Results offset (default: 0)

  • server_context (Hash)

    Server context with msf_client, rate_limiter, config

Returns:

  • (MCP::Tool::Response)

    Structured response with host information



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/msf/core/mcp/tools/host_info.rb', line 108

def call(workspace: 'default', addresses: nil, only_up: false, limit: Msf::MCP::Security::InputValidator::LIMIT_DEFAULT, offset: 0, server_context:)
  with_tool_context(server_context, 'host_info') do |msf_client|
    # Validate inputs
    Msf::MCP::Security::InputValidator.validate_only_up!(only_up)
    Msf::MCP::Security::InputValidator.validate_ip_address!(addresses) if addresses
    Msf::MCP::Security::InputValidator.validate_pagination!(limit, offset)

    # Call Metasploit API
    # Note that `workspace` is optional in the MSF API, the default workspace is used if not provided.
    # The default value is sent anyway for clarity.
    options = { workspace: workspace }
    options[:addresses] = addresses if addresses
    options[:only_up] = only_up if only_up
    raw_hosts, elapsed = Rex::Stopwatch.elapsed_time do
      msf_client.db_hosts(options)
    end

    # Transform response
    transformed = Metasploit::ResponseTransformer.transform_hosts(raw_hosts)

    # Apply pagination
    #
    # Note that to get the total number of entries, we gather the entire data set and apply pagination here
    # instead of sending the limit and offset to the API call to be processed by MSF.
    # This is needed to provide accurate total_items count in the metadata.
    total_items = transformed.size
    paginated_data = transformed[offset, limit] || []

    # Build metadata
     = {
      workspace: workspace,
      query_time: elapsed.round(3),
      total_items: total_items,
      returned_items: paginated_data.size,
      limit: limit,
      offset: offset
    }

    # Return MCP response
    ::MCP::Tool::Response.new(
      [
        {
          type: 'text',
          text: JSON.generate(
            metadata: ,
            data: paginated_data
          )
        }
      ],
      structured_content: {
        metadata: ,
        data: paginated_data
      }
    )
  end
end