[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.
Try this tool in Playground.
| Name | Type | Required | Description |
|---|---|---|---|
| filePath | string | Yes | File path to write. Can be absolute or relative to the loaded root. |
| content | string | Yes | Full file contents to write. |
| applyChanges | boolean | No | If true, writes the file to disk. If false, returns a preview only. Default is false. |
| createIfMissing | boolean | No | If true, creates a new file when it does not already exist. Default is false. |
| includeDiff | boolean | No | Include unified diff output in the response. Default is true. |
| maxDiffChars | number | No | Maximum diff characters to return. Use 0 for unlimited. Default is 50000. |
| pathStyle | string | No | Path style: 'absolute' (default) or 'relative' (to solution root). |
Preview a file edit without writing
{
"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 { }"
}
}
}{
"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
{
"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
}
}
}{
"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
{
"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"
}
}
}{
"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
}Returns diff output plus workspace update status after preview or apply