agentleFS
MicrosoftKnown publishermicrosoft/fabric-toolbox/tools/MicrosoftFabricMgmtMCPServer/CLAUDE.md

fabric-toolbox / MicrosoftFabricMgmtMCPServer

CLAUDE.md917 starsChanged 5 months agoMITView on GitHub

This directory contains a Python-based Model Context Protocol (MCP) server that wraps the MicrosoftFabricMgmt PowerShell module and exposes its capabilities to AI assistants (Claude, GitHub Copilot, etc.). DO NOT edit the PowerShell module source here. All PowerShell module development belongs in ../MicrosoftFabricMgmt/source/ and must follow the module's own CLAUDE.md. Key principle: Python is a thin protocol-translation layer only. All Fabric API logic lives in the PowerShell module. Python does not call Fabric REST APIs directly. Maintains a single long-lived pwsh…

The file

We split this CLAUDE.md into the rules it gives an agent

11 rules. About 0 are common, 4 are distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    DO NOT edit the PowerShell module source here: All PowerShell module development belongs in ../MicrosoftFabricMgmt/source/ and must follow …

    line 9
  2. Very specific:

    Every command is wrapped in a try/catch/finally block by Python before being written to the process stdin:

    line 41
  3. Very specific:

    If pwsh crashes, the next call to session.run() detects the dead process, restarts it, re-imports the module, and retries the command.

    line 64
  4. Very specific:

    Look in ../MicrosoftFabricMgmt/source/Public/ for the function you want to wrap.

    line 79
  5. Distinctive:

    Read its parameter names and types carefully.

    line 80
  6. Distinctive:

    Create a new file if the resource category doesn't exist yet.

    line 103
  7. Distinctive:

    Follow this template exactly:

    line 107
  8. Very specific:

    If you added a new tools file, import and register it in server.py:

    line 132
  9. Very specific:

    Use -Depth 10 for definition-heavy calls like Get-FabricNotebookDefinition.

    line 179
  10. Very specific:

    Use -Depth 5 for standard list/get operations.

    line 180
  11. Distinctive:

    All tools must return a valid JSON string.

    line 186

≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
CLAUDE.md

CLAUDE.md — MicrosoftFabricMgmt MCP Server Development Guide

Overview

This directory contains a Python-based Model Context Protocol (MCP) server that wraps the MicrosoftFabricMgmt PowerShell module and exposes its capabilities to AI assistants (Claude, GitHub Copilot, etc.).

DO NOT edit the PowerShell module source here. All PowerShell module development belongs in ../MicrosoftFabricMgmt/source/ and must follow the module's own CLAUDE.md.


Architecture

server.py
  └─ PowerShellSession (singleton)          ← core/powershell_session.py
  └─ register_auth_tools(mcp, session)      ← tools/auth_tools.py
  └─ register_workspace_tools(mcp, session) ← tools/workspace_tools.py
  └─ register_lakehouse_tools(...)
  └─ ... (14 tool modules total)

Key principle: Python is a thin protocol-translation layer only. All Fabric API logic lives in the PowerShell module. Python does not call Fabric REST APIs directly.


The PowerShell Session (core/powershell_session.py)

What it does

Maintains a single long-lived pwsh subprocess for the lifetime of the MCP server. The module is imported once at startup; authentication (Set-FabricApiHeaders) persists across all tool calls.

Sentinel protocol

Every command is wrapped in a try/catch/finally block by Python before being written to the process stdin:

try {
    <your command>
} catch {
    @{ success=$false; error=$_.Exception.Message; error_type=... } | ConvertTo-Json -Compress
} finally {
    Write-Host '__MGMT_DONE__:' + ($LASTEXITCODE -as [int])
}

Python reads stdout lines until it sees __MGMT_DONE__:<exit_code>, then parses the captured lines as JSON.

Thread safety

PowerShellSession.run() acquires a threading.Lock(). Concurrent MCP tool calls are serialised — only one command runs in the pwsh process at a time.

Auto-restart

If pwsh crashes, the next call to session.run() detects the dead process, restarts it, re-imports the module, and retries the command.

Module path resolution (in priority order)

  1. FABRIC_MGMT_MODULE_PATH environment variable
  2. ../MicrosoftFabricMgmt/output/module/MicrosoftFabricMgmt (repo-relative built module)
  3. MicrosoftFabricMgmt (relies on $env:PSModulePath)

Adding a New Tool

Step 1 — Find the PS function

Look in ../MicrosoftFabricMgmt/source/Public/ for the function you want to wrap. Read its parameter names and types carefully.

Step 2 — Choose the right tools file

Each file maps to a resource category:

FileResource Category
tools/auth_tools.pyAuthentication
tools/workspace_tools.pyWorkspaces
tools/lakehouse_tools.pyLakehouses
tools/warehouse_tools.pyWarehouses
tools/notebook_tools.pyNotebooks
tools/pipeline_tools.pyData Pipelines
tools/environment_tools.pyEnvironments
tools/semantic_model_tools.pySemantic Models
tools/eventhouse_tools.pyEventhouses + KQL Databases
tools/spark_tools.pySpark
tools/admin_tools.pyAdmin
tools/capacity_tools.pyCapacities + Domains
tools/sql_database_tools.pySQL Databases
tools/escape_hatch_tools.pyGeneric escape hatches

Create a new file if the resource category doesn't exist yet.

Step 3 — Add the @mcp.tool function

Follow this template exactly:

