datasecops-cli 0.7.0b7__tar.gz → 0.7.0b8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/.github/workflows/auto-tag.yml +1 -1
  2. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/.github/workflows/test.yml +1 -1
  3. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/CHANGELOG.md +4 -0
  4. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/PKG-INFO +2 -2
  5. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/docs/mcp-server.md +7 -3
  6. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/pyproject.toml +2 -2
  7. datasecops_cli-0.7.0b8/src/datasecops_cli/__init__.py +1 -0
  8. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_mcp/server.py +143 -71
  9. datasecops_cli-0.7.0b8/tests/test_mcp_lint_tools.py +200 -0
  10. datasecops_cli-0.7.0b7/src/datasecops_cli/__init__.py +0 -1
  11. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/.github/workflows/publish-cli.yml +0 -0
  12. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/.gitignore +0 -0
  13. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/AGENTS.md +0 -0
  14. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/DEVELOPMENT.md +0 -0
  15. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/LICENSE +0 -0
  16. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/README.md +0 -0
  17. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/docs/dcm.md +0 -0
  18. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/docs/getting-started.md +0 -0
  19. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/docs/git-operations.md +0 -0
  20. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/docs/legacy.md +0 -0
  21. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/docs/legacy_plan_of_action.md +0 -0
  22. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/mcp-servers.json +0 -0
  23. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/scripts/vendor_model.py +0 -0
  24. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/setup.ps1 +0 -0
  25. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/setup.sh +0 -0
  26. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/config.py +0 -0
  27. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/main.py +0 -0
  28. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/menus/__init__.py +0 -0
  29. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/menus/configuration.py +0 -0
  30. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/menus/dcm_operations.py +0 -0
  31. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/menus/development.py +0 -0
  32. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/menus/downloads.py +0 -0
  33. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/menus/git_operations.py +0 -0
  34. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/models/__init__.py +0 -0
  35. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/models/git_helpers.py +0 -0
  36. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/models/project_config.py +0 -0
  37. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/__init__.py +0 -0
  38. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/bootstrap_service.py +0 -0
  39. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/dbt_project_generator.py +0 -0
  40. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/dbt_runner.py +0 -0
  41. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/dcm_orchestrator.py +0 -0
  42. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/dcm_pipelines.py +0 -0
  43. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/dcm_registry.py +0 -0
  44. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/dcm_service.py +0 -0
  45. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/directory_scaffolder.py +0 -0
  46. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/download_service.py +0 -0
  47. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/git_service.py +0 -0
  48. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/linting_service.py +0 -0
  49. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/model_commands.py +0 -0
  50. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/pipeline_env.py +0 -0
  51. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/skill_service.py +0 -0
  52. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/snowflake_service.py +0 -0
  53. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/services/upstream_service.py +0 -0
  54. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/utilities/__init__.py +0 -0
  55. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/utilities/display.py +0 -0
  56. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/utilities/file_utils.py +0 -0
  57. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/utilities/mcp.py +0 -0
  58. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_cli/utilities/yaml_utils.py +0 -0
  59. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_mcp/__init__.py +0 -0
  60. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_mcp/__main__.py +0 -0
  61. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_mcp/connection.py +0 -0
  62. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/__init__.py +0 -0
  63. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/convert.py +0 -0
  64. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/diagram.py +0 -0
  65. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/expand.py +0 -0
  66. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/extract.py +0 -0
  67. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/render.py +0 -0
  68. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/schema.py +0 -0
  69. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/scope.py +0 -0
  70. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/scripts.py +0 -0
  71. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/starter.py +0 -0
  72. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/templates/macros/role.sql +0 -0
  73. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/templates/macros/role_guard.sql +0 -0
  74. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/templates/macros/warehouse.sql +0 -0
  75. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/src/datasecops_model/validate.py +0 -0
  76. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/__init__.py +0 -0
  77. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_config.py +0 -0
  78. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_dbt_project_generator_profiles.py +0 -0
  79. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_dbt_runner.py +0 -0
  80. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_dcm_model.py +0 -0
  81. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_dcm_orchestrator.py +0 -0
  82. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_dcm_service.py +0 -0
  83. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_download_service.py +0 -0
  84. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_file_utils.py +0 -0
  85. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_linting_service.py +0 -0
  86. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_main.py +0 -0
  87. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_model_commands.py +0 -0
  88. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_models.py +0 -0
  89. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_pipeline_env.py +0 -0
  90. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_version.py +0 -0
  91. {datasecops_cli-0.7.0b7 → datasecops_cli-0.7.0b8}/tests/test_yaml_utils.py +0 -0
@@ -81,7 +81,7 @@ jobs:
81
81
  python-version: ${{ matrix.python-version }}
82
82
 
83
83
  - name: Install package with test dependencies
84
- run: pip install -e ".[test]"
84
+ run: pip install -e ".[test,mcp]"
85
85
 
86
86
  - name: Run tests
87
87
  run: pytest --tb=short
@@ -22,7 +22,7 @@ jobs:
22
22
  python-version: ${{ matrix.python-version }}
23
23
 
24
24
  - name: Install package with test dependencies
25
- run: pip install -e ".[test]"
25
+ run: pip install -e ".[test,mcp]"
26
26
 
27
27
  - name: Run tests
28
28
  run: pytest --tb=short
@@ -4,8 +4,12 @@ All notable changes to the DataSecOps CLI are documented in this file.
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.7.0b8] - 2026-10-27
8
+
7
9
  ### Fixed
8
10
 
11
+ - **MCP server's linting tools ignored `dbt_engine` and always ran `sqlfluff`** — `lint_sql`, `fix_sql`, and `lint_project` hardcoded the standalone `sqlfluff` CLI even on dbt 2.0 projects, where it isn't installed or pinned (the CLI's own `datasecops lint` already used `dbt lint` there). They now read the project's `dbt_engine` from `.datasecops.yml` (checking the repository root too when the dbt project is nested under a `dbt/` subdirectory) and dispatch to `dbt lint`/`dbt lint --fix` for dbt 2.x, matching the CLI's engine selection; `lint_project` also drops the `models/` directory requirement for dbt 2.x, since `dbt lint` scopes to the whole project itself. The dbt-snowflake adapter install (needed only by SQLFluff's dbt templater) is now skipped for dbt 2.x projects, where it could otherwise fail before `dbt lint` even runs.
12
+ - **`mcp` optional dependency had no upper bound** — `mcp>=1.0` could resolve to `mcp` 2.x, which renamed `FastMCP` to `MCPServer` and broke `datasecops_mcp.server`'s import outright; pinned to `mcp>=1.0,<2`.
9
13
  - **`profiles.yml` writes could still widen file permissions or miss a symlinked file** — the atomic-replace temp file is now created with restrictive (`0600`) permissions and, if `profiles.yml` already existed, its mode is reapplied before the swap, instead of inheriting the process umask (typically `0644`) and exposing credentials. The profiles path itself (not just its parent directory) is now resolved before deriving the lock and temp-file paths, so a symlinked `profiles.yml` is replaced at its real location instead of only the link.
10
14
 
11
15
  ## [0.7.0b7] - 2026-10-26
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: datasecops-cli
3
- Version: 0.7.0b7
3
+ Version: 0.7.0b8
4
4
  Summary: DataSecOps Framework CLI for Snowflake Native App
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -12,7 +12,7 @@ Requires-Dist: pydantic>=2.0
12
12
  Requires-Dist: pyyaml>=6.0
13
13
  Requires-Dist: snowflake-connector-python>=3.0
14
14
  Provides-Extra: mcp
15
- Requires-Dist: mcp>=1.0; extra == 'mcp'
15
+ Requires-Dist: mcp<2,>=1.0; extra == 'mcp'
16
16
  Provides-Extra: test
17
17
  Requires-Dist: pytest-cov>=4.0; extra == 'test'
18
18
  Requires-Dist: pytest>=7.0; extra == 'test'
@@ -97,9 +97,13 @@ It uses your Snowflake connection from `~/.snowflake/connections.toml` to authen
97
97
 
98
98
  | Tool | Description |
99
99
  |------|-------------|
100
- | `lint_sql` | Lint a single SQL file using the project's SQLFluff config |
101
- | `fix_sql` | Auto-fix linting issues in a SQL file |
102
- | `lint_project` | Lint all models in the project (optionally auto-fix) |
100
+ | `lint_sql` | Lint a single SQL file — SQLFluff (dbt Core 1.x) or `dbt lint` (dbt 2.x), per the project's `dbt_engine` |
101
+ | `fix_sql` | Auto-fix linting issues in a SQL file — SQLFluff `fix` or `dbt lint --fix`, per `dbt_engine` |
102
+ | `lint_project` | Lint all models in the project (optionally auto-fix) — same engine selection as above |
103
+
104
+ All three read the project's `dbt_engine` setting from `.datasecops.yml` and run the same
105
+ tool the CLI's `datasecops lint` would use, so results match what `lint`/`lint --fix` produce
106
+ locally.
103
107
 
104
108
  ### Source Control Tools
