This repository demonstrates how to create a LangGraph that can interact with a Copilot Studio Agent, handle requests with FastAPI, and respond to user queries.
The following instructions and the code in this repo are adapted from these two samples:
- Agents-for-python/test_samples/copilot_studio_client_sample
- Agents/samples/basic/copilotstudio-client/nodejs
This is a sample to show how to use the @microsoft/agents-copilotstudio-client package to talk to an Agent hosted in CopilotStudio.
To set up this sample, you will need the following:
- An Azure Open AI resource with a deployed model.
- An Agent Created in Microsoft Copilot Studio or access to an existing Agent.
- Ability to Create an Application Identity in Azure for a Public Client/Native App Registration Or access to an existing Public Client/Native App registration with the
CopilotStudio.Copilots.InvokeAPI Permission assigned.
The Copilot Studio Client requires a User Token to operate. For this sample, we are using a user interactive flow to get the user token for the application ID created above. Other flows are allowed.
Important
The token is cached in the user machine
- Create an Agent in Copilot Studio
- Publish your newly created Copilot
- Goto Settings => Advanced => Metadata and copy the following values, You will need them later:
- Schema name
- Environment Id
This step will require permissions to create application identities in your Azure tenant. For this sample, you will create a Native Client Application Identity, which does not have secrets.
- Open https://portal.azure.com
- Navigate to Entra Id
- Create a new App Registration in Entra ID
- Provide a Name
- Choose "Accounts in this organization directory only"
- In the "Select a Platform" list, Choose "Public Client/native (mobile & desktop)
- In the Redirect URI url box, type in
http://localhost(note: use HTTP, not HTTPS) - Then click register.
- In your newly created application
- On the Overview page, Note down for use later when configuring the example application:
- The Application (client) ID
- The Directory (tenant) ID
- Go to API Permissions in
Managesection - Click Add Permission
- In the side panel that appears, Click the tab
API's my organization uses - Search for
Power Platform API.- If you do not see
Power Platform APIsee the note at the bottom of this section.
- If you do not see
- In the Delegated permissions list, choose
CopilotStudioand CheckCopilotStudio.Copilots.Invoke - Click
Add Permissions
- In the side panel that appears, Click the tab
- (Optional) Click
Grant Admin consent for copilotsdk
- On the Overview page, Note down for use later when configuring the example application:
Tip
If you do not see Power Platform API in the list of API's your organization uses, you need to add the Power Platform API to your tenant. To do that, goto Power Platform API Authentication and follow the instructions on Step 2 to add the Power Platform Admin API to your Tenant
With the above information, you can now run the client CopilotStudioClient sample.
- Open the
env.TEMPLATEfile and rename it to.env. - Configure the values based on what was recorded during the setup phase.
OPENAI_API_KEY="" # API Key for the OpenAI resource, this is used to call the OpenAI model.
OPENAI_ENDPOINT="" # Endpoint for the OpenAI resource, this should be in the format `https://<resource-name>.openai.azure.com/`.
OPENAI_API_VERSION="" # API Version for the OpenAI resource, this should be in the format `2023-05-15`.
OPENAI_MODEL="" # Model to use for the OpenAI resource, this should be in the format `gpt-4o-mini`.
COPILOT_AGENT_NAME="" # Name of the Copilot to use, such as "SharePoint".
ENVIRONMENT_ID="" # Environment ID of environment with the CopilotStudio App.
AGENT_IDENTIFIER="" # Schema Name of the Copilot to use
TENANT_ID="" # Tenant ID of the App Registration used to login, this should be in the same tenant as the Copilot.
APP_CLIENT_ID="" # App ID of the App Registration used to login, this should be in the same tenant as the CopilotStudio environment.This guide explains how to create and activate a Python virtual environment using venv for Python versions 3.9 to 3.11.
A virtual environment is an isolated Python environment that allows you to install packages for a specific project without affecting your system's global Python installation. This helps avoid package conflicts between different projects.
- Python 3.9, 3.10, or 3.11 installed on your system
- Basic knowledge of command line operations
- Open a terminal window
- Navigate to your project directory:
cd the-root-of-this-project - Create a virtual environment:
python3 -m venv venv
- Open Command Prompt or PowerShell
- Navigate to your project directory:
cd the-root-of-this-project - Create a virtual environment:
python -m venv venv
source venv/bin/activatevenv\Scripts\activate.bat
venv\Scripts\Activate.ps1
Once activated, you'll notice your command prompt changes to show the name of the activated environment. For example:
(venv) $
When you're done working in the virtual environment, you can deactivate it by running:
deactivateTo verify the Python version in your virtual environment:
python --versionMake sure it shows a version between 3.9 and 3.11 as required.
After activating your virtual environment, you can install packages from pypi test (the test index will be replaced with the regular index before going out of preview):
pip install -r requirements.txt
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-core
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-authorization
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-connector
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-client
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-builder
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-authentication-msal
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-copilotstudio-client
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-hosting-aiohttp
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-storage- To run the FastAPI server, you can use the following command:
uvicorn app:app --host 0.0.0.0 --port 8000
- This should challenge you to login and connect to the Copilot Studio Hosted agent.
- Send a POST request to the '/chat' endpoint with a JSON body containing the query in the 'query' field. For example, you can use
curlto send a request like this in another terminal:curl -Method POST "http://localhost:8000/chat" -Headers @{"Content-Type" = "application/json"} -Body '{"query": "your_query_here"}'