Documentation
¶
Index ¶
- Constants
- func ContentToJSON(content []Content) ([]json.RawMessage, error)
- func GetSupportedVersions() []string
- func IDToString(id json.RawMessage) string
- func IsVersionSupported(version string) bool
- func StringToID(id string) json.RawMessage
- func ValidateStructuredOutput(data interface{}, schema JSONSchema) error
- type CallToolParams
- type CallToolRequest
- type CallToolResult
- func NewToolResult(content []Content, isError bool) *CallToolResult
- func NewToolResultError(errorMsg string) *CallToolResult
- func NewToolResultText(text string) *CallToolResult
- func NewToolResultTextWithStructured(text string, structuredContent interface{}) *CallToolResult
- func NewToolResultWithStructured(content []Content, structuredContent interface{}) *CallToolResult
- type ClientCapabilities
- type ClientInfo
- type Content
- type ContentType
- type GetPromptParams
- type GetPromptRequest
- type GetPromptResult
- type ImageContent
- type InitializeRequest
- type InitializeResult
- type JSONRPCError
- type JSONRPCMessage
- type JSONSchema
- type ListPromptsParams
- type ListPromptsRequest
- type ListPromptsResult
- type ListResourceTemplatesRequest
- type ListResourceTemplatesResult
- type ListResourcesParams
- type ListResourcesRequest
- type ListResourcesResult
- type ListToolsParams
- type ListToolsRequest
- type ListToolsResult
- type LoggingCapability
- type PaginatedResult
- type PaginationParams
- type Prompt
- type PromptArgument
- type PromptMessage
- type PromptsCapability
- type PromptsListChangedNotification
- type ReadResourceParams
- type ReadResourceRequest
- type ReadResourceResult
- type Resource
- type ResourceContents
- type ResourceTemplate
- type ResourcesCapability
- type ResourcesListChangedNotification
- type ResourcesUpdatedNotification
- type Role
- type RootsCapability
- type SamplingCapability
- type ServerCapabilities
- type ServerInfo
- type TextContent
- type Tool
- type ToolList
- type ToolParameter
- func BooleanParameter(name, description string, required bool) ToolParameter
- func NumberParameter(name, description string, required bool) ToolParameter
- func ObjectParameter(name, description string, required bool, properties JSONSchema, ...) ToolParameter
- func StringParameter(name, description string, required bool) ToolParameter
- type ToolsCapability
- type ToolsListChangedNotification
Constants ¶
const ( MCPVersion = "2025-06-18" JSONRPCVersion = "2.0" // 支持的协议版本列表(用于向后兼容性检查) MCPVersion2025_03_26 = "2025-03-26" MCPVersionLegacy = "2024-11-05" )
const ( ParseError = -32700 InvalidRequest = -32600 MethodNotFound = -32601 InvalidParams = -32602 InternalError = -32603 )
JSON-RPC 2.0 标准错误代码
const ( ToolNotFound = -32000 // 工具未找到 ResourceNotFound = -32002 // 资源未找到 PromptNotFound = -32001 // 提示模板未找到 InvalidTool = -32003 // 无效工具 InvalidResource = -32004 // 无效资源 InvalidPrompt = -32005 // 无效提示模板 )
MCP 特定错误代码
Variables ¶
This section is empty.
Functions ¶
func ContentToJSON ¶
func ContentToJSON(content []Content) ([]json.RawMessage, error)
func IsVersionSupported ¶
IsVersionSupported 检查协议版本是否受支持
func ValidateStructuredOutput ¶ added in v1.1.0
func ValidateStructuredOutput(data interface{}, schema JSONSchema) error
ValidateStructuredOutput 验证结构化输出是否符合模式
Types ¶
type CallToolParams ¶
type CallToolRequest ¶
type CallToolResult ¶
type CallToolResult struct {
Content []Content `json:"content"`
IsError bool `json:"isError,omitempty"`
StructuredContent interface{} `json:"structuredContent,omitempty"` // MCP 2025-06-18 新增
}
func NewToolResult ¶
func NewToolResult(content []Content, isError bool) *CallToolResult
func NewToolResultError ¶
func NewToolResultError(errorMsg string) *CallToolResult
func NewToolResultText ¶
func NewToolResultText(text string) *CallToolResult
func NewToolResultTextWithStructured ¶
func NewToolResultTextWithStructured(text string, structuredContent interface{}) *CallToolResult
NewToolResultTextWithStructured 创建带有文本和结构化内容的工具结果
func NewToolResultWithStructured ¶
func NewToolResultWithStructured(content []Content, structuredContent interface{}) *CallToolResult
NewToolResultWithStructured 创建带有结构化内容的工具结果 (MCP 2025-06-18)
func (*CallToolResult) UnmarshalJSON ¶
func (ctr *CallToolResult) UnmarshalJSON(data []byte) error
UnmarshalJSON 实现自定义JSON反序列化
type ClientCapabilities ¶
type ClientCapabilities struct {
Roots *RootsCapability `json:"roots,omitempty"`
Sampling *SamplingCapability `json:"sampling,omitempty"`
Experimental map[string]interface{} `json:"experimental,omitempty"`
}
type ClientInfo ¶
type Content ¶
type Content interface {
GetType() ContentType
}
func UnmarshalContent ¶
UnmarshalJSON 为 Content 接口实现自定义 JSON 反序列化
type ContentType ¶
type ContentType string
const ( ContentTypeText ContentType = "text" ContentTypeImage ContentType = "image" )
type GetPromptParams ¶
type GetPromptParams struct {
Name string `json:"name"`
Arguments map[string]string `json:"arguments,omitempty"`
}
GetPromptParams 获取提示模板的参数类型
type GetPromptRequest ¶
type GetPromptRequest struct {
Name string `json:"name"`
Arguments map[string]string `json:"arguments,omitempty"`
}
GetPromptRequest prompts/get 请求和响应
type GetPromptResult ¶
type GetPromptResult struct {
Description string `json:"description,omitempty"`
Messages []PromptMessage `json:"messages"`
}
func NewGetPromptResult ¶
func NewGetPromptResult(description string, messages ...PromptMessage) *GetPromptResult
type ImageContent ¶
type ImageContent struct {
Type ContentType `json:"type"`
Data string `json:"data"`
MimeType string `json:"mimeType"`
}
func NewImageContent ¶
func NewImageContent(data, mimeType string) ImageContent
func (ImageContent) GetType ¶
func (ic ImageContent) GetType() ContentType
type InitializeRequest ¶
type InitializeRequest struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities ClientCapabilities `json:"capabilities"`
ClientInfo ClientInfo `json:"clientInfo"`
}
type InitializeResult ¶
type InitializeResult struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities ServerCapabilities `json:"capabilities"`
ServerInfo ServerInfo `json:"serverInfo"`
Instructions string `json:"instructions,omitempty"`
}
type JSONRPCError ¶
type JSONRPCMessage ¶
type JSONRPCMessage struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
}
func (*JSONRPCMessage) GetIDString ¶
func (m *JSONRPCMessage) GetIDString() string
GetIDString 获取消息 ID 的字符串表示
func (*JSONRPCMessage) IsNotification ¶
func (m *JSONRPCMessage) IsNotification() bool
IsNotification 检查消息是否为通知(没有 ID)
type JSONSchema ¶
type JSONSchema map[string]interface{}
type ListPromptsParams ¶
type ListPromptsParams struct {
Cursor string `json:"cursor,omitempty"`
}
ListPromptsParams 列表提示模板的参数类型
type ListPromptsRequest ¶
type ListPromptsRequest struct {
Cursor string `json:"cursor,omitempty"`
}
ListPromptsRequest prompts/list 请求和响应
type ListPromptsResult ¶
type ListPromptsResult struct {
Prompts []Prompt `json:"prompts"`
PaginatedResult
}
type ListResourceTemplatesRequest ¶
type ListResourceTemplatesRequest struct {
Cursor string `json:"cursor,omitempty"`
}
ListResourceTemplatesRequest resources/templates/list 请求和响应
type ListResourceTemplatesResult ¶
type ListResourceTemplatesResult struct {
ResourceTemplates []ResourceTemplate `json:"resourceTemplates"`
PaginatedResult
}
type ListResourcesParams ¶
type ListResourcesParams struct {
Cursor string `json:"cursor,omitempty"`
}
ListResourcesParams 列表资源的参数类型
type ListResourcesRequest ¶
type ListResourcesRequest struct {
Cursor string `json:"cursor,omitempty"`
}
ListResourcesRequest resources/list 请求和响应
type ListResourcesResult ¶
type ListResourcesResult struct {
Resources []Resource `json:"resources"`
PaginatedResult
}
type ListToolsParams ¶
type ListToolsParams struct {
Cursor string `json:"cursor,omitempty"`
}
type ListToolsRequest ¶
type ListToolsRequest struct {
Cursor string `json:"cursor,omitempty"`
}
type ListToolsResult ¶
type ListToolsResult struct {
Tools []Tool `json:"tools"`
PaginatedResult
}
type LoggingCapability ¶
type LoggingCapability struct{}
type PaginatedResult ¶
type PaginatedResult struct {
NextCursor *string `json:"nextCursor,omitempty"`
}
type PaginationParams ¶
type PaginationParams struct {
Cursor string `json:"cursor,omitempty"`
}
type Prompt ¶
type Prompt struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Arguments []PromptArgument `json:"arguments,omitempty"`
}
Prompt 提示模板定义
func NewPrompt ¶
func NewPrompt(name, description string, arguments ...PromptArgument) Prompt
type PromptArgument ¶
type PromptArgument struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
}
PromptArgument 提示模板参数
func NewPromptArgument ¶
func NewPromptArgument(name, description string, required bool) PromptArgument
type PromptMessage ¶
PromptMessage 提示消息
func NewPromptMessage ¶
func NewPromptMessage(role Role, content Content) PromptMessage
func (*PromptMessage) UnmarshalJSON ¶
func (pm *PromptMessage) UnmarshalJSON(data []byte) error
UnmarshalJSON 实现自定义JSON反序列化
type PromptsCapability ¶
type PromptsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
type PromptsListChangedNotification ¶
type PromptsListChangedNotification struct{}
PromptsListChangedNotification 提示模板变更通知
type ReadResourceParams ¶
type ReadResourceParams struct {
URI string `json:"uri"`
}
ReadResourceParams 读取资源的参数类型
type ReadResourceRequest ¶
type ReadResourceRequest struct {
URI string `json:"uri"`
}
ReadResourceRequest resources/read 请求和响应
type ReadResourceResult ¶
type ReadResourceResult struct {
Contents []ResourceContents `json:"contents"`
}
func NewReadResourceResult ¶
func NewReadResourceResult(contents ...ResourceContents) *ReadResourceResult
type Resource ¶
type Resource struct {
URI string `json:"uri"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
Resource 资源定义
func NewResource ¶
type ResourceContents ¶
type ResourceContents struct {
URI string `json:"uri"`
MimeType string `json:"mimeType,omitempty"`
Text string `json:"text,omitempty"`
Blob string `json:"blob,omitempty"`
}
ResourceContents 资源内容
func NewBlobResourceContents ¶
func NewBlobResourceContents(uri, blob, mimeType string) ResourceContents
func NewTextResourceContents ¶
func NewTextResourceContents(uri, text string) ResourceContents
type ResourceTemplate ¶
type ResourcesCapability ¶
type ResourcesListChangedNotification ¶
type ResourcesListChangedNotification struct{}
ResourcesListChangedNotification 资源变更通知
type ResourcesUpdatedNotification ¶
type ResourcesUpdatedNotification struct {
URI string `json:"uri"`
Title string `json:"title,omitempty"` // 2025-06-18 新增:可选的资源标题
}
func NewResourcesUpdatedNotification ¶
func NewResourcesUpdatedNotification(uri string, title ...string) ResourcesUpdatedNotification
NewResourcesUpdatedNotification 创建资源更新通知
type RootsCapability ¶
type RootsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
type SamplingCapability ¶
type SamplingCapability struct{}
type ServerCapabilities ¶
type ServerCapabilities struct {
Tools *ToolsCapability `json:"tools,omitempty"`
Resources *ResourcesCapability `json:"resources,omitempty"`
Prompts *PromptsCapability `json:"prompts,omitempty"`
Logging *LoggingCapability `json:"logging,omitempty"`
Experimental map[string]interface{} `json:"experimental,omitempty"`
}
type ServerInfo ¶
type TextContent ¶
type TextContent struct {
Type ContentType `json:"type"`
Text string `json:"text"`
}
func NewTextContent ¶
func NewTextContent(text string) TextContent
func (TextContent) GetType ¶
func (tc TextContent) GetType() ContentType
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema JSONSchema `json:"inputSchema"`
OutputSchema JSONSchema `json:"outputSchema,omitempty"` // MCP 2025-06-18 新增
}
func NewTool ¶
func NewTool(name, description string, inputSchema JSONSchema) Tool
func NewToolWithOutput ¶ added in v1.1.0
func NewToolWithOutput(name, description string, inputSchema, outputSchema JSONSchema) Tool
NewToolWithOutput 创建带有输出模式的工具 (MCP 2025-06-18)
type ToolParameter ¶
type ToolParameter struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Schema JSONSchema `json:"schema,omitempty"`
}
func BooleanParameter ¶
func BooleanParameter(name, description string, required bool) ToolParameter
func NumberParameter ¶
func NumberParameter(name, description string, required bool) ToolParameter
func ObjectParameter ¶
func ObjectParameter(name, description string, required bool, properties JSONSchema, required_props []string) ToolParameter
func StringParameter ¶
func StringParameter(name, description string, required bool) ToolParameter
type ToolsCapability ¶
type ToolsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
type ToolsListChangedNotification ¶
type ToolsListChangedNotification struct{}