# Programmatically Retrieve Prep Data For AI Configuration of Semantic Models

For Power BI Copilot and [Data agents with semantic models](https://learn.microsoft.com/en-us/fabric/data-science/semantic-model-best-practices#prep-for-ai-make-semantic-model-ai-ready), you must use [Prep Data for AI configuration](https://learn.microsoft.com/en-us/power-bi/create-reports/copilot-prepare-data-ai) 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](https://learn.microsoft.com/en-us/power-bi/developer/mcp/remote-mcp-server-get-started) to get the configuration.

![](https://cdn.hashnode.com/uploads/covers/619d4cccfa52cd31fe52d25d/9ac424c6-3a4b-4340-9058-2e1a3a66cad8.png align="center")

## Code

Below I call the `GetSemanticModelSchema` tool from the hosted MCP to get the Prep for AI configurations. Run below in a Fabric notebook.

```python
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>")
```

![](https://cdn.hashnode.com/uploads/covers/619d4cccfa52cd31fe52d25d/b6ff9a43-2a98-4c22-af07-b163d67bca92.png align="center")

Here you have it. Note the [verified answer](https://learn.microsoft.com/en-us/power-bi/create-reports/copilot-prepare-data-ai-verified-answers). 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 !

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Note that you cannot make changes to Prep for AI programmatically, this is just retrieving the configs.</div>
</div>

You can use this to programmatically check, verify, monitor the configurations or make it part of your best practices analyzer for AI.
