m365-agents-ts

โดย microsoft

Microsoft 365 Agents SDK สำหรับ TypeScript/Node.js สร้างเอเจนต์แบบหลายช่องทางสำหรับ Teams/M365/Copilot Studio ด้วยการกำหนดเส้นทาง AgentApplication, โฮสติ้ง Express, การตอบสนองแบบสตรีมมิ่ง และการรวมไคลเอนต์ Copilot Studio ทริกเกอร์: "Microsoft 365 Agents SDK", "@microsoft/agents-hosting", "AgentApplication", "startServer", "streamingResponse", "Copilot Studio client", "@microsoft/agents-copilotstudio-client

npx skills add https://github.com/microsoft/skills --skill m365-agents-ts

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.

Installation

npm install @microsoft/agents-hosting @microsoft/agents-hosting-express @microsoft/agents-activity
npm install @microsoft/agents-copilotstudio-client

Environment Variables

PORT=3978
AZURE_RESOURCE_NAME=<azure-openai-resource>
AZURE_API_KEY=<azure-openai-key>
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4o-mini

TENANT_ID=<tenant-id>
CLIENT_ID=<client-id>
CLIENT_SECRET=<client-secret>

COPILOT_ENVIRONMENT_ID=<environment-id>
COPILOT_SCHEMA_NAME=<schema-name>
COPILOT_CLIENT_ID=<copilot-app-client-id>
COPILOT_BEARER_TOKEN=<copilot-jwt>

Core Workflow: Express-hosted AgentApplication

import {
  AgentApplication,
  TurnContext,
  TurnState,
} from "@microsoft/agents-hosting";
import { startServer } from "@microsoft/agents-hosting-express";

const agent = new AgentApplication<TurnState>();

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

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<TurnState>();

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

import { Activity, ActivityTypes } from "@microsoft/agents-activity";
import {
  AgentApplication,
  TurnContext,
  TurnState,
} from "@microsoft/agents-hosting";

const agent = new AgentApplication<TurnState>();

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)

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<string> => {
  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

import { CopilotStudioWebChat } from "@microsoft/agents-copilotstudio-client";

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

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

Best Practices

  1. Use AgentApplication for routing and keep handlers focused on one responsibility.
  2. Prefer streamingResponse for long-running completions and call endStream in finally blocks.
  3. Keep secrets out of source code; load tokens from environment variables or secure stores.
  4. Reuse CopilotStudioClient instances and cache tokens in your token provider.
  5. Validate invoke payloads before logging or persisting feedback.

Reference Links

ResourceURL
Microsoft 365 Agents SDKhttps://learn.microsoft.com/en-us/microsoft-365/agents-sdk/
JavaScript SDK overviewhttps://learn.microsoft.com/en-us/javascript/api/overview/agents-overview?view=agents-sdk-js-latest
@microsoft/agents-hosting-expresshttps://learn.microsoft.com/en-us/javascript/api/%40microsoft/agents-hosting-express?view=agents-sdk-js-latest
@microsoft/agents-copilotstudio-clienthttps://learn.microsoft.com/en-us/javascript/api/%40microsoft/agents-copilotstudio-client?view=agents-sdk-js-latest
Integrate with Copilot Studiohttps://learn.microsoft.com/en-us/microsoft-365/agents-sdk/integrate-with-mcs
GitHub sampleshttps://github.com/microsoft/Agents/tree/main/samples/nodejs

Skills เพิ่มเติมจาก microsoft

a365-code-validator
microsoft
ตรวจสอบและแก้ไขโค้ดการสังเกตการณ์ของ Agent 365 เพื่อให้เอเจนต์ส่งข้อมูลเทเลเมทรีของ MAC Activity จริง ใช้เมื่อเทเลเมทรีของเอเจนต์ Agent 365 ไม่…
a365-setup
microsoft
จุดเริ่มต้นสำหรับการลงทะเบียน Agent 365 (A365) ทั่วไปและการตั้งค่า CLI — ใช้สกิลนี้เมื่อผู้ใช้ต้องการ "ตั้งค่า A365", "ลงทะเบียนเอเจนต์", "สร้าง…
aca-sandboxes
microsoft
Use when the user wants to create, manage, or operate Azure Container Apps sandboxes — hardware-isolated microVMs driven by the `aca` CLI. USE FOR: create…
accessibility
microsoft
ทักษะการช่วยการเข้าถึงหลักสำหรับ VS Code จำเป็นสำหรับงานฟีเจอร์ใหม่และการมีส่วนร่วม และยังใช้กับการอัปเดต UI ที่มีอยู่ด้วย ครอบคลุมความช่วยเหลือด้านการช่วยการเข้าถึง…
accessibility
microsoft
จุดเริ่มต้นทักษะการเข้าถึงที่รวมเข้าด้วยกันสำหรับ WCAG 2.2, แนวปฏิบัติการเขียน ARIA, การเข้าถึงสำหรับผู้ที่มีความบกพร่องทางสติปัญญา, มาตรา 508, EN 301 549, และการเข้าถึง…
accessibility-aria-expert
microsoft
ตรวจจับและแก้ไขปัญหาการเข้าถึงใน React/Fluent UI webviews ใช้เมื่อตรวจสอบโค้ดเพื่อความเข้ากันได้กับโปรแกรมอ่านหน้าจอ แก้ไขป้ายกำกับ ARIA ทำให้มั่นใจว่า…
action-item-extractor
microsoft
แยกไอเท็มที่ต้องดำเนินการพร้อมผู้รับผิดชอบ กำหนดเวลา และลำดับความสำคัญจากเนื้อหาการประชุม
activate-site
microsoft
ตรวจสอบปลั๊กอิน : รัน node "${CLAUDE_PLUGIN_ROOT}/scripts/check-version.js" — หากมีข้อความแสดงผล ให้แสดงให้ผู้ใช้เห็นก่อนดำเนินการต่อ