Class: RubyLLM::MCP::Elicitation

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/mcp/elicitation.rb

Defined Under Namespace

Classes: DeferredCancellation

Constant Summary collapse

ACCEPT_ACTION =
"accept"
CANCEL_ACTION =
"cancel"
DECLINE_ACTION =
"decline"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coordinator, result) ⇒ Elicitation

Returns a new instance of Elicitation.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_llm/mcp/elicitation.rb', line 24

def initialize(coordinator, result)
  @coordinator = coordinator
  @result = result
  @id = result.id

  @message = @result.params["message"]
  @requested_schema = @result.params["requestedSchema"]

  # Async support
  @deferred = false
  @async_response = nil
  @timeout = nil
  @deferred_request_registered = false
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'lib/ruby_llm/mcp/elicitation.rb', line 22

def id
  @id
end

#requested_schemaObject (readonly)

Returns the value of attribute requested_schema.



22
23
24
# File 'lib/ruby_llm/mcp/elicitation.rb', line 22

def requested_schema
  @requested_schema
end

#structured_response=(value) ⇒ Object (writeonly)

Sets the attribute structured_response

Parameters:

  • value

    the value to set the attribute structured_response to.



21
22
23
# File 'lib/ruby_llm/mcp/elicitation.rb', line 21

def structured_response=(value)
  @structured_response = value
end

#timeoutObject (readonly)

Get timeout value for this elicitation



108
109
110
# File 'lib/ruby_llm/mcp/elicitation.rb', line 108

def timeout
  @timeout
end

Instance Method Details

#cancel_async(reason) ⇒ Object

Cancel async elicitation

Parameters:

  • reason (String)

    cancellation reason



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_llm/mcp/elicitation.rb', line 82

def cancel_async(reason)
  return unless @deferred

  RubyLLM::MCP.logger.info("Cancelling elicitation #{@id}: #{reason}")
  @coordinator.elicitation_response(
    id: @id,
    elicitation: { action: CANCEL_ACTION, content: nil }
  )

  finalize_deferred_request
end

#complete(response_data) ⇒ Object

Complete async elicitation with response data

Parameters:

  • response_data (Hash)

    the structured response



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_llm/mcp/elicitation.rb', line 59

def complete(response_data)
  return unless @deferred

  @structured_response = response_data
  valid = response_valid?

  if valid
    @coordinator.elicitation_response(
      id: @id,
      elicitation: { action: ACCEPT_ACTION, content: @structured_response }
    )
  else
    @coordinator.elicitation_response(
      id: @id,
      elicitation: { action: CANCEL_ACTION, content: nil }
    )
  end

  finalize_deferred_request
end

#executeObject



39
40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/mcp/elicitation.rb', line 39

def execute
  handler = @coordinator.elicitation_callback

  if Handlers.handler_class?(handler)
    execute_with_handler_class(handler)
  else
    execute_with_block
  end
end

#messageObject



49
50
51
# File 'lib/ruby_llm/mcp/elicitation.rb', line 49

def message
  @result.params["message"]
end

#response_valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ruby_llm/mcp/elicitation.rb', line 53

def response_valid?
  SchemaValidator.valid?(@requested_schema, @structured_response)
end

#timeout!Object

Mark as timed out



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_llm/mcp/elicitation.rb', line 95

def timeout!
  return unless @deferred

  RubyLLM::MCP.logger.warn("Elicitation #{@id} timed out")
  @coordinator.elicitation_response(
    id: @id,
    elicitation: { action: CANCEL_ACTION, content: nil }
  )

  finalize_deferred_request
end