Class: PromptObjects::Connectors::MCP::Tools::GetConversation

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/prompt_objects/connectors/mcp.rb

Overview

Get conversation history for a PO

Class Method Summary collapse

Class Method Details

.call(po_name:, session_id: nil, server_context:) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/prompt_objects/connectors/mcp.rb', line 299

def self.call(po_name:, session_id: nil, server_context:)
  env = server_context[:env]

  po = env.registry.get(po_name)
  unless po.is_a?(PromptObjects::PromptObject)
    return ::MCP::Tool::Response.new([{
      type: "text",
      text: JSON.generate({ error: "Prompt object '#{po_name}' not found" })
    }])
  end

  workspace_id = env.active_session(initiated_by: "service:mcp")[:id]
  threads = Repositories::ThreadRepository.new(env.session_store)
    .list(workspace_session_id: workspace_id, po_name: po_name)
  thread = session_id ? threads.find { |candidate| candidate.id == session_id } : threads.last
  history = thread ? Repositories::MessageRepository.new(env.session_store)
    .list(thread_id: thread.id, limit: 10_000)
    .map { |message| { role: message[:role].to_s, content: message[:content] } } : []

  ::MCP::Tool::Response.new([{
    type: "text",
    text: JSON.generate({
      po_name: po_name,
      session_id: thread&.id,
      thread_id: thread&.id,
      history: history
    })
  }])
end