Class: TUITD::MCP::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_td/mcp/server.rb

Overview

Model Context Protocol server for tui-td.

Implements JSON-RPC 2.0 over stdio transport. Compatible with MCP specification (protocol version 2024-11-05).

Usage:

tui-td serve

Exposes tools that any MCP client can call:

tui_start, tui_send, tui_send_key, tui_wait_for_text,
tui_state, tui_screenshot, tui_html_render, tui_plain_text, tui_close

Constant Summary collapse

PROTOCOL_VERSION =
"2024-11-05"
SERVER_NAME =
"tui-td"
SERVER_VERSION =
TUITD::VERSION

Instance Method Summary collapse

Constructor Details

#initialize(rows: 40, cols: 120, timeout: 30) ⇒ Server

Returns a new instance of Server.



24
25
26
27
28
29
30
# File 'lib/tui_td/mcp/server.rb', line 24

def initialize(rows: 40, cols: 120, timeout: 30)
  @rows = rows
  @cols = cols
  @timeout = timeout
  @driver = nil
  @running = true
end

Instance Method Details

#startObject

Start the MCP server (reads from stdin, writes to stdout)



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
# File 'lib/tui_td/mcp/server.rb', line 33

def start
  $stdout.sync = true
  $stderr.sync = true

  # Signal readiness
  $stderr.puts "[tui-td MCP] Server started, awaiting JSON-RPC on stdin..."

  while @running && (line = $stdin.gets)
    line = line.strip
    next if line.empty?

    begin
      request = JSON.parse(line)
      response = handle_request(request)

      puts JSON.generate(response) if response
      $stdout.flush
    rescue JSON::ParserError => e
      error_response(nil, -32700, "Parse error: #{e.message}")
    rescue StandardError => e
      $stderr.puts "[tui-td MCP] Error: #{e.class}: #{e.message}"
      $stderr.puts e.backtrace.first(5).join("\n  ") if $DEBUG
    end
  end

  @driver&.close
end