|
| 1 | +# Semantic Kernel - CopilotStudioAgent Quickstart |
| 2 | + |
| 3 | +This README provides an overview on how to use the [CopilotStudioAgent](../../../semantic_kernel/agents/copilot_studio/copilot_studio_agent.py) within Semantic Kernel. |
| 4 | +This agent allows you to interact with Microsoft Copilot Studio agents through programmatic APIs. |
| 5 | + |
| 6 | +> ℹ️ **Note:** Knowledge sources must be configured **within** Microsoft Copilot Studio first. Streaming responses are **not currently supported**. |
| 7 | +
|
| 8 | +--- |
| 9 | + |
| 10 | +## 🔧 Prerequisites |
| 11 | + |
| 12 | +1. Python 3.10+ |
| 13 | +2. Install Semantic Kernel with Copilot Studio dependencies: |
| 14 | + ```bash |
| 15 | + pip install semantic-kernel[copilot_studio] |
| 16 | + ``` |
| 17 | +3. An agent created in **Microsoft Copilot Studio** |
| 18 | +4. Ability to create an application identity in Azure for a **Public Client/Native App Registration**, |
| 19 | +or access to an existing app registration with the `CopilotStudio.Copilots.Invoke` API permission assigned. |
| 20 | + |
| 21 | +## Create a Copilot Agent in Copilot Studio |
| 22 | + |
| 23 | +1. Go to [Microsoft Copilot Studio](https://copilotstudio.microsoft.com). |
| 24 | +2. Create a new **Agent**. |
| 25 | +3. Publish your newly created Agent. |
| 26 | +4. In Copilot Studio, navigate to: |
| 27 | + `Settings` → `Advanced` → `Metadata` |
| 28 | + |
| 29 | + Save the following values: |
| 30 | + - `Schema Name` (maps to `agent_identifier`) |
| 31 | + - `Environment ID` |
| 32 | + |
| 33 | +## Create an Application Registration in Entra ID – User Interactive Login |
| 34 | + |
| 35 | +> This step requires permissions to create application identities in your Azure tenant. |
| 36 | +
|
| 37 | +You will create a **Native Client Application Identity** (no client secret required). |
| 38 | + |
| 39 | +1. Open [Azure Portal](https://portal.azure.com) |
| 40 | +2. Navigate to **Entra ID** |
| 41 | +3. Go to **App registrations** → **New registration** |
| 42 | +4. Fill out: |
| 43 | + - **Name**: Any name you like |
| 44 | + - **Supported account types**: `Accounts in this organization directory only` |
| 45 | + - **Redirect URI**: |
| 46 | + - Platform: `Public client/native (mobile & desktop)` |
| 47 | + - URI: `http://localhost` |
| 48 | +5. Click **Register** |
| 49 | +6. From the **Overview** page, note: |
| 50 | + - `Application (client) ID` |
| 51 | + - `Directory (tenant) ID` |
| 52 | +7. Go to: `Manage` → `API permissions` |
| 53 | + - Click **Add permission** |
| 54 | + - Choose **APIs my organization uses** |
| 55 | + - Search for: **Power Platform API** |
| 56 | + |
| 57 | + If it's not listed, see **Tip** below. |
| 58 | + |
| 59 | +8. Choose: |
| 60 | + - **Delegated Permissions** |
| 61 | + - Expand `CopilotStudio` |
| 62 | + - Select `CopilotStudio.Copilots.Invoke` |
| 63 | +9. Click **Add permissions** |
| 64 | +10. (Optional) Click **Grant admin consent** |
| 65 | + |
| 66 | +### Tip |
| 67 | + |
| 68 | +If you **do not see Power Platform API**, follow [Step 2 in Power Platform API Authentication](https://learn.microsoft.com/en-us/power-platform/admin/programmability-authentication-v2) to add the API to your tenant. |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +### Configure the Example Application - User Interactive Login |
| 73 | + |
| 74 | +Once you've collected all required values: |
| 75 | + |
| 76 | +1. Set the following environment variables in your terminal or .env file: |
| 77 | + |
| 78 | +```env |
| 79 | +COPILOT_STUDIO_AGENT_APP_CLIENT_ID=<your-app-client-id> |
| 80 | +COPILOT_STUDIO_AGENT_TENANT_ID=<your-tenant-id> |
| 81 | +COPILOT_STUDIO_AGENT_ENVIRONMENT_ID=<your-env-id> |
| 82 | +COPILOT_STUDIO_AGENT_AGENT_IDENTIFIER=<your-agent-id> |
| 83 | +COPILOT_STUDIO_AGENT_AUTH_MODE=interactive |
| 84 | +``` |
| 85 | + |
| 86 | +## Create an Application Registration in Entra ID – Service Principal Login |
| 87 | + |
| 88 | +> **Warning**: Service Principal login is **not yet supported** in the current version of the `CopilotStudioClient`. |
| 89 | +
|
| 90 | +## Creating a `CopilotStudioAgent` Client |
| 91 | + |
| 92 | +If all required environment variables are set correctly, you don't need to manually create or pass a `client`. Semantic Kernel will automatically construct the client using the environment configuration. |
| 93 | + |
| 94 | +However, if you need to override any environment variables—such as when specifying custom credentials or cloud settings—you should create the `client` explicitly using `CopilotStudioAgent.create_client(...)` and then pass it to the agent constructor. |
| 95 | + |
| 96 | +```python |
| 97 | +client: CopilotClient = CopilotStudioAgent.create_client( |
| 98 | + auth_mode: CopilotStudioAgentAuthMode | Literal["interactive", "service"] | None = None, |
| 99 | + agent_identifier: str | None = None, |
| 100 | + app_client_id: str | None = None, |
| 101 | + client_secret: str | None = None, |
| 102 | + client_certificate: str | None = None, |
| 103 | + cloud: PowerPlatformCloud | None = None, |
| 104 | + copilot_agent_type: AgentType | None = None, |
| 105 | + custom_power_platform_cloud: str | None = None, |
| 106 | + env_file_encoding: str | None = None, |
| 107 | + env_file_path: str | None = None, |
| 108 | + environment_id: str | None = None, |
| 109 | + tenant_id: str | None = None, |
| 110 | + user_assertion: str | None = None, |
| 111 | +) |
| 112 | + |
| 113 | +agent = CopilotStudioAgent( |
| 114 | + client=client, |
| 115 | + name="<name>", |
| 116 | + instructions="<instructions>", |
| 117 | +) |
| 118 | +``` |
0 commit comments