@salesforce/agents
Version:
Client side APIs for working with Salesforce agents
117 lines • 5.1 kB
JavaScript
;
/*
* Copyright 2026, 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiCatalog = void 0;
const core_1 = require("@salesforce/core");
const JSON_HEADERS = { 'Content-Type': 'application/json' };
function base(connection) {
return `/services/data/v${String(connection.version)}/api-catalog`;
}
function encode(...segments) {
return segments.map((s) => encodeURIComponent(s)).join('/');
}
/**
* Thin client over the API Catalog Connect API. Every method is a one-to-one
* wrapper around a Connect endpoint via `connection.request`, mirroring the
* shape of `AgentDataLibrary` from `@salesforce/agents`.
*/
class ApiCatalog {
// ── MCP servers (CRUD) ──────────────────────────────────────
/** GET /api-catalog/mcp-servers */
static async listMcpServers(connection, options) {
const params = new URLSearchParams();
if (options?.label)
params.set('label', options.label);
if (options?.type)
params.set('type', options.type);
if (options?.status)
params.set('status', options.status);
const qs = params.toString();
const url = qs ? `${base(connection)}/mcp-servers?${qs}` : `${base(connection)}/mcp-servers`;
return ApiCatalog.request(connection, 'GET', url, 'listMcpServers');
}
/** POST /api-catalog/mcp-servers */
static async createMcpServer(connection, input) {
return ApiCatalog.request(connection, 'POST', `${base(connection)}/mcp-servers`, 'createMcpServer', input);
}
/** GET /api-catalog/mcp-servers/{id} */
static async getMcpServer(connection, id) {
const url = `${base(connection)}/mcp-servers/${encode(id)}`;
return ApiCatalog.request(connection, 'GET', url, 'getMcpServer');
}
/** PUT /api-catalog/mcp-servers/{id} */
static async updateMcpServer(connection, id, input) {
const url = `${base(connection)}/mcp-servers/${encode(id)}`;
return ApiCatalog.request(connection, 'PUT', url, 'updateMcpServer', input);
}
/** DELETE /api-catalog/mcp-servers/{id} */
static async deleteMcpServer(connection, id) {
const url = `${base(connection)}/mcp-servers/${encode(id)}`;
await ApiCatalog.request(connection, 'DELETE', url, 'deleteMcpServer');
}
/** POST /api-catalog/mcp-servers/{id}/fetch */
static async fetchMcpServer(connection, id) {
const url = `${base(connection)}/mcp-servers/${encode(id)}/fetch`;
return ApiCatalog.request(connection, 'POST', url, 'fetchMcpServer');
}
/** GET /api-catalog/mcp-servers/{id}/assets */
static async listMcpServerAssets(connection, id) {
const url = `${base(connection)}/mcp-servers/${encode(id)}/assets`;
return ApiCatalog.request(connection, 'GET', url, 'listMcpServerAssets');
}
/** PUT /api-catalog/mcp-servers/{id}/assets */
static async replaceMcpServerAssets(connection, id, input) {
const url = `${base(connection)}/mcp-servers/${encode(id)}/assets`;
return ApiCatalog.request(connection, 'PUT', url, 'replaceMcpServerAssets', input);
}
// ── Shared request helper ───────────────────────────────────
static async request(connection, method, url, op, body) {
const req = {
method,
url,
};
if (body !== undefined) {
req.body = JSON.stringify(body);
req.headers = JSON_HEADERS;
}
let result;
try {
result = await connection.request(req);
}
catch (error) {
const wrapped = core_1.SfError.wrap(error);
await ApiCatalog.emitTelemetry(`api_catalog_${op}_failed`);
throw wrapped;
}
// Emit a success event for mutating operations, mirroring AgentDataLibrary.
if (method !== 'GET') {
await ApiCatalog.emitTelemetry(`api_catalog_${op}_success`);
}
return result;
}
/** Best-effort telemetry — never let a telemetry failure mask the real result or error. */
static async emitTelemetry(eventName) {
try {
await core_1.Lifecycle.getInstance().emitTelemetry({ eventName });
}
catch {
// telemetry is best-effort
}
}
}
exports.ApiCatalog = ApiCatalog;
//# sourceMappingURL=apiCatalog.js.map