Connecting Claude Code to Salesforce via MCP

 

 

Before starting, ensure you have the following installed:

  • Node.js (LTS version) — required to run the Salesforce CLI and MCP server
  • Visual Studio Code — our code editor and workspace
  • Claude Code extension in VS Code — the AI interface
  • Salesforce Developer Edition org
  • Install Salesforce CLI Extension Pack

Part 1: Salesforce CLI Setup

Step 1. Create project folder

Every Salesforce DX project lives in its own folder. This folder will eventually contain your metadata, configuration files, and the MCP server setup.

Create a folder in your Documents directory:

C:\Users\pc\OneDrive\Documents\salesforce-mcp-demo

Step 2. Open folder in VS Code

VS Code needs to know which folder is your active project.

Go to File → Open Folder and select the new folder:

VS Code opened with the project folder as workspace

Step 3. Open integrated terminal

The integrated terminal runs inside VS Code and automatically starts in your project folder.

go to Terminal → New Terminal.

Step 4. Verify Salesforce CLI

Check whether the Salesforce CLI is already installed. If it is, you will see a version number. If not, you need to install it first.

sf –version

Expected output shows the installed version and Node.js version:

CLI version check — showing version 2.96.4

If the command is not recognized, install the CLI globally via npm:

npm install -g @salesforce/cli

The installation downloads around 725 packages

Salesforce CLI installation via npm

Part 2: Authorize Your Dev Org

With the CLI installed, we now connect it to your Salesforce Developer Edition org.

Step 5. Run login command

This single command sets up the authentication flow and registers the org under a memorable alias.

sf org login web –alias MyDevOrg –set-default

Three things happen:

  • sf org login web — starts a browser-based OAuth flow
  • –alias MyDevOrg — assigns a short nickname so you don’t type full usernames later
  • –set-default — marks this as your default org, so commands target it automatically

Step 6. Allow access in browser

Your default browser opens the Salesforce login page. After logging in, you see a consent screen listing the permissions the CLI needs: identity, web access, API access, and refresh tokens.

Salesforce OAuth consent screen — click Allow

Salesforce OAuth consent screen — click Allow

Click Allow. Salesforce then confirms the authentication:

Authentication successful — safe to close tab

Authentication successful — safe to close tab

Step 7. Verify in terminal

Return to VS Code. The terminal confirms the authorization with your org ID and username:

Terminal confirms successful authorization

Step 8. Confirm org is connected

Double-check that your org is registered and accepting connections. This is a good sanity test before moving to MCP configuration.

sf org list

Authenticated org listed with Connected status

Authenticated org listed with Connected status

The “(U)” marker indicates this is your default org. All future commands will target it unless you specify otherwise.

Part 3: Configure MCP Server

This is where the AI connects to Salesforce. The .mcp.json file is Claude Code’s address book — it tells Claude which MCP servers to load and how to communicate with them.

Step 9. Create .mcp.json file

In the root of your project folder, create a new file named exactly .mcp.json. The leading dot marks it as a configuration file.

You can create it via the VS Code sidebar: right-click the project folder → New File → type .mcp.json.

Step 10. Add the MCP configuration

Paste the following JSON into the file. This tells Claude Code to run the Salesforce DX MCP package via npx, targeting your default org:

{

“mcpServers”: {

“Salesforce DX”: {

“command”: “npx”,

“args”: [

“-y”,

“@salesforce/mcp”,

“–orgs”,

“DEFAULT_TARGET_ORG”,

“–toolsets”,

“orgs,metadata,data,users,testing”,

“–allow-non-ga-tools”

]

}

}

}

Save with Ctrl + S.

Completed .mcp.json file in VS Code

Part 4: First Claude Demo

We will ask Claude in plain English to list all custom objects in your Salesforce org. This proves the full chain is working: VS Code → Claude Code → MCP configuration → Salesforce CLI → your org.

Step 11. Open Claude Code panel

Find the Claude Code panel in VS Code’s right sidebar (or open it via Command Palette: Claude Code: New Chat). Start a fresh chat session so the .mcp.json is picked up cleanly.

Step 12. Send your first prompt

Type this natural language request into the chat box:

List all the custom objects in my Salesforce org

Natural language prompt in Claude Code

Natural language prompt in Claude Code

Step 13. Approve tool usage

Claude analyzes your request and determines it needs to run a Salesforce CLI command. For safety, Claude Code asks your permission before executing anything that touches your system or accounts.

Claude requests permission to run a bash command

Claude requests permission to run a bash command

Click Yes. This is a one-time approval

Step 14. Review the results

If you have Field Service Lightning or other managed packages installed, their objects show up here:

Claude returns a complete list of custom objects

Claude returns a complete list of custom objects

Part 5: Generate Project Structure

So far, our folder only contains the .mcp.json file. For real development work — retrieving metadata, editing files, deploying changes — we need the standard Salesforce DX project structure.

Step 15. Generate the SFDX project

This command creates all the standard folders and config files Salesforce DX uses. The –name . (dot) tells it to use the current folder rather than creating a subfolder.

sf project generate –name . –default-package-dir force-app

Full SFDX project structure generated

Full SFDX project structure generated

Part 6: Retrieve Metadata

Retrieving metadata means pulling your org’s configuration (Apex classes, custom objects, flows, layouts, permissions) into local files.

Step 16. Create the manifest folder

The manifest folder holds your package.xml — a manifest file that tells Salesforce exactly what metadata to retrieve.

mkdir manifest

Step 17. Create package.xml

In the manifest folder, create a file named package.xml.

<?xml version=”1.0″ encoding=”UTF-8″?>

<Package xmlns=”http://soap.sforce.com/2006/04/metadata”>

<types>

<members>*</members>

<n>ApexClass</n>

</types>

<types>

<members>*</members>

<n>ApexTrigger</n>

</types>

<types>

<members>*</members>

<members>Account</members>

<members>Contact</members>

<members>Opportunity</members>

<members>Lead</members>

<members>Case</members>

<n>CustomObject</n>

</types>

<types>

<members>*</members>

<n>Flow</n>

</types>

<types>

<members>*</members>

<n>Layout</n>

</types>

<version>62.0</version>

</Package>

Step 18. Run the retrieve command

This command reads your package.xml and downloads matching metadata from your org into force-app/main/default

sf project retrieve start –manifest manifest/package.xml

Metadata retrieve in progress — profiles being created locally

Metadata retrieve in progress — profiles being created locally

Step 19. Explore retrieved metadata

Expand force-app/main/default/ in the sidebar. Each metadata type gets its own folder:

Full project structure with retrieved metadata

Part 7: AI-Powered Field Creation

Step 20. Request a new custom field

In the Claude Code chat, describe the field you want. Be specific about the type, length, and location:

Add a new custom field to the Account object called

Customer_Health_Score__c of type Number (length 3, decimal places 0).

Add a description “Internal score 0-100 representing customer health”.

Update the Account object XML file in

force-app/main/default/objects/Account/

Natural language field creation request

Natural language field creation request

Step 21. Claude analyzes existing patterns

Claude does not blindly generate code. It first reads existing custom fields to learn your project’s conventions. It sees you use the DX source format (one file per field, not inline in the object definition) and follows that same pattern.

Claude inspecting existing field files before writing new code

Claude inspecting existing field files before writing new code

Step 22. Approve file creation

Claude asks permission to write the new file. Click Yes. Option 2 would approve all file writes for the session — useful when you trust Claude’s actions and want faster iteration.

File creation approved — Claude explains the DX format choice

Step 23. Review the generated XML

Claude creates Customer_Health_Score__c.field-meta.xml with the correct Salesforce CustomField XML structure:

Generated field-meta.xml with proper structure

Generated field-meta.xml with proper structure

Part 8: Deploy to Salesforce

The field currently exists only as a file on your local machine.

Step 24. Deploy via right-click menu

The cleanest way to deploy a single file is the VS Code right-click menu. In the sidebar, right-click the new field file and select “SFDX: Deploy This Source to Org”:

Right-click context menu showing SFDX deploy options

Right-click context menu showing SFDX deploy options

If you prefer the terminal, the command is:

sf project deploy start -d force-app/main/default/objects/Account/fields/Customer_Health_Score__c.field-meta.xml

Step 25. Verify the field in Salesforce

Log in to your Dev Org in a browser and confirm the field is live:

  1. Go to Setup → Object Manager → Account
  2. Click Fields & Relationships

A screenshot of a computer Description automatically generated

 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes

Leave a Reply

Your email address will not be published. Required fields are marked *