Labsco
microsoft logo

m365-agents-ts

✓ Official2,700

by microsoft · part of microsoft/skills

Build 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.

🔥🔥🔥✓ VerifiedFreeQuick setup
🧩 One of 7 skills in the microsoft/skills package — works on its own, and pairs well with its siblings.

Build 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.

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.

by microsoft

Build 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. npx skills add https://github.com/microsoft/agent-skills --skill m365-agents-ts Download ZIPGitHub2.7k

Microsoft 365 Agents SDK (TypeScript)

Build 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.

Before implementation

  • Use the microsoft-docs MCP to verify the latest API signatures for AgentApplication, startServer, and CopilotStudioClient.

  • Confirm package versions on npm before wiring up samples or templates.

Environment Variables

Copy & paste — that's it
PORT=3978
AZURE_RESOURCE_NAME= 
AZURE_API_KEY= 
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4o-mini

TENANT_ID= 
CLIENT_ID= 
CLIENT_SECRET= 

COPILOT_ENVIRONMENT_ID= 
COPILOT_SCHEMA_NAME= 
COPILOT_CLIENT_ID= 
COPILOT_BEARER_TOKEN= 

Core Workflow: Express-hosted AgentApplication

Copy & paste — that's it
import {
 AgentApplication,
 TurnContext,
 TurnState,
} from "@microsoft/agents-hosting";
import { startServer } from "@microsoft/agents-hosting-express";

const agent = new AgentApplication ();

agent.onConversationUpdate("membersAdded", async (context: TurnContext) => {
 await context.sendActivity("Welcome to the agent.");
});

agent.onMessage("hello", async (context: TurnContext) => {
 await context.sendActivity(`Echo: ${context.activity.text}`);
});

startServer(agent);

Streaming responses with Azure OpenAI

Copy & paste — that's it
import { azure } from "@ai-sdk/azure";
import {
 AgentApplication,
 TurnContext,
 TurnState,
} from "@microsoft/agents-hosting";
import { startServer } from "@microsoft/agents-hosting-express";
import { streamText } from "ai";

const agent = new AgentApplication ();

agent.onMessage("poem", async (context: TurnContext) => {
 context.streamingResponse.setFeedbackLoop(true);
 context.streamingResponse.setGeneratedByAILabel(true);
 context.streamingResponse.setSensitivityLabel({
 type: "https://schema.org/Message",
 "@type": "CreativeWork",
 name: "Internal",
 });

 await context.streamingResponse.queueInformativeUpdate("starting a poem...");

 const { fullStream } = streamText({
 model: azure(process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o-mini"),
 system: "You are a creative assistant.",
 prompt: "Write a poem about Apollo.",
 });

 try {
 for await (const part of fullStream) {
 if (part.type === "text-delta" && part.text.length > 0) {
 await context.streamingResponse.queueTextChunk(part.text);
 }
 if (part.type === "error") {
 throw new Error(`Streaming error: ${part.error}`);
 }
 }
 } finally {
 await context.streamingResponse.endStream();
 }
});

startServer(agent);

Invoke activity handling

Copy & paste — that's it
import { Activity, ActivityTypes } from "@microsoft/agents-activity";
import {
 AgentApplication,
 TurnContext,
 TurnState,
} from "@microsoft/agents-hosting";

const agent = new AgentApplication ();

agent.onActivity("invoke", async (context: TurnContext) => {
 const invokeResponse = Activity.fromObject({
 type: ActivityTypes.InvokeResponse,
 value: { status: 200 },
 });

 await context.sendActivity(invokeResponse);
 await context.sendActivity("Thanks for submitting your feedback.");
});

Copilot Studio client (Direct to Engine)

Copy & paste — that's it
import { CopilotStudioClient } from "@microsoft/agents-copilotstudio-client";

const settings = {
 environmentId: process.env.COPILOT_ENVIRONMENT_ID!,
 schemaName: process.env.COPILOT_SCHEMA_NAME!,
 clientId: process.env.COPILOT_CLIENT_ID!,
};

const tokenProvider = async (): Promise => {
 return process.env.COPILOT_BEARER_TOKEN!;
};

const client = new CopilotStudioClient(settings, tokenProvider);

const conversation = await client.startConversationAsync();
const reply = await client.askQuestionAsync("Hello!", conversation.id);
console.log(reply);

Copilot Studio WebChat integration

Copy & paste — that's it
import { CopilotStudioWebChat } from "@microsoft/agents-copilotstudio-client";

const directLine = CopilotStudioWebChat.createConnection(client, {
 showTyping: true,
});

window.WebChat.renderWebChat(
 {
 directLine,
 },
 document.getElementById("webchat")!,
);

Best Practices

  • Use AgentApplication for routing and keep handlers focused on one responsibility.

  • Prefer streamingResponse for long-running completions and call endStream in finally blocks.

  • Keep secrets out of source code; load tokens from environment variables or secure stores.

  • Reuse CopilotStudioClient instances and cache tokens in your token provider.

  • Validate invoke payloads before logging or persisting feedback.

Reference Links

Resource URL Microsoft 365 Agents SDK https://learn.microsoft.com/en-us/microsoft-365/agents-sdk/ JavaScript SDK overview https://learn.microsoft.com/en-us/javascript/api/overview/agents-overview?view=agents-sdk-js-latest @microsoft/agents-hosting-express https://learn.microsoft.com/en-us/javascript/api/%40microsoft/agents-hosting-express?view=agents-sdk-js-latest @microsoft/agents-copilotstudio-client https://learn.microsoft.com/en-us/javascript/api/%40microsoft/agents-copilotstudio-client?view=agents-sdk-js-latest Integrate with Copilot Studio https://learn.microsoft.com/en-us/microsoft-365/agents-sdk/integrate-with-mcs GitHub samples https://github.com/microsoft/Agents/tree/main/samples/nodejs