105
109
 
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "datasecops-cli"
7
- version = "0.7.0b7"
7
+ version = "0.7.0b8"
8
8
  description = "DataSecOps Framework CLI for Snowflake Native App"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -20,7 +20,7 @@ dependencies = [
20
20
 
21
21
  [project.optional-dependencies]
22
22
  test = ["pytest>=7.0", "pytest-cov>=4.0"]
23
- mcp = ["mcp>=1.0"]
23
+ mcp = ["mcp>=1.0,<2"]
24
24
 
25
25
  [tool.hatch.build.targets.wheel]
26
26
  packages = ["src/datasecops_cli", "src/datasecops_mcp", "src/datasecops_model"]
@@ -0,0 +1 @@
1
+ __version__ = "0.7.0b8"
@@ -507,13 +507,15 @@ def get_deployment_workflow() -> str:
507
507
 
508
508
  @mcp.tool()
509
509
  async def lint_sql(file_path: str, ctx: Context) -> str:
510
- """Lint a SQL file using SQLFluff with the project's configured rules.
510
+ """Lint a SQL file with the project's configured linting engine.
511
511
 
512
512
  Args:
513
513
  file_path: Path to the SQL file to lint (relative or absolute).
514
514
 
515
- Returns the linting results including rule violations, line numbers,
516
- and descriptions. Uses the project's .sqlfluff config from the framework.
515
+ Uses SQLFluff for dbt Core 1.x projects, or dbt 2.0's built-in `dbt lint` for
516
+ dbt 2.x projects (per the project's `dbt_engine` setting) — both read the same
517
+ .sqlfluff config from the framework. Returns the linting results including rule
518
+ violations, line numbers, and descriptions where the engine provides them.
517
519
  """
518
520
  import asyncio
519
521
  import subprocess
@@ -522,7 +524,8 @@ async def lint_sql(file_path: str, ctx: Context) -> str:
522
524
  await ctx.report_progress(0, 3)
523
525
  await ctx.info(f"Finding dbt project for: {file_path}")
524
526
  project_dir = _find_project_dir()
525
- _ensure_dbt_packages(project_dir)
527
+ dbt_engine = _get_dbt_engine(project_dir)
528
+ _ensure_dbt_packages(project_dir, dbt_engine)
526
529
 
527
530
  target = Path(file_path)
528
531
  if not target.is_absolute():
@@ -532,12 +535,9 @@ async def lint_sql(file_path: str, ctx: Context) -> str:
532
535
  return json.dumps({"error": f"File not found: {target}"})
533
536
 
534
537
  await ctx.report_progress(1, 3)
535
- await ctx.info(f"Running SQLFluff lint on {target.name}...")
538
+ await ctx.info(f"Running {_lint_tool_label(dbt_engine)} lint on {target.name}...")
536
539
 
537
- cmd = ["sqlfluff", "lint", str(target), "--format", "json"]
538
- config_path = project_dir / ".sqlfluff"
539
- if config_path.exists():
540
- cmd += ["--config", str(config_path)]
540
+ cmd = _build_lint_command(dbt_engine, project_dir, "lint", target)
541
541
 
542
542
  result = await asyncio.to_thread(
543
543
  subprocess.run, cmd, capture_output=True, text=True, cwd=str(project_dir)
@@ -546,31 +546,21 @@ async def lint_sql(file_path: str, ctx: Context) -> str:
546
546
  await ctx.report_progress(2, 3)
547
547
  await ctx.info("Processing lint results...")
548
548
 
549
- # SQLFluff returns exit code 1 when violations are found (not an error)
550
- if result.returncode > 1:
551
- return json.dumps({"error": f"SQLFluff failed: {result.stderr.strip()}"})
552
-
553
- if result.stdout.strip():
554
- try:
555
- violations = json.loads(result.stdout)
556
- await ctx.report_progress(3, 3)
557
- return json.dumps(violations, indent=2)
558
- except json.JSONDecodeError:
559
- await ctx.report_progress(3, 3)
560
- return result.stdout
549
+ output = _format_lint_result(dbt_engine, "lint", result, clean_message="No linting issues found")
561
550
  await ctx.report_progress(3, 3)
562
- return json.dumps({"status": "clean", "message": "No linting issues found"})
551
+ return output
563
552
 
564
553
 
565
554
  @mcp.tool()
566
555
  async def fix_sql(file_path: str, ctx: Context) -> str:
567
- """Auto-fix linting issues in a SQL file using SQLFluff.
556
+ """Auto-fix linting issues in a SQL file with the project's configured linting engine.
568
557
 
569
558
  Args:
570
559
  file_path: Path to the SQL file to fix (relative or absolute).
571
560
 
572
- Applies automatic fixes using the project's .sqlfluff config.
573
- Returns the fix results and what was changed.
561
+ Uses SQLFluff for dbt Core 1.x projects, or dbt 2.0's built-in `dbt lint --fix` for
562
+ dbt 2.x projects (per the project's `dbt_engine` setting), applying fixes from the
563
+ .sqlfluff config. Returns the fix results and what was changed.
574
564
  """
575
565
  import asyncio
576
566
  import subprocess
@@ -579,7 +569,8 @@ async def fix_sql(file_path: str, ctx: Context) -> str:
579
569
  await ctx.report_progress(0, 3)
580
570
  await ctx.info(f"Finding dbt project for: {file_path}")
581
571
  project_dir = _find_project_dir()
582
- _ensure_dbt_packages(project_dir)
572
+ dbt_engine = _get_dbt_engine(project_dir)
573
+ _ensure_dbt_packages(project_dir, dbt_engine)
583
574
 
584
575
  target = Path(file_path)
585
576
  if not target.is_absolute():
@@ -589,12 +580,9 @@ async def fix_sql(file_path: str, ctx: Context) -> str:
589
580
  return json.dumps({"error": f"File not found: {target}"})
590
581
 
591
582
  await ctx.report_progress(1, 3)
592
- await ctx.info(f"Running SQLFluff fix on {target.name}...")
583
+ await ctx.info(f"Running {_lint_tool_label(dbt_engine)} fix on {target.name}...")
593
584
 
594
- cmd = ["sqlfluff", "fix", str(target), "--force", "--format", "json"]
595
- config_path = project_dir / ".sqlfluff"
596
- if config_path.exists():
597
- cmd += ["--config", str(config_path)]
585
+ cmd = _build_lint_command(dbt_engine, project_dir, "fix", target)
598
586
 
599
587
  result = await asyncio.to_thread(
600
588
  subprocess.run, cmd, capture_output=True, text=True, cwd=str(project_dir)
@@ -603,55 +591,46 @@ async def fix_sql(file_path: str, ctx: Context) -> str:
603
591
  await ctx.report_progress(2, 3)
604
592
  await ctx.info("Processing fix results...")
605
593
 
606
- if result.returncode > 1:
607
- return json.dumps({"error": f"SQLFluff fix failed: {result.stderr.strip()}"})
608
-
609
- if result.stdout.strip():
610
- try:
611
- fix_results = json.loads(result.stdout)
612
- await ctx.report_progress(3, 3)
613
- return json.dumps(fix_results, indent=2)
614
- except json.JSONDecodeError:
615
- await ctx.report_progress(3, 3)
616
- return result.stdout
594
+ output = _format_lint_result(dbt_engine, "fix", result, clean_message=f"Fixes applied to {target.name}")
617
595
  await ctx.report_progress(3, 3)
618
- return json.dumps({"status": "fixed", "message": f"Fixes applied to {target.name}"})
596
+ return output
619
597
 
620
598
 
621
599
  @mcp.tool()
622
600
  async def lint_project(fix: bool, ctx: Context) -> str:
623
- """Lint all SQL models in the dbt project.
601
+ """Lint all SQL models in the dbt project with the project's configured linting engine.
624
602
 
625
603
  Args:
626
604
  fix: If True, auto-fix issues instead of just reporting them. Defaults to False.
627
605
 
628
- Lints the models/ directory using the project's .sqlfluff config.
629
- Returns a summary of violations or fixes applied.
606
+ Uses SQLFluff for dbt Core 1.x projects (scoped to models/), or dbt 2.0's built-in
607
+ `dbt lint`/`dbt lint --fix` for dbt 2.x projects (scoped to the whole project, per the
608
+ project's `dbt_engine` setting) — both read the project's .sqlfluff config. Returns a
609
+ summary of violations or fixes applied.
630
610
  """
631
611
  import asyncio
632
612
  import subprocess
633
- from pathlib import Path
634
613
 
635
614
  action = "fix" if fix else "lint"
636
615
 
637
616
  await ctx.report_progress(0, 3)
638
617
  await ctx.info("Finding dbt project...")
639
618
  project_dir = _find_project_dir()
640
- _ensure_dbt_packages(project_dir)
641
- models_dir = project_dir / "models"
642
- if not models_dir.exists():
643
- return json.dumps({"error": f"No models directory found at {models_dir}"})
619
+ dbt_engine = _get_dbt_engine(project_dir)
620
+ _ensure_dbt_packages(project_dir, dbt_engine)
644
621
 
645
- await ctx.report_progress(1, 3)
646
- await ctx.info(f"Running SQLFluff {action} on models/...")
622
+ # dbt lint (dbt 2.x) lints the whole project by default — no separate models/ scoping
623
+ # is needed or supported. sqlfluff (dbt Core 1.x) requires an explicit target path.
624
+ target = None
625
+ if dbt_engine != "dbt2":
626
+ target = project_dir / "models"
627
+ if not target.exists():
628
+ return json.dumps({"error": f"No models directory found at {target}"})
647
629
 
648
- cmd = ["sqlfluff", action, str(models_dir), "--format", "json"]
649
- if fix:
650
- cmd.append("--force")
630
+ await ctx.report_progress(1, 3)
631
+ await ctx.info(f"Running {_lint_tool_label(dbt_engine)} {action} on the project...")
651
632
 
652
- config_path = project_dir / ".sqlfluff"
653
- if config_path.exists():
654
- cmd += ["--config", str(config_path)]
633
+ cmd = _build_lint_command(dbt_engine, project_dir, action, target)
655
634
 
656
635
  result = await asyncio.to_thread(
657
636
  subprocess.run, cmd, capture_output=True, text=True, cwd=str(project_dir)
@@ -660,6 +639,11 @@ async def lint_project(fix: bool, ctx: Context) -> str:
660
639
  await ctx.report_progress(2, 3)
661
640
  await ctx.info("Processing results...")
662
641
 
642
+ if dbt_engine == "dbt2":
643
+ output = _format_lint_result(dbt_engine, action, result, clean_message="No issues found")
644
+ await ctx.report_progress(3, 3)
645
+ return output
646
+
663
647
  if result.returncode > 1:
664
648
  return json.dumps({"error": f"SQLFluff {action} failed: {result.stderr.strip()}"})
665
649
 
@@ -702,23 +686,111 @@ def _find_project_dir() -> "Path":
702
686
  return cwd
703
687
 
704
688
 
705
- def _ensure_dbt_packages(project_dir: "Path") -> None:
706
- """Ensure dbt-snowflake and dbt packages are available for sqlfluff dbt templater.
689
+ def _get_dbt_engine(project_dir: "Path") -> str:
690
+ """Read the project's configured dbt engine ("dbt2" or "dbt_core") from .datasecops.yml.
691
+
692
+ .datasecops.yml lives at the repository root, which is project_dir's parent when the
693
+ dbt project is nested under a dbt/ subdirectory — _find_project_dir()'s other candidate
694
+ location — so that's checked too if project_dir itself has no config, matching how
695
+ Config.load() resolves the same file. Falls back to "dbt_core" (SQLFluff) if neither
696
+ location has the file or the value is invalid.
697
+ """
698
+ from datasecops_cli.utilities.yaml_utils import read_datasecops_config
699
+
700
+ raw = read_datasecops_config(project_dir)
701
+ if raw is None and project_dir.name == "dbt":
702
+ raw = read_datasecops_config(project_dir.parent)
703
+ raw = raw or {}
704
+ engine = raw.get("dbt_engine", "dbt_core")
705
+ return engine if engine in ("dbt2", "dbt_core") else "dbt_core"
706
+
707
+
708
+ def _lint_tool_label(dbt_engine: str) -> str:
709
+ """Human-readable name of the linting tool the configured engine actually runs."""
710
+ return "dbt lint" if dbt_engine == "dbt2" else "SQLFluff"
711
+
712
+
713
+ def _build_lint_command(
714
+ dbt_engine: str, project_dir: "Path", action: str, target: "Path" = None
715
+ ) -> list[str]:
716
+ """Build the lint/fix command for the project's configured dbt engine.
717
+
718
+ dbt 2.0 projects (`dbt_engine == "dbt2"`) use the built-in `dbt lint` command;
719
+ dbt Core 1.x projects use the standalone `sqlfluff` CLI. Both read the same
720
+ `.sqlfluff` config file, but only `sqlfluff` supports `--format json`.
721
+ """
722
+ config_path = project_dir / ".sqlfluff"
723
+
724
+ if dbt_engine == "dbt2":
725
+ cmd = ["dbt", "lint"]
726
+ if action == "fix":
727
+ cmd.append("--fix")
728
+ if target is not None:
729
+ cmd.append(str(target))
730
+ else:
731
+ if target is None:
732
+ target = project_dir / "models"
733
+ cmd = ["sqlfluff", action, str(target)]
734
+ if action == "fix":
735
+ cmd.append("--force")
736
+ cmd += ["--format", "json"]
737
+
738
+ if config_path.exists():
739
+ cmd += ["--config", str(config_path)]
740
+
741
+ return cmd
742
+
743
+
744
+ def _format_lint_result(
745
+ dbt_engine: str, action: str, result: "subprocess.CompletedProcess", clean_message: str
746
+ ) -> str:
747
+ """Format a completed lint/fix subprocess result as the tool's JSON response.
707
748
 
708
- Skips expensive dbt deps if packages are already present, but always
709
- ensures the dbt-snowflake Python package is importable.
749
+ Both engines use exit code 1 to mean "issues found", not a failure. `sqlfluff`
750
+ returns structured JSON violations; `dbt lint` (dbt 2.0) has no `--format json`
751
+ option, so its result carries dbt's own human-readable output instead.
752
+ """
753
+ label = _lint_tool_label(dbt_engine)
754
+ if result.returncode > 1:
755
+ return json.dumps({"error": f"{label} {action} failed: {result.stderr.strip()}"})
756
+
757
+ if dbt_engine == "dbt2":
758
+ status = "clean" if result.returncode == 0 else "violations_found"
759
+ return json.dumps(
760
+ {"engine": "dbt2", "action": action, "status": status, "output": result.stdout.strip()}, indent=2
761
+ )
762
+
763
+ if result.stdout.strip():
764
+ try:
765
+ return json.dumps(json.loads(result.stdout), indent=2)
766
+ except json.JSONDecodeError:
767
+ return result.stdout
768
+ return json.dumps({"status": "fixed" if action == "fix" else "clean", "message": clean_message})
769
+
770
+
771
+ def _ensure_dbt_packages(project_dir: "Path", dbt_engine: str = "dbt_core") -> None:
772
+ """Ensure dbt packages are available for the configured engine.
773
+
774
+ dbt Core 1.x (sqlfluff-templater-dbt) needs the separate dbt-snowflake adapter package
775
+ importable — installed here if missing. dbt 2.0's built-in `dbt lint` bundles its own
776
+ adapter, so that install is skipped for it (and would otherwise fail outright wherever
777
+ `uv` isn't installed, before `dbt lint` even runs).
778
+
779
+ Project packages (dbt_packages/packages.yml) are installed for both engines, skipping
780
+ the expensive `dbt deps` if they're already present.
710
781
  """
711
782
  import subprocess
712
783
  from pathlib import Path
713
784
 
714
- # Always ensure dbt-snowflake Python package is available (required by sqlfluff-templater-dbt)
715
- try:
716
- import dbt.adapters.snowflake # noqa: F401
717
- except ImportError:
718
- subprocess.run(
719
- ["uv", "pip", "install", "dbt-snowflake"],
720
- capture_output=True, text=True,
721
- )
785
+ if dbt_engine != "dbt2":
786
+ # Ensure dbt-snowflake Python package is available (required by sqlfluff-templater-dbt)
787
+ try:
788
+ import dbt.adapters.snowflake # noqa: F401
789
+ except ImportError:
790
+ subprocess.run(
791
+ ["uv", "pip", "install", "dbt-snowflake"],
792
+ capture_output=True, text=True,
793
+ )
722
794
 
723
795
  # Ensure dbt project packages are installed (refs to macros in packages)
724
796
  packages_dir = project_dir / "dbt_packages"
@@ -0,0 +1,200 @@
1
+ """Tests for the dbt-engine-aware linting tools in datasecops_mcp.server."""
2
+ import asyncio
3
+ import json
4
+ from unittest.mock import MagicMock, patch
5
+
6
+ from datasecops_mcp import server
7
+
8
+
9
+ class _FakeContext:
10
+ """Minimal stand-in for mcp.server.fastmcp.Context — only the methods the tools call."""
11
+
12
+ async def report_progress(self, *args, **kwargs):
13
+ pass
14
+
15
+ async def info(self, *args, **kwargs):
16
+ pass
17
+
18
+
19
+ def _write_datasecops_yml(tmp_path, dbt_engine: str) -> None:
20
+ (tmp_path / ".datasecops.yml").write_text(f"dbt_engine: {dbt_engine}\n", encoding="utf-8")
21
+
22
+
23
+ class TestGetDbtEngine:
24
+ def test_defaults_to_dbt_core_when_missing(self, tmp_path):
25
+ assert server._get_dbt_engine(tmp_path) == "dbt_core"
26
+
27
+ def test_reads_dbt2_from_datasecops_yml(self, tmp_path):
28
+ _write_datasecops_yml(tmp_path, "dbt2")
29
+ assert server._get_dbt_engine(tmp_path) == "dbt2"
30
+
31
+ def test_falls_back_to_dbt_core_on_invalid_value(self, tmp_path):
32
+ _write_datasecops_yml(tmp_path, "bogus")
33
+ assert server._get_dbt_engine(tmp_path) == "dbt_core"
34
+
35
+ def test_reads_from_repo_root_for_nested_dbt_project_dir(self, tmp_path):
36
+ """.datasecops.yml lives at the repo root; project_dir may be its dbt/ subdirectory."""
37
+ _write_datasecops_yml(tmp_path, "dbt2")
38
+ dbt_dir = tmp_path / "dbt"
39
+ dbt_dir.mkdir()
40
+ assert server._get_dbt_engine(dbt_dir) == "dbt2"
41
+
42
+ def test_prefers_config_in_project_dir_over_parent(self, tmp_path):
43
+ _write_datasecops_yml(tmp_path, "dbt2")
44
+ dbt_dir = tmp_path / "dbt"
45
+ dbt_dir.mkdir()
46
+ _write_datasecops_yml(dbt_dir, "dbt_core")
47
+ assert server._get_dbt_engine(dbt_dir) == "dbt_core"
48
+
49
+
50
+ class TestBuildLintCommand:
51
+ def test_dbt_core_uses_sqlfluff(self, tmp_path):
52
+ cmd = server._build_lint_command("dbt_core", tmp_path, "lint")
53
+ assert cmd[0] == "sqlfluff"
54
+ assert cmd[1] == "lint"
55
+ assert "--format" in cmd and "json" in cmd
56
+
57
+ def test_dbt2_uses_dbt_lint(self, tmp_path):
58
+ cmd = server._build_lint_command("dbt2", tmp_path, "lint")
59
+ assert cmd[:2] == ["dbt", "lint"]
60
+ assert "--format" not in cmd
61
+
62
+ def test_dbt2_fix_passes_flag(self, tmp_path):
63
+ cmd = server._build_lint_command("dbt2", tmp_path, "fix")
64
+ assert "--fix" in cmd
65
+
66
+ def test_dbt_core_fix_passes_force_flag(self, tmp_path):
67
+ cmd = server._build_lint_command("dbt_core", tmp_path, "fix")
68
+ assert "--force" in cmd
69
+
70
+ def test_dbt2_with_no_target_omits_path(self, tmp_path):
71
+ cmd = server._build_lint_command("dbt2", tmp_path, "lint", target=None)
72
+ assert str(tmp_path) not in " ".join(cmd)
73
+
74
+ def test_dbt_core_with_no_target_defaults_to_models_dir(self, tmp_path):
75
+ cmd = server._build_lint_command("dbt_core", tmp_path, "lint", target=None)
76
+ assert str(tmp_path / "models") in cmd
77
+
78
+ def test_includes_sqlfluff_config_when_present(self, tmp_path):
79
+ (tmp_path / ".sqlfluff").write_text("[sqlfluff]\n", encoding="utf-8")
80
+ cmd = server._build_lint_command("dbt2", tmp_path, "lint")
81
+ assert "--config" in cmd
82
+ assert str(tmp_path / ".sqlfluff") in cmd
83
+
84
+ def test_omits_config_flag_when_absent(self, tmp_path):
85
+ cmd = server._build_lint_command("dbt2", tmp_path, "lint")
86
+ assert "--config" not in cmd
87
+
88
+
89
+ class TestFormatLintResult:
90
+ def test_dbt2_clean(self):
91
+ result = MagicMock(returncode=0, stdout="All good\n", stderr="")
92
+ out = json.loads(server._format_lint_result("dbt2", "lint", result, "clean"))
93
+ assert out == {"engine": "dbt2", "action": "lint", "status": "clean", "output": "All good"}
94
+
95
+ def test_dbt2_violations_found(self):
96
+ result = MagicMock(returncode=1, stdout="1 issue\n", stderr="")
97
+ out = json.loads(server._format_lint_result("dbt2", "lint", result, "clean"))
98
+ assert out["status"] == "violations_found"
99
+
100
+ def test_dbt2_error(self):
101
+ result = MagicMock(returncode=2, stdout="", stderr="boom")
102
+ out = json.loads(server._format_lint_result("dbt2", "lint", result, "clean"))
103
+ assert out == {"error": "dbt lint lint failed: boom"}
104
+
105
+ def test_sqlfluff_parses_json_output(self):
106
+ result = MagicMock(returncode=1, stdout='[{"filepath": "a.sql", "violations": []}]', stderr="")
107
+ out = json.loads(server._format_lint_result("dbt_core", "lint", result, "clean"))
108
+ assert out == [{"filepath": "a.sql", "violations": []}]
109
+
110
+ def test_sqlfluff_clean_message_when_no_output(self):
111
+ result = MagicMock(returncode=0, stdout="", stderr="")
112
+ out = json.loads(server._format_lint_result("dbt_core", "lint", result, "no issues"))
113
+ assert out == {"status": "clean", "message": "no issues"}
114
+
115
+ def test_sqlfluff_fixed_status_when_fix_has_no_output(self):
116
+ """A successful fix with no stdout must report 'fixed', not 'clean'."""
117
+ result = MagicMock(returncode=0, stdout="", stderr="")
118
+ out = json.loads(server._format_lint_result("dbt_core", "fix", result, "fixes applied"))
119
+ assert out == {"status": "fixed", "message": "fixes applied"}
120
+
121
+ def test_sqlfluff_error(self):
122
+ result = MagicMock(returncode=2, stdout="", stderr="crashed")
123
+ out = json.loads(server._format_lint_result("dbt_core", "lint", result, "clean"))
124
+ assert out == {"error": "SQLFluff lint failed: crashed"}
125
+
126
+
127
+ class TestEnsureDbtPackages:
128
+ def test_dbt2_skips_adapter_install(self, tmp_path, monkeypatch):
129
+ """dbt2 bundles its own adapter — installing dbt-snowflake must not be attempted."""
130
+ import builtins
131
+
132
+ real_import = builtins.__import__
133
+
134
+ def fake_import(name, *args, **kwargs):
135
+ if name == "dbt.adapters.snowflake" or name.startswith("dbt.adapters"):
136
+ raise ImportError("not installed")
137
+ return real_import(name, *args, **kwargs)
138
+
139
+ monkeypatch.setattr(builtins, "__import__", fake_import)
140
+
141
+ with patch("subprocess.run") as mock_run:
142
+ server._ensure_dbt_packages(tmp_path, dbt_engine="dbt2")
143
+ assert not any(call.args[0][:3] == ["uv", "pip", "install"] for call in mock_run.call_args_list)
144
+
145
+ def test_dbt_core_installs_adapter_when_missing(self, tmp_path, monkeypatch):
146
+ import builtins
147
+
148
+ real_import = builtins.__import__
149
+
150
+ def fake_import(name, *args, **kwargs):
151
+ if name == "dbt.adapters.snowflake" or name.startswith("dbt.adapters"):
152
+ raise ImportError("not installed")
153
+ return real_import(name, *args, **kwargs)
154
+
155
+ monkeypatch.setattr(builtins, "__import__", fake_import)
156
+
157
+ with patch("subprocess.run") as mock_run:
158
+ mock_run.return_value = MagicMock(returncode=0)
159
+ server._ensure_dbt_packages(tmp_path, dbt_engine="dbt_core")
160
+ assert any(call.args[0][:3] == ["uv", "pip", "install"] for call in mock_run.call_args_list)
161
+
162
+
163
+ class TestLintSqlEngineDispatch:
164
+ def test_uses_dbt_lint_for_dbt2_project(self, tmp_path, monkeypatch):
165
+ _write_datasecops_yml(tmp_path, "dbt2")
166
+ sql_file = tmp_path / "model.sql"
167
+ sql_file.write_text("select 1", encoding="utf-8")
168
+
169
+ monkeypatch.setattr(server, "_find_project_dir", lambda: tmp_path)
170
+ monkeypatch.setattr(server, "_ensure_dbt_packages", lambda project_dir, dbt_engine=None: None)
171
+
172
+ captured = {}
173
+
174
+ def fake_run(cmd, **kwargs):
175
+ captured["cmd"] = cmd
176
+ return MagicMock(returncode=0, stdout="", stderr="")
177
+
178
+ with patch("subprocess.run", side_effect=fake_run):
179
+ output = asyncio.run(server.lint_sql(str(sql_file), _FakeContext()))
180
+
181
+ assert captured["cmd"][:2] == ["dbt", "lint"]
182
+ assert json.loads(output)["engine"] == "dbt2"
183
+
184
+ def test_uses_sqlfluff_for_dbt_core_project(self, tmp_path, monkeypatch):
185
+ sql_file = tmp_path / "model.sql"
186
+ sql_file.write_text("select 1", encoding="utf-8")
187
+
188
+ monkeypatch.setattr(server, "_find_project_dir", lambda: tmp_path)
189
+ monkeypatch.setattr(server, "_ensure_dbt_packages", lambda project_dir, dbt_engine=None: None)
190
+
191
+ captured = {}
192
+
193
+ def fake_run(cmd, **kwargs):
194
+ captured["cmd"] = cmd
195
+ return MagicMock(returncode=0, stdout="", stderr="")
196
+
197
+ with patch("subprocess.run", side_effect=fake_run):
198
+ asyncio.run(server.lint_sql(str(sql_file), _FakeContext()))
199
+
200
+ assert captured["cmd"][0] == "sqlfluff"
@@ -1 +0,0 @@
1
- __version__ = "0.7.0b7"