write_file

[solution]

Writes a .cs file under the loaded root, then updates or reloads the workspace. Set applyChanges=false for a preview that writes no file.

Why this tool exists

File writes should be previewable and synchronized with the loaded workspace when possible.

How it helps the agent

The model can propose/apply edits and immediately know how the semantic workspace was updated.

Try it locally in the Glider playground

Parameters

NameTypeRequiredDescription
filePathstringYesFile path to write. Can be absolute or relative to the loaded root. Writes take the path exactly as given. The repository-relative form the read tools accept does not apply here.
contentstringYesFull file contents to write.
applyChangesbooleanNoIf true, writes the file to disk. If false, returns a preview only. Default is true.
createIfMissingbooleanNoIf true, creates a new file when it does not already exist. Default is false.
includeDiffbooleanNoInclude unified diff output in the response. Default is true.
maxDiffCharsnumberNoMaximum diff characters to return. Use 0 for unlimited. Default is 50000.
pathStylestringNoPath style: 'absolute' (default) or 'relative' (to solution root).

Examples

Write a file edit

Request
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "write_file",
    "arguments": {
      "filePath": "/Users/dev/MyProject/Program.cs",
      "content": "namespace MyProject;\n\npublic class Program { }"
    }
  }
}
Response
{
  "success": true,
  "data": {
    "filePath": "/Users/dev/MyProject/Program.cs",
    "preview": false,
    "applied": true,
    "changed": true,
    "created": false,
    "filesChanged": 1,
    "unifiedDiff": "--- /Users/dev/MyProject/Program.cs\n+++ /Users/dev/MyProject/Program.cs\n@@ ...",
    "changedFiles": [
      {
        "filePath": "/Users/dev/MyProject/Program.cs",
        "changeCount": 1,
        "diff": "--- /Users/dev/MyProject/Program.cs\n+++ /Users/dev/MyProject/Program.cs\n@@ ..."
      }
    ],
    "workspaceUpdate": {
      "kind": "applied",
      "updated": true,
      "inWorkspaceBefore": true,
      "inWorkspaceAfter": true,
      "message": null
    }
  },
  "meta": {
    "durationMs": 112,
    "cancelled": false,
    "timedOut": false,
    "timeoutMs": 1200000
  },
  "error": null
}

Preview a file edit without writing

Request
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "write_file",
    "arguments": {
      "filePath": "/Users/dev/MyProject/Program.cs",
      "content": "namespace MyProject;\n\npublic class Program { }",
      "applyChanges": false
    }
  }
}
Response
{
  "success": true,
  "data": {
    "filePath": "/Users/dev/MyProject/Program.cs",
    "preview": false,
    "applied": true,
    "changed": true,
    "created": false,
    "filesChanged": 1,
    "unifiedDiff": "--- /Users/dev/MyProject/Program.cs\n+++ /Users/dev/MyProject/Program.cs\n@@ ...",
    "changedFiles": [
      {
        "filePath": "/Users/dev/MyProject/Program.cs",
        "changeCount": 1,
        "diff": "--- /Users/dev/MyProject/Program.cs\n+++ /Users/dev/MyProject/Program.cs\n@@ ..."
      }
    ],
    "workspaceUpdate": {
      "kind": "applied",
      "updated": true,
      "inWorkspaceBefore": true,
      "inWorkspaceAfter": true,
      "message": null
    }
  },
  "meta": {
    "durationMs": 112,
    "cancelled": false,
    "timedOut": false,
    "timeoutMs": 1200000
  },
  "error": null
}

Create a new file

Request
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "write_file",
    "arguments": {
      "filePath": "src/NewType.cs",
      "content": "namespace MyProject;\n\npublic class NewType { }",
      "createIfMissing": true,
      "pathStyle": "relative"
    }
  }
}
Response
{
  "success": true,
  "data": {
    "filePath": "/Users/dev/MyProject/Program.cs",
    "preview": false,
    "applied": true,
    "changed": true,
    "created": false,
    "filesChanged": 1,
    "unifiedDiff": "--- /Users/dev/MyProject/Program.cs\n+++ /Users/dev/MyProject/Program.cs\n@@ ...",
    "changedFiles": [
      {
        "filePath": "/Users/dev/MyProject/Program.cs",
        "changeCount": 1,
        "diff": "--- /Users/dev/MyProject/Program.cs\n+++ /Users/dev/MyProject/Program.cs\n@@ ..."
      }
    ],
    "workspaceUpdate": {
      "kind": "applied",
      "updated": true,
      "inWorkspaceBefore": true,
      "inWorkspaceAfter": true,
      "message": null
    }
  },
  "meta": {
    "durationMs": 112,
    "cancelled": false,
    "timedOut": false,
    "timeoutMs": 1200000
  },
  "error": null
}

Response Notes

Returns diff output plus workspace update status. A preview (applyChanges=false) leads with preview:true and reports wouldChange/wouldCreate/filesThatWouldChange instead of the past-tense counts below.

↑/↓ NavigateEnter OpenSpace Expand