Class: Ask::MCP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/mcp/client.rb

Constant Summary collapse

PROTOCOL_VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ask/mcp/client.rb', line 10

def initialize(transport, options = {})
  @transport = transport
  @options = options
  @capabilities = {}
  @server_info = {}
  @tools_cache = nil
  @resources_cache = nil
  @prompts_cache = nil
  @pending_requests = {}
  @pending_mutex = Mutex.new
  @pending_condition = ConditionVariable.new
  @next_id = 0
  @initialized = false
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



8
9
10
# File 'lib/ask/mcp/client.rb', line 8

def capabilities
  @capabilities
end

#server_infoObject (readonly)

Returns the value of attribute server_info.



8
9
10
# File 'lib/ask/mcp/client.rb', line 8

def server_info
  @server_info
end

#transportObject (readonly)

Returns the value of attribute transport.



8
9
10
# File 'lib/ask/mcp/client.rb', line 8

def transport
  @transport
end

Instance Method Details

#call_tool(name, arguments = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/ask/mcp/client.rb', line 61

def call_tool(name, arguments = {})
  if @options[:validate] && @tools_cache
    tool = @tools_cache[name]
    if tool && tool.input_schema && !tool.input_schema.empty?
      Validator.new(tool.input_schema).validate!(arguments)
    end
  end
  response = send_request("tools/call", name: name, arguments: arguments)
  response[:content] || response
end

#get_prompt(name, arguments = {}) ⇒ Object



77
78
79
80
# File 'lib/ask/mcp/client.rb', line 77

def get_prompt(name, arguments = {})
  response = send_request("prompts/get", name: name, arguments: arguments)
  response[:messages] || response
end

#initialized?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ask/mcp/client.rb', line 82

def initialized?
  @initialized
end

#promptsObject



53
54
55
56
57
58
59
# File 'lib/ask/mcp/client.rb', line 53

def prompts
  return @prompts_cache if @prompts_cache && !@options[:no_cache]

  response = send_request("prompts/list")
  prompts = (response[:prompts] || []).map { |p| Prompt.from_h(p) }
  @prompts_cache = index_by_name(prompts)
end

#read_resource(uri) ⇒ Object



72
73
74
75
# File 'lib/ask/mcp/client.rb', line 72

def read_resource(uri)
  response = send_request("resources/read", uri: uri)
  response[:contents] || response
end

#resourcesObject



45
46
47
48
49
50
51
# File 'lib/ask/mcp/client.rb', line 45

def resources
  return @resources_cache if @resources_cache && !@options[:no_cache]

  response = send_request("resources/list")
  resources = (response[:resources] || []).map { |r| Resource.from_h(r) }
  @resources_cache = index_by_uri(resources)
end

#startObject



25
26
27
28
29
30
# File 'lib/ask/mcp/client.rb', line 25

def start
  @transport.on_message { |message| handle_message(message) }
  @transport.start
  initialize_session
  self
end

#stopObject



32
33
34
35
# File 'lib/ask/mcp/client.rb', line 32

def stop
  @transport.stop
  @initialized = false
end

#toolsObject



37
38
39
40
41
42
43
# File 'lib/ask/mcp/client.rb', line 37

def tools
  return @tools_cache if @tools_cache && !@options[:no_cache]

  response = send_request("tools/list")
  tools = (response[:tools] || []).map { |t| Tool.from_h(t) }
  @tools_cache = index_by_name(tools)
end