Salesforce命令行工具
STDIO为LLM工具提供Salesforce CLI功能
为LLM工具提供Salesforce CLI功能
Model Context Protocol (MCP) server for providing Salesforce CLI functionality to LLM tools like Claude Desktop.
This MCP server wraps the Salesforce CLI (sf
) command-line tool and exposes its commands as MCP tools and resources, allowing LLM-powered agents to:
sf
) installed and configured# Clone the repository git clone <repository-url> cd sfMcp # Install dependencies npm install
# Basic usage npm start # With project roots npm start /path/to/project1 /path/to/project2 # or using the convenience script npm run with-roots /path/to/project1 /path/to/project2 # As an npx package with roots npx -y codefriar/sf-mcp /path/to/project1 /path/to/project2
The MCP server uses stdio transport, which can be used with MCP clients like the MCP Inspector or Claude Desktop.
To configure this MCP in Claude Desktop's .claude.json
configuration:
{ "tools": { "salesforce": { "command": "/path/to/node", "args": [ "/path/to/sf-mcp/build/index.js", "/path/to/project1", "/path/to/project2" ] } } }
Using the npm package directly:
{ "tools": { "salesforce": { "command": "/path/to/npx", "args": [ "-y", "codefriar/sf-mcp", "/path/to/project1", "/path/to/project2" ] } } }
# Watch mode (recompiles on file changes) npm run dev # In another terminal npm start [optional project roots...]
This MCP server provides Salesforce CLI commands as MCP tools. It automatically discovers and registers all available commands from the Salesforce CLI, and also specifically implements the most commonly used commands.
sf_version
- Get the Salesforce CLI version informationsf_help
- Get help information for Salesforce CLI commandssf_cache_clear
- Clear the command discovery cachesf_cache_refresh
- Refresh the command discovery cacheFor commands that require a Salesforce project context (like deployments), you must specify the project directory. The MCP supports multiple project directories (roots) similar to the filesystem MCP.
Method 1: Via Command Line Arguments
# Start the MCP with project roots npm start /path/to/project1 /path/to/project2 # or npx -y codefriar/sf-mcp /path/to/project1 /path/to/project2
When configured this way, the roots will be automatically named root1
, root2
, etc.,
with the first one set as default.
Method 2: Using MCP Tools
sf_set_project_directory
- Set a Salesforce project directory to use for commands
directory
- Path to a directory containing a sfdx-project.json filename
- (Optional) Name for this project rootdescription
- (Optional) Description for this project rootisDefault
- (Optional) Set this root as the default for command executionsf_list_roots
- List all configured project rootssf_detect_project_directory
- Attempt to detect project directory from user messagesExample usage:
# Set project directory with a name
sf_set_project_directory --directory=/path/to/your/sfdx/project --name=project1 --isDefault=true
# List all configured roots
sf_list_roots
# Or include in your message:
"Please deploy the apex code from the project in /path/to/your/sfdx/project to my scratch org"
Method 3: Claude Desktop Configuration
Configure project roots in .claude.json
as described below.
You can execute commands in specific project roots:
# Using resource URI
sf://roots/project1/commands/project deploy start --sourcedir=force-app
# Using rootName parameter
sf_project_deploy_start --sourcedir=force-app --rootName=project1
Project directory must be specified for commands such as deployments, source retrieval, and other project-specific operations. If multiple roots are configured, the default root will be used unless otherwise specified.
The following commands are specifically implemented and guaranteed to work:
sf_org_list
- List Salesforce orgs
json
, verbose
sf_auth_list_orgs
- List authenticated Salesforce orgs
json
, verbose
sf_org_display
- Display details about an org
targetusername
, json
sf_org_open
- Open an org in the browser
targetusername
, path
, urlonly
sf_apex_run
- Run anonymous Apex code
targetusername
, file
, apexcode
, json
sf_apex_test_run
- Run Apex tests
targetusername
, testnames
, suitenames
, classnames
, json
sf_data_query
- Execute a SOQL query
targetusername
, query
, json
sf_schema_list_objects
- List sObjects in the org
targetusername
, json
sf_schema_describe
- Describe a Salesforce object
targetusername
, sobject
, json
sf_project_deploy_start
- Deploy the source to an org
targetusername
, sourcedir
, json
, wait
The server discovers all available Salesforce CLI commands and registers them as tools with format: sf_<topic>_<command>
.
For example:
sf_apex_run
- Run anonymous Apex codesf_data_query
- Execute a SOQL queryFor nested topic commands, the tool name includes the full path with underscores:
sf_apex_log_get
- Get apex logssf_org_login_web
- Login to an org using web flowThe server also creates simplified aliases for common nested commands where possible:
sf_get
as an alias for sf_apex_log_get
sf_web
as an alias for sf_org_login_web
The available commands vary depending on the installed Salesforce CLI plugins.
Note: Command discovery is cached to improve startup performance. If you install new SF CLI plugins, use the
sf_cache_refresh
tool to update the cache, then restart the server.
The following resources provide documentation about Salesforce CLI:
sf://help
- Main CLI documentationsf://topics/{topic}/help
- Topic help documentationsf://commands/{command}/help
- Command help documentationsf://topics/{topic}/commands/{command}/help
- Topic-command help documentationsf://version
- Version informationsf://roots
- List all configured project rootssf://roots/{root}/commands/{command}
- Execute a command in a specific project root~/.sf-mcp/command-cache.json
)sf commands --json
to get a complete list of available commandsFor commands that require a Salesforce project context:
sf_set_project_directory
Project-specific commands (like deployments, retrievals, etc.) will automatically run in the appropriate project directory. For commands that don't require a project context, the working directory doesn't matter.
You can execute commands in specific project roots by:
sf://roots/{rootName}/commands/{command}
rootName
parameter to command tools (internal implementation details)sf_set_project_directory --isDefault=true
To improve startup performance, the MCP server caches discovered commands:
~/.sf-mcp/command-cache.json
sf_cache_refresh
to update the cacheThe first run of the server performs a full command discovery which can take some time. If you encounter any issues with missing commands or cache problems:
rm ~/.sf-mcp/command-cache.json
npm start
This will force a complete rediscovery of all commands using the official CLI metadata.
If specific commands are still missing, or you've installed new SF CLI plugins:
sf_cache_refresh
tool from Claude DesktopThe Salesforce CLI has a hierarchical command structure that can be several levels deep. This MCP server handles these nested commands by:
apex:log:get
→ sf_apex_log_get
)sf_get
for sf_apex_log_get
)sf commands --json
Nested topic commands are registered twice when possible—once with the full hierarchy name and once with a simplified alias, making them easier to discover and use.
ISC