Class: Msf::Plugin::MCP

Inherits:
Msf::Plugin show all
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 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.



152
153
154
155
156
157
158
159
# File 'plugins/mcp.rb', line 152

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.



149
150
151
# File 'plugins/mcp.rb', line 149

def auto_started_rpc
  @auto_started_rpc
end

#mcp_serverObject

Returns the value of attribute mcp_server.



149
150
151
# File 'plugins/mcp.rb', line 149

def mcp_server
  @mcp_server
end

#msf_clientObject

Returns the value of attribute msf_client.



149
150
151
# File 'plugins/mcp.rb', line 149

def msf_client
  @msf_client
end

#rate_limiterObject

Returns the value of attribute rate_limiter.



149
150
151
# File 'plugins/mcp.rb', line 149

def rate_limiter
  @rate_limiter
end

#server_configObject

Returns the value of attribute server_config.



149
150
151
# File 'plugins/mcp.rb', line 149

def server_config
  @server_config
end

#server_threadObject

Returns the value of attribute server_thread.



149
150
151
# File 'plugins/mcp.rb', line 149

def server_thread
  @server_thread
end

#started_atObject

Returns the value of attribute started_at.



149
150
151
# File 'plugins/mcp.rb', line 149

def started_at
  @started_at
end

Instance Method Details

#cleanupObject

Cleans up resources when the plugin is unloaded.



178
179
180
181
182
183
184
185
186
# File 'plugins/mcp.rb', line 178

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.



171
172
173
# File 'plugins/mcp.rb', line 171

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

#nameObject

Returns ‘mcp’



164
165
166
# File 'plugins/mcp.rb', line 164

def name
  'mcp'
end

Public interface for the command dispatcher to control the server.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'plugins/mcp.rb', line 192

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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'plugins/mcp.rb', line 239

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
  print_error("Failed to restart MCP server: #{e.message}")
end

#start_server(opts = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'plugins/mcp.rb', line 210

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
  print_error("Failed to start MCP server: #{e.message}")
end

#stop_serverObject



229
230
231
232
233
234
235
236
237
# File 'plugins/mcp.rb', line 229

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

  stop_mcp_server
  print_status('MCP server stopped')
end