Microsoft 365 Agents SDK for Python

repository·main·Indexed 41 days ago

https://github.com/microsoft/agents-for-python

A comprehensive framework for building, hosting, and orchestrating enterprise-grade conversational agents for platforms like M365, Teams, and Copilot Studio. Includes a dedicated testing framework featuring AiohttpScenario, ExternalScenario, and ActivityHandlerScenario, a fluent assertion API with Expect and Select, and the agt CLI for interactive and load testing.

Tokens
52.9K
Snippets
150
Records
222
Agent score
52%

What's inside Microsoft 365 Agents SDK for Python

  1. Overview of Microsoft Agents Copilot Studio Client

    main
    The microsoft-agents-copilotstudio-client is a specialized client within the Microsoft 365 Agents SDK for Python. It provides a direct-to-engine connection for interacting with agents built in Microsoft Copilot Studio. This is useful for developers who want to embed Copilot Studio agent capabilities into custom Python-based environments without relying on standard chat interface integrations.
  2. Overview of microsoft-agents-hosting-msteams

    main

    The microsoft-agents-hosting-msteams library is a Teams-specific extension for the Microsoft 365 Agents SDK for Python. It allows developers to build enterprise-grade conversational AI agents that integrate deeply with Microsoft Teams.

    Key features include:

    • TeamsAgentExtension: Provides typed, decorator-based route registrars for various Teams activities.
    • TeamsTurnContext: A Teams-aware wrapper around the base TurnContext that provides access to the Teams API client and typed activity helpers.
    • Comprehensive Activity Support: Handles messaging extensions, task modules, meeting lifecycle events, channel/team management, file consent, and configuration invokes.
  3. Overview of Microsoft Agents Activity

    main
    The microsoft-agents-activity library provides core types and schemas for building conversational AI agents that work across Microsoft 365 platforms, including Microsoft Teams, Copilot Studio, and Webchat. It is a key component of the Microsoft 365 Agents SDK for Python, which is a framework for creating enterprise-grade agents with support for third-party integrations like Slack, Facebook Messenger, and Twilio.
  4. What is microsoft-agents-hosting-teams?

    main

    The microsoft-agents-hosting-teams library is an integration package for the Microsoft 365 Agents SDK for Python. It acts as a bridge to make agents "Teams-native" by providing specialized handlers and utilities for Teams-specific interaction patterns.

    Key capabilities include:

    • Messaging Extensions: Search and action-based extensions.
    • Task Modules: Interactive dialogs and forms.
    • Adaptive Cards: Rich card interactions.
    • Meeting Events: Handling start, end, and participant changes.
    • Team Management: Operations for members and channels.
    • File Handling: Upload/download with consent flows.
    • Tab Apps: Personal and team tab interactions.
    • Proactive Messaging: Sending messages to channels or users.
  5. Overview of Microsoft 365 Agents SDK for Python

    main
    The Microsoft 365 Agent SDK is a framework for building full-stack, multichannel, enterprise-grade agents for platforms such as M365, Teams, Copilot Studio, and Webchat. It provides building blocks to handle user interactions, orchestrate requests, reason responses, and collaborate with other agents. The SDK allows integration with components from Azure AI Foundry SDK, Semantic Kernel, and other AI vendors.
  6. Available Teams Extension Samples

    main

    The following samples demonstrate different TeamsAgentExtension capabilities using the Teams SDK Pydantic models from microsoft-teams-api:

    • message_extensions_agent.py: Demonstrates composeExtension/* invocations including search query, select item, submit action, query link, settings URL, configure settings, and fetch task.
    • task_modules_agent.py: Demonstrates task/fetch and task/submit for simple forms, webpage dialogs, and multi-step (chained submits) flows.
    • meeting_events_agent.py: Demonstrates meeting start/end using the MeetingDetails model, as well as participant join/leave and read receipts.
  7. Supported features in microsoft-agents-hosting-msteams

    main

    The microsoft-agents-hosting-msteams package supports a wide range of Microsoft Teams interactions, including:

    • Messaging Extensions: query, fetch action, submit action, link unfurling, anonymous link unfurling, settings URL, configure settings, card button clicked, and bot message preview.
    • Task Modules: fetch and submit with optional verb matching.
    • Lifecycle Events: Meeting lifecycle (start, end, participant join/leave), Channel management (created, deleted, renamed, shared, unshared, restored, members added/removed), and Team management (archived, unarchived, deleted, hard deleted, renamed, restored).
    • Other Flows: File consent (accept/decline), Config invokes (config/fetch and config/submit), and Message updates (edit, delete, undelete, read receipts, actionable message execute).
    • Routing: TeamsAgentExtension routing, which wraps any AgentApplication with zero configuration.
  8. Use `contains` and `Contains` to search nested data

    main

    The contains factory and Contains class provide predicates that recursively walk through Pydantic models, dictionaries, and iterables to find matching values. This is useful for searching inside nested properties like attachments, channel_data, or card payloads without needing to know the exact path.

    Matching Behavior

    • Pydantic models: Visits field values.
    • Dictionaries: Visits dictionary values.
    • Iterables: Visits each item (except strings and bytes).
    • Scalars: Stops traversal if the predicate does not match.
    • Field Aliases: You can use Python field names (e.g., content_type) even if the serialized payload uses aliases (e.g., contentType).

    Configuration and Constraints

    • Depth Limits: Use .depth(n) to limit how deep the search goes. The root object is depth 0. .depth() returns a new instance and does not mutate the original.
    • Invalid Inputs: contains() (empty), contains({}) (unfiltered), and contains(None) are invalid because they would match everything.
    from microsoft_agents.hosting.testing.utils import contains
    from microsoft_agents.hosting.testing.utils.contains import Contains
    
    # Using the factory
    client.expect().that_for_any(
        attachments=contains(content_type="application/vnd.microsoft.card.hero")
    )
    
    # Using the class for reuse
    has_hero_card = Contains(content_type="application/vnd.microsoft.card.hero")
    hero_replies = client.select().where(has_hero_card).get()
    
    # Limiting depth
    deep_search = contains(content_type="hero")
    shallow_search = deep_search.depth(1)
  9. How Entra ID Auth Sidecar works

    main

    The microsoft-agents-authentication-entra-auth-sidecar library integrates the Microsoft 365 Agents SDK with the Microsoft Entra ID Agent Container (the sidecar).

    Instead of the agent process handling secrets, certificates, or keys directly via MSAL, the SDK delegates token acquisition to the sidecar's HTTP API. This provides:

    • Credential-free agent code: All credential management (Managed Identity, Workload Identity, etc.) lives in the sidecar.
    • Language-agnosticism: The sidecar provides a simple HTTP API used by all language SDKs.
    • Consistent environments: The same container can run locally (Docker) and in production.

    The sidecar handles the entire agentic identity chain (Blueprint → Instance → agentic User) internally and returns the final resource token in a single request.

    from microsoft_agents.hosting.core import ConnectionManager
    from microsoft_agents.authentication.entra_auth_sidecar import SidecarAuth
    
    connection_manager = ConnectionManager(
        provider_factory=SidecarAuth,
        CONNECTIONS={
            "SERVICE_CONNECTION": {
                "SETTINGS": {
                    "AUTHTYPE": "EntraAuthSideCar",
                    "CLIENTID": "<blueprint-app-id>",
                }
            }
        },
        CONNECTIONSMAP=[{"SERVICEURL": "*", "CONNECTION": "SERVICE_CONNECTION"}],
    )
  10. Use Fluent Assertions for collections

    main

    The fluent assertion engine provides a way to assert over collections of dictionaries and Pydantic models. It is primarily used via Expect to validate items and Select to filter or manipulate collections before validation. Most tests should begin with either Expect(items) or Select(items).

    Specialized typed wrappers are available for specific collection types:

    • ActivityExpect / ActivitySelect: For activity collections.
    • ExchangeExpect / ExchangeSelect: For exchange collections.
    from microsoft_agents.hosting.testing import Expect, Select
    
    items = [
        {"type": "message", "text": "welcome"},
        {"type": "typing"},
    ]
    
    # Assert that at least one item is a message containing 'welcome'
    Expect(items).that_for_any(type="message", text="~welcome")
    
    # Filter for messages, then assert none of them contain 'error'
    Select(items).where(type="message").expect().that_for_none(text="~error")
  11. Security Best Practices for Microsoft Agents

    main

    When implementing authentication, follow these security guidelines:

    • Secret Management: Store secrets in Azure Key Vault or environment variables rather than hardcoding them.
    • Managed Identities: Use managed identities whenever possible to eliminate the need for manual secret management.
    • Rotation: Regularly rotate client secrets and certificates.
    • Least Privilege: Apply the principle of least privilege when defining scopes and permissions.