Module: ActionMCP::Server::Tools

Included in:
TransportHandler
Defined in:
lib/action_mcp/server/tools.rb

Instance Method Summary collapse

Instance Method Details

#send_tools_call(request_id, tool_name, arguments, _meta = {}, task_params = nil) ⇒ Object



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
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
# File 'lib/action_mcp/server/tools.rb', line 28

def send_tools_call(request_id, tool_name, arguments, _meta = {}, task_params = nil)
  # Find tool in session's registry
  tool_class = session.registered_tools.find { |t| t.tool_name == tool_name }

  unless tool_class
    Rails.logger.error "Tool not found: #{tool_name}. Registered tools: #{session.registered_tools.map(&:tool_name).join(', ')}"
    send_jsonrpc_error(request_id, :method_not_found,
                       "Tool '#{tool_name}' not found or not registered for this session")
    return
  end

  # Check if tool requires consent and if consent is granted
  if tool_class.respond_to?(:requires_consent?) && tool_class.requires_consent? && !session.consent_granted_for?(tool_name)
    # Use custom error response for consent required (-32002)
    error = {
      code: -32_002,
      message: "Consent required for tool '#{tool_name}'"
    }
    send_jsonrpc_response(request_id, error: error)
    return
  end

  task_support = tool_task_support(tool_class)
  task_requested = !task_params.nil?

  if task_requested && !tasks_enabled?
    send_jsonrpc_error(request_id, :method_not_found,
                       "Task-augmented execution is not available for this session")
    return
  end

  if task_requested
    unless task_params.respond_to?(:to_h)
      send_jsonrpc_error(request_id, :invalid_params, "Task parameters must be an object")
      return
    end

    task_params = task_params.to_h

    if task_support == :forbidden
      send_jsonrpc_error(request_id, :method_not_found,
                         "Tool '#{tool_name}' does not support task-augmented execution")
      return
    end

    handle_task_augmented_tool_call(request_id, tool_name, arguments, _meta, task_params)
    return
  end

  if !task_requested && task_support == :required
    send_jsonrpc_error(request_id, :method_not_found,
                       "Tool '#{tool_name}' requires task-augmented execution")
    return
  end

  # Standard synchronous execution
  execute_tool_synchronously(request_id, tool_class, tool_name, arguments, _meta)
end

#send_tools_list(request_id, params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/action_mcp/server/tools.rb', line 6

def send_tools_list(request_id, params = {})
  protocol_version = session.protocol_version
  progress_token = params.dig("_meta", "progressToken")

  if progress_token
    send_progress_notification(progressToken: progress_token, progress: 0, message: "Starting tools list retrieval")
  end

  page, next_cursor = paginate(session.registered_tools, cursor: params["cursor"])

  result = { tools: page.map { |t| t.to_h(protocol_version: protocol_version) } }
  result[:nextCursor] = next_cursor if next_cursor

  if progress_token
    send_progress_notification(progressToken: progress_token, progress: 100, message: "Tools list retrieval complete")
  end

  send_jsonrpc_response(request_id, result: result)
rescue Server::CursorError => e
  send_jsonrpc_error(request_id, :invalid_params, e.message)
end