protocol

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MCPVersion     = "2025-06-18"
	JSONRPCVersion = "2.0"

	// 支持的协议版本列表(用于向后兼容性检查)
	MCPVersion2025_03_26 = "2025-03-26"
	MCPVersionLegacy     = "2024-11-05"
)
View Source
const (
	ParseError     = -32700
	InvalidRequest = -32600
	MethodNotFound = -32601
	InvalidParams  = -32602
	InternalError  = -32603
)

JSON-RPC 2.0 标准错误代码

View Source
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 GetSupportedVersions

func GetSupportedVersions() []string

GetSupportedVersions 返回所有支持的协议版本

func IDToString

func IDToString(id json.RawMessage) string

IDToString 将 JSON-RPC ID 转换为字符串

func IsVersionSupported

func IsVersionSupported(version string) bool

IsVersionSupported 检查协议版本是否受支持

func StringToID

func StringToID(id string) json.RawMessage

StringToID 将字符串转换为 JSON-RPC ID

func ValidateStructuredOutput added in v1.1.0

func ValidateStructuredOutput(data interface{}, schema JSONSchema) error

ValidateStructuredOutput 验证结构化输出是否符合模式

Types

type CallToolParams

type CallToolParams struct {
	Name      string                 `json:"name"`
	Arguments map[string]interface{} `json:"arguments"`
}

type CallToolRequest

type CallToolRequest struct {
	Name      string                 `json:"name"`
	Arguments map[string]interface{} `json:"arguments,omitempty"`
}

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 ClientInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

type Content

type Content interface {
	GetType() ContentType
}

func UnmarshalContent

func UnmarshalContent(data []byte) (Content, error)

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 JSONRPCError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

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

type PromptMessage struct {
	Role    Role    `json:"role"`
	Content Content `json:"content"`
}

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

func NewResource(uri, name, description, mimeType string) Resource

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 ResourceTemplate struct {
	URITemplate string `json:"uriTemplate"`
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	MimeType    string `json:"mimeType,omitempty"`
}

type ResourcesCapability

type ResourcesCapability struct {
	Subscribe   bool `json:"subscribe,omitempty"`
	ListChanged bool `json:"listChanged,omitempty"`
}

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 Role

type Role string
const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleSystem    Role = "system"
)

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 ServerInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

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 ToolList

type ToolList struct {
	Tools []Tool `json:"tools"`
}

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{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL