add_member

[refactoring]

Inserts a member (method, property, field, event, or constructor) into an existing type from C# source, and returns a diff. The member text is parsed and validated before it is inserted, and only the inserted code is formatted, so the change lands indented in the body without reformatting the rest of the file. For a partial type, filePath chooses which part receives the member; by default the largest non-generated part is used.

Why this tool exists

C# refactors are semantic operations, not search-and-replace operations.

How it helps the agent

The model can preview or apply semantic changes while preserving symbol identity across overloads, files, and projects.

Try it locally in the Glider playground

Parameters

NameTypeRequiredDescription
containerNamestringYesThe type to add the member to. A stable symbolKey (preferred) or a name (simple or fully qualified).
memberCodestringYesThe full C# source of the member to insert.
filePathstringNoFor a partial type, the file whose part receives the member. Absolute or loaded-root-relative. Defaults to the largest non-generated part.
projectNamestringNoOptional project name to limit search when finding the type by name.
applyChangesbooleanNoIf true (default), applies changes to disk. If false, returns a preview diff.
failOnErrorsbooleanNoIf true, do not write when the edit introduces a compile error; the response reports the diagnostics.
includeDiffbooleanNoInclude unified diff in response. Default is true.
maxDiffCharsnumberNoMax diff characters. Use 0 for unlimited. Default is 50000.
includePerFileDiffbooleanNoInclude per-file diffs in changedFiles. Default is true.
pathStylestringNoPath style: 'absolute' (default) or 'relative' (to solution root).

Examples

Preview adding a method to a class

Request
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "add_member",
    "arguments": {
      "containerName": "OrderService",
      "memberCode": "public decimal Total() => 0m;",
      "applyChanges": false
    }
  }
}
Response
{
  "success": true,
  "data": {
    "containerName": "MyProject.OrderService",
    "memberName": "Total",
    "memberKind": "Method",
    "filePath": "/path/to/OrderService.cs",
    "filesChanged": 1,
    "applied": false,
    "message": null,
    "diagnostics": {
      "errorCount": 0,
      "warningCount": 0,
      "items": []
    },
    "unifiedDiff": "...",
    "changedFiles": [
      {
        "filePath": "/path/to/OrderService.cs",
        "changeCount": 1,
        "diff": "..."
      }
    ]
  },
  "error": null
}

Response Notes

Returns a unified diff of the insertion. containerName, memberName, and memberKind identify what was added, and filePath names the file that received it (the chosen part for a partial type). diagnostics reports the compile errors and warnings the inserted member introduces; when failOnErrors is true and it introduces an error, applied is false and message explains why. When the file is linked into several projects or targets, the check spans all of them.

↑/↓ NavigateEnter OpenSpace Expand