Salesforce-Hosted-Custom-Mcp-Server
Enables interaction with Salesforce data and services via custom MCP tools, including account analytics, opportunity queries, case creation, and AI agent invocation.
README
Salesforce Hosted Custom MCP Server
A reference SFDX project demonstrating every supported pattern for building a Salesforce-hosted custom MCP (Model Context Protocol) server. Each McpServerDefinition shows a different way to expose Salesforce functionality — Apex classes, Flows, Named Queries, and Prompt Templates — as tools that any MCP-compatible client (Claude Desktop, OpenAI, Cursor, etc.) can discover and call.
Blog series: Salesforce Diaries by Sanket
What is a Salesforce Hosted MCP Server?
Salesforce lets you publish a standard MCP endpoint directly from your org — no middleware, no Lambda, no Node server. You define what tools are exposed through McpServerDefinition metadata, and Salesforce handles the authentication, schema generation, and HTTP transport. An external AI agent connects to https://<your-org>.my.salesforce.com/services/mcp/v1/<serverName> using OAuth and gets a live, permissioned connection to your data.
Project Structure
force-app/main/default/
├── mcpServerDefinitions/ # 14 MCP server definitions (one per pattern)
├── externalServiceRegistrations/ # OpenAPI registrations for each Apex endpoint
├── flows/
│ └── Get_Account_By_Name.flow-meta.xml
├── classes/ # 7 Apex classes backing the MCP tools
└── genAiPromptTemplates/ # 4 Prompt Builder templates exposed as MCP prompts
sfdx-project.json
MCP Server Definitions
Each definition registers one MCP server in your org and declares which tools and prompts it exposes.
1. FlowAsMcpService — Flow as MCP Tool
Exposes the Get_Account_By_Name AutoLaunchedFlow as a tool.
Pattern: fa:flow-<FlowApiName> (Flow via API Catalog)
Tool → Get_Account_By_Name
2. FlowMCP — Flow MCP (variant)
A second server exposing the same Get_Account_By_Name flow, demonstrating that multiple servers can surface the same underlying action.
Pattern: fa:flow-<FlowApiName>
3. InvocableApex — Invocable Apex + Prompt Templates
Combines an @InvocableMethod tool with three Prompt Builder prompts in one server.
Pattern: aa:apex-<ClassName> + promptTemplateName
Tool → CreateCaseAction
Prompt → Draft_IT_Troubleshooting
Prompt → Get_Event_Info
Prompt → Case_Research_from_Web
4. AuraEnabledApexMCP — @AuraEnabled Method
Exposes the getHighValueOpportunities method from OpportunityMcpService (annotated @AuraEnabled).
Pattern: ae:<ClassName>
Tool → getHighValueOpportunities (OpportunityMcpService)
5. AuraEnbaledClassMcp — @AuraEnabled (alternate class)
Same tool pattern as above but backed by OpportunityMcpServiceAuraEnbaled.
Pattern: ae:<ClassName>
Tool → getHighValueOpportunities (OpportunityMcpServiceAuraEnbaled)
6. PublicInvocableApexMCP — @AuraEnabled for Account Analytics
Exposes AccountAnalytics.invoke — a multi-mode analytics tool that runs aggregations over Account records.
Pattern: ae:<ClassName>
Tool → invoke (AccountAnalytics)
7. AIAgentMcpServer — Talk to an Agentforce Agent
Lets an MCP client send a natural-language message to an Agentforce agent and get its reply. Supports multi-turn sessions via sessionId.
Pattern: ae:<ClassName>
Tool → askAgent (InvokeAgentAction)
8. CaseCreationInvocableMcpService — Create Cases via Invocable
Exposes CreateCaseAction as an MCP tool so external agents can open support cases directly.
Pattern: aa:apex-<ClassName>
Tool → CreateCaseAction
9. NamedQueryMcpService — Named Query
Exposes a Named Query (GetOpportunitiesByAmount) defined in the org as a zero-code MCP tool.
Pattern: nq:<QueryApiName>
Tool → GetOpportunitiesByAmount
10. PromptTemplateMcpServer — Named Query + Prompt Template
Mixes a Named Query tool with a Prompt Builder prompt in one server.
Pattern: nq:<QueryApiName> + promptTemplateName
Tool → GetOpportunitiesByAmount
Prompt → Research_on_Case_subject
11. RestResourceApexMCP — @RestResource Apex
Exposes MyCustomAPI — a @RestResource-annotated class — as an MCP tool.
Pattern: ar:<ClassName>
Tool → doGet (MyCustomAPI)
12. PromptMcpServer — Prompt-Only Server
A server that exposes only a Prompt Builder template, with no tool.
Pattern: promptTemplateName
Prompt → Case_Research_from_Web
13. AgentforceMCP — Agentforce Agent Shell
Skeleton server demonstrating how to declare an Agentforce-connected MCP server.
14. RestAPIMCP — REST API Shell
Skeleton server for REST-based MCP tool patterns.
Apex Classes
| Class | Annotation | What it does |
|---|---|---|
OpportunityMcpService |
@AuraEnabled |
Returns Opportunities with Amount > threshold (default $50k), ordered largest first. Enforces sharing and FLS via WITH USER_MODE. |
OpportunityMcpServiceAuraEnbaled |
@AuraEnabled |
Variant of the above, demonstrating the same pattern in a separate class. |
AccountAnalytics |
@InvocableMethod |
Runs one of four aggregation modes over Account: top_revenue, by_industry, recent, or health_summary. Returns a human-readable summary string. |
CreateCaseAction |
@InvocableMethod |
Creates a Salesforce Case from a subject line. Bulk-safe; re-queries to return the auto-assigned Case Number. |
InvokeAgentAction |
@InvocableMethod |
Sends a natural-language message to any active Agentforce agent via generateAiAgentResponse. Returns the agent's reply and a sessionId for multi-turn conversations. Default agent: Case_CRUD_Agent. |
MyCustomAPI |
@RestResource |
GET /services/apexrest/MyCustomAPI/ — returns Opportunities above a configurable minAmount threshold. Used by RestResourceApexMCP. |
PromptTemplateMcpService |
@AuraEnabled |
Runs a named Prompt Builder template against a record using ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate. Returns the generated text. |
Flow
Get_Account_By_Name
Type: AutoLaunchedFlow
Input: AccountName (String)
Output: AccountDetails (Account SObject)
Queries the first Account whose Name equals the input string and returns the full record. Used by FlowAsMcpService and FlowMCP.
Prompt Templates (genAiPromptTemplates)
These Prompt Builder templates are registered on MCP servers as prompts — the MCP equivalent of a reusable, parameterised instruction that a client can invoke by name.
| Template | Referenced by |
|---|---|
Draft_IT_Troubleshooting |
InvocableApex |
Get_Event_Info |
InvocableApex |
Case_Research_from_Web |
InvocableApex, PromptMcpServer |
Research_on_Case_subject |
PromptTemplateMcpServer |
External Service Registrations
OpenAPI-based registrations that describe the shape of each Apex endpoint so Salesforce can generate the tool schemas for the MCP catalog.
| Registration | Apex class |
|---|---|
AccountAnalytics |
AccountAnalytics |
GetCaseByCaseNumber |
Case lookup endpoint |
GetOpportunitiesByAmount |
Named Query |
InvokeAgentAction |
InvokeAgentAction |
MyCustomAPI |
MyCustomAPI |
OpportunityMcpService |
OpportunityMcpService |
OpportunityMcpServiceAuraEnbaled |
OpportunityMcpServiceAuraEnbaled |
PromptTemplateMcpService |
PromptTemplateMcpService |
Deploy
Prerequisites
- Salesforce CLI (
sf) v2+ - API version 66.0 org (Summer '25+)
- "Salesforce Hosted MCP Servers" feature enabled in your org
Deploy all metadata
sf project deploy start --source-dir force-app
Deploy a specific server only
sf project deploy start --metadata McpServerDefinition:FlowAsMcpService
Connect a client (example: Claude Desktop)
Add this to your claude_desktop_config.json:
{
"mcpServers": {
"salesforce-flow": {
"url": "https://<your-org>.my.salesforce.com/services/mcp/v1/FlowAsMcpService",
"transport": "http",
"headers": {
"Authorization": "Bearer <access_token>"
}
}
}
}
References
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。