Class: MailCatcher::Integrations::MCPServer

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_catcher/integrations/mcp_server.rb

Overview

MCP Server Implements the Model Context Protocol (MCP) over stdio Allows Claude and other MCP clients to interact with MailCatcher tools

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MCPServer

Returns a new instance of MCPServer.



14
15
16
17
18
# File 'lib/mail_catcher/integrations/mcp_server.rb', line 14

def initialize(options = {})
  @options = options
  @running = false
  @request_id_counter = 0
end

Instance Attribute Details

#runningObject

Returns the value of attribute running.



12
13
14
# File 'lib/mail_catcher/integrations/mcp_server.rb', line 12

def running
  @running
end

Class Method Details

.start(options = {}) ⇒ Object



20
21
22
23
24
# File 'lib/mail_catcher/integrations/mcp_server.rb', line 20

def self.start(options = {})
  server = new(options)
  server.run
  server
end

Instance Method Details

#runObject

Main server loop - handles JSON-RPC messages from stdin



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
56
57
58
59
60
61
62
63
64
65
# File 'lib/mail_catcher/integrations/mcp_server.rb', line 27

def run
  @running = true
  $stderr.puts "[MCP Server] Starting MailCatcher MCP Server"

  # Output MCP initialization
  send_response(
    jsonrpc: "2.0",
    id: 0,
    result: {
      protocolVersion: "2024-11-05",
      capabilities: {
        tools: {},
        resources: {},
        logging: {}
      },
      serverInfo: {
        name: "mailcatcher-ng",
        version: MailCatcher::VERSION
      }
    }
  )

  # Main loop - read and process requests
  while @running && (line = $stdin.gets)
    begin
      request = JSON.parse(line)
      handle_request(request)
    rescue JSON::ParserError => e
      $stderr.puts "[MCP Server] JSON parse error: #{e.message}"
      send_error_response(nil, -32700, "Parse error")
    rescue => e
      $stderr.puts "[MCP Server] Error: #{e.message}"
      $stderr.puts e.backtrace.first(5)
    end
  end

  $stderr.puts "[MCP Server] MCP Server stopped"
  @running = false
end

#stopObject



67
68
69
# File 'lib/mail_catcher/integrations/mcp_server.rb', line 67

def stop
  @running = false
end