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

def send_tools_call(request_id, tool_name, arguments, _meta = {})
  # 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

  # Check for task-augmented execution (MCP 2025-11-25)
  task_params = _meta["task"] || _meta[:task]
  if task_params && tasks_enabled?
    handle_task_augmented_tool_call(request_id, tool_name, arguments, _meta, task_params)
    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