Class: TUITD::MCP::Server
- Inherits:
-
Object
- Object
- TUITD::MCP::Server
- 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
Instance Method Summary collapse
-
#initialize(rows: 40, cols: 120, timeout: 30) ⇒ Server
constructor
A new instance of Server.
-
#start ⇒ Object
Start the MCP server (reads from stdin, writes to stdout).
Constructor Details
#initialize(rows: 40, cols: 120, timeout: 30) ⇒ Server
Returns a new instance of Server.
26 27 28 29 30 31 32 |
# File 'lib/tui_td/mcp/server.rb', line 26 def initialize(rows: 40, cols: 120, timeout: 30) @rows = rows @cols = cols @timeout = timeout @driver = nil @running = true end |
Instance Method Details
#start ⇒ Object
Start the MCP server (reads from stdin, writes to stdout)
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 |
# File 'lib/tui_td/mcp/server.rb', line 35 def start $stdout.sync = true $stderr.sync = true # Signal readiness warn "[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, -32_700, "Parse error: #{e.}") rescue StandardError => e warn "[tui-td MCP] Error: #{e.class}: #{e.}" warn e.backtrace.first(5).join("\n ") if $DEBUG end end @driver&.close end |