Module: MicroMcp::RuntimeHelpers

Defined in:
lib/micro_mcp/runtime_helpers.rb

Instance Method Summary collapse

Instance Method Details

#ask_assistant(question, system_prompt: "You are a helpful assistant.", max_tokens: 100, model_hints: ["o4-mini"]) ⇒ Object

Helper method to make create_message calls more ergonomic Automatically extracts the text content from the response



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/micro_mcp/runtime_helpers.rb', line 9

def ask_assistant(question, system_prompt: "You are a helpful assistant.", max_tokens: 100, model_hints: ["o4-mini"])
  params = {
    "messages" => [
      {
        "role" => "user",
        "content" => {"type" => "text", "text" => question}
      }
    ],
    "modelPreferences" => {
      "hints" => model_hints.map { |name| {"name" => name} },
      "intelligencePriority" => 0.8,
      "speedPriority" => 0.5
    },
    "systemPrompt" => system_prompt,
    "maxTokens" => max_tokens
  }

  # Validate before sending
  errors = ValidationHelpers.validate_create_message_params(params)
  if errors.any?
    raise ArgumentError, "Invalid create_message parameters: #{errors.join(", ")}"
  end

  result = create_message(params)

  # Automatically extract the text content with error handling
  # Response format: { "role": "assistant", "content": { "type": "text", "text": "..." }, "model": "...", "stopReason": "..." }
  if result.is_a?(Hash) && result.dig("content", "text")
    result["content"]["text"]
  else
    raise "Unexpected response format from create_message: #{result.inspect}"
  end
end

#chat_with_assistant(messages, system_prompt: "You are a helpful assistant.", max_tokens: 100, model_hints: ["o4-mini"]) ⇒ Object

More advanced helper that handles different message types



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/micro_mcp/runtime_helpers.rb', line 44

def chat_with_assistant(messages, system_prompt: "You are a helpful assistant.", max_tokens: 100, model_hints: ["o4-mini"])
  # Normalize messages to the expected format
  normalized_messages = messages.map do |msg|
    case msg
    when String
      {"role" => "user", "content" => {"type" => "text", "text" => msg}}
    when Hash
      # Ensure string keys and validate structure
      normalized = ValidationHelpers.stringify_keys(msg)
      unless normalized.key?("role") && normalized.key?("content")
        raise ArgumentError, "Message hash must have 'role' and 'content' keys: #{msg.inspect}"
      end
      normalized
    else
      raise ArgumentError, "Messages must be strings or hashes, got: #{msg.class}"
    end
  end

  params = {
    "messages" => normalized_messages,
    "modelPreferences" => {
      "hints" => model_hints.map { |name| {"name" => name} },
      "intelligencePriority" => 0.8,
      "speedPriority" => 0.5
    },
    "systemPrompt" => system_prompt,
    "maxTokens" => max_tokens
  }

  # Validate before sending
  errors = ValidationHelpers.validate_create_message_params(params)
  if errors.any?
    raise ArgumentError, "Invalid create_message parameters: #{errors.join(", ")}"
  end

  result = create_message(params)

  # Automatically extract the text content with error handling
  # Response format: { "role": "assistant", "content": { "type": "text", "text": "..." }, "model": "...", "stopReason": "..." }
  if result.is_a?(Hash) && result.dig("content", "text")
    result["content"]["text"]
  else
    raise "Unexpected response format from create_message: #{result.inspect}"
  end
end

#safe_create_message(params) ⇒ Object

Safe wrapper around create_message with validation



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/micro_mcp/runtime_helpers.rb', line 91

def safe_create_message(params)
  # Ensure all keys are strings
  safe_params = ValidationHelpers.stringify_keys(params)

  # Validate
  errors = ValidationHelpers.validate_create_message_params(safe_params)
  if errors.any?
    raise ArgumentError, "Invalid create_message parameters: #{errors.join(", ")}"
  end

  create_message(safe_params)
end