|
| 1 | +--- |
| 2 | +title: "File System" |
| 3 | +description: "Client filesystem access methods" |
| 4 | +--- |
| 5 | + |
| 6 | +The filesystem methods allow Agents to read and write text files within the Client's environment. These methods enable Agents to access unsaved editor state and allow Clients to track file modifications made during agent execution. |
| 7 | + |
| 8 | +## Checking Support |
| 9 | + |
| 10 | +Before attempting to use filesystem methods, Agents **MUST** verify that the Client supports these capabilities by checking the [Client Capabilities](./initialization#client-capabilities) field in the `initialize` response: |
| 11 | + |
| 12 | +```json highlight={8,9} |
| 13 | +{ |
| 14 | + "jsonrpc": "2.0", |
| 15 | + "id": 0, |
| 16 | + "result": { |
| 17 | + "protocolVersion": 1, |
| 18 | + "clientCapabilities": { |
| 19 | + "fs": { |
| 20 | + "readTextFile": true, |
| 21 | + "writeTextFile": true |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +If `readTextFile` or `writeTextFile` is `false` or not present, the Agent **MUST NOT** attempt to call the corresponding filesystem method. |
| 29 | + |
| 30 | +## Reading Files |
| 31 | + |
| 32 | +The `fs/read_text_file` method allows Agents to read text file contents from the Client's filesystem, including unsaved changes in the editor. |
| 33 | + |
| 34 | +```json |
| 35 | +{ |
| 36 | + "jsonrpc": "2.0", |
| 37 | + "id": 3, |
| 38 | + "method": "fs/read_text_file", |
| 39 | + "params": { |
| 40 | + "sessionId": "sess_abc123def456", |
| 41 | + "path": "src/main.py", |
| 42 | + "line": 10, |
| 43 | + "limit": 50 |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +<ParamField path="sessionId" type="SessionId" required> |
| 49 | + The [Session ID](./session-setup#session-id) for this request |
| 50 | +</ParamField> |
| 51 | + |
| 52 | +<ParamField path="path" type="string" required> |
| 53 | + Path to the file to read |
| 54 | +</ParamField> |
| 55 | + |
| 56 | +<ParamField path="line" type="number"> |
| 57 | + Optional line number to start reading from (1-based) |
| 58 | +</ParamField> |
| 59 | + |
| 60 | +<ParamField path="limit" type="number"> |
| 61 | + Optional maximum number of lines to read |
| 62 | +</ParamField> |
| 63 | + |
| 64 | +The Client responds with the file contents: |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "jsonrpc": "2.0", |
| 69 | + "id": 3, |
| 70 | + "result": { |
| 71 | + "content": "def hello_world():\n print('Hello, world!')\n" |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Writing Files |
| 77 | + |
| 78 | +The `fs/write_text_file` method allows Agents to write or update text files in the Client's filesystem. |
| 79 | + |
| 80 | +```json |
| 81 | +{ |
| 82 | + "jsonrpc": "2.0", |
| 83 | + "id": 4, |
| 84 | + "method": "fs/write_text_file", |
| 85 | + "params": { |
| 86 | + "sessionId": "sess_abc123def456", |
| 87 | + "path": "config.json", |
| 88 | + "content": "{\n \"debug\": true,\n \"version\": \"1.0.0\"\n}" |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +<ParamField path="sessionId" type="SessionId" required> |
| 94 | + The [Session ID](./session-setup#session-id) for this request |
| 95 | +</ParamField> |
| 96 | + |
| 97 | +<ParamField path="path" type="string" required> |
| 98 | + Path to the file to write. |
| 99 | + The Client **MUST** create the file if it doesn't exist. |
| 100 | +</ParamField> |
| 101 | + |
| 102 | +<ParamField path="content" type="string" required> |
| 103 | + The text content to write to the file |
| 104 | +</ParamField> |
| 105 | + |
| 106 | +The Client responds with an empty result on success: |
| 107 | + |
| 108 | +```json |
| 109 | +{ |
| 110 | + "jsonrpc": "2.0", |
| 111 | + "id": 4, |
| 112 | + "result": null |
| 113 | +} |
| 114 | +``` |
0 commit comments