mcpField
T
typespec-create-api-plugin

typespec-create-api-plugin

github.com/github开发40.5k8.7k 次安装1 天前更新
代码仓库

关于 typespec-create-api-plugin

typespec-create-api-plugin 是一项使用 TypeSpec 为 Microsoft 365 Copilot 创建 API 插件的技能,面向将外部 REST API 接入 Copilot 的开发者。它生成 Agent 定义和 API 操作文件,并支持多种身份验证及响应卡片配置。

主要功能

  1. 01生成 main.tsp Agent 定义
  2. 02生成 actions.tsp API 操作
  3. 03配置 API Key、OAuth2 等身份验证
  4. 04设置操作确认和 Adaptive Cards
  5. 05添加推理与响应指令
  6. 06按 REST 约定描述接口操作

适用场景

  1. 01为 REST API 创建 Copilot 插件
  2. 02根据 API 配置身份验证方式
  3. 03为有风险的操作添加确认
  4. 04将结果配置为 Adaptive Cards

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/github/awesome-copilot --skill typespec-create-api-plugin

安装后在 Claude Code 中输入/typespec-create-api-plugin确认已加载。

详细介绍

创建 TypeSpec API 插件

为 Microsoft 365 Copilot 创建一个完整的 TypeSpec API 插件,用于与外部 REST API 集成。

要求

生成 TypeSpec 文件,包含:

main.tsp - Agent 定义
import "@typespec/http";
import "@typespec/openapi3";
import "@microsoft/typespec-m365-copilot";
import "./actions.tsp";

using TypeSpec.Http;
using TypeSpec.M365.Copilot.Agents;
using TypeSpec.M365.Copilot.Actions;

@agent({
  name: "[Agent Name]",
  description: "[Description]"
})
@instructions("""
  [Instructions for using the API operations]
""")
namespace [AgentName] {
  // Reference operations from actions.tsp
  op operation1 is [APINamespace].operationName;
}
actions.tsp - API 操作
import "@typespec/http";
import "@microsoft/typespec-m365-copilot";

using TypeSpec.Http;
using TypeSpec.M365.Copilot.Actions;

@service
@actions(#{
    nameForHuman: "[API Display Name]",
    descriptionForModel: "[Model description]",
    descriptionForHuman: "[User description]"
})
@server("[API_BASE_URL]", "[API Name]")
@useAuth([AuthType]) // Optional
namespace [APINamespace] {
  
  @route("[/path]")
  @get
  @action
  op operationName(
    @path param1: string,
    @query param2?: string
  ): ResponseModel;

  model ResponseModel {
    // Response structure
  }
}

身份验证选项

根据 API 要求选择:

  1. 无身份验证(公共 API)

    // No @useAuth decorator needed
    
  2. API 密钥

    @useAuth(ApiKeyAuth<ApiKeyLocation.header, "X-API-Key">)
    
  3. OAuth2

    @useAuth(OAuth2Auth<[{
      type: OAuth2FlowType.authorizationCode;
      authorizationUrl: "https://oauth.example.com/authorize";
      tokenUrl: "https://oauth.example.com/token";
      refreshUrl: "https://oauth.example.com/token";
      scopes: ["read", "write"];
    }]>)
    
  4. 已注册的身份验证引用

    @useAuth(Auth)
    
    @authReferenceId("registration-id-here")
    model Auth is ApiKeyAuth<ApiKeyLocation.header, "X-API-Key">
    

功能能力

确认对话框
@capabilities(#{
  confirmation: #{
    type: "AdaptiveCard",
    title: "Confirm Action",
    body: """
    Are you sure you want to perform this action?
      * **Parameter**: {{ function.parameters.paramName }}
    """
  }
})
自适应卡片响应
@card(#{
  dataPath: "$.items",
  title: "$.title",
  url: "$.link",
  file: "cards/card.json"
})
推理与响应指令
@reasoning("""
  Consider user's context when calling this operation.
  Prioritize recent items over older ones.
""")
@responding("""
  Present results in a clear table format with columns: ID, Title, Status.
  Include a summary count at the end.
""")

最佳实践

  1. 操作名称:使用清晰、面向操作的名称(listProjects、createTicket)
  2. 模型:为请求和响应定义类似 TypeScript 的模型
  3. HTTP 方法:使用合适的动词(@get、@post、@patch、@delete)
  4. 路径:使用符合 RESTful 规范的路径约定并配合 @route
  5. 参数:适当使用 @path、@query、@header、@body
  6. 描述:提供清晰的描述以便模型理解
  7. 确认:为破坏性操作添加确认(删除、更新关键数据)
  8. 卡片:用于包含多个数据项的丰富可视化响应

工作流

询问用户:

  1. API 的基础 URL 和用途是什么?
  2. 需要哪些操作(CRUD 操作)?
  3. API 使用哪种身份验证方法?
  4. 是否有任何操作需要确认?
  5. 响应是否需要自适应卡片?

然后生成:

  • 包含 Agent 定义的完整 main.tsp
  • 包含 API 操作和模型的完整 actions.tsp
  • 如果需要自适应卡片,可选的 cards/card.json

评论

支持 Markdown