Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

feat(microsoft-sql-server): add Entra ID service principal auth for Azure SQL (#15681)

Kishan Parmar60b124739d72
diff --git a/bun.lock b/bun.lock
index 0d57fee7e5a..14089e0f2d0 100644
--- a/bun.lock
+++ b/bun.lock
@@ -5881,7 +5881,7 @@
},
"packages/pieces/community/microsoft-sql-server": {
"name": "@activepieces/piece-microsoft-sql-server",
- "version": "0.0.2",
+ "version": "0.0.3",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
diff --git a/packages/pieces/community/microsoft-sql-server/package.json b/packages/pieces/community/microsoft-sql-server/package.json
index 50da512d984..6f8cc6e3990 100644
--- a/packages/pieces/community/microsoft-sql-server/package.json
+++ b/packages/pieces/community/microsoft-sql-server/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-microsoft-sql-server",
- "version": "0.0.2",
+ "version": "0.0.3",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/community/microsoft-sql-server/src/lib/auth.ts b/packages/pieces/community/microsoft-sql-server/src/lib/auth.ts
index 5fb39e15a5d..f5f1fe6df60 100644
--- a/packages/pieces/community/microsoft-sql-server/src/lib/auth.ts
+++ b/packages/pieces/community/microsoft-sql-server/src/lib/auth.ts
@@ -6,8 +6,9 @@ import {
import { mssqlCommon } from './common';
export const mssqlAuth = PieceAuth.CustomAuth({
- description:
- 'For Azure SQL, use the Connection String field: in the portal open your database, choose Connection strings, and copy the ADO.NET (SQL authentication) one — not an Active Directory variant. Replace the {your_password} placeholder, and quote a password containing a semicolon: Password="my;pass";.',
+ description: `**Connection String (Azure SQL, SQL authentication):** in the portal open your database, choose Connection strings, and copy the ADO.NET (SQL authentication) one, not an Active Directory variant. Replace the {your_password} placeholder, and quote a password containing a semicolon: Password="my;pass";.
+
+**Microsoft Entra ID (service principal):** leave Username and Password empty, and fill in Tenant ID, Client ID and Client Secret from an app registration instead. See [Create Microsoft Entra Users Using Service Principals](https://learn.microsoft.com/en-us/azure/azure-sql/database/authentication-aad-service-principal?view=azuresql) for how to register the app and grant it a database user.`,
props: {
connection_string: Property.LongText({
displayName: 'Connection String',
@@ -35,7 +36,7 @@ export const mssqlAuth = PieceAuth.CustomAuth({
user: Property.ShortText({
displayName: 'Username',
description:
- 'The SQL Server login to authenticate as. On Azure SQL this is usually user@servername.',
+ 'The SQL Server login to authenticate as. On Azure SQL this is usually user@servername. Leave empty when authenticating with Tenant ID, Client ID and Client Secret below.',
required: false,
}),
password: PieceAuth.SecretText({
@@ -43,6 +44,22 @@ export const mssqlAuth = PieceAuth.CustomAuth({
description: 'The password for the login above.',
required: false,
}),
+ tenant_id: Property.ShortText({
+ displayName: 'Tenant ID',
+ description:
+ 'Microsoft Entra ID tenant ID, for authenticating as a service principal instead of a SQL login. Requires Client ID and Client Secret below, and leaves Username and Password empty.',
+ required: false,
+ }),
+ client_id: Property.ShortText({
+ displayName: 'Client ID',
+ description: 'Application (client) ID of the Entra ID app registration.',
+ required: false,
+ }),
+ client_secret: PieceAuth.SecretText({
+ displayName: 'Client Secret',
+ description: 'Client secret value of the Entra ID app registration.',
+ required: false,
+ }),
encrypt: Property.Checkbox({
displayName: 'Encrypt Connection',
description:
diff --git a/packages/pieces/community/microsoft-sql-server/src/lib/common/build-config.test.ts b/packages/pieces/community/microsoft-sql-server/src/lib/common/build-config.test.ts
new file mode 100644
index 00000000000..58749a847cd
--- /dev/null
+++ b/packages/pieces/community/microsoft-sql-server/src/lib/common/build-config.test.ts
@@ -0,0 +1,80 @@
+import { AppConnectionType } from '@activepieces/pieces-framework';
+import { describe, expect, it } from 'vitest';
+import { mssqlCommon, MssqlAuth } from '.';
+
+function auth(props: Partial<MssqlAuth['props']>): MssqlAuth {
+ return {
+ type: AppConnectionType.CUSTOM_AUTH,
+ props: {
+ encrypt: true,
+ trust_server_certificate: false,
+ ...props,
+ },
+ } as MssqlAuth;
+}
+
+describe('buildConfig', () => {
+ it('builds an Entra ID service-principal config when tenant, client id and secret are set', () => {
+ const config = mssqlCommon.buildConfig({
+ auth: auth({
+ host: 'myserver.database.windows.net',
+ database: 'mydb',
+ tenant_id: 'tenant-1',
+ client_id: 'client-1',
+ client_secret: 'secret-1',
+ }),
+ });
+
+ expect(config.authentication).toEqual({
+ type: 'azure-active-directory-service-principal-secret',
+ options: {
+ clientId: 'client-1',
+ clientSecret: 'secret-1',
+ tenantId: 'tenant-1',
+ },
+ });
+ expect(config.user).toBeUndefined();
+ expect(config.password).toBeUndefined();
+ expect(config.server).toBe('myserver.database.windows.net');
+ });
+
+ it('rejects a partial service-principal configuration', () => {
+ expect(() =>
+ mssqlCommon.buildConfig({
+ auth: auth({
+ host: 'myserver.database.windows.net',
+ tenant_id: 'tenant-1',
+ client_id: 'client-1',
+ }),
+ })
+ ).toThrow(
+ 'Tenant ID, Client ID and Client Secret are all required to authenticate with Microsoft Entra ID.'
+ );
+ });
+
+ it('requires a host for service-principal authentication', () => {
+ expect(() =>
+ mssqlCommon.buildConfig({
+ auth: auth({
+ tenant_id: 'tenant-1',
+ client_id: 'client-1',
+ client_secret: 'secret-1',
+ }),
+ })
+ ).toThrow('Host is required to authenticate with Microsoft Entra ID.');
+ });
+
+ it('still builds SQL-auth config when no service-principal fields are set', () => {
+ const config = mssqlCommon.buildConfig({
+ auth: auth({
+ host: 'myserver.database.windows.net',
+ user: 'sa',
+ password: 'pw',
+ }),
+ });
+
+ expect(config.authentication).toBeUndefined();
+ expect(config.user).toBe('sa');
+ expect(config.password).toBe('pw');
+ });
+});
diff --git a/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts b/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts
index bf6675364c3..2fad7f1ce3c 100644
--- a/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts
+++ b/packages/pieces/community/microsoft-sql-server/src/lib/common/index.ts
@@ -37,6 +37,9 @@ function buildConfig({
database,
user,
password,
+ tenant_id,
+ client_id,
+ client_secret,
encrypt,
trust_server_certificate,
certificate,
@@ -64,7 +67,7 @@ function buildConfig({
const entra = trimmed.match(/Authentication\s*=\s*(Active Directory[^;]*)/i);
if (entra) {
throw new Error(
- `This piece supports SQL Server authentication only, but the connection string asks for "${entra[1].trim()}". Copy the ADO.NET (SQL authentication) string from the Azure portal instead, or fill in the Username and Password fields.`
+ `The Connection String field does not support "${entra[1].trim()}". Copy the ADO.NET (SQL authentication) string from the Azure portal instead and replace the credentials inside that string, including the {your_password} placeholder, or clear the Connection String and fill in Host, Tenant ID, Client ID and Client Secret to authenticate with Microsoft Entra ID.`
);
}
const parsed = sql.ConnectionPool.parseConnectionString(trimmed);
@@ -81,6 +84,39 @@ function buildConfig({
return parsed;
}
+ if (tenant_id || client_id || client_secret) {
+ if (!tenant_id || !client_id || !client_secret) {
+ throw new Error(
+ 'Tenant ID, Client ID and Client Secret are all required to authenticate with Microsoft Entra ID.'
+ );
+ }
+ if (!host) {
+ throw new Error(
+ 'Host is required to authenticate with Microsoft Entra ID.'
+ );
+ }
+ return {
+ server: host,
+ port: port ? Number(port) : DEFAULT_PORT,
+ database: database || undefined,
+ connectionTimeout: TIMEOUT_MS,
+ requestTimeout,
+ authentication: {
+ type: 'azure-active-directory-service-principal-secret',
+ options: {
+ clientId: client_id,
+ clientSecret: client_secret,
+ tenantId: tenant_id,
+ },
+ },
+ options: {
+ encrypt: encrypt ?? true,
+ trustServerCertificate: trust_server_certificate ?? false,
+ cryptoCredentialsDetails,
+ },
+ };
+ }
+
if (!host || !user || !password) {
throw new Error(
'Host, Username and Password are required unless a Connection String is provided.'