Agent Framework Copilot Studio Integration

1.0.0b260409 · active · verified Wed Apr 15

The `agent-framework-copilotstudio` library provides a Python integration for connecting to Microsoft Copilot Studio agents using the Microsoft Agent Framework. It enables developers to build, orchestrate, and deploy AI agents and multi-agent workflows, supporting both low-code Copilot Studio agents and custom Python agents. The library is actively maintained, with frequent preview releases (indicated by 'b' in version numbers), and is part of Microsoft's broader Agent Framework ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and interact with a `CopilotStudioAgent`. It assumes the necessary environment variables (`COPILOTSTUDIOAGENT__ENVIRONMENTID`, `COPILOTSTUDIOAGENT__SCHEMANAME`, `COPILOTSTUDIOAGENT__AGENTAPPID`, `COPILOTSTUDIOAGENT__TENANTID`) are configured for authentication and agent identification. The example includes both a standard `run` command and a streaming `run` for real-time responses.

import asyncio
import os

# Ensure these environment variables are set before running:
# COPILOTSTUDIOAGENT__ENVIRONMENTID="Default-<guid>"
# COPILOTSTUDIOAGENT__SCHEMANAME="<your-agent-schema-name>"
# COPILOTSTUDIOAGENT__AGENTAPPID="<your-client-id>"
# COPILOTSTUDIOAGENT__TENANTID="<your-tenant-id>"

# Example of how you might get them (replace with actual secure methods in production)
# os.environ['COPILOTSTUDIOAGENT__ENVIRONMENTID'] = os.environ.get('COPILOTSTUDIOAGENT__ENVIRONMENTID', '')
# os.environ['COPILOTSTUDIOAGENT__SCHEMANAME'] = os.environ.get('COPILOTSTUDIOAGENT__SCHEMANAME', '')
# os.environ['COPILOTSTUDIOAGENT__AGENTAPPID'] = os.environ.get('COPILOTSTUDIOAGENT__AGENTAPPID', '')
# os.environ['COPILOTSTUDIOAGENT__TENANTID'] = os.environ.get('COPILOTSTUDIOAGENT__TENANTID', '')

from agent_framework.microsoft import CopilotStudioAgent

async def main():
    try:
        agent = CopilotStudioAgent()
        print(f"Agent initialized with Environment ID: {os.environ.get('COPILOTSTUDIOAGENT__ENVIRONMENTID', 'Not Set')}")
        print("Sending query to Copilot Studio agent...")
        result = await agent.run("What are our company policies on remote work?")
        print(f"Agent response: {result}")
        
        print("\nStreaming example:")
        print("Agent (streaming): ", end="", flush=True)
        async for chunk in agent.run("What is the largest city in France?", stream=True):
            if chunk.text:
                print(chunk.text, end="", flush=True)
        print()

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Please ensure all required environment variables are set and your Copilot Studio agent is correctly configured and published.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →