agentleFS
advancedcommunitiesadvancedcommunities/salesforce-mcp-server/CLAUDE.md

salesforce-mcp-server

CLAUDE.md33 starsChanged 2 months agoMITView on GitHub

This is a Model Context Protocol (MCP) server that enables AI assistants to interact with Salesforce orgs through the Salesforce CLI. The codebase follows a modular architecture with clear separation of concerns. When implementing new tools, follow this pattern: The MCP server provides tools organized by functionality:

  • Installs packages
  • In this repositoryOne other file it gives its agents.

The file

Show its rules and text

We split this CLAUDE.md into the rules it gives an agent

19 rules. About 0 are common, 8 are distinctive, and 11 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 11

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Permission System: All tools check permissions via src/config/permissions.ts which enforces:

    line 29
  2. Distinctive:

    When implementing new tools, follow this pattern:

    line 41
  3. Very specific:

    Create or update the appropriate file in src/tools/

    line 98
  4. Very specific:

    Make sure new tools are registered in both the tool file and manifest.json

    line 102
  5. Distinctive:

    Always return structured JSON responses instead of throwing errors

    line 108
  6. Very specific:

    Use the pattern: { error: "message" } for error responses

    line 109
  7. Very specific:

    All Salesforce CLI commands should use executeSfCommand() from src/utils/sfCommand.ts

    line 134
  8. Distinctive:

    Always validate inputs with Zod schemas before processing

    line 136
  9. Distinctive:

    Check org access permissions before executing any Salesforce operations

    line 137
  10. Very specific:

    Use --json flag for CLI commands to ensure consistent JSON output

    line 138
  11. Very specific:

    Do not add indentation to JSON results (use JSON.stringify(result) not JSON.stringify(result, null, 2))

    line 140
  12. Distinctive:

    Verify permission checks are implemented for all destructive operations

    line 141
  13. Distinctive:

    Record CRUD Operations: Create, update, and delete Salesforce records via REST API

    line 150
  14. Very specific:

    Read-Only Mode Support: All write operations respect READ_ONLY permission setting

    line 153
  15. Distinctive:

    Run prettier on all modified files after making changes

    line 159
  16. Very specific:

    After making changes, always update documentation in both README.MD and manifest.json

    line 160
  17. Very specific:

    After making changes, rebuild the project (npm run build) and the MCPB file (npm run build:mcpb)

    line 161
  18. Very specific:

    Always add the shebang line to build/index.js after creating a new build and before publishing — this is critical

    line 162
  19. Distinctive:

    Use web search to gather the latest information that may not be in the training dataset — Salesforce CLI command syntax, MCP SDK APIs, npm …

    line 163

≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
CLAUDE.md

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

Build and Development Commands

# Build the TypeScript project
npm run build

# Run development mode with MCP inspector for debugging
npm run dev

# Build MCP Bundle (.mcpb) for distribution
npm run build:mcpb

# Install dependencies
npm install

Architecture Overview

This is a Model Context Protocol (MCP) server that enables AI assistants to interact with Salesforce orgs through the Salesforce CLI. The codebase follows a modular architecture with clear separation of concerns.

Core Components

  1. Tool Registration Pattern: Each tool category has its own file in src/tools/ that exports a registration function. Tools are registered in src/index.ts using functions like registerApexTools(server).
  2. Permission System: All tools check permissions via src/config/permissions.ts which enforces:
    • READ_ONLY mode: Prevents Apex execution when enabled
    • ALLOWED_ORGS: Restricts access to specific org aliases
  3. Salesforce Integration: Two approaches are used:
    • Native API calls via @salesforce/apex-node for Apex operations
    • CLI commands via src/utils/sfCommand.ts for most other operations
  4. Connection Management: src/shared/connection.ts handles Salesforce authentication by reusing existing Salesforce CLI auth tokens.

Tool Implementation Pattern

When implementing new tools, follow this pattern:

// 1. Define Zod schema for input validation (targetOrg is optional — falls back to SF CLI default)
const MyToolSchema = z.object({
    targetOrg: z.string().optional(),
    // other parameters
});

// 2. Resolve target org (uses default if not provided)
let targetOrg: string;
try {
    targetOrg = await resolveTargetOrg(input.targetOrg);
} catch (error: any) {
    return {
        content: [
            {
                type: "text",
                text: JSON.stringify({
                    success: false,
                    message: error.message,
                }),
            },
        ],
    };
}

// 3. Check permissions
if (!permissions.canAccessOrg(targetOrg)) {
    return { error: "Access denied" };
}

// 4. Execute operation (via CLI or native API)
const result = await executeSfCommand(["command", "args"], targetOrg);

// 5. Return structured response
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };

Tool Categories

The MCP server provides tools organized by functionality:

  • Apex Tools (src/tools/apex.ts): Execute anonymous Apex, run tests, get coverage, manage logs, generate classes/triggers
  • Query Tools (src/tools/query.ts): Query records with SOQL, export to files
  • SObject Tools (src/tools/sobjects.ts): List and describe Salesforce objects
  • Org Tools (src/tools/orgs.ts): List connected orgs, login, logout, open org in browser
  • Admin Tools (src/tools/admin.ts): Manage permissions, users, metadata
  • Schema Tools (src/tools/schema.ts): Generate custom tabs
  • Package Tools (src/tools/package.ts): Install and uninstall packages
  • Code Analysis Tools (src/tools/code-analyzer.ts, src/tools/scanner.ts): Static code analysis and security scanning
  • Record Tools (src/tools/records.ts): Open records in browser, create/update/delete records via REST API
  • Lightning Tools (src/tools/lightning.ts): Generate Lightning Web Components and Aura components
  • Project Tools (src/tools/project.ts): Deploy metadata to Salesforce orgs

Adding New Tools

  1. Create or update the appropriate file in src/tools/
  2. Define the tool with Zod schema validation
  3. Implement permission checks if needed
  4. Register the tool in the export function
  5. Make sure new tools are registered in both the tool file and manifest.json
  6. Rebuild the project with npm run build
  7. Build the MCP Bundle with npm run build:mcpb

Error Handling

  • Always return structured JSON responses instead of throwing errors
  • Use the pattern: { error: "message" } for error responses
  • Include helpful context in error messages

Key Files to Understand

  • src/index.ts - Server initialization and tool registration
  • src/config/permissions.ts - Permission enforcement logic
  • src/utils/sfCommand.ts - Salesforce CLI integration
  • src/utils/resolveTargetOrg.ts - Default org resolution with caching
  • src/shared/connection.ts - Salesforce authentication handling
  • manifest.json - Desktop Extension configuration and tool metadata

TypeScript Configuration

  • Target: ES2022
  • Module: Node16
  • Output: ./build directory
  • Type: ES modules ("type": "module" in package.json)

Distribution

The project supports MCP Bundle (.mcpb) packaging for one-click installation. The manifest.json file defines the extension metadata, tools, and user configuration options.

Important Implementation Notes

  • All Salesforce CLI commands should use executeSfCommand() from src/utils/sfCommand.ts
  • Native Apex operations should use the @salesforce/apex-node library when possible for better performance
  • Always validate inputs with Zod schemas before processing
  • Check org access permissions before executing any Salesforce operations
  • Use --json flag for CLI commands to ensure consistent JSON output
  • The server version is maintained in both package.json and src/index.ts
  • Do not add indentation to JSON results (use JSON.stringify(result) not JSON.stringify(result, null, 2))
  • Verify permission checks are implemented for all destructive operations

Recent Features Added

  • Lightning Component Generation: Generate Lightning Web Components and Aura components with customizable templates
  • Project Deployment: Deploy metadata to Salesforce orgs with various configuration options
  • Apex Debug Logs: Fetch and view Apex debug logs from the org
  • Apex Code Generation: Generate Apex classes and triggers with metadata
  • Record Navigation: Open Salesforce records directly in browser
  • Record CRUD Operations: Create, update, and delete Salesforce records via REST API
  • Enhanced Error Handling: Improved error messages with more context
  • Prettier Integration: Automatic code formatting with .prettierrc configuration
  • Read-Only Mode Support: All write operations respect READ_ONLY permission setting
  • Removed Interactive Schema Tools: Field and object generation tools removed as they require interactive CLI prompts

Workflow Rules

  • Always execute tasks and agents in parallel when possible. If multiple independent operations need to be performed (e.g., reading files, running searches, editing unrelated files, running builds), do them simultaneously rather than sequentially. Only run tasks and agents sequentially when there is a dependency between them.
  • Run prettier on all modified files after making changes
  • After making changes, always update documentation in both README.MD and manifest.json
  • After making changes, rebuild the project (npm run build) and the MCPB file (npm run build:mcpb)
  • Always add the shebang line to build/index.js after creating a new build and before publishing — this is critical
  • Use web search to gather the latest information that may not be in the training dataset — Salesforce CLI command syntax, MCP SDK APIs, npm package versions, Salesforce platform updates, and third-party library documentation

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.