Class: FastMcp::Transports::StdioTransport

Inherits:
BaseTransport show all
Defined in:
lib/mcp/transports/stdio_transport.rb

Overview

STDIO transport for MCP This transport uses standard input/output for communication

Instance Attribute Summary

Attributes inherited from BaseTransport

#logger, #server

Instance Method Summary collapse

Methods inherited from BaseTransport

#process_message

Constructor Details

#initialize(server, logger: nil) ⇒ StdioTransport

Returns a new instance of StdioTransport.



10
11
12
13
# File 'lib/mcp/transports/stdio_transport.rb', line 10

def initialize(server, logger: nil)
  super
  @running = false
end

Instance Method Details

#send_message(message) ⇒ Object

Send a message to the client



39
40
41
42
43
44
# File 'lib/mcp/transports/stdio_transport.rb', line 39

def send_message(message)
  json_message = message.is_a?(String) ? message : JSON.generate(message)

  $stdout.puts(json_message)
  $stdout.flush
end

#startObject

Start the transport



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mcp/transports/stdio_transport.rb', line 16

def start
  @logger.info('Starting STDIO transport')
  @running = true

  # Process input from stdin
  while @running && (line = $stdin.gets)
    begin
      process_message(line.strip)
    rescue StandardError => e
      @logger.error("Error processing message: #{e.message}")
      @logger.error(e.backtrace.join("\n"))
      send_error(-32_000, "Internal error: #{e.message}")
    end
  end
end

#stopObject

Stop the transport



33
34
35
36
# File 'lib/mcp/transports/stdio_transport.rb', line 33

def stop
  @logger.info('Stopping STDIO transport')
  @running = false
end