UNPKG

powerplatform-mcp

Version:

PowerPlatform Model Context Protocol server

181 lines (180 loc) 8.64 kB
import { outputResult } from '../output.js'; export function registerConfigurationCommands(program, registry) { program .command('connection-references') .description('List connection references in the environment') .option('--max-records <n>', 'Maximum records to return', '100') .option('--managed-only', 'Only show managed connection references') .option('--has-connection', 'Only show references with a connection set') .option('--no-connection', 'Only show references without a connection set (orphaned)') .option('--inactive', 'Only show inactive connection references') .action(async (opts, command) => { const ctx = registry.getContext(command.optsWithGlobals().env); // Resolve mutually exclusive connection flags let hasConnection; if (opts.hasConnection) { hasConnection = true; } else if (opts.connection === false) { hasConnection = false; } const service = ctx.getConfigurationService(); const result = await service.getConnectionReferences({ maxRecords: parseInt(opts.maxRecords, 10), managedOnly: opts.managedOnly, hasConnection, inactive: opts.inactive, }); const refs = result.value || []; // Count by connector type const connectorCounts = {}; for (const ref of refs) { const connectorId = String(ref.connectorid ?? 'Unknown'); connectorCounts[connectorId] = (connectorCounts[connectorId] || 0) + 1; } const breakdown = Object.entries(connectorCounts) .sort(([, a], [, b]) => b - a) .map(([connector, count]) => `${connector}: ${count}`) .join(', '); const nameList = refs .slice(0, 10) .map((r) => `${r.connectionreferencedisplayname || r.connectionreferencelogicalname} (${r.statecode === 0 ? 'Active' : 'Inactive'})`) .join('\n '); outputResult({ fileName: 'connection-references', data: result, summary: [ `Found ${refs.length} connection references:`, breakdown ? ` By connector: ${breakdown}` : '', refs.length > 0 ? ` References:\n ${nameList}${refs.length > 10 ? '\n ...' : ''}` : '', ].filter(Boolean).join('\n'), }, ctx.environmentName); }); program .command('create-connection-reference <logicalName> <displayName> <connectorId>') .description('Create a connection reference. Pass the full connectorId, e.g. /providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps') .option('--description <desc>', 'Description') .option('--solution <name>', 'Solution unique name (uses MSCRM.SolutionUniqueName so no separate add-solution-component is needed)') .action(async (logicalName, displayName, connectorId, opts, command) => { const ctx = registry.getContext(command.optsWithGlobals().env); const service = ctx.getConfigurationService(); const result = await service.createConnectionReference({ logicalName, displayName, connectorId, description: opts.description, solutionName: opts.solution, }); outputResult({ fileName: `create-connection-reference-${logicalName}`, data: result, summary: [ `Created connection reference:`, ` Logical Name: ${logicalName}`, ` Display Name: ${displayName}`, ` Connector: ${connectorId}`, opts.solution ? ` Solution: ${opts.solution}` : '', ` Connection Reference ID: ${result.connectionReferenceId}`, '', 'Next step: bind this connection reference to an actual connection in make.powerautomate.com.', ].filter(Boolean).join('\n'), }, ctx.environmentName); }); program .command('create-environment-variable <schemaName> <displayName>') .description('Create a new environment variable definition') .option('--type <type>', 'Variable type: String, Number, Boolean, JSON, DataSource', 'String') .option('--default-value <val>', 'Default value') .option('--description <desc>', 'Description') .option('--solution <name>', 'Solution unique name to add the component to') .action(async (schemaName, displayName, opts, command) => { const typeMap = { String: 100000000, Number: 100000001, Boolean: 100000002, JSON: 100000003, DataSource: 100000004, }; const ctx = registry.getContext(command.optsWithGlobals().env); const service = ctx.getConfigurationService(); const result = await service.createEnvironmentVariableDefinition({ schemaName, displayName, type: typeMap[opts.type] ?? 100000000, defaultValue: opts.defaultValue, description: opts.description, solutionName: opts.solution, }); outputResult({ fileName: `create-envvar-${schemaName}`, data: result, summary: [ `Created environment variable:`, ` Schema Name: ${schemaName}`, ` Display Name: ${displayName}`, ` Type: ${opts.type}`, ` Definition ID: ${result.definitionId}`, ].join('\n'), }, ctx.environmentName); }); program .command('set-environment-variable-value <definitionId> <value>') .description('Set or update the current value of an environment variable') .option('--existing-value-id <id>', 'Existing value record ID to update') .action(async (definitionId, value, opts, command) => { const ctx = registry.getContext(command.optsWithGlobals().env); const service = ctx.getConfigurationService(); const result = await service.setEnvironmentVariableValue({ definitionId, value, existingValueId: opts.existingValueId, }); outputResult({ fileName: `set-envvar-value-${definitionId}`, data: result, summary: [ `${opts.existingValueId ? 'Updated' : 'Created'} environment variable value:`, ` Definition ID: ${definitionId}`, ` Value ID: ${result.valueId}`, ].join('\n'), }, ctx.environmentName); }); program .command('environment-variables') .description('List environment variable definitions and their current values') .option('--max-records <n>', 'Maximum records to return', '100') .option('--managed-only', 'Only show managed environment variables') .action(async (opts, command) => { const ctx = registry.getContext(command.optsWithGlobals().env); const service = ctx.getConfigurationService(); const result = await service.getEnvironmentVariables({ maxRecords: parseInt(opts.maxRecords, 10), managedOnly: opts.managedOnly, }); const vars = result.value || []; // Map numeric type codes to labels const typeMap = { 100000000: 'String', 100000001: 'Number', 100000002: 'Boolean', 100000003: 'JSON', 100000004: 'Data Source', }; // Count by type const typeCounts = {}; for (const v of vars) { const t = typeMap[v.type] ?? `Unknown(${v.type})`; typeCounts[t] = (typeCounts[t] || 0) + 1; } const breakdown = Object.entries(typeCounts) .sort(([, a], [, b]) => b - a) .map(([type, count]) => `${type}: ${count}`) .join(', '); const nameList = vars .slice(0, 10) .map((v) => { const cv = v.currentValues; const hasOverride = cv && cv.length > 0; return `${v.displayname || v.schemaname} (${hasOverride ? 'overridden' : 'default'}, managed: ${v.ismanaged})`; }) .join('\n '); outputResult({ fileName: 'environment-variables', data: result, summary: [ `Found ${vars.length} environment variables:`, breakdown ? ` By type: ${breakdown}` : '', vars.length > 0 ? ` Variables:\n ${nameList}${vars.length > 10 ? '\n ...' : ''}` : '', ].filter(Boolean).join('\n'), }, ctx.environmentName); }); }