Class: Utcp::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
# File 'lib/utcp/client.rb', line 28

def initialize(config = {})
  @config = config || {}
  @repo = ToolRepository.new
  env_files = Array(@config["load_variables_from"] || @config[:load_variables_from] || [".env"])
  env_files.each { |f| Utils::EnvLoader.load_file(f) }
end

Instance Attribute Details

#repoObject (readonly)

Returns the value of attribute repo.



35
36
37
# File 'lib/utcp/client.rb', line 35

def repo
  @repo
end

Class Method Details

.create(config = {}) ⇒ Object



24
25
26
# File 'lib/utcp/client.rb', line 24

def self.create(config = {})
  new(config).tap(&:load_providers!)
end

Instance Method Details

#call_tool(full_tool_name, arguments = {}, stream: false, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/utcp/client.rb', line 83

def call_tool(full_tool_name, arguments = {}, stream: false, &block)
  t = @repo.find(full_tool_name)
  p = t.provider || {}
  type = (p["provider_type"] || "http").downcase
  auth = Auth.from_hash(p["auth"])

  case type
  when "http"
    exec = Providers::HttpProvider.new(
      name: full_tool_name,
      url: p["url"],
      http_method: p["http_method"] || "GET",
      content_type: p["content_type"] || "application/json",
      headers: p["headers"] || {},
      manual: false,
      auth: auth,
      body_field: p["body_field"]
    )
    exec.call_tool(t, arguments)

  when "sse"
    raise ConfigError, "Streaming requires a block for SSE" if stream && !block_given?
    exec = Providers::SseProvider.new(name: full_tool_name, auth: auth)
    exec.call_tool(t, arguments, &block)

  when "http_stream"
    raise ConfigError, "Streaming requires a block for http_stream" if stream && !block_given?
    exec = Providers::HttpStreamProvider.new(name: full_tool_name, auth: auth)
    exec.call_tool(t, arguments, &block)

  when "websocket"
    exec = Providers::WebSocketProvider.new(name: full_tool_name, auth: auth)
    exec.call_tool(t, arguments, &block)

  when "graphql"
    exec = Providers::GraphQLProvider.new(name: full_tool_name, auth: auth)
    exec.call_tool(t, arguments, &block)

  when "tcp"
    exec = Providers::TcpProvider.new(name: full_tool_name)
    exec.call_tool(t, arguments, &block)

  when "udp"
    exec = Providers::UdpProvider.new(name: full_tool_name)
    exec.call_tool(t, arguments, &block)

  when "cli"
    exec = Providers::CliProvider.new(name: full_tool_name)
    exec.call_tool(t, arguments, &block)

  when "mcp"
    exec = Providers::McpProvider.new(
      name: full_tool_name,
      url: p["url"],
      headers: p["headers"] || {},
      auth: auth
    )
    exec.call_tool(t, arguments, &block)

  else
    raise ConfigError, "Unsupported execution provider type: #{type}"
  end
end

#load_providers!Object

Raises:



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

def load_providers!
  path = @config["providers_file_path"] || @config[:providers_file_path]
  raise ConfigError, "providers_file_path required" unless path && File.file?(path)
  arr = JSON.parse(File.read(path))
  arr.each { |prov| register_manual_provider(prov) }
  self
end

#register_manual_provider(prov) ⇒ Object



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
# File 'lib/utcp/client.rb', line 45

def register_manual_provider(prov)
  name = prov["name"] || prov[:name] || "provider"
  type = (prov["provider_type"] || prov[:provider_type] || "http").downcase
  auth = Auth.from_hash(prov["auth"] || prov[:auth])

  tools = case type
  when "http"
    Providers::HttpProvider
      .new(name: name,
           url: prov["url"] || prov[:url],
           http_method: prov["http_method"] || prov[:http_method] || "GET",
           content_type: prov["content_type"] || "application/json",
           headers: prov["headers"] || {},
           manual: true,
           auth: auth)
      .discover_tools!
  when "mcp"
    Providers::McpProvider
      .new(name: name,
           url: prov["url"] || prov[:url],
           headers: prov["headers"] || {},
           auth: auth,
           manual: true,
           discovery_path: prov["discovery_path"] || "/manual")
      .discover_tools!
  when "text"
    manual_path = prov["file_path"] || prov[:file_path]
    raise ConfigError, "text provider missing file_path" unless manual_path && File.file?(manual_path)
    manual = JSON.parse(File.read(manual_path))
    to_tools(manual)
  else
    raise ConfigError, "Unsupported manual provider type: #{type}"
  end

  @repo.save_provider_with_tools(name, tools)
  tools
end

#search_tools(query, limit: 5) ⇒ Object



147
148
149
# File 'lib/utcp/client.rb', line 147

def search_tools(query, limit: 5)
  Search.new(@repo).search(query, limit: limit)
end