Skip to content

Commit dbcd903

Browse files
committed
Remove read_ruby_file tool as it can be abused
1 parent 9a95003 commit dbcd903

File tree

2 files changed

+0
-108
lines changed

2 files changed

+0
-108
lines changed

lib/ruby_lsp/mcp_server.rb

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,6 @@ def process_jsonrpc_request(json)
167167
},
168168
},
169169
},
170-
{
171-
# This may be redundant to some clients if they can access terminal to cat the files
172-
# but it's useful for some clients that don't have that capability
173-
name: "read_ruby_files",
174-
description: <<~DESCRIPTION,
175-
Read the contents of the given Ruby files, including files from dependencies.
176-
DESCRIPTION
177-
inputSchema: {
178-
type: "object",
179-
properties: {
180-
file_uris: {
181-
type: "array",
182-
items: { type: "string" },
183-
},
184-
},
185-
required: ["file_uris"],
186-
},
187-
},
188170
{
189171
name: "get_methods_details",
190172
description: <<~DESCRIPTION,
@@ -245,8 +227,6 @@ def process_jsonrpc_request(json)
245227
contents = case params[:name]
246228
when "get_classes_and_modules"
247229
handle_get_classes_and_modules(params.dig(:arguments, :query))
248-
when "read_ruby_files"
249-
handle_read_ruby_files(params.dig(:arguments, :file_uris))
250230
when "get_methods_details"
251231
handle_get_methods_details(params.dig(:arguments, :signatures))
252232
when "get_class_module_details"
@@ -316,42 +296,6 @@ def handle_get_classes_and_modules(query)
316296
end
317297
end
318298

319-
#: (Array[String]) -> Array[Hash[Symbol, untyped]]
320-
def handle_read_ruby_files(file_uris)
321-
file_uris.map do |file_uri|
322-
file_uri_obj = URI(file_uri)
323-
file_path = file_uri_obj.path
324-
next unless file_path
325-
326-
begin
327-
file_content = File.read(file_path)
328-
{
329-
type: "text",
330-
text: {
331-
file_path: file_path,
332-
file_content: file_content,
333-
}.to_yaml,
334-
}
335-
rescue Errno::ENOENT
336-
{
337-
type: "text",
338-
text: {
339-
file_path: file_path,
340-
error: "File not found",
341-
}.to_yaml,
342-
}
343-
rescue => e
344-
{
345-
type: "text",
346-
text: {
347-
file_path: file_path,
348-
error: "Error reading file: #{e.message}",
349-
}.to_yaml,
350-
}
351-
end
352-
end.compact
353-
end
354-
355299
#: (Array[String]) -> Array[Hash[Symbol, untyped]]
356300
def handle_get_methods_details(signatures)
357301
signatures.map do |signature|

test/mcp_server_test.rb

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -73,58 +73,6 @@ class Class2; end
7373
MCPServer.const_set(:MAX_CLASSES_TO_RETURN, original_max_classes)
7474
end
7575

76-
def test_handle_read_ruby_files_single_file
77-
file_path = File.join(Dir.pwd, "test_read.rb")
78-
file_content = "# Test content"
79-
File.write(file_path, file_content)
80-
81-
begin
82-
result = @server.send(:handle_read_ruby_files, ["file://#{file_path}"])
83-
expected_yaml = { file_path: file_path, file_content: file_content }.to_yaml
84-
expected_result = [{ type: "text", text: expected_yaml }]
85-
86-
assert_equal(expected_result, result)
87-
ensure
88-
File.delete(file_path) if File.exist?(file_path)
89-
end
90-
end
91-
92-
def test_handle_read_ruby_files_multiple_files
93-
file_path1 = File.join(Dir.pwd, "test_read1.rb")
94-
file_content1 = "# Test content 1"
95-
File.write(file_path1, file_content1)
96-
97-
file_path2 = File.join(Dir.pwd, "test_read2.rb")
98-
file_content2 = "# Test content 2"
99-
File.write(file_path2, file_content2)
100-
101-
begin
102-
result = @server.send(:handle_read_ruby_files, ["file://#{file_path1}", "file://#{file_path2}"])
103-
104-
expected_yaml1 = { file_path: file_path1, file_content: file_content1 }.to_yaml
105-
expected_yaml2 = { file_path: file_path2, file_content: file_content2 }.to_yaml
106-
expected_result = [
107-
{ type: "text", text: expected_yaml1 },
108-
{ type: "text", text: expected_yaml2 },
109-
]
110-
111-
assert_equal(expected_result, result)
112-
ensure
113-
File.delete(file_path1) if File.exist?(file_path1)
114-
File.delete(file_path2) if File.exist?(file_path2)
115-
end
116-
end
117-
118-
def test_handle_read_ruby_files_non_existent_file
119-
non_existent_path = File.join(Dir.pwd, "non_existent.rb")
120-
result = @server.send(:handle_read_ruby_files, ["file://#{non_existent_path}"])
121-
122-
expected_yaml = { file_path: non_existent_path, error: "File not found" }.to_yaml
123-
expected_result = [{ type: "text", text: expected_yaml }]
124-
125-
assert_equal(expected_result, result)
126-
end
127-
12876
def test_handle_get_methods_details_instance_method
12977
uri = URI("file:///fake_instance.rb")
13078
@index.index_single(uri, <<~RUBY)

0 commit comments

Comments
 (0)