Class: PromptObjects::Connectors::MCP::Tools::ListSessions

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

Overview

List sessions for a PO or all sessions

Class Method Summary collapse

Class Method Details

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



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/prompt_objects/connectors/mcp.rb', line 349

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

  unless env.session_store
    return ::MCP::Tool::Response.new([{
      type: "text",
      text: JSON.generate({ error: "Sessions not available in this environment" })
    }])
  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)
  threads = threads.select { |thread| thread.thread_type == source } if source
  messages = Repositories::MessageRepository.new(env.session_store)
  sessions_data = threads.map do |thread|
    {
      id: thread.id,
      thread_id: thread.id,
      workspace_session_id: thread.workspace_session_id,
      po_name: thread.po_name,
      name: thread.name,
      source: thread.thread_type,
      thread_type: thread.thread_type,
      message_count: messages.list(thread_id: thread.id, limit: 10_000).length,
      created_at: thread.created_at&.iso8601,
      updated_at: thread.updated_at&.iso8601
    }
  end

  ::MCP::Tool::Response.new([{
    type: "text",
    text: JSON.generate({ sessions: sessions_data, threads: sessions_data })
  }])
end