Class: ModelContextProtocol::Server::StdioTransport::RequestStore

Inherits:
Object
  • Object
show all
Defined in:
lib/model_context_protocol/server/stdio_transport/request_store.rb

Overview

Thread-safe in-memory storage for tracking active requests and their cancellation status. This store is used by StdioTransport to manage request lifecycle and handle cancellation.

Instance Method Summary collapse

Constructor Details

#initializeRequestStore

Returns a new instance of RequestStore.



6
7
8
9
# File 'lib/model_context_protocol/server/stdio_transport/request_store.rb', line 6

def initialize
  @mutex = Mutex.new
  @requests = {}
end

Instance Method Details

#cancelled?(jsonrpc_request_id) ⇒ Boolean

Check if a request has been cancelled

Parameters:

  • jsonrpc_request_id (String)

    the unique JSON-RPC request identifier

Returns:

  • (Boolean)

    true if the request is cancelled, false otherwise



44
45
46
47
48
# File 'lib/model_context_protocol/server/stdio_transport/request_store.rb', line 44

def cancelled?(jsonrpc_request_id)
  @mutex.synchronize do
    @requests[jsonrpc_request_id]&.fetch(:cancelled, false) || false
  end
end

#mark_cancelled(jsonrpc_request_id) ⇒ Boolean

Mark a request as cancelled

Parameters:

  • jsonrpc_request_id (String)

    the unique JSON-RPC request identifier

Returns:

  • (Boolean)

    true if request was found and marked cancelled, false otherwise



30
31
32
33
34
35
36
37
38
# File 'lib/model_context_protocol/server/stdio_transport/request_store.rb', line 30

def mark_cancelled(jsonrpc_request_id)
  @mutex.synchronize do
    if (request = @requests[jsonrpc_request_id])
      request[:cancelled] = true
      return true
    end
    false
  end
end

#register_request(jsonrpc_request_id, thread = Thread.current) ⇒ void

This method returns an undefined value.

Register a new request with its associated thread

Parameters:

  • jsonrpc_request_id (String)

    the unique JSON-RPC request identifier

  • thread (Thread) (defaults to: Thread.current)

    the thread processing this request (defaults to current thread)



16
17
18
19
20
21
22
23
24
# File 'lib/model_context_protocol/server/stdio_transport/request_store.rb', line 16

def register_request(jsonrpc_request_id, thread = Thread.current)
  @mutex.synchronize do
    @requests[jsonrpc_request_id] = {
      thread:,
      cancelled: false,
      started_at: Time.now
    }
  end
end

#unregister_request(jsonrpc_request_id) ⇒ Hash?

Unregister a request (typically called when request completes)

Parameters:

  • jsonrpc_request_id (String)

    the unique JSON-RPC request identifier

Returns:

  • (Hash, nil)

    the removed request data, or nil if not found



54
55
56
57
58
# File 'lib/model_context_protocol/server/stdio_transport/request_store.rb', line 54

def unregister_request(jsonrpc_request_id)
  @mutex.synchronize do
    @requests.delete(jsonrpc_request_id)
  end
end