Skip to main content
Glama

salesforce_query

Execute SOQL queries to retrieve and analyze Salesforce data in JSON format, enabling direct access to organization records and information.

Instructions

Run a SOQL query and return JSON rows

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
argsYes

Implementation Reference

  • The core asynchronous handler function for the 'salesforce_query' MCP tool. It uses SalesforceClient to run the SOQL query and returns results as QueryResult.
    async def salesforce_query(args: QueryArgs) -> QueryResult:
        sf = SalesforceClient.from_env()
        rows = await sf.run_soql(args.soql)
        if args.max_records is not None:
            rows = rows[: args.max_records]
        return QueryResult(total_size=len(rows), records=rows)
  • Pydantic models for input (QueryArgs: soql and optional max_records) and output (QueryResult: total_size and records list) schema validation.
    class QueryArgs(BaseModel):
        soql: str = Field(..., description="SOQL query string")
        max_records: int | None = Field(None, ge=1, le=50000)
    
    
    class QueryResult(BaseModel):
        total_size: int
        records: List[Dict[str, Any]]
  • Module register function using FastMCP @tool decorator to define and register the salesforce_query tool.
    def register(mcp: FastMCP) -> None:
        @mcp.tool(
            name="salesforce_query", description="Run a SOQL query and return JSON rows"
        )
        async def salesforce_query(args: QueryArgs) -> QueryResult:
            sf = SalesforceClient.from_env()
            rows = await sf.run_soql(args.soql)
            if args.max_records is not None:
                rows = rows[: args.max_records]
            return QueryResult(total_size=len(rows), records=rows)
  • sfmcp/server.py:23-33 (registration)
    Top-level registration in server.py: imports tool_query from .tools.query and calls tool_query.register(mcp) to activate the salesforce_query tool among others.
    def _register_all() -> None:
        tool_query.register(mcp)
        tool_describe.register(mcp)
        tool_list_objects.register(mcp)
        tool_list_flows.register(mcp)
        tool_list_reports.register(mcp)
        tool_list_dashboards.register(mcp)
        tool_describe_flow.register(mcp)
        # res_saved_queries.register(mcp)
        # prm_opps_by_stage.register(mcp)
  • Key helper method in SalesforceClient that executes the SOQL query using Salesforce CLI subprocess and parses the JSON response to return records.
    async def run_soql(self, soql: str) -> List[Dict[str, Any]]:
        """Run a SOQL query and return the records"""
        command = [
            "sf",
            "data",
            "query",
            "--target-org",
            self._org_alias,
            "--query",
            soql,
            "--result-format",
            "json",
        ]
        result = await self._run_cli_command(command)
    
        if "result" in result and "records" in result["result"]:
            return result["result"]["records"]  # type: ignore[no-any-return]
        else:
            raise Exception("Unexpected response format from Salesforce CLI")

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mattmahowald/sfmcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server