UNPKG

@sinch/mcp

Version:

Sinch MCP server

96 lines 5.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setTemplateRegion = exports.setConversationRegion = exports.getConversationAppId = void 0; exports.getConversationClient = getConversationClient; exports.getConversationTemplateClient = getConversationTemplateClient; const sdk_core_1 = require("@sinch/sdk-core"); const process_1 = __importDefault(require("process")); const types_1 = require("../../../types"); const utils_1 = require("../../../utils"); function getConversationClient(toolName) { return getSinchClient(sdk_core_1.CONVERSATION_HOSTNAME, toolName, (client, fetcher, hostname) => configureConversationApis(client, fetcher, hostname)); } function getConversationTemplateClient(toolName) { return getSinchClient(sdk_core_1.CONVERSATION_TEMPLATES_HOSTNAME, toolName, (client, fetcher, hostname) => configureTemplatesApis(client, fetcher, hostname)); } /** Shared helper for both “conversation” and “templates” */ function getSinchClient(hostnameTemplate, toolName, configure) { const projectId = process_1.default.env.CONVERSATION_PROJECT_ID; const keyId = process_1.default.env.CONVERSATION_KEY_ID; const keySecret = process_1.default.env.CONVERSATION_KEY_SECRET; if (!projectId || !keyId || !keySecret) { return new types_1.PromptResponse('Missing env vars: CONVERSATION_PROJECT_ID, CONVERSATION_KEY_ID, CONVERSATION_KEY_SECRET.'); } const client = new sdk_core_1.SinchClient({}); const fetcher = new sdk_core_1.ApiFetchClient({ projectId, requestPlugins: [ new sdk_core_1.Oauth2TokenRequest(keyId, keySecret), new sdk_core_1.AdditionalHeadersRequest({ headers: (0, sdk_core_1.buildHeader)('User-Agent', (0, utils_1.formatUserAgent)(toolName, projectId)), }), ], }); // Remove the VersionRequest plugin, as we override the user-agent header fetcher.apiClientOptions.requestPlugins?.shift(); // Replace the region placeholder with US by default const hostname = hostnameTemplate.replace(sdk_core_1.REGION_PATTERN, `${sdk_core_1.ConversationRegion.UNITED_STATES}.`); configure(client, fetcher, hostname); return client; } const addPropertiesToApi = (api, client, hostname) => { api.client = client; api.setHostname(hostname); }; const configureConversationApis = (sinchClient, apiFetchClient, hostnameTemplate) => { const hostname = hostnameTemplate.replace(sdk_core_1.REGION_PATTERN, `${sdk_core_1.ConversationRegion.UNITED_STATES}.`); const apis = [ sinchClient.conversation.app, sinchClient.conversation.contact, sinchClient.conversation.conversation, sinchClient.conversation.messages, sinchClient.conversation.events, sinchClient.conversation.capability, sinchClient.conversation.transcoding, sinchClient.conversation.webhooks, ]; apis.forEach((api) => addPropertiesToApi(api, apiFetchClient, hostname)); }; const configureTemplatesApis = (sinchClient, apiFetchClient, hostnameTemplate) => { const hostname = hostnameTemplate.replace(sdk_core_1.REGION_PATTERN, `${sdk_core_1.ConversationRegion.UNITED_STATES}.`); addPropertiesToApi(sinchClient.conversation.templatesV2, apiFetchClient, hostname); }; const getConversationAppId = (appId) => { if (!appId) { appId = process_1.default.env.CONVERSATION_APP_ID; if (!appId) { return new types_1.PromptResponse('The "CONVERSATION_APP_ID" is not set in the environment variables and the "appId" property is not provided.'); } } return appId; }; exports.getConversationAppId = getConversationAppId; const setConversationRegion = (promptRegion, sinchClient) => { const region = promptRegion ?? process_1.default.env.CONVERSATION_REGION ?? sdk_core_1.ConversationRegion.UNITED_STATES; sinchClient.conversation.setRegion(region); const formattedRegion = region !== '' ? `${region}.` : ''; const hostname = (0, sdk_core_1.formatRegionalizedHostname)(sdk_core_1.CONVERSATION_HOSTNAME, formattedRegion); sinchClient.conversation.messages.client.apiClientOptions.hostname = hostname; sinchClient.conversation.messages.sinchClientParameters.conversationHostname = hostname; sinchClient.conversation.app.client.apiClientOptions.hostname = hostname; sinchClient.conversation.app.sinchClientParameters.conversationHostname = hostname; }; exports.setConversationRegion = setConversationRegion; const setTemplateRegion = (promptRegion, sinchClient) => { const region = promptRegion ?? process_1.default.env.CONVERSATION_REGION ?? sdk_core_1.ConversationRegion.UNITED_STATES; sinchClient.conversation.setRegion(region); const formattedRegion = region !== '' ? `${region}.` : ''; const hostname = (0, sdk_core_1.formatRegionalizedHostname)(sdk_core_1.CONVERSATION_TEMPLATES_HOSTNAME, formattedRegion); sinchClient.conversation.templatesV2.client.apiClientOptions.hostname = hostname; sinchClient.conversation.templatesV2.sinchClientParameters.conversationTemplatesHostname = hostname; }; exports.setTemplateRegion = setTemplateRegion; //# sourceMappingURL=conversation-service-helper.js.map