write_file

[solution]

Writes a .cs file under the loaded root. Uses preview-first behavior by default and explicitly updates or reloads the workspace after applyChanges=true.

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.

Parameters

NameTypeRequiredDescription
filePathstringYesFile path to write. Can be absolute or relative to the loaded root.
contentstringYesFull file contents to write.
applyChangesbooleanNoIf true, writes the file to disk. If false, returns a preview only. Default is false.
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

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 { }"
    }
  }
}
Response
{
  "success": true,
  "data": {
    "filePath": "/Users/dev/MyProject/Program.cs",
    "changed": true,
    "applied": false,
    "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": "preview",
      "updated": false,
      "inWorkspaceBefore": true,
      "inWorkspaceAfter": true,
      "message": null
    }
  },
  "meta": {
    "durationMs": 112,
    "cancelled": false,
    "timedOut": false,
    "timeoutMs": 1200000
  },
  "error": null
}

Apply 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 { }",
      "applyChanges": true
    }
  }
}
Response
{
  "success": true,
  "data": {
    "filePath": "/Users/dev/MyProject/Program.cs",
    "changed": true,
    "applied": false,
    "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": "preview",
      "updated": false,
      "inWorkspaceBefore": true,
      "inWorkspaceAfter": true,
      "message": null
    }
  },
  "meta": {
    "durationMs": 112,
    "cancelled": false,
    "timedOut": false,
    "timeoutMs": 1200000
  },
  "error": null
}

Create a new file and apply the change

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 { }",
      "applyChanges": true,
      "createIfMissing": true,
      "pathStyle": "relative"
    }
  }
}
Response
{
  "success": true,
  "data": {
    "filePath": "/Users/dev/MyProject/Program.cs",
    "changed": true,
    "applied": false,
    "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": "preview",
      "updated": false,
      "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 after preview or apply

↑/↓ NavigateEnter OpenSpace Expand