Class: Llmemory::MCP::Tools::MemoryRetrieve

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

Class Method Summary collapse

Class Method Details

.call(query:, user_id:, session_id: nil, max_tokens: nil, include_timeline_context: nil, timeline_window: nil, server_context: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/llmemory/mcp/tools/memory_retrieve.rb', line 22

def call(query:, user_id:, session_id: nil, max_tokens: nil, include_timeline_context: nil, timeline_window: nil, server_context: nil)
  session = session_id || "default"
  tokens = max_tokens || 2000
  include_timeline = include_timeline_context == true
  window = timeline_window || 3

  memory = Llmemory::Memory.new(user_id: user_id, session_id: session)
  context = memory.retrieve(query, max_tokens: tokens)

  # Add timeline context if requested
  if include_timeline && !context.to_s.strip.empty?
    timeline_context = fetch_timeline_context(user_id, query, window)
    context = "#{context}\n\n#{timeline_context}" unless timeline_context.empty?
  end

  if context.to_s.strip.empty?
    ::MCP::Tool::Response.new([{
      type: "text",
      text: "No relevant context found for this query."
    }])
  else
    ::MCP::Tool::Response.new([{
      type: "text",
      text: context
    }])
  end
rescue => e
  ::MCP::Tool::Response.new([{
    type: "text",
    text: "Error retrieving context: #{e.message}"
  }], error: true)
end