|
Add this skill
npx mdskills install sickn33/m365-agents-tsComprehensive SDK skill with complete code patterns, streaming support, and multi-environment integration examples.
1---2name: m365-agents-ts3description: |4 Microsoft 365 Agents SDK for TypeScript/Node.js. Build multichannel agents for Teams/M365/Copilot Studio with AgentApplication routing, Express hosting, streaming responses, and Copilot Studio client integration. Triggers: "Microsoft 365 Agents SDK", "@microsoft/agents-hosting", "AgentApplication", "startServer", "streamingResponse", "Copilot Studio client", "@microsoft/agents-copilotstudio-client".5package: "@microsoft/agents-hosting, @microsoft/agents-hosting-express, @microsoft/agents-activity, @microsoft/agents-copilotstudio-client"6---78# Microsoft 365 Agents SDK (TypeScript)910Build enterprise agents for Microsoft 365, Teams, and Copilot Studio using the Microsoft 365 Agents SDK with Express hosting, AgentApplication routing, streaming responses, and Copilot Studio client integrations.1112## Before implementation13- Use the microsoft-docs MCP to verify the latest API signatures for AgentApplication, startServer, and CopilotStudioClient.14- Confirm package versions on npm before wiring up samples or templates.1516## Installation1718```bash19npm install @microsoft/agents-hosting @microsoft/agents-hosting-express @microsoft/agents-activity20npm install @microsoft/agents-copilotstudio-client21```2223## Environment Variables2425```bash26PORT=397827AZURE_RESOURCE_NAME=<azure-openai-resource>28AZURE_API_KEY=<azure-openai-key>29AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4o-mini3031TENANT_ID=<tenant-id>32CLIENT_ID=<client-id>33CLIENT_SECRET=<client-secret>3435COPILOT_ENVIRONMENT_ID=<environment-id>36COPILOT_SCHEMA_NAME=<schema-name>37COPILOT_CLIENT_ID=<copilot-app-client-id>38COPILOT_BEARER_TOKEN=<copilot-jwt>39```4041## Core Workflow: Express-hosted AgentApplication4243```typescript44import { AgentApplication, TurnContext, TurnState } from "@microsoft/agents-hosting";45import { startServer } from "@microsoft/agents-hosting-express";4647const agent = new AgentApplication<TurnState>();4849agent.onConversationUpdate("membersAdded", async (context: TurnContext) => {50 await context.sendActivity("Welcome to the agent.");51});5253agent.onMessage("hello", async (context: TurnContext) => {54 await context.sendActivity(`Echo: ${context.activity.text}`);55});5657startServer(agent);58```5960## Streaming responses with Azure OpenAI6162```typescript63import { azure } from "@ai-sdk/azure";64import { AgentApplication, TurnContext, TurnState } from "@microsoft/agents-hosting";65import { startServer } from "@microsoft/agents-hosting-express";66import { streamText } from "ai";6768const agent = new AgentApplication<TurnState>();6970agent.onMessage("poem", async (context: TurnContext) => {71 context.streamingResponse.setFeedbackLoop(true);72 context.streamingResponse.setGeneratedByAILabel(true);73 context.streamingResponse.setSensitivityLabel({74 type: "https://schema.org/Message",75 "@type": "CreativeWork",76 name: "Internal",77 });7879 await context.streamingResponse.queueInformativeUpdate("starting a poem...");8081 const { fullStream } = streamText({82 model: azure(process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o-mini"),83 system: "You are a creative assistant.",84 prompt: "Write a poem about Apollo.",85 });8687 try {88 for await (const part of fullStream) {89 if (part.type === "text-delta" && part.text.length > 0) {90 await context.streamingResponse.queueTextChunk(part.text);91 }92 if (part.type === "error") {93 throw new Error(`Streaming error: ${part.error}`);94 }95 }96 } finally {97 await context.streamingResponse.endStream();98 }99});100101startServer(agent);102```103104## Invoke activity handling105106```typescript107import { Activity, ActivityTypes } from "@microsoft/agents-activity";108import { AgentApplication, TurnContext, TurnState } from "@microsoft/agents-hosting";109110const agent = new AgentApplication<TurnState>();111112agent.onActivity("invoke", async (context: TurnContext) => {113 const invokeResponse = Activity.fromObject({114 type: ActivityTypes.InvokeResponse,115 value: { status: 200 },116 });117118 await context.sendActivity(invokeResponse);119 await context.sendActivity("Thanks for submitting your feedback.");120});121```122123## Copilot Studio client (Direct to Engine)124125```typescript126import { CopilotStudioClient } from "@microsoft/agents-copilotstudio-client";127128const settings = {129 environmentId: process.env.COPILOT_ENVIRONMENT_ID!,130 schemaName: process.env.COPILOT_SCHEMA_NAME!,131 clientId: process.env.COPILOT_CLIENT_ID!,132};133134const tokenProvider = async (): Promise<string> => {135 return process.env.COPILOT_BEARER_TOKEN!;136};137138const client = new CopilotStudioClient(settings, tokenProvider);139140const conversation = await client.startConversationAsync();141const reply = await client.askQuestionAsync("Hello!", conversation.id);142console.log(reply);143```144145## Copilot Studio WebChat integration146147```typescript148import { CopilotStudioWebChat } from "@microsoft/agents-copilotstudio-client";149150const directLine = CopilotStudioWebChat.createConnection(client, {151 showTyping: true,152});153154window.WebChat.renderWebChat({155 directLine,156}, document.getElementById("webchat")!);157```158159## Best Practices1601611. Use AgentApplication for routing and keep handlers focused on one responsibility.1622. Prefer streamingResponse for long-running completions and call endStream in finally blocks.1633. Keep secrets out of source code; load tokens from environment variables or secure stores.1644. Reuse CopilotStudioClient instances and cache tokens in your token provider.1655. Validate invoke payloads before logging or persisting feedback.166167## Reference Files168169| File | Contents |170| --- | --- |171| [references/acceptance-criteria.md](references/acceptance-criteria.md) | Import paths, hosting pipeline, streaming, and Copilot Studio patterns |172173## Reference Links174175| Resource | URL |176| --- | --- |177| Microsoft 365 Agents SDK | https://learn.microsoft.com/en-us/microsoft-365/agents-sdk/ |178| JavaScript SDK overview | https://learn.microsoft.com/en-us/javascript/api/overview/agents-overview?view=agents-sdk-js-latest |179| @microsoft/agents-hosting-express | https://learn.microsoft.com/en-us/javascript/api/%40microsoft/agents-hosting-express?view=agents-sdk-js-latest |180| @microsoft/agents-copilotstudio-client | https://learn.microsoft.com/en-us/javascript/api/%40microsoft/agents-copilotstudio-client?view=agents-sdk-js-latest |181| Integrate with Copilot Studio | https://learn.microsoft.com/en-us/microsoft-365/agents-sdk/integrate-with-mcs |182| GitHub samples | https://github.com/microsoft/Agents/tree/main/samples/nodejs |183
Full transparency — inspect the skill content before installing.