使用说明
TypeSpec MCP服务器
一种模型上下文协议(MCP)服务器,提供TypeSpec编译和API规范管理功能。
特性
- TypeSpec编译:将TypeSpec源代码编译为OpenAPI 3.0规范
- 验证:验证TypeSpec语法和语义
- API规范管理:存储、检索和管理API规范
- 模板生成:为常见的API模式生成TypeSpec模板
- OpenAPI输出:从TypeSpec模型生成OpenAPI 3.0规范
可用工具
核心编译工具
compile-typespec-将TypeSpec源代码编译为OpenAPI/JSON模式validate-typespec-验证TypeSpec源代码generate-typespec-template-生成TypeSpec模板
API规范管理
create-api-spec-创建并存储新的API规范list-api-specs-列出所有存储的API规范get-api-spec-检索特定的API规范
资源
typespec://docs/getting-started-TypeSpec入门指南和文档
安装
- 安装依赖项:
npm install
- 构建项目:
npm run build
- 运行服务器:
npm start
发展
在开发模式下启动服务器,并自动重新加载:
npm run dev
编译TypeSpec文件:
npm run typespec:compile
观察TypeSpec文件是否有更改:
npm run typespec:watch
MCP配置
将以下内容添加到MCP客户端配置中:
{
"servers": {
"typespec-mcp-server": {
"type": "stdio",
"command": "node",
"args": ["dist/index.js"]
}
}
}
类型规范示例
基本服务定义
import "@typespec/http";
import "@typespec/rest";
using TypeSpec.Http;
using TypeSpec.Rest;
@service({
title: "My API"
})
namespace MyAPI;
@route("/users")
interface Users {
@get list(): User[];
@post create(@body user: CreateUserRequest): User;
}
model User {
id: string;
name: string;
email: string;
createdAt: utcDateTime;
}
高级CRUD服务
import "@typespec/http";
import "@typespec/rest";
using TypeSpec.Http;
using TypeSpec.Rest;
@service({
title: "Advanced API"
})
namespace AdvancedAPI;
model PaginationParams {
@query page?: int32 = 1;
@query limit?: int32 = 20;
}
model PaginatedResponse<T> {
data: T[];
pagination: {
page: int32;
limit: int32;
total: int32;
};
}
@route("/api/v1/items")
interface Items {
@get
list(...PaginationParams): PaginatedResponse<Item>;
@post
create(@body item: CreateItemRequest): Item;
@get
get(@path id: string): Item;
@put
update(@path id: string, @body item: UpdateItemRequest): Item;
@delete
delete(@path id: string): void;
}
使用示例
将TypeSpec编译为OpenAPI
使用 compile-typespec 带有TypeSpec源的工具:
{
"source": "...",
"outputFormat": "openapi3",
"includeSchemas": true
}
创建和存储API规范
使用 create-api-spec 工具:
{
"name": "My API",
"version": "1.0.0",
"description": "A sample API",
"typespecSource": "..."
}
生成模板
使用 generate-typespec-template 工具:
{
"templateType": "rest-api",
"serviceName": "My Service",
"version": "1.0.0"
}
项目结构
├── src/
│ ├── index.ts # Main MCP server
│ ├── compiler.ts # TypeSpec compiler wrapper
│ └── storage.ts # API specification storage
├── dist/ # Compiled JavaScript output
├── data/ # Stored API specifications
├── main.tsp # Example TypeSpec file
├── tspconfig.yaml # TypeSpec configuration
├── tsconfig.json # TypeScript configuration
└── package.json # Project configuration
依赖项
- @模型上下文协议/sdk -MCP服务器实现
- @类型规范/编译器 -TypeSpec编译器核心
- @类型规范/openapi3 -OpenAPI 3.0发射器
- @类型规范/http -HTTP协议支持
- @型号规格/休息 -REST API约定
- 黄道带 -架构验证
- 表达 -Web框架(用于未来的HTTP传输)
许可证
MIT许可证
贡献
- 复刻仓库
- 创建要素分支
- 进行更改
- 如果适用,添加测试
- 提交拉取请求
支持
对于问题和疑问: