mcp到打字代码生成器
从MCP(模型上下文协议)服务器生成TypeScript类型。
安装
npm install -g mcp-to-typescript-codegen或者与npx一起使用:
npx mcp-to-typescript-codegen --command "your-mcp-server"用法
标准传输(本地服务器)
# Basic usage
mcp-to-typescript-codegen --command "mcp-server"
# With arguments
mcp-to-typescript-codegen --command "npx" --args "@modelcontextprotocol/server-filesystem,/tmp"
# Custom output file
mcp-to-typescript-codegen --command "mcp-server" --output "./types/mcp.ts"
# With namespace prefix (for multiple servers)
mcp-to-typescript-codegen --command "apollo-mcp" --name Apollo --output "./types/apollo.ts"HTTP传输(远程服务器)
mcp-to-typescript-codegen --server "http://localhost:3000/mcp"选项
| 标志 | 简短 | 描述 |
|---|---|---|
--command | -c | 生成MCP服务器的命令(stdio传输) |
--args | -a | 命令的逗号分隔参数 |
--server | -s | HTTP MCP服务器的URL(可流式传输HTTP) |
--output | -o | 输出文件路径(默认: generated/mcp-tools.ts) |
| `--name | ||
| ` | -n | 生成的类型名称的前缀(例如“Apollo”) |
--debug | -d | 从服务器打印原始工具架构 |
--help | -h | 显示帮助 |
生成的输出
// Auto-generated by mcp-to-typescript-codegen
// Do not edit manually
export type ReadFileParams = {
path: string;
};
export type ListDirectoryParams = {
path: string;
};
export type ToolName = "read_file" | "list_directory";
export const toolNames = ["read_file", "list_directory"] as const;随着 --name Apollo:
export type ApolloExecuteParams = {
query: string;
variables?: string;
};
export type ApolloToolName = "execute" | "introspect";
export const apolloToolNames = ["execute", "introspect"] as const;使用生成的类型
import { ReadFileParams, ToolName } from "./generated/mcp-tools";
// Type-safe params
const params: ReadFileParams = {
path: "/etc/hosts",
};
// Type-safe tool name
const toolName: ToolName = "read_file";多个MCP服务器
为具有不同前缀的多个服务器生成类型:
# Apollo GraphQL MCP
mcp-to-typescript-codegen -c "apollo-mcp" -n Apollo -o "./types/apollo.ts"
# Filesystem MCP
mcp-to-typescript-codegen -c "npx" -a "@modelcontextprotocol/server-filesystem,/tmp" -n Filesystem -o "./types/filesystem.ts"然后导入每个:
import { ApolloExecuteParams, ApolloToolName } from "./types/apollo";
import { FilesystemReadFileParams, FilesystemToolName } from "./types/filesystem";许可证
麻省理工学院
