文件系统
客户端文件系统访问方法
文件系统方法允许 Agent 在客户端环境中读取和写入文本文件。这些方法使 Agent 能够访问未保存的编辑器状态,并允许客户端跟踪 Agent 执行期间进行的文件修改。
检查支持
在尝试使用文件系统方法之前,Agent 必须通过检查 initialize 响应中的客户端能力字段来验证客户端是否支持这些能力:
{
"jsonrpc": "2.0",
"id": 0,
"result": {
"protocolVersion": 1,
"clientCapabilities": {
"fs": {
"readTextFile": true,
"writeTextFile": true
}
}
}
}
如果 readTextFile 或 writeTextFile 为 false 或不存在,Agent 不得尝试调用相应的文件系统方法。
读取文件
fs/read_text_file 方法允许 Agent 从客户端文件系统读取文本文件内容,包括编辑器中的未保存更改。
{
"jsonrpc": "2.0",
"id": 3,
"method": "fs/read_text_file",
"params": {
"sessionId": "sess_abc123def456",
"path": "/home/user/project/src/main.py",
"line": 10,
"limit": 50
}
}
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
sessionId | SessionId | 是 | 此请求的会话 ID |
path | string | 是 | 要读取的文件的绝对路径 |
line | number | - | 开始读取的可选行号(从 1 开始) |
limit | number | - | 要读取的最大行数(可选) |
客户端以文件内容响应:
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"content": "def hello_world():\n print('Hello, world!')\n"
}
}
写入文件
fs/write_text_file 方法允许 Agent 在客户端文件系统中写入或更新文本文件。
{
"jsonrpc": "2.0",
"id": 4,
"method": "fs/write_text_file",
"params": {
"sessionId": "sess_abc123def456",
"path": "/home/user/project/config.json",
"content": "{\n \"debug\": true,\n \"version\": \"1.0.0\"\n}"
}
}
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
sessionId | SessionId | 是 | 此请求的会话 ID |
path | string | 是 | 要写入的文件的绝对路径。客户端必须在文件不存在时创建它。 |
content | string | 是 | 要写入文件的文本内容 |
客户端在成功时以空结果响应:
{
"jsonrpc": "2.0",
"id": 4,
"result": null
}