Class: Ask::MCP::Client
- Inherits:
-
Object
- Object
- Ask::MCP::Client
- Defined in:
- lib/ask/mcp/client.rb
Constant Summary collapse
- PROTOCOL_VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#server_info ⇒ Object
readonly
Returns the value of attribute server_info.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
- #call_tool(name, arguments = {}) ⇒ Object
- #get_prompt(name, arguments = {}) ⇒ Object
-
#initialize(transport, options = {}) ⇒ Client
constructor
A new instance of Client.
- #initialized? ⇒ Boolean
- #prompts ⇒ Object
- #read_resource(uri) ⇒ Object
- #resources ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #tools ⇒ Object
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, = {}) @transport = transport @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
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
8 9 10 |
# File 'lib/ask/mcp/client.rb', line 8 def capabilities @capabilities end |
#server_info ⇒ Object (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 |
#transport ⇒ Object (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
82 83 84 |
# File 'lib/ask/mcp/client.rb', line 82 def initialized? @initialized end |
#prompts ⇒ Object
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 |
#resources ⇒ Object
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 |
#start ⇒ Object
25 26 27 28 29 30 |
# File 'lib/ask/mcp/client.rb', line 25 def start @transport. { || () } @transport.start initialize_session self end |
#stop ⇒ Object
32 33 34 35 |
# File 'lib/ask/mcp/client.rb', line 32 def stop @transport.stop @initialized = false end |