@mcp.tool(
    name="list_reports",
    description=(
        "List all reports in a Microsoft Fabric workspace.\n\n"
        "Returns report IDs, display names, and metadata."
    ),
)
def list_reports(workspace_id: str, report_name: Optional[str] = None) -> str:
    try:
        params = f"-WorkspaceId '{workspace_id}'"
        if report_name:
            params += f" -ReportName '{report_name.replace(chr(39), chr(39)*2)}'"
        cmd = f"Get-FabricReport {params} | ConvertTo-Json -Depth 5 -Compress"
        return session.run(cmd)
    except PowerShellSessionError as exc:
        return _err(str(exc), "powershell_session_error")
    except Exception as exc:
        return _err(str(exc))

Step 4 — Register in server.py

If you added a new tools file, import and register it in server.py:

from tools.report_tools import register_report_tools
# ...
register_report_tools(mcp, _ps_session)

PS Command Construction Conventions

Get-* commands (always end with ConvertTo-Json)

cmd = "Get-FabricWorkspace | ConvertTo-Json -Depth 5 -Compress"
cmd = f"Get-FabricLakehouse -WorkspaceId '{workspace_id}' | ConvertTo-Json -Depth 5 -Compress"

Remove-* commands (no output expected)

cmd = f"Remove-FabricLakehouse -WorkspaceId '{workspace_id}' -LakehouseId '{lakehouse_id}'"
# session.run() returns {"success": true, "output": null} automatically

Optional parameters — build param string conditionally

params = f"-WorkspaceId '{workspace_id}' -DisplayName '{name}'"
if description:
    params += f" -Description '{description.replace(chr(39), chr(39)*2)}'"
cmd = f"New-FabricLakehouse {params} | ConvertTo-Json -Depth 5 -Compress"

Escaping single quotes in PS string literals

# Always escape single quotes in any user-supplied string
safe = value.replace("'", "''")  # PS convention: '' inside a '-quoted string
# OR use chr() to avoid linter warnings:
safe = value.replace(chr(39), chr(39)*2)

Boolean PS parameters

flag = "$true" if python_bool else "$false"
cmd = f"New-FabricSparkCustomPool ... -AutoScaleEnabled {flag}"

Depth for deeply nested objects

Use -Depth 10 for definition-heavy calls like Get-FabricNotebookDefinition. Use -Depth 5 for standard list/get operations.


Error Response Format

All tools must return a valid JSON string. On error:

def _err(msg: str, err_type: str = "tool_error") -> str:
    return json.dumps({"success": False, "error": msg, "error_type": err_type}, indent=2)

Standard error_type values:

  • "tool_error" — Python-level validation error (bad arguments, etc.)
  • "powershell_session_error" — PowerShellSessionError from session crash/timeout
  • (PS errors surface via the catch block in the sentinel wrapper as "error_type" = PS exception class name)

Security Constraints

  • Never use shell=True in subprocess.Popen — use a list of arguments.
  • App secrets passed to connect_to_fabric are immediately converted to PS SecureString inside the PS session. They are never stored as Python strings beyond the function scope.
  • Do not create a second PowerShellSession instance — the singleton in server.py is the only process; a second instance would have a separate (unauthenticated) auth context.
  • invoke_fabric_ps has no input sanitisation by design — document this clearly and only use it in trusted AI-agent environments.

Testing

Unit tests (no pwsh required)

Mock PowerShellSession.run() and assert the correct PS command string is constructed:

from unittest.mock import MagicMock, patch
from fastmcp import FastMCP
from tools.workspace_tools import register_workspace_tools

def test_list_workspaces_builds_correct_command():
    mcp = FastMCP("test")
    session = MagicMock()
    session.run.return_value = '{"success": true}'
    register_workspace_tools(mcp, session)

    # Invoke the registered tool function
    tool = next(t for t in mcp._tools.values() if t.name == "list_workspaces")
    tool._fn()

    cmd = session.run.call_args[0][0]
    assert "Get-FabricWorkspace" in cmd
    assert "ConvertTo-Json" in cmd

Integration tests (requires real pwsh + credentials)

Set env vars: FABRIC_TENANT_ID, FABRIC_APP_ID, FABRIC_APP_SECRET

import pytest, os, json
from core.powershell_session import PowerShellSession

@pytest.fixture(scope="module")
def authenticated_session():
    tid = os.environ.get("FABRIC_TENANT_ID")
    app_id = os.environ.get("FABRIC_APP_ID")
    app_secret = os.environ.get("FABRIC_APP_SECRET")
    if not all([tid, app_id, app_secret]):
        pytest.skip("Integration credentials not set")
    session = PowerShellSession()
    session.run(
        f"$s = ConvertTo-SecureString '{app_secret}' -AsPlainText -Force; "
        f"Set-FabricApiHeaders -TenantId '{tid}' -AppId '{app_id}' -AppSecret $s"
    )
    yield session
    session.close()

Manual end-to-end

# Activate venv and start the server
.venv/Scripts/python.exe server.py

# In Claude Desktop or VS Code with MCP client:
# 1. connect_to_fabric(tenant_id="<your-guid>")
# 2. list_workspaces()
# 3. list_lakehouses(workspace_id="<guid-from-step-2>")
# 4. invoke_fabric_ps("Get-FabricCapacity | ConvertTo-Json -Depth 5 -Compress")

Known Pitfalls

IssueMitigation
readline() hangsTimeout loop in _read_until_sentinel checks process.poll()
PS errors go to stderrtry/catch in every wrapped command routes errors to stdout as JSON
Interactive auth blocks stdinDocument clearly; use SP or MI in CI/automated environments
Module path wrongFABRIC_MGMT_MODULE_PATH env var override; build module first
Single quotes in namesAlways escape: value.replace("'", "''")
pwsh BOM on stdoutStripped via line.lstrip("\ufeff") in _read_until_sentinel

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.