context_drop 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +184 -107
- data/SPEC.md +74 -26
- data/app/assets/javascripts/context_drop/context_drop.js +5121 -651
- data/app/controllers/context_drop/agent_controller.rb +170 -0
- data/app/controllers/context_drop/configs_controller.rb +2 -1
- data/app/controllers/context_drop/feedbacks_controller.rb +13 -3
- data/app/controllers/context_drop/mcp_controller.rb +2 -1
- data/app/helpers/context_drop/widget_helper.rb +11 -1
- data/app/javascript/context_drop/__tests__/widget.test.ts +661 -89
- data/app/javascript/context_drop/api-client.ts +130 -11
- data/app/javascript/context_drop/element-picker.ts +185 -33
- data/app/javascript/context_drop/icons.ts +24 -0
- data/app/javascript/context_drop/markdown.ts +146 -0
- data/app/javascript/context_drop/sanitizer.ts +413 -2
- data/app/javascript/context_drop/screenshot.ts +102 -8
- data/app/javascript/context_drop/styles.ts +1528 -252
- data/app/javascript/context_drop/types.ts +147 -0
- data/app/javascript/context_drop/widget.ts +2591 -290
- data/app/models/context_drop/feedback.rb +23 -0
- data/app/views/context_drop/dashboard/feedbacks/index.html.erb +5 -0
- data/app/views/context_drop/dashboard/feedbacks/show.html.erb +10 -0
- data/app/views/layouts/context_drop/application.html.erb +1 -0
- data/config/routes.rb +11 -0
- data/db/migrate/20261112000004_add_environment_to_context_drop_feedbacks.rb +10 -0
- data/lib/context_drop/agent/key_store.rb +290 -0
- data/lib/context_drop/agent/llm_client.rb +413 -0
- data/lib/context_drop/agent/orchestrator.rb +314 -0
- data/lib/context_drop/agent/workspace_sandbox.rb +499 -0
- data/lib/context_drop/configuration.rb +216 -3
- data/lib/context_drop/mcp/dispatcher.rb +71 -29
- data/lib/context_drop/mcp/environment_router.rb +115 -0
- data/lib/context_drop/mcp/prompts/investigate_feedback.rb +87 -8
- data/lib/context_drop/mcp/remote_client.rb +120 -0
- data/lib/context_drop/mcp/server.rb +9 -2
- data/lib/context_drop/mcp/tools/get_feedback.rb +50 -4
- data/lib/context_drop/mcp/tools/get_feedback_attachment.rb +26 -4
- data/lib/context_drop/mcp/tools/list_environments.rb +60 -0
- data/lib/context_drop/mcp/tools/list_feedbacks.rb +174 -22
- data/lib/context_drop/mcp/tools/update_feedback_status.rb +45 -4
- data/lib/context_drop/version.rb +1 -1
- data/lib/context_drop.rb +8 -2
- data/lib/generators/context_drop/install/templates/initializer.rb.tt +29 -2
- metadata +11 -1
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
3
5
|
module ContextDrop
|
|
4
6
|
class Configuration
|
|
5
7
|
VALID_POSITIONS = %i[bottom_right bottom_left top_right top_left].freeze
|
|
8
|
+
VALID_SELECTION_MODES = %i[auto smart verbose].freeze
|
|
9
|
+
LOOPBACK_IPS = %w[127.0.0.1 ::1 ::ffff:127.0.0.1 0:0:0:0:0:0:0:1].freeze
|
|
10
|
+
LOOPBACK_HOST_PATTERN = /\A(?:localhost|127\.0\.0\.1|\[::1\]|[a-z0-9-]+\.localhost)(?::\d+)?\z/i
|
|
6
11
|
|
|
7
12
|
MANDATORY_EXCLUDED_SELECTORS = [
|
|
8
13
|
"[data-feedback-ignore]",
|
|
@@ -20,6 +25,7 @@ module ContextDrop
|
|
|
20
25
|
].freeze
|
|
21
26
|
|
|
22
27
|
attr_accessor :enabled,
|
|
28
|
+
:environment,
|
|
23
29
|
:storage,
|
|
24
30
|
:active_storage_service,
|
|
25
31
|
:allow_anonymous,
|
|
@@ -31,6 +37,7 @@ module ContextDrop
|
|
|
31
37
|
:element_selection,
|
|
32
38
|
:screenshot_capture,
|
|
33
39
|
:element_capture_padding,
|
|
40
|
+
:smart_capture_padding,
|
|
34
41
|
:excluded_selectors,
|
|
35
42
|
:current_submitter,
|
|
36
43
|
:authorize_submission,
|
|
@@ -39,13 +46,20 @@ module ContextDrop
|
|
|
39
46
|
:on_feedback_created,
|
|
40
47
|
:mcp_enabled,
|
|
41
48
|
:mcp_api_key,
|
|
42
|
-
:authorize_mcp
|
|
49
|
+
:authorize_mcp,
|
|
50
|
+
:mcp_environments,
|
|
51
|
+
:local_agent_enabled,
|
|
52
|
+
:enable_local_agent,
|
|
53
|
+
:local_agent_workspace_root,
|
|
54
|
+
:local_agent_key_store_path
|
|
43
55
|
|
|
44
|
-
attr_reader :position
|
|
56
|
+
attr_reader :position, :selection_mode
|
|
45
57
|
|
|
46
58
|
def initialize
|
|
47
59
|
@enabled = false
|
|
60
|
+
@environment = nil # Auto-resolves from Rails.env / RAILS_ENV / RACK_ENV
|
|
48
61
|
@position = :bottom_right
|
|
62
|
+
@selection_mode = :auto # :smart in production, :verbose in development/staging
|
|
49
63
|
@storage = :active_storage
|
|
50
64
|
@active_storage_service = nil
|
|
51
65
|
@allow_anonymous = true
|
|
@@ -59,6 +73,7 @@ module ContextDrop
|
|
|
59
73
|
@element_selection = true
|
|
60
74
|
@screenshot_capture = true
|
|
61
75
|
@element_capture_padding = 16
|
|
76
|
+
@smart_capture_padding = 84
|
|
62
77
|
|
|
63
78
|
@excluded_selectors = MANDATORY_EXCLUDED_SELECTORS.dup
|
|
64
79
|
|
|
@@ -81,6 +96,26 @@ module ContextDrop
|
|
|
81
96
|
@mcp_enabled = false
|
|
82
97
|
@mcp_api_key = nil
|
|
83
98
|
@authorize_mcp = nil
|
|
99
|
+
@mcp_environments = {}
|
|
100
|
+
|
|
101
|
+
@local_agent_enabled = ENV.fetch("CONTEXT_DROP_LOCAL_AGENT_ENABLED", "false") == "true"
|
|
102
|
+
@enable_local_agent = @local_agent_enabled
|
|
103
|
+
@local_agent_workspace_root = nil
|
|
104
|
+
@local_agent_key_store_path = nil
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def enable_local_agent
|
|
108
|
+
@local_agent_enabled
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def enable_local_agent=(value)
|
|
112
|
+
@local_agent_enabled = Boolean(value)
|
|
113
|
+
@enable_local_agent = @local_agent_enabled
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def local_agent_enabled=(value)
|
|
117
|
+
@local_agent_enabled = Boolean(value)
|
|
118
|
+
@enable_local_agent = @local_agent_enabled
|
|
84
119
|
end
|
|
85
120
|
|
|
86
121
|
def position=(value)
|
|
@@ -92,6 +127,152 @@ module ContextDrop
|
|
|
92
127
|
@position = sym
|
|
93
128
|
end
|
|
94
129
|
|
|
130
|
+
def selection_mode=(value)
|
|
131
|
+
sym = value.nil? ? :auto : value.to_s.strip.downcase.to_sym
|
|
132
|
+
unless VALID_SELECTION_MODES.include?(sym)
|
|
133
|
+
raise ArgumentError, "Invalid ContextDrop selection_mode #{value.inspect}. Must be one of: #{VALID_SELECTION_MODES.join(', ')}"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
@selection_mode = sym
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def resolved_selection_mode(override = nil)
|
|
140
|
+
candidate = override.nil? ? @selection_mode : override.to_s.strip.downcase.to_sym
|
|
141
|
+
return "smart" if candidate == :smart
|
|
142
|
+
return "verbose" if candidate == :verbose
|
|
143
|
+
|
|
144
|
+
rails_production? ? "smart" : "verbose"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def resolved_environment
|
|
148
|
+
explicit = @environment.to_s.strip
|
|
149
|
+
return explicit.downcase unless explicit.empty?
|
|
150
|
+
|
|
151
|
+
if defined?(Rails) && Rails.respond_to?(:env) && Rails.env
|
|
152
|
+
env_str = Rails.env.to_s.strip
|
|
153
|
+
return env_str.downcase unless env_str.empty?
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
fallback = (ENV["CONTEXT_DROP_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development").to_s.strip
|
|
157
|
+
fallback.empty? ? "development" : fallback.downcase
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def rails_production?
|
|
161
|
+
(defined?(Rails) && Rails.respond_to?(:env) && Rails.env&.production?) || resolved_environment == "production"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def local_environment?
|
|
165
|
+
return false if rails_production?
|
|
166
|
+
|
|
167
|
+
resolved_environment == "development"
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Local agentic coding sidebar is strictly restricted to non-production `development` environments.
|
|
171
|
+
def local_agent_allowed?
|
|
172
|
+
return false if rails_production?
|
|
173
|
+
|
|
174
|
+
Boolean(@local_agent_enabled) && local_environment?
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def resolved_workspace_root
|
|
178
|
+
explicit = @local_agent_workspace_root.to_s.strip
|
|
179
|
+
return explicit unless explicit.empty?
|
|
180
|
+
|
|
181
|
+
if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
|
|
182
|
+
Rails.root.to_s
|
|
183
|
+
else
|
|
184
|
+
Dir.pwd
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def resolved_key_store_path
|
|
189
|
+
explicit = @local_agent_key_store_path.to_s.strip
|
|
190
|
+
return explicit unless explicit.empty?
|
|
191
|
+
|
|
192
|
+
File.join(resolved_workspace_root, "tmp", "context_drop_byok.enc")
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# Verifies that an HTTP request originates from strict local loopback without external proxy forwarding
|
|
196
|
+
# or non-local Host headers (DNS-rebinding defense).
|
|
197
|
+
def loopback_request?(request)
|
|
198
|
+
return false if request.nil?
|
|
199
|
+
|
|
200
|
+
remote_addr = (request.respond_to?(:remote_addr) ? request.remote_addr : nil).to_s.strip
|
|
201
|
+
remote_ip = (request.respond_to?(:remote_ip) ? request.remote_ip : nil).to_s.strip
|
|
202
|
+
|
|
203
|
+
addr_ok = (!remote_addr.empty? && LOOPBACK_IPS.include?(remote_addr)) ||
|
|
204
|
+
(remote_addr.empty? && LOOPBACK_IPS.include?(remote_ip))
|
|
205
|
+
ip_ok = remote_ip.empty? || LOOPBACK_IPS.include?(remote_ip)
|
|
206
|
+
return false unless addr_ok && ip_ok
|
|
207
|
+
|
|
208
|
+
if request.respond_to?(:headers) && request.headers
|
|
209
|
+
xff = request.headers["X-Forwarded-For"].to_s.strip
|
|
210
|
+
unless xff.empty?
|
|
211
|
+
hops = xff.split(",").map(&:strip).reject(&:empty?)
|
|
212
|
+
return false unless hops.all? { |hop| LOOPBACK_IPS.include?(hop) }
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
host_with_port = if request.respond_to?(:host_with_port) && request.host_with_port
|
|
217
|
+
request.host_with_port.to_s
|
|
218
|
+
elsif request.respond_to?(:host) && request.host
|
|
219
|
+
request.host.to_s
|
|
220
|
+
else
|
|
221
|
+
""
|
|
222
|
+
end
|
|
223
|
+
return false if !host_with_port.empty? && !LOOPBACK_HOST_PATTERN.match?(host_with_port)
|
|
224
|
+
|
|
225
|
+
true
|
|
226
|
+
rescue StandardError
|
|
227
|
+
false
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Returns normalized remote MCP environment definitions merged from `config.mcp_environments`,
|
|
231
|
+
# `ENV["CONTEXT_DROP_REMOTE_ENVIRONMENTS"]`, and `ENV["CONTEXT_DROP_<ENV>_MCP_URL"]` pairs.
|
|
232
|
+
def effective_mcp_environments(env_vars = ENV)
|
|
233
|
+
merged = {}
|
|
234
|
+
|
|
235
|
+
raw_json = env_vars["CONTEXT_DROP_REMOTE_ENVIRONMENTS"].to_s.strip
|
|
236
|
+
unless raw_json.empty?
|
|
237
|
+
begin
|
|
238
|
+
parsed = JSON.parse(raw_json)
|
|
239
|
+
if parsed.is_a?(Hash)
|
|
240
|
+
parsed.each do |name, cfg|
|
|
241
|
+
norm = normalize_mcp_env_entry(name, cfg)
|
|
242
|
+
merged[norm[:name]] = norm[:config] if norm
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
rescue JSON::ParserError
|
|
246
|
+
# Ignore malformed JSON env variable
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
env_vars.each do |key, val|
|
|
251
|
+
match = /\ACONTEXT_DROP_([A-Z0-9_]+)_MCP_URL\z/.match(key.to_s)
|
|
252
|
+
next unless match
|
|
253
|
+
|
|
254
|
+
env_token_key = match[1]
|
|
255
|
+
next if env_token_key.empty?
|
|
256
|
+
|
|
257
|
+
url_str = val.to_s.strip
|
|
258
|
+
next if url_str.empty?
|
|
259
|
+
|
|
260
|
+
env_name = env_token_key.downcase
|
|
261
|
+
api_key = env_vars["CONTEXT_DROP_#{env_token_key}_MCP_API_KEY"].to_s.strip
|
|
262
|
+
norm = normalize_mcp_env_entry(env_name, { url: url_str, api_key: api_key })
|
|
263
|
+
merged[norm[:name]] = norm[:config] if norm
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
if @mcp_environments.is_a?(Hash)
|
|
267
|
+
@mcp_environments.each do |name, cfg|
|
|
268
|
+
norm = normalize_mcp_env_entry(name, cfg)
|
|
269
|
+
merged[norm[:name]] = norm[:config] if norm
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
merged
|
|
274
|
+
end
|
|
275
|
+
|
|
95
276
|
def effective_excluded_selectors
|
|
96
277
|
(MANDATORY_EXCLUDED_SELECTORS + Array(@excluded_selectors).map(&:to_s)).uniq
|
|
97
278
|
end
|
|
@@ -118,15 +299,26 @@ module ContextDrop
|
|
|
118
299
|
|
|
119
300
|
def public_widget_config(overrides = {})
|
|
120
301
|
resolved_position = overrides[:position] ? normalize_position(overrides[:position]) : @position
|
|
302
|
+
agent_active = if overrides.key?(:agent_enabled)
|
|
303
|
+
local_agent_allowed? && !!overrides[:agent_enabled]
|
|
304
|
+
else
|
|
305
|
+
local_agent_allowed?
|
|
306
|
+
end
|
|
121
307
|
|
|
122
308
|
{
|
|
123
309
|
enabled: !!@enabled,
|
|
310
|
+
environment: resolved_environment,
|
|
124
311
|
position: resolved_position.to_s,
|
|
125
312
|
placeholder: (overrides[:placeholder] || "What would you like to tell us?").to_s,
|
|
313
|
+
agent: {
|
|
314
|
+
enabled: agent_active
|
|
315
|
+
},
|
|
126
316
|
features: {
|
|
127
317
|
element_selection: !!@element_selection,
|
|
318
|
+
selection_mode: resolved_selection_mode(overrides[:selection_mode]),
|
|
128
319
|
screenshot_capture: !!@screenshot_capture,
|
|
129
|
-
element_capture_padding: @element_capture_padding.to_i
|
|
320
|
+
element_capture_padding: @element_capture_padding.to_i,
|
|
321
|
+
smart_capture_padding: @smart_capture_padding.to_i
|
|
130
322
|
},
|
|
131
323
|
limits: {
|
|
132
324
|
max_attachments: @max_attachments.to_i,
|
|
@@ -143,6 +335,27 @@ module ContextDrop
|
|
|
143
335
|
|
|
144
336
|
private
|
|
145
337
|
|
|
338
|
+
def normalize_mcp_env_entry(name, raw_cfg)
|
|
339
|
+
env_name = name.to_s.strip.downcase
|
|
340
|
+
return nil if env_name.empty? || %w[local all].include?(env_name)
|
|
341
|
+
return nil unless raw_cfg.is_a?(Hash)
|
|
342
|
+
|
|
343
|
+
str_cfg = raw_cfg.transform_keys(&:to_s)
|
|
344
|
+
url = str_cfg["url"].to_s.strip
|
|
345
|
+
return nil if url.empty?
|
|
346
|
+
|
|
347
|
+
headers = str_cfg["headers"].is_a?(Hash) ? str_cfg["headers"].transform_keys(&:to_s) : {}
|
|
348
|
+
{
|
|
349
|
+
name: env_name,
|
|
350
|
+
config: {
|
|
351
|
+
url: url,
|
|
352
|
+
api_key: str_cfg["api_key"].to_s.strip,
|
|
353
|
+
headers: headers,
|
|
354
|
+
allow_insecure_http: !!str_cfg["allow_insecure_http"]
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
end
|
|
358
|
+
|
|
146
359
|
def normalize_position(pos)
|
|
147
360
|
sym = pos.to_s.strip.to_sym
|
|
148
361
|
VALID_POSITIONS.include?(sym) ? sym : @position
|
|
@@ -8,6 +8,7 @@ module ContextDrop
|
|
|
8
8
|
PROTOCOL_VERSION = "2025-03-26"
|
|
9
9
|
|
|
10
10
|
TOOLS = {
|
|
11
|
+
"list_environments" => ContextDrop::MCP::Tools::ListEnvironments,
|
|
11
12
|
"list_feedbacks" => ContextDrop::MCP::Tools::ListFeedbacks,
|
|
12
13
|
"get_feedback" => ContextDrop::MCP::Tools::GetFeedback,
|
|
13
14
|
"get_feedback_attachment" => ContextDrop::MCP::Tools::GetFeedbackAttachment,
|
|
@@ -19,10 +20,16 @@ module ContextDrop
|
|
|
19
20
|
}.freeze
|
|
20
21
|
|
|
21
22
|
RESOURCES = [
|
|
23
|
+
{
|
|
24
|
+
uri: "contextdrop://environments",
|
|
25
|
+
name: "Connected ContextDrop Environments",
|
|
26
|
+
description: "Host Rails environment and all federated remote ContextDrop environments.",
|
|
27
|
+
mimeType: "application/json"
|
|
28
|
+
},
|
|
22
29
|
{
|
|
23
30
|
uri: "contextdrop://feedbacks/new",
|
|
24
|
-
name: "Unresolved New Feedbacks",
|
|
25
|
-
description: "List of
|
|
31
|
+
name: "Unresolved New Feedbacks (All Connected Environments)",
|
|
32
|
+
description: "List of unresolved ContextDrop feedback records currently in 'new' status across local and federated environments.",
|
|
26
33
|
mimeType: "application/json"
|
|
27
34
|
}
|
|
28
35
|
].freeze
|
|
@@ -31,7 +38,19 @@ module ContextDrop
|
|
|
31
38
|
{
|
|
32
39
|
uriTemplate: "contextdrop://feedbacks/{id}",
|
|
33
40
|
name: "Feedback Record by ID",
|
|
34
|
-
description: "Full ContextDrop feedback submission, DOM element snapshots, and attachment metadata for a
|
|
41
|
+
description: "Full ContextDrop feedback submission, DOM element snapshots, and attachment metadata for a local ID or '<env>:<id>'.",
|
|
42
|
+
mimeType: "application/json"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
uriTemplate: "contextdrop://{environment}/feedbacks/new",
|
|
46
|
+
name: "Unresolved New Feedbacks by Environment",
|
|
47
|
+
description: "Unresolved 'new' feedback records scoped to a specific environment ('local', 'staging', 'production', 'all').",
|
|
48
|
+
mimeType: "application/json"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
uriTemplate: "contextdrop://{environment}/feedbacks/{id}",
|
|
52
|
+
name: "Feedback Record by Environment and ID",
|
|
53
|
+
description: "Full ContextDrop feedback submission for a specific environment and ID.",
|
|
35
54
|
mimeType: "application/json"
|
|
36
55
|
}
|
|
37
56
|
].freeze
|
|
@@ -74,7 +93,9 @@ module ContextDrop
|
|
|
74
93
|
},
|
|
75
94
|
serverInfo: {
|
|
76
95
|
name: "context_drop",
|
|
77
|
-
version: ContextDrop::VERSION
|
|
96
|
+
version: ContextDrop::VERSION,
|
|
97
|
+
environment: ContextDrop::MCP::EnvironmentRouter.host_environment,
|
|
98
|
+
connectedEnvironments: ContextDrop::MCP::EnvironmentRouter.available_environments.map { |e| e[:name] }
|
|
78
99
|
}
|
|
79
100
|
})
|
|
80
101
|
when "ping"
|
|
@@ -119,46 +140,67 @@ module ContextDrop
|
|
|
119
140
|
def handle_resource_read(id, params)
|
|
120
141
|
uri = params["uri"].to_s.strip
|
|
121
142
|
case uri
|
|
122
|
-
when "contextdrop://
|
|
123
|
-
records = ContextDrop::Feedback
|
|
124
|
-
.includes(:submitter, :element_references, :attachments)
|
|
125
|
-
.new_records
|
|
126
|
-
.recent
|
|
127
|
-
.limit(50)
|
|
128
|
-
.map { |fb| ContextDrop::MCP::Tools::ListFeedbacks.serialize_summary(fb) }
|
|
143
|
+
when "contextdrop://environments"
|
|
129
144
|
jsonrpc_result(id, {
|
|
130
145
|
contents: [
|
|
131
146
|
{
|
|
132
147
|
uri: uri,
|
|
133
148
|
mimeType: "application/json",
|
|
134
|
-
text: JSON.pretty_generate(
|
|
149
|
+
text: JSON.pretty_generate(
|
|
150
|
+
host_environment: ContextDrop::MCP::EnvironmentRouter.host_environment,
|
|
151
|
+
environments: ContextDrop::MCP::EnvironmentRouter.available_environments
|
|
152
|
+
)
|
|
135
153
|
}
|
|
136
154
|
]
|
|
137
155
|
})
|
|
138
|
-
when
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
.includes(:submitter, :element_references, :attachments)
|
|
142
|
-
.find_by(id: feedback_id)
|
|
143
|
-
unless feedback
|
|
144
|
-
return jsonrpc_error(id, -32_602, "Feedback ##{feedback_id} not found")
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
payload = ContextDrop::MCP::Tools::GetFeedback.serialize_feedback(feedback)
|
|
156
|
+
when "contextdrop://feedbacks/new"
|
|
157
|
+
res = ContextDrop::MCP::Tools::ListFeedbacks.call("status" => "new", "limit" => 50, "environment" => "all")
|
|
158
|
+
text = res[:content].first[:text]
|
|
148
159
|
jsonrpc_result(id, {
|
|
149
|
-
contents: [
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
160
|
+
contents: [{ uri: uri, mimeType: "application/json", text: text }]
|
|
161
|
+
})
|
|
162
|
+
when %r{\Acontextdrop://([a-z0-9_-]+)/feedbacks/new\z}i
|
|
163
|
+
env_name = Regexp.last_match(1).downcase
|
|
164
|
+
res = ContextDrop::MCP::Tools::ListFeedbacks.call("status" => "new", "limit" => 50, "environment" => env_name)
|
|
165
|
+
return jsonrpc_error(id, -32_602, extract_tool_error(res)) if res[:isError]
|
|
166
|
+
|
|
167
|
+
jsonrpc_result(id, {
|
|
168
|
+
contents: [{ uri: uri, mimeType: "application/json", text: res[:content].first[:text] }]
|
|
169
|
+
})
|
|
170
|
+
when %r{\Acontextdrop://feedbacks/([a-z0-9_:-]+)\z}i
|
|
171
|
+
raw_id = Regexp.last_match(1)
|
|
172
|
+
res = ContextDrop::MCP::Tools::GetFeedback.call("id" => raw_id, "include_images" => false)
|
|
173
|
+
return jsonrpc_error(id, -32_602, extract_tool_error(res)) if res[:isError]
|
|
174
|
+
|
|
175
|
+
jsonrpc_result(id, {
|
|
176
|
+
contents: [{ uri: uri, mimeType: "application/json", text: res[:content].first[:text] }]
|
|
177
|
+
})
|
|
178
|
+
when %r{\Acontextdrop://([a-z0-9_-]+)/feedbacks/(\d+)\z}i
|
|
179
|
+
env_name = Regexp.last_match(1).downcase
|
|
180
|
+
feedback_id = Regexp.last_match(2)
|
|
181
|
+
res = ContextDrop::MCP::Tools::GetFeedback.call(
|
|
182
|
+
"id" => feedback_id,
|
|
183
|
+
"environment" => env_name,
|
|
184
|
+
"include_images" => false
|
|
185
|
+
)
|
|
186
|
+
return jsonrpc_error(id, -32_602, extract_tool_error(res)) if res[:isError]
|
|
187
|
+
|
|
188
|
+
jsonrpc_result(id, {
|
|
189
|
+
contents: [{ uri: uri, mimeType: "application/json", text: res[:content].first[:text] }]
|
|
156
190
|
})
|
|
157
191
|
else
|
|
158
192
|
jsonrpc_error(id, -32_602, "Unknown resource URI: #{uri}")
|
|
159
193
|
end
|
|
160
194
|
end
|
|
161
195
|
|
|
196
|
+
def extract_tool_error(res)
|
|
197
|
+
raw = res.dig(:content, 0, :text).to_s
|
|
198
|
+
parsed = JSON.parse(raw)
|
|
199
|
+
parsed["error"] || raw
|
|
200
|
+
rescue StandardError
|
|
201
|
+
raw
|
|
202
|
+
end
|
|
203
|
+
|
|
162
204
|
def handle_prompt_get(id, params)
|
|
163
205
|
prompt_name = params["name"].to_s
|
|
164
206
|
prompt_cls = PROMPTS[prompt_name]
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ContextDrop
|
|
4
|
+
module MCP
|
|
5
|
+
module EnvironmentRouter
|
|
6
|
+
THREAD_LOCAL_ONLY_KEY = :context_drop_mcp_local_only
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
def local_only?
|
|
10
|
+
!!Thread.current[THREAD_LOCAL_ONLY_KEY]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def with_local_only
|
|
14
|
+
previous = Thread.current[THREAD_LOCAL_ONLY_KEY]
|
|
15
|
+
Thread.current[THREAD_LOCAL_ONLY_KEY] = true
|
|
16
|
+
yield
|
|
17
|
+
ensure
|
|
18
|
+
Thread.current[THREAD_LOCAL_ONLY_KEY] = previous
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def host_environment
|
|
22
|
+
ContextDrop.config.resolved_environment
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def remote_environments
|
|
26
|
+
return {} if local_only?
|
|
27
|
+
|
|
28
|
+
ContextDrop.config.effective_mcp_environments.reject do |env_name, _cfg|
|
|
29
|
+
env_name == host_environment
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def available_environments
|
|
34
|
+
envs = [
|
|
35
|
+
{
|
|
36
|
+
name: host_environment,
|
|
37
|
+
aliases: host_environment == "local" ? [] : ["local"],
|
|
38
|
+
mode: "local_db",
|
|
39
|
+
configured: true
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
remote_environments.each do |name, cfg|
|
|
44
|
+
envs << {
|
|
45
|
+
name: name,
|
|
46
|
+
aliases: [],
|
|
47
|
+
mode: "remote_http",
|
|
48
|
+
url: redact_url(cfg[:url]),
|
|
49
|
+
has_api_key: !cfg[:api_key].to_s.strip.empty?,
|
|
50
|
+
configured: true
|
|
51
|
+
}
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
envs
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Parses either (`id: "staging:42"`) or (`id: 42, environment: "staging"`).
|
|
58
|
+
# Returns `[resolved_env_or_nil, raw_record_id_string]`.
|
|
59
|
+
def parse_target(raw_id, explicit_environment = nil)
|
|
60
|
+
env_str = explicit_environment.to_s.strip.downcase
|
|
61
|
+
id_str = raw_id.to_s.strip
|
|
62
|
+
|
|
63
|
+
if id_str.include?(":")
|
|
64
|
+
prefix, rest = id_str.split(":", 2)
|
|
65
|
+
if !prefix.to_s.strip.empty? && !rest.to_s.strip.empty?
|
|
66
|
+
env_str = prefix.strip.downcase if env_str.empty?
|
|
67
|
+
id_str = rest.strip
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
[env_str.empty? ? nil : env_str, id_str]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def local_target?(env_name)
|
|
75
|
+
return true if env_name.nil? || env_name.to_s.strip.empty?
|
|
76
|
+
|
|
77
|
+
norm = env_name.to_s.strip.downcase
|
|
78
|
+
norm == "local" || norm == host_environment
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def remote_config_for(env_name)
|
|
82
|
+
return nil if env_name.nil?
|
|
83
|
+
|
|
84
|
+
norm = env_name.to_s.strip.downcase
|
|
85
|
+
remote_environments[norm]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def record_environment(record)
|
|
89
|
+
if record.respond_to?(:environment_name)
|
|
90
|
+
record.environment_name
|
|
91
|
+
elsif record.respond_to?(:environment) && !record.environment.to_s.strip.empty?
|
|
92
|
+
record.environment.to_s
|
|
93
|
+
elsif record.respond_to?(:metadata) && record.metadata.is_a?(Hash) && !record.metadata["environment"].to_s.strip.empty?
|
|
94
|
+
record.metadata["environment"].to_s
|
|
95
|
+
else
|
|
96
|
+
host_environment
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def qualify_id(env_name, id)
|
|
101
|
+
"#{env_name}:#{id}"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
private
|
|
105
|
+
|
|
106
|
+
def redact_url(url_str)
|
|
107
|
+
uri = URI.parse(url_str.to_s)
|
|
108
|
+
"#{uri.scheme}://#{uri.host}#{uri.port && ![80, 443].include?(uri.port) ? ":#{uri.port}" : ''}#{uri.path}"
|
|
109
|
+
rescue StandardError
|
|
110
|
+
url_str.to_s
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|