MCP工具IDL解析器
一个基于Go的MCP工具IDL文件解析器,为MCP输入、输出和数据类型生成Go结构。
特性
- 多工具支持:在单个文件中解析多个MCP工具定义
- 数据类型定义:定义可重用的数据类型
data关键词 - 丰富的数据类型:支持
string,int,int64,float64,bool,object(转换为map[string]any)以及阵列(例如。,[string]) - 指针类型:自动为非必填字段生成指针类型
- 吸气方法:为结构中的所有字段生成getter方法
- Go软件包支持:指定生成的代码的包名称
- 代码格式化:生成的代码格式为
go/format - 生成的文件注释:包括
DO NOT EDIT注释以防止手动修改 - 递归处理:支持
./...和dir/...处理多个文件的模式 - 简易安装:通过安装
go install
安装
go install github.com/MegcFn/mcp-idl/cmd/mcpidl@latest用法
基本用法
mcpidl 自定义输出文件名
mcpidl 递归处理当前目录中的所有文件
mcpidl ./...递归处理特定目录中的所有文件
mcpidl test/...IDL语法
文件结构
go_package = "package_name"
// Define reusable data types
data data_type_name {
field_name: type "Field description" [required]
// More fields...
}
// Tool definition
tool tool_name {
description "Tool description"
input {
field_name: type "Field description" [required]
// More fields...
}
output {
field_name: type "Field description"
// More fields...
}
}
// Additional tools...数据类型定义
// Define a simple data type
data system {
curr_app: string "Current application" required
version: string "Version" required
is_active: bool "Is active"
}
// Define a data type with nested data reference
data user_info {
user_id: string "User ID" required
user_name: string "User name" required
system_info: system "System information" required
settings: object "User settings" required
}工具定义
tool GetUserInfo {
description "Get user information"
input {
user_id: string "User ID" required
}
output {
success: bool "Whether the operation succeeded"
error_message: string "Error message if operation failed"
user_info: user_info "User information" required
}
}支持的类型
- 数量类型:
string,int,int64,float64,bool - 对象类型:
object(转换为map[string]any) - 数组类型:
[string],[int],[object]等等。(转换为[]string,[]int,[]map[string]any等等) - 数据类型引用:引用以前定义的数据类型
字段修改器
- 必需的:将字段标记为必填字段(非必填字段以指针类型生成)
示例
输入IDL文件(example.mcp)
go_package = "main"
// Define data types
data System {
currApp: string "当前app" required
version: string "版本号" required
isActive: bool "是否激活"
}
data UserInfo {
userId: string "用户ID" required
userName: string "用户名" required
email: string "邮箱"
phone: string "手机号"
systemInfo: System "系统信息" required
settings: object "用户设置" required
tags: [string] "用户标签"
}
// Define tools
tool GetUserInfo {
description "Get user information"
input {
userId: string "User ID" required
}
output {
success: bool "Whether the operation succeeded"
error_message: string "Error message if operation failed"
userInfo: UserInfo "User information" required
}
}生成的Go代码(example.mcpc.go)
// Code generated by mcpidl. DO NOT EDIT.
package main
type System struct {
CurrApp string `json:"curr_app" jsonschema:"当前app"`
Version string `json:"version" jsonschema:"版本号"`
IsActive *bool `json:"is_active" jsonschema:"是否激活"`
}
// GetCurrApp returns the value of CurrApp field
func (s System) GetCurrApp() string {
return s.CurrApp
}
// GetVersion returns the value of Version field
func (s System) GetVersion() string {
return s.Version
}
// GetIsActive returns the value of IsActive field
func (s System) GetIsActive() bool {
if s.IsActive == nil {
var zero bool
return zero
}
return *s.IsActive
}
type UserInfo struct {
UserId string `json:"user_id" jsonschema:"用户ID"`
UserName string `json:"user_name" jsonschema:"用户名"`
Email *string `json:"email" jsonschema:"邮箱"`
Phone *string `json:"phone" jsonschema:"手机号"`
SystemInfo System `json:"system_info" jsonschema:"系统信息"`
Settings map[string]any `json:"settings" jsonschema:"用户设置"`
Tags []string `json:"tags" jsonschema:"用户标签"`
}
// GetUserId returns the value of UserId field
func (s UserInfo) GetUserId() string {
return s.UserId
}
// GetUserName returns the value of UserName field
func (s UserInfo) GetUserName() string {
return s.UserName
}
// GetEmail returns the value of Email field
func (s UserInfo) GetEmail() string {
if s.Email == nil {
var zero string
return zero
}
return *s.Email
}
// GetPhone returns the value of Phone field
func (s UserInfo) GetPhone() string {
if s.Phone == nil {
var zero string
return zero
}
return *s.Phone
}
// GetSystemInfo returns the value of SystemInfo field
func (s UserInfo) GetSystemInfo() System {
return s.SystemInfo
}
// GetSettings returns the value of Settings field
func (s UserInfo) GetSettings() map[string]any {
return s.Settings
}
// GetTags returns the value of Tags field
func (s UserInfo) GetTags() []string {
return s.Tags
}
const GetUserInfoTool string = "get_user_info"
const GetUserInfoDescription string = `Get user information`
type GetUserInfoInput struct {
UserId string `json:"user_id" jsonschema:"User ID"`
}
// GetUserId returns the value of UserId field
func (s GetUserInfoInput) GetUserId() string {
return s.UserId
}
type GetUserInfoOutput struct {
Success *bool `json:"success" jsonschema:"Whether the operation succeeded"`
ErrorMessage *string `json:"error_message" jsonschema:"Error message if operation failed"`
UserInfo UserInfo `json:"user_info" jsonschema:"User information"`
}
// GetSuccess returns the value of Success field
func (s GetUserInfoOutput) GetSuccess() bool {
if s.Success == nil {
var zero bool
return zero
}
return *s.Success
}
// GetErrorMessage returns the value of ErrorMessage field
func (s GetUserInfoOutput) GetErrorMessage() string {
if s.ErrorMessage == nil {
var zero string
return zero
}
return *s.ErrorMessage
}
// GetUserInfo returns the value of UserInfo field
func (s GetUserInfoOutput) GetUserInfo() UserInfo {
return s.UserInfo
}文件名约定
- 输入文件:
.mcp - 输出文件(默认):
.mcpc.go
项目结构
mcp-idl/
├── cmd/
│ └── mcpidl/
│ └── main.go # Main parser implementation
├── test/
│ ├── *.mcp # Test IDL files
│ └── *.mcpc.go # Generated test files
├── go.mod # Go module definition
└── README.md # This file发展
建筑
go build -o mcpidl ./cmd/mcpidl测试
go run ./cmd/mcpidl 许可证
麻省理工学院
