Class: Mistri::MCP::Wires::Stdio
- Inherits:
-
Object
- Object
- Mistri::MCP::Wires::Stdio
- Defined in:
- lib/mistri/mcp/wires.rb
Overview
Stdio: a spawned child process, one JSON-RPC message per line, with credentials in its environment, as the spec prescribes for local servers. Its stderr stays attached for honest local debugging.
Instance Method Summary collapse
- #call(payload) ⇒ Object
- #close ⇒ Object
-
#initialize(command:, env: {}, read_timeout: 120, max_record_bytes: DEFAULT_MAX_RECORD_BYTES) ⇒ Stdio
constructor
A new instance of Stdio.
- #notify(payload) ⇒ Object
- #protocol_version=(_version) ⇒ Object
- #refreshable? ⇒ Boolean
- #reset_session ⇒ Object
- #session? ⇒ Boolean
Constructor Details
#initialize(command:, env: {}, read_timeout: 120, max_record_bytes: DEFAULT_MAX_RECORD_BYTES) ⇒ Stdio
Returns a new instance of Stdio.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mistri/mcp/wires.rb', line 112 def initialize(command:, env: {}, read_timeout: 120, max_record_bytes: DEFAULT_MAX_RECORD_BYTES) unless max_record_bytes.is_a?(Integer) && max_record_bytes.positive? raise ConfigurationError, "max_record_bytes: must be a positive integer" end @command = Array(command).map(&:to_s) @env = env.transform_keys(&:to_s).transform_values(&:to_s) @read_timeout = read_timeout @max_record_bytes = max_record_bytes @pid = nil end |
Instance Method Details
#call(payload) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mistri/mcp/wires.rb', line 125 def call(payload, **) spawn_server unless @pid write(payload) loop do record = read_record yield record break if record.is_a?(Hash) && record["id"] == payload[:id] end nil end |
#close ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/mistri/mcp/wires.rb', line 150 def close if @pid [@stdin, @stdout].each { |io| io.close unless io.closed? } terminate @pid = nil end @read_buffer&.clear nil end |
#notify(payload) ⇒ Object
136 137 138 139 140 |
# File 'lib/mistri/mcp/wires.rb', line 136 def notify(payload) spawn_server unless @pid write(payload) nil end |
#protocol_version=(_version) ⇒ Object
148 |
# File 'lib/mistri/mcp/wires.rb', line 148 def protocol_version=(_version); end |
#refreshable? ⇒ Boolean
144 |
# File 'lib/mistri/mcp/wires.rb', line 144 def refreshable? = false |
#reset_session ⇒ Object
146 |
# File 'lib/mistri/mcp/wires.rb', line 146 def reset_session = nil |
#session? ⇒ Boolean
142 |
# File 'lib/mistri/mcp/wires.rb', line 142 def session? = false |