Skip to content

Do not merge - POC - W-19294210 #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/mcp.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"servers": {
"salesforce": {
"command": "node",
"args": ["${workspaceFolder}/bin/run.js", "--toolsets", "all", "--orgs", "DEFAULT_TARGET_ORG", "--no-telemetry"]
"args": ["./bin/run.js", "--toolsets", "all", "--orgs", "ALLOW_ALL_ORGS", "--no-telemetry"]
}
}
}
6 changes: 5 additions & 1 deletion src/shared/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ USAGE:
...for my '[email protected]' user
...for the '[email protected]' org`);

export const useToolingApiParam = z.boolean().optional().describe('Use Tooling API for the operation');
export const useToolingApiParam = z
.boolean()
.optional()
.default(false)
.describe('Use Tooling API for the operation (default is false).');

export const baseAbsolutePathParam = z
.string()
Expand Down
82 changes: 82 additions & 0 deletions src/tools/EXTERNAL/external-tool-query-org.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2025, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* External tool logic for querying Salesforce orgs
* This file contains only the pure business logic that can be exported to external repos
*/

import { type Connection } from '@salesforce/core';
import { type CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import { z } from 'zod';

// Define the parameter schema for this external tool
export const queryOrgParamsSchema = z.object({
query: z.string().describe('SOQL query to run'),
useToolingApi: z
.boolean()
.optional()
.default(false)
.describe('Use Tooling API for the operation (default is false).'),
});

export type ExternalQueryOrgParams = z.infer<typeof queryOrgParamsSchema>;

// Description of the external tool
export const queryOrgDescription = 'Run a SOQL query against a Salesforce org.';

// Annotations for the external tool
// https://modelcontextprotocol.io/specification/2025-06-18/schema#toolannotations
export const queryOrgAnnotations = {
title: 'Query Org',
openWorldHint: false,
readOnlyHint: true,
};

// Logic-only function that can be imported in external MCP Servers
export const queryOrgExecutable = async (
params: ExternalQueryOrgParams,
config: {
connection: Connection;
}
): Promise<CallToolResult> => {
const { query, useToolingApi } = params;
const { connection } = config;

try {
const result = useToolingApi ? await connection.tooling.query(query) : await connection.query(query);

return {
isError: false,
content: [
{
type: 'text',
text: `SOQL query results:\n\n${JSON.stringify(result, null, 2)}`,
},
],
};
} catch (error) {
return {
isError: true,
content: [
{
type: 'text',
text: `Failed to query org: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
};
}
};
2 changes: 1 addition & 1 deletion src/tools/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './sf-query-org.js';
export * from './sf-query-org-using-external.js';
67 changes: 67 additions & 0 deletions src/tools/data/sf-query-org-using-external.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2025, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { z } from 'zod';

import { getConnection } from '../../shared/auth.js';
import { directoryParam, usernameOrAliasParam, useToolingApiParam } from '../../shared/params.js';
import { SfMcpServer } from '../../sf-mcp-server.js';
import {
queryOrgDescription,
queryOrgAnnotations,
queryOrgExecutable,
queryOrgParamsSchema as queryOrgParamsSchemaExternal,
} from '../EXTERNAL/external-tool-query-org.js';

export const queryOrgParamsSchema = z.object({
// Shared parameters (used only in tool setup)
usernameOrAlias: usernameOrAliasParam,
directory: directoryParam,
// Shared parameters (passed through to external tool)
useToolingApi: useToolingApiParam,
// External tool parameters (passed through to external tool)
query: queryOrgParamsSchemaExternal.shape.query,
});

export type QueryOrgOptions = z.infer<typeof queryOrgParamsSchema>;

export const registerToolQueryOrg = (server: SfMcpServer): void => {
server.tool(
'sf-query-org',
`${queryOrgDescription}

EXAMPLES:
...query Contacts that have a Phone listed
...find the 3 newest Property__c records`,
queryOrgParamsSchema.shape,
queryOrgAnnotations,
async ({ directory, usernameOrAlias, query, useToolingApi }) => {
process.chdir(directory);
const connection = await getConnection(usernameOrAlias);

const passThroughParams = {
query,
useToolingApi,
};

const passThroughConfig = {
connection,
};

return queryOrgExecutable(passThroughParams, passThroughConfig);
}
);
};
Loading