Browse documentation

add_type

[refactoring]

Creates a top-level C# type in a new file and returns a diff.

Before you use this tool

Writes files by default. An authorized, unambiguous edit with a clear scope can apply directly. Set applyChanges: false for a preview without file writes. Preview when the user requests inspection only, the scope is unclear, or a broad refactor needs inspection.

Try it locally in the Glider playground

Parameters

NameTypeRequiredDescription
typeCodestringYesThe full C# source of the type. May include using directives and one namespace.
filePathstringYesThe file to create. Absolute or loaded-root-relative. Must not already exist.
targetNamespacestringNoThe namespace for the type. If omitted, taken from the source or inferred from a sibling file.
projectNamestringNoOptional project name when the target directory is under more than one project.
applyChangesbooleanNoIf true (default), applies changes to disk. If false, returns a preview diff. Default: true
failOnErrorsbooleanNoIf true, do not write when the edit introduces a compile error; the response reports the diagnostics. Default: false
includeDiffbooleanNoInclude unified diff in response. Default is true. Default: true
maxDiffCharsnumberNoMax diff characters. Use 0 for unlimited. Default is 50000. Default: 50000
includePerFileDiffbooleanNoInclude diffs in changedFiles independently of includeDiff. File paths and counts remain available when false. Development builds after 13.4.0 default to false; released 13.4.0 defaults to true. Follow the installed schema. Default: false
pathStylestringNoPath style: 'absolute' (default) or 'relative' (to solution root). Default: "absolute"
responseDetailstringNocompact returns results and actionable state. full adds diagnostic metadata under responseDiagnostics. Default: "compact"

Examples

Replace sample paths and symbol keys with values from your workspace.

Preview creating a new class

Arguments
{
  "typeCode": "public class OrderService { }",
  "filePath": "src/Orders/OrderService.cs",
  "targetNamespace": "MyProject.Orders",
  "applyChanges": false
}
Full MCP request
Full MCP request
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "add_type",
    "arguments": {
      "typeCode": "public class OrderService { }",
      "filePath": "src/Orders/OrderService.cs",
      "targetNamespace": "MyProject.Orders",
      "applyChanges": false
    }
  }
}
Illustrative response
{
  "success": true,
  "typeName": "OrderService",
  "typeKind": "class",
  "namespace": "MyProject.Orders",
  "namespaceSource": "explicit",
  "filePath": "/path/to/OrderService.cs",
  "filesChanged": 1,
  "filesCreated": 1,
  "applied": false,
  "message": null,
  "diagnostics": {
    "errorCount": 0,
    "warningCount": 0,
    "items": []
  },
  "unifiedDiff": "...",
  "changedFiles": [
    {
      "filePath": "/path/to/OrderService.cs",
      "changeCount": 3
    }
  ],
  "workspaceUpdate": null
}

Response Notes

The source may include using directives and a single namespace. Chooses the project that owns the target path, infers the namespace from a sibling file when it is not given, and formats the created file. The target file must not already exist. For a partial type, an existing type of the same name is rejected.

Returns a unified diff of the created file (and any project-file edit). typeName, typeKind, namespace, and namespaceSource identify what was created and how the namespace was chosen. workspaceUpdate reports whether the workspace compiles the new file, and is null when nothing was created. diagnostics reports the compile errors and warnings the new file introduces; when failOnErrors is true and it introduces an error, applied is false and message explains why. In a multi-targeted project the check spans every target.

Unreleased development builds after 13.4.0 return one unified diff by default; the example uses that contract. Follow the installed schema for supported parameters and defaults. Set includeDiff=false for compact output, with includePerFileDiff=false where supported. Set includePerFileDiff=true only when separate file diffs are needed; it works independently of includeDiff. These controls preserve application state, file metadata, counts, and any diagnostics, errors, conflicts, or limits the tool reports. Results use top-level fields with success. Compact is the default; full adds responseDiagnostics. Both modes retain errors, coverage, partial state, and recovery advice.