Gianloko/salesforce-apex-mcp-a2a-server
If you are the rightful owner of salesforce-apex-mcp-a2a-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
The Salesforce MCP Server is an Apex-based implementation that enables AI systems to interact with Salesforce CRM data through the Model Context Protocol (MCP).
Salesforce MCP Server
A Model Context Protocol (MCP) implementation for Salesforce, enabling AI systems to interact with Salesforce CRM data through standardized protocols.
Overview
The Salesforce MCP Server is an Apex-based implementation that exposes Salesforce functionality through the MCP protocol. This allows AI models and tools to seamlessly access and manipulate CRM data, execute tools, and utilize pre-defined prompts within the Salesforce ecosystem.
Architecture
The server follows a modular architecture with the following key components:
Core Components
āāāāāāāāāāāāāāāāāāāāāāāāā
ā REST Endpoint ā ā Entry point (/mcp/*)
ā RestResourceMcpServer ā
āāāāāāāāāāāā¬āāāāāāāāāāāāā
ā
v
āāāāāāāāāāāāāāāāāāāāāāā
ā Server ā ā Main orchestrator
ā (MCP Protocol) ā
āāāāāāāāāāāā¬āāāāāāāāāāā
ā
āāāā Tools āāāāāāāāā
āāāā Resources āāāāā¼āā Pluggable Components
āāāā Prompts āāāāāāā
Request Flow
- HTTP Request ā REST Resource endpoint (
/mcp/*
) - Server Initialization ā Creates MCP server instance with registered components
- Method Routing ā Routes MCP protocol methods to appropriate handlers
- Component Execution ā Executes tools, reads resources, or processes prompts
- JSON Response ā Returns standardized MCP response
File Structure
RestResourceMcpServer.cls
The main REST endpoint that handles incoming MCP requests.
Key Features:
- Maps to
/mcp/*
URL pattern - Initializes the MCP server with components
- Registers example tool, resource, and prompt
- Delegates request processing to the Server class
Server.cls
The core MCP protocol implementation that manages the server lifecycle.
Key Responsibilities:
- Protocol Compliance: Implements MCP 2025-06-18 specification
- Component Registry: Manages tools, resources, and prompts
- Request Routing: Routes MCP methods to appropriate handlers
- Error Handling: Standardized error responses and exception management
- JSON Response Generation: Creates compliant MCP responses
Supported MCP Methods:
initialize
- Server initialization and capability negotiationresources/list
- List available resourcesresources/templates/list
- List resource templatesresources/read
- Read resource contenttools/list
- List available toolstools/call
- Execute a toolprompts/list
- List available promptsprompts/get
- Retrieve prompt contentping
- Health check
Method.cls
Handles the execution logic for each MCP protocol method.
Core Methods:
initialize()
Performs the MCP handshake, returning:
- Protocol version compatibility
- Server information (name, version, title)
- Available capabilities (tools, resources, prompts)
- Optional server instructions
resourcesList(Boolean retrieveTemplates)
Returns either static resources or resource templates based on the parameter.
resourcesRead()
Reads content from a specific resource URI, supporting template-based resource matching.
toolsList()
/ toolsCall()
Lists available tools and executes tool calls with provided arguments.
promptsList()
/ promptsGet()
Manages prompt templates and retrieves processed prompt content.
Component Types
Tools
Executable functions that perform actions within Salesforce.
Example: LeadTool
- Manages lead operations (create, update, query)
Structure:
global abstract class Tool {
global abstract String getName();
global abstract String call(Map<String, Object> arguments);
global abstract String toJson();
}
Resources
Data sources that can be read by AI systems.
Example: ProductResource
- Exposes product catalog data
Structure:
global abstract class Resource {
global abstract Boolean isTemplate();
global abstract Boolean matchesTemplate(String uri);
global abstract List<Content> read();
global abstract String toJson();
}
Prompts
Reusable prompt templates with parameter substitution.
Example: CodeReviewPrompt
- Provides code review guidelines
Structure:
global abstract class Prompt {
global abstract String getName();
global abstract List<Message> get(Map<String, Object> arguments);
global abstract String toJson();
}
Error Handling
The server implements comprehensive error handling with:
- MCP-specific exceptions (
mcpException
) for protocol violations - Standard Salesforce exceptions for platform errors
- Structured error responses with error codes and details
- Graceful degradation for partial failures
Configuration
Server Setup
Server server = new Server('1.0.0', 'salesforce-mcp-server',
'Salesforce MCP Server',
'MCP Server to expose CRM data');
// Register components
server.registerTool(new LeadTool());
server.registerResource(new ProductResource());
server.registerPrompt(new CodeReviewPrompt());
Testing the MCP Server
To test the server, use the MCP Inspector tool.
Connecting to Your Server with MCP Inspector
- Enable and activate a public Salesforce Site, and add RestResourceMcpServer apex class to it.
- Open the terminal, and run "npx @modelcontextprotocol/inspector" (install the library if needed)
- After the webserver is started, select Transport Type: Streamable HTTP
- Enter your server URL (e.g.,
https://{instance_url}/services/apexrest/mcp
) - Click Connect
After a successful connection, you'll see the list of available resources, tools, and prompts, as shown in the screenshot below:
Connecting to Your Server with a Python client
- Install dependencies:
pip install aiohttp
- Run the python MCP Client
python client/mcp-client.py
- Results
Security Considerations (Salesforce MCP Apex Server)
- Uses
global without sharing
for the REST endpoint to allow external access - Individual components can implement their own sharing rules
- Recommend implementing proper authentication and authorization
- Consider rate limiting for production deployments
Usage Examples
Initialize Connection
POST /services/apexrest/mcp/
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"clientInfo": {
"name": "my-client",
"version": "1.0.0"
}
}
}
List Available Tools
POST /services/apexrest/mcp/
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list"
}
Execute a Tool
POST /services/apexrest/mcp/
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "LeadTool",
"arguments": {
"action": "create",
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com"
}
}
}
Deployment
- Deploy Apex Classes: Deploy the server classes to your Salesforce org
- Configure Remote Site Settings: If accessing external resources
- Set Up Connected Apps: For OAuth authentication if needed
- Test Endpoints: Verify the
/services/apexrest/mcp/
endpoint is accessible
Extending the Server
Adding New Tools
global class CustomTool extends Tool {
global override String getName() {
return 'CustomTool';
}
global override String call(Map<String, Object> arguments) {
// Implementation logic
return 'Tool execution result';
}
global override String toJson() {
// Tool definition in JSON format
}
}
Adding New Resources
global class CustomResource extends Resource {
global override Boolean isTemplate() {
return true; // or false for static resources
}
global override List<Content> read() {
// Resource reading logic
}
// Other required methods...
}
Protocol Compliance
This implementation follows the MCP specification version 2025-06-18
and supports:
- ā Server initialization and capability negotiation
- ā Tool listing and execution
- ā Resource listing and reading (including templates)
- ā Prompt listing and retrieval
- ā Structured error responses
- ā JSON-RPC 2.0 message format
Contributing
When extending this server:
- Follow Salesforce coding standards and best practices
- Implement proper error handling for all operations
- Add comprehensive test coverage
- Document new components and their usage
- Ensure security and sharing rules are properly configured
License
This project follows Salesforce's standard licensing terms. Consult your Salesforce licensing agreement for details.
For more information about the Model Context Protocol, visit the MCP specification documentation.