Skip to main content
Glama
simonl77

Salesforce MCP Server

by simonl77

salesforce_read_apex

Read Apex classes from Salesforce by name or pattern, retrieve code bodies, list classes, and access metadata like API version and modification dates.

Instructions

Read Apex classes from Salesforce.

Examples:

  1. Read a specific Apex class by name: { "className": "AccountController" }

  2. List all Apex classes with an optional name pattern: { "namePattern": "Controller" }

  3. Get metadata about Apex classes: { "includeMetadata": true, "namePattern": "Trigger" }

  4. Use wildcards in name patterns: { "namePattern": "AccountCont" }

Notes:

  • When className is provided, the full body of that specific class is returned

  • When namePattern is provided, all matching class names are returned (without body)

  • Use includeMetadata to get additional information like API version, length, and last modified date

  • If neither className nor namePattern is provided, all Apex class names will be listed

  • Wildcards are supported in namePattern: * (matches any characters) and ? (matches a single character)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
classNameNoName of a specific Apex class to read
namePatternNoPattern to match Apex class names (supports wildcards * and ?)
includeMetadataNoWhether to include metadata about the Apex classes

Implementation Reference

  • Main handler function that implements the core logic for reading Apex classes or listing them from Salesforce using SOQL queries on ApexClass object.
    export async function handleReadApex(conn: any, args: ReadApexArgs) {
      try {
        // If a specific class name is provided, get the full class body
        if (args.className) {
          console.error(`Reading Apex class: ${args.className}`);
          
          // Query the ApexClass object to get the class body
          const result = await conn.query(`
            SELECT Id, Name, Body, ApiVersion, LengthWithoutComments, Status, 
                   IsValid, LastModifiedDate, LastModifiedById
            FROM ApexClass 
            WHERE Name = '${args.className}'
          `);
          
          if (result.records.length === 0) {
            return {
              content: [{ 
                type: "text", 
                text: `No Apex class found with name: ${args.className}` 
              }],
              isError: true,
            };
          }
          
          const apexClass = result.records[0];
          
          // Format the response with the class body and metadata
          return {
            content: [
              { 
                type: "text", 
                text: `# Apex Class: ${apexClass.Name}\n\n` +
                      (args.includeMetadata ? 
                        `**API Version:** ${apexClass.ApiVersion}\n` +
                        `**Length:** ${apexClass.LengthWithoutComments} characters\n` +
                        `**Status:** ${apexClass.Status}\n` +
                        `**Valid:** ${apexClass.IsValid ? 'Yes' : 'No'}\n` +
                        `**Last Modified:** ${new Date(apexClass.LastModifiedDate).toLocaleString()}\n\n` : '') +
                      "```apex\n" + apexClass.Body + "\n```"
              }
            ]
          };
        } 
        // Otherwise, list classes matching the pattern
        else {
          console.error(`Listing Apex classes${args.namePattern ? ` matching: ${args.namePattern}` : ''}`);
          
          // Build the query
          let query = `
            SELECT Id, Name${args.includeMetadata ? ', ApiVersion, LengthWithoutComments, Status, IsValid, LastModifiedDate' : ''}
            FROM ApexClass
          `;
          
          // Add name pattern filter if provided
          if (args.namePattern) {
            const likePattern = wildcardToLikePattern(args.namePattern);
            query += ` WHERE Name LIKE '${likePattern}'`;
          }
          
          // Order by name
          query += ` ORDER BY Name`;
          
          const result = await conn.query(query);
          
          if (result.records.length === 0) {
            return {
              content: [{ 
                type: "text", 
                text: `No Apex classes found${args.namePattern ? ` matching: ${args.namePattern}` : ''}` 
              }]
            };
          }
          
          // Format the response as a list of classes
          let responseText = `# Found ${result.records.length} Apex Classes\n\n`;
          
          if (args.includeMetadata) {
            // Table format with metadata
            responseText += "| Name | API Version | Length | Status | Valid | Last Modified |\n";
            responseText += "|------|------------|--------|--------|-------|---------------|\n";
            
            for (const cls of result.records) {
              responseText += `| ${cls.Name} | ${cls.ApiVersion} | ${cls.LengthWithoutComments} | ${cls.Status} | ${cls.IsValid ? 'Yes' : 'No'} | ${new Date(cls.LastModifiedDate).toLocaleString()} |\n`;
            }
          } else {
            // Simple list format
            for (const cls of result.records) {
              responseText += `- ${cls.Name}\n`;
            }
          }
          
          return {
            content: [{ type: "text", text: responseText }]
          };
        }
      } catch (error) {
        console.error('Error reading Apex classes:', error);
        return {
          content: [{ 
            type: "text", 
            text: `Error reading Apex classes: ${error instanceof Error ? error.message : String(error)}` 
          }],
          isError: true,
        };
      }
    }
  • Tool schema definition including name, description, and input schema for validating arguments.
    export const READ_APEX: Tool = {
      name: "salesforce_read_apex",
      description: `Read Apex classes from Salesforce.
      
    Examples:
    1. Read a specific Apex class by name:
       {
         "className": "AccountController"
       }
    
    2. List all Apex classes with an optional name pattern:
       {
         "namePattern": "Controller"
       }
    
    3. Get metadata about Apex classes:
       {
         "includeMetadata": true,
         "namePattern": "Trigger"
       }
    
    4. Use wildcards in name patterns:
       {
         "namePattern": "Account*Cont*"
       }
    
    Notes:
    - When className is provided, the full body of that specific class is returned
    - When namePattern is provided, all matching class names are returned (without body)
    - Use includeMetadata to get additional information like API version, length, and last modified date
    - If neither className nor namePattern is provided, all Apex class names will be listed
    - Wildcards are supported in namePattern: * (matches any characters) and ? (matches a single character)`,
      inputSchema: {
        type: "object",
        properties: {
          className: {
            type: "string",
            description: "Name of a specific Apex class to read"
          },
          namePattern: {
            type: "string",
            description: "Pattern to match Apex class names (supports wildcards * and ?)"
          },
          includeMetadata: {
            type: "boolean",
            description: "Whether to include metadata about the Apex classes"
          }
        }
      }
    };
  • src/index.ts:227-238 (registration)
    Registration in the main switch dispatcher that maps tool calls to the handleReadApex function.
    case "salesforce_read_apex": {
      const apexArgs = args as Record<string, unknown>;
      
      // Type check and conversion
      const validatedArgs: ReadApexArgs = {
        className: apexArgs.className as string | undefined,
        namePattern: apexArgs.namePattern as string | undefined,
        includeMetadata: apexArgs.includeMetadata as boolean | undefined
      };
    
      return await handleReadApex(conn, validatedArgs);
    }
  • Helper function to convert wildcard patterns (*, ?) to SQL LIKE patterns for querying Apex class names.
    function wildcardToLikePattern(pattern: string): string {
      if (!pattern.includes('*') && !pattern.includes('?')) {
        // If no wildcards, wrap with % for substring match
        return `%${pattern}%`;
      }
      
      // Replace * with % and ? with _ for SQL LIKE
      let likePattern = pattern.replace(/\*/g, '%').replace(/\?/g, '_');
      
      return likePattern;
    }
  • src/index.ts:45-62 (registration)
    Tool registration in the listTools handler, including READ_APEX in the exported tools list.
    server.setRequestHandler(ListToolsRequestSchema, async () => ({
      tools: [
        SEARCH_OBJECTS, 
        DESCRIBE_OBJECT, 
        QUERY_RECORDS, 
        AGGREGATE_QUERY,
        DML_RECORDS,
        MANAGE_OBJECT,
        MANAGE_FIELD,
        MANAGE_FIELD_PERMISSIONS,
        SEARCH_ALL,
        READ_APEX,
        WRITE_APEX,
        READ_APEX_TRIGGER,
        WRITE_APEX_TRIGGER,
        EXECUTE_ANONYMOUS,
        MANAGE_DEBUG_LOGS
      ],
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden and does an excellent job disclosing behavioral traits. It explains what gets returned under different conditions (full body vs. names only), wildcard support, default behavior when no parameters are provided, and metadata inclusion details. The only minor gap is lack of information about authentication requirements or rate limits.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is perfectly structured with a clear purpose statement, numbered examples showing common use cases, and a notes section with behavioral details. Every sentence earns its place by providing specific guidance without redundancy. The information is front-loaded with the core purpose immediately stated.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a read-only tool with no output schema, the description provides excellent coverage of behavior, parameter interactions, and return formats. It explains what gets returned under different parameter combinations. The only minor gap is the lack of information about authentication or permissions needed to access Apex classes, which would be helpful given the Salesforce context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, so the schema already documents all parameters well. The description adds some value through examples showing how parameters interact (e.g., includeMetadata with namePattern) and clarifies that className and namePattern are mutually exclusive in their effects, but doesn't add significant semantic meaning beyond what's in the schema descriptions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose as 'Read Apex classes from Salesforce' with a specific verb ('Read') and resource ('Apex classes'). It distinguishes itself from siblings like salesforce_write_apex (write operation) and salesforce_read_apex_trigger (different resource type).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context on when to use different parameter combinations (e.g., className for full body, namePattern for matching names, includeMetadata for additional info). However, it doesn't explicitly mention when NOT to use this tool versus alternatives like salesforce_search_all or salesforce_query_records for different types of Salesforce data retrieval.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/simonl77/mcp-server-salesforce'

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