Programmatically Retrieve Prep Data For AI Configuration of Semantic Models
Principal Program Manager, Microsoft Fabric CAT helping users and organizations build scalable, insightful, secure solutions. Blogs, opinions are my own and do not represent my employer.
For Power BI Copilot and Data agents with semantic models, you must use Prep Data for AI configuration to ground the responses in the context added in Prep for AI. In this blog, I will show you how you can use the Power BI remote MCP server to get the configuration.
Code
Below I call the GetSemanticModelSchema tool from the hosted MCP to get the Prep for AI configurations. Run below in a Fabric notebook.
import httpx
import json
async def get_semantic_model_schema(model_id):
"""Fetch and parse the semantic model prep for ai config from Power BI MCP server.
Author : Sandeep Pawar | Fabric.guru
"""
MCP_SERVER_URL = "https://api.fabric.microsoft.com/v1/mcp/powerbi"
token = notebookutils.credentials.getToken("pbi")
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "GetSemanticModelSchema",
"arguments": {"artifactId": model_id}
}
}
def parse_sse_response(text):
for line in text.split('\n'):
if line.startswith('data: '):
return json.loads(line[6:])
return {}
async with httpx.AsyncClient(timeout=120.0) as client:
response = await client.post(MCP_SERVER_URL, headers=headers, json=payload)
data = parse_sse_response(response.text)
parsed = json.loads(data['result']['content'][0]['text'])
return {
"name": parsed['semanticModel']['Name'],
"tables": parsed['schema']['Tables'],
"relationships": parsed['schema']['ActiveRelationships'],
"custom_instructions": parsed['schema']['CustomInstructions'],
"verified_answers": parsed['schema']['VerifiedAnswers']
}
# retrieve
prep4ai = await get_semantic_model_schema("<semantic_model_guid>")
Here you have it. Note the verified answer. This is often a confusion, what does the Verified Answer actually store ? As you can see it includes the projections - the tables, columns, measures, filters used in the visual and not the DAX query of the visual !
You can use this to programmatically check, verify, monitor the configurations or make it part of your best practices analyzer for AI.