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

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

Overview

Get conversation history for a prompt object

Class Method Summary collapse

Class Method Details

.call(po_name:, limit: nil, thread_id: nil, server_context:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/prompt_objects/mcp/tools/get_conversation.rb', line 30

def self.call(po_name:, limit: nil, thread_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 = thread_id ? threads.find { |candidate| candidate.id == thread_id } : threads.last
  history = thread ? Repositories::MessageRepository.new(env.session_store)
    .list(thread_id: thread.id, limit: limit || 10_000)
    .map do |message|
      { role: message[:role].to_s, content: message[:content], from: message[:from_po] }.compact
    end : []

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