Module: Axn::MCP::Serializer

Defined in:
lib/axn/mcp/serializer.rb

Constant Summary collapse

ADAPTER_FAILURE_MESSAGE =

Client-facing text when the transport layer raises while turning a successful result into a response (serialization, response-building — see Invocation.perform's guard). Deliberately generic: the actionable detail is a gem/tool bug, so it rides on the reported exception (logs / on_exception), not the tool's response — mirroring how axn keeps a failure's detail off the user-facing message.

"The tool could not produce a valid response"

Class Method Summary collapse

Class Method Details

.error_response(text) ⇒ Object



31
32
33
# File 'lib/axn/mcp/serializer.rb', line 31

def error_response(text)
  ::MCP::Tool::Response.new([{ type: "text", text: }], error: true)
end

.result_to_mcp_response(result, text_content: :structured, reject_opaque_exposed_values: false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/axn/mcp/serializer.rb', line 18

def result_to_mcp_response(result, text_content: :structured, reject_opaque_exposed_values: false)
  if result.ok?
    exposed = Axn::Extensions::Serialization.render(result, reject_opaque: reject_opaque_exposed_values)
    success_text = success_response_text(result, exposed, text_content)
    ::MCP::Tool::Response.new(
      [{ type: "text", text: success_text }],
      structured_content: exposed.presence,
    )
  else
    error_response(result.error)
  end
end

.success_response_text(result, exposed, text_content) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/axn/mcp/serializer.rb', line 35

def success_response_text(result, exposed, text_content)
  use_message = text_content == :message
  success_message = result.respond_to?(:success) ? result.success : result.message
  if use_message || exposed.blank?
    success_message
  else
    JSON.generate(exposed)
  end
end