Class: LLM::MCP::Router
- Inherits:
-
Object
- Object
- LLM::MCP::Router
- Defined in:
- lib/llm/mcp/router.rb
Overview
Coordinates shared access to a transport by routing JSON-RPC responses to the mailbox waiting on the matching request id.
Instance Method Summary collapse
- #clear(id) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #read(transport) ⇒ Object
- #register ⇒ Object
- #route(response) ⇒ Object
- #write(transport, message) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
8 9 10 11 12 13 14 |
# File 'lib/llm/mcp/router.rb', line 8 def initialize @request_id = -1 @pending = {} @lock = Monitor.new @writer = Monitor.new @reader = Monitor.new end |
Instance Method Details
#clear(id) ⇒ Object
25 26 27 |
# File 'lib/llm/mcp/router.rb', line 25 def clear(id) @lock.synchronize { @pending.delete(id) } end |
#read(transport) ⇒ Object
29 30 31 |
# File 'lib/llm/mcp/router.rb', line 29 def read(transport) @reader.synchronize { transport.read_nonblock } end |
#register ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/llm/mcp/router.rb', line 16 def register @lock.synchronize do @request_id += 1 mailbox = LLM::MCP::Mailbox.new @pending[@request_id] = mailbox [@request_id, mailbox] end end |
#route(response) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/llm/mcp/router.rb', line 37 def route(response) mailbox = @lock.synchronize { @pending[response["id"]] } raise LLM::MCP::MismatchError.new(expected_id: nil, actual_id: response["id"]) unless mailbox mailbox << response nil end |
#write(transport, message) ⇒ Object
33 34 35 |
# File 'lib/llm/mcp/router.rb', line 33 def write(transport, ) @writer.synchronize { transport.write() } end |