|
15 | 15 | (some #(fs/starts-with? path (shared/uri->filename (:uri %))) |
16 | 16 | (:workspace-folders db))) |
17 | 17 |
|
18 | | -(defn ^:private list-allowed-directories [_arguments db] |
19 | | - (single-text-content |
20 | | - (str "Allowed directories:\n" |
21 | | - (string/join "\n" |
22 | | - (map (comp shared/uri->filename :uri) (:workspace-folders db)))))) |
23 | | - |
24 | 18 | (defn ^:private invalid-arguments [arguments validator] |
25 | 19 | (first (keep (fn [[key pred error-msg]] |
26 | 20 | (let [value (get arguments key)] |
27 | 21 | (when-not (pred value) |
28 | | - (single-text-content (string/replace error-msg (str "$" key) value))))) |
| 22 | + (single-text-content (string/replace error-msg (str "$" key) value) :error)))) |
29 | 23 | validator))) |
30 | 24 |
|
| 25 | +(defn ^:private path-validations [db] |
| 26 | + [["path" fs/exists? "$path is not a valid path"] |
| 27 | + ["path" (partial allowed-path? db) "Access denied - path $path outside allowed directories"]]) |
| 28 | + |
| 29 | +(defn ^:private list-allowed-directories [_arguments db] |
| 30 | + (single-text-content |
| 31 | + (str "Allowed directories:\n" |
| 32 | + (string/join "\n" |
| 33 | + (map (comp shared/uri->filename :uri) (:workspace-folders db)))))) |
| 34 | + |
31 | 35 | (defn ^:private list-directory [arguments db] |
32 | 36 | (let [path (delay (fs/canonicalize (get arguments "path")))] |
33 | | - (or (invalid-arguments arguments [["path" fs/regular-file? "$path is not a valid path"] |
34 | | - ["path" (partial allowed-path? db) "Access denied - path $path outside allowed directories"]]) |
| 37 | + (or (invalid-arguments arguments (path-validations db)) |
35 | 38 | (single-text-content |
36 | 39 | (reduce |
37 | 40 | (fn [out path] |
|
42 | 45 | "" |
43 | 46 | (fs/list-dir @path)))))) |
44 | 47 |
|
| 48 | +(defn ^:private read-file [arguments db] |
| 49 | + (or (invalid-arguments arguments (concat (path-validations db) |
| 50 | + [["path" fs/readable? "File $path is not readable"]])) |
| 51 | + (single-text-content (slurp (fs/file (fs/canonicalize (get arguments "path"))))))) |
| 52 | + |
45 | 53 | (def definitions |
46 | 54 | {"list_allowed_directories" |
47 | | - {:name "list_allowed_directories" |
48 | | - :description (str "Returns the list of directories that this server is allowed to access." |
| 55 | + {:description (str "Returns the list of directories that this server is allowed to access. " |
49 | 56 | "Use this to understand which directories are available before trying to access files.") |
50 | 57 | :parameters {:type "object" |
51 | 58 | :properties {} |
52 | 59 | :required []} |
53 | 60 | :handler #'list-allowed-directories} |
54 | 61 | "list_directory" |
55 | | - {:name "list_directory" |
56 | | - :description (str "Get a detailed listing of all files and directories in a specified path. " |
| 62 | + {:description (str "Get a detailed listing of all files and directories in a specified path. " |
57 | 63 | "Results clearly distinguish between files and directories with [FILE] and [DIR] " |
58 | 64 | "prefixes. This tool is essential for understanding directory structure and " |
59 | 65 | "finding specific files within a directory. Only works within workspace root.") |
60 | 66 | :parameters {:type "object" |
61 | 67 | :properties {"path" {:type "string" |
62 | | - :description "The path to the directory to list."}} |
| 68 | + :description "The absolute path to the directory to list."}} |
| 69 | + :required ["path"]} |
| 70 | + :handler #'list-directory} |
| 71 | + "read_file" |
| 72 | + {:description (str "Read the complete contents of a file from the file system. " |
| 73 | + "Handles various text encodings and provides detailed error messages " |
| 74 | + "if the file cannot be read. Use this tool when you need to examine " |
| 75 | + "the contents of a single file. Use the 'head' parameter to read only " |
| 76 | + "the first N lines of a file, or the 'tail' parameter to read only " |
| 77 | + "the last N lines of a file. Only works within allowed directories.") |
| 78 | + :parameters {:type "object" |
| 79 | + :properties {"path" {:type "string" |
| 80 | + :description "The absolute path to the file to read."} |
| 81 | + "head" {:type "number" |
| 82 | + :description "If provided, returns only the first N lines of the file"} |
| 83 | + "tail" {:type "number" |
| 84 | + :description "If provided, returns only the last N lines of the file"}} |
63 | 85 | :required ["path"]} |
64 | | - :handler #'list-directory}}) |
| 86 | + :handler #'read-file}}) |
0 commit comments