Class: Msf::Plugin::MCP

Inherits:
Msf::Plugin show all
Includes:
Exploit::Retry
Defined in:
plugins/mcp.rb

Overview

This plugin manages the lifecycle of the Metasploit MCP (Model Context Protocol) server from within the msfconsole session.

Defined Under Namespace

Classes: McpCommandDispatcher

Instance Attribute Summary collapse

Attributes inherited from Msf::Plugin

#opts

Attributes included from Framework::Offspring

#framework

Instance Method Summary collapse

Methods included from Exploit::Retry

#poll_until_truthy, #retry_until_truthy

Methods inherited from Msf::Plugin

#add_console_dispatcher, create, #flush, #input, #output, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #remove_console_dispatcher

Constructor Details

#initialize(framework, opts) ⇒ MCP

Returns a new instance of MCP.



157
158
159
160
161
162
163
164
# File 'plugins/mcp.rb', line 157

def initialize(framework, opts)
  super

  @server_config = nil
  @auto_started_rpc = false
  register_dispatcher
  print_status("MCP plugin loaded. Use #{Msf::Ui::Tip.highlight('mcp start')} to start the server.")
end

Instance Attribute Details

#auto_started_rpcObject

Returns the value of attribute auto_started_rpc.



154
155
156
# File 'plugins/mcp.rb', line 154

def auto_started_rpc
  @auto_started_rpc
end

#mcp_serverObject

Returns the value of attribute mcp_server.



154
155
156
# File 'plugins/mcp.rb', line 154

def mcp_server
  @mcp_server
end

#msf_clientObject

Returns the value of attribute msf_client.



154
155
156
# File 'plugins/mcp.rb', line 154

def msf_client
  @msf_client
end

#rate_limiterObject

Returns the value of attribute rate_limiter.



154
155
156
# File 'plugins/mcp.rb', line 154

def rate_limiter
  @rate_limiter
end

#server_configObject

Returns the value of attribute server_config.



154
155
156
# File 'plugins/mcp.rb', line 154

def server_config
  @server_config
end

#server_threadObject

Returns the value of attribute server_thread.



154
155
156
# File 'plugins/mcp.rb', line 154

def server_thread
  @server_thread
end

#started_atObject

Returns the value of attribute started_at.



154
155
156
# File 'plugins/mcp.rb', line 154

def started_at
  @started_at
end

Instance Method Details

#cleanupObject

Cleans up resources when the plugin is unloaded.



183
184
185
186
187
188
189
190
191
# File 'plugins/mcp.rb', line 183

def cleanup
  if @mcp_server
    stop_mcp_server
    print_status('MCP server stopped')
  end
  deregister_dispatcher
  unload_auto_started_rpc
  super
end

#descObject

Returns the plugin description.



176
177
178
# File 'plugins/mcp.rb', line 176

def desc
  'Manages the Metasploit MCP server from within msfconsole'
end

#nameObject

Returns ‘mcp’



169
170
171
# File 'plugins/mcp.rb', line 169

def name
  'mcp'
end

Public interface for the command dispatcher to control the server.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'plugins/mcp.rb', line 197

def print_mcp_status
  unless @server_config
    print_status('MCP server status: stopped (not configured)')
    print_status("  Use #{Msf::Ui::Tip.highlight('mcp start')} to configure and start the server")
    return
  end

  mcp_config = @server_config[:mcp]

  if @mcp_server
    print_status('MCP server status: running')
    print_status("  Listening: http://#{Rex::Socket.to_authority(mcp_config[:host], mcp_config[:port])}")
    print_status("  Uptime:    #{format_uptime}")
  else
    print_status('MCP server status: stopped')
  end
end

#restart_server(opts = {}) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'plugins/mcp.rb', line 246

def restart_server(opts = {})
  stop_mcp_server if @mcp_server
  unload_auto_started_rpc

  validate_options!(opts)
  @server_config = resolve_config(opts)
  @server_config[:rpc] = resolve_rpc_config(opts)

  rpc = @server_config[:rpc]
  start_mcp_server(rpc, @server_config)
rescue StandardError => e
  # Ensure server is left in a clean stopped state on failure
  stop_mcp_server
  unload_auto_started_rpc
  # Prevent stale config from making status report a "stopped" server
  @server_config = nil
  print_error("Failed to restart MCP server: #{e.message}")
end

#start_server(opts = {}) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'plugins/mcp.rb', line 215

def start_server(opts = {})
  if @mcp_server
    print_error('MCP server is already running')
    return
  end

  validate_options!(opts)
  @server_config = resolve_config(opts)
  @server_config[:rpc] = resolve_rpc_config(opts)

  rpc = @server_config[:rpc]
  start_mcp_server(rpc, @server_config)
rescue StandardError => e
  # Ensure server is left in a clean stopped state on failure
  stop_mcp_server
  unload_auto_started_rpc
  # Prevent stale config from making status report a "stopped" server
  @server_config = nil
  print_error("Failed to start MCP server: #{e.message}")
end

#stop_serverObject



236
237
238
239
240
241
242
243
244
# File 'plugins/mcp.rb', line 236

def stop_server
  unless @mcp_server
    print_error('MCP server is already stopped')
    return
  end

  stop_mcp_server
  print_status('MCP server stopped')
end