protocol

package
v1.2.3 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	MethodInitialize = "initialize"
	MethodPing       = "ping"

	MethodToolsList = "tools/list"
	MethodToolsCall = "tools/call"

	MethodResourcesList          = "resources/list"
	MethodResourcesRead          = "resources/read"
	MethodResourcesTemplatesList = "resources/templates/list"
	MethodResourcesSubscribe     = "resources/subscribe"
	MethodResourcesUnsubscribe   = "resources/unsubscribe"

	MethodPromptsList = "prompts/list"
	MethodPromptsGet  = "prompts/get"

	MethodCompletionComplete = "completion/complete"

	MethodRootsList = "roots/list"

	MethodSamplingCreateMessage = "sampling/createMessage"

	MethodElicitationCreate = "elicitation/create"

	MethodLoggingSetLevel = "logging/setLevel"
)
View Source
const (
	NotificationInitialized = "notifications/initialized"

	NotificationToolsListChanged = "notifications/tools/list_changed"

	NotificationResourcesListChanged          = "notifications/resources/list_changed"
	NotificationResourcesUpdated              = "notifications/resources/updated"
	NotificationResourcesTemplatesListChanged = "notifications/resources/templates/list_changed"

	NotificationPromptsListChanged = "notifications/prompts/list_changed"

	NotificationRootsListChanged = "notifications/roots/list_changed"

	NotificationProgress  = "notifications/progress"
	NotificationCancelled = "notifications/cancelled"

	NotificationLoggingMessage = "notifications/message"
)
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 // 无效提示模板

	ErrorCodeInvalidParams = InvalidParams
)

MCP 特定错误代码

Variables

This section is empty.

Functions

func ContentToJSON

func ContentToJSON(content []Content) ([]json.RawMessage, error)

func GetSupportedVersions

func GetSupportedVersions() []string

func IDToString

func IDToString(id json.RawMessage) string

func IsVersionSupported

func IsVersionSupported(version string) bool

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

func ShouldLog added in v1.2.2

func ShouldLog(messageLevel, minLevel LoggingLevel) bool

ShouldLog 判断是否应该发送指定级别的日志 messageLevel: 要发送的消息级别 minLevel: 客户端设置的最低级别 返回 true 表示应该发送(messageLevel >= minLevel)

func StringToID

func StringToID(id string) json.RawMessage

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

func ValidateElicitationAction added in v1.1.1

func ValidateElicitationAction(action string) bool

ValidateElicitationAction validates whether the elicitation action is valid

func ValidateStructuredOutput added in v1.1.0

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

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

Types

type Annotation added in v1.1.2

type Annotation struct {
	Audience     []Role  `json:"audience,omitempty"`     // 目标受众 (user, assistant)
	Priority     float64 `json:"priority,omitempty"`     // 优先级 (0.0-1.0)
	LastModified string  `json:"lastModified,omitempty"` // 最后修改时间 (ISO 8601)
}

Annotation 内容注解 (MCP 2025-06-18)

func NewAnnotation added in v1.1.2

func NewAnnotation() *Annotation

NewAnnotation 创建注解 (MCP 2025-06-18)

func (*Annotation) WithAudience added in v1.1.2

func (a *Annotation) WithAudience(audience ...Role) *Annotation

func (*Annotation) WithLastModified added in v1.1.2

func (a *Annotation) WithLastModified(lastModified string) *Annotation

func (*Annotation) WithPriority added in v1.1.2

func (a *Annotation) WithPriority(priority float64) *Annotation

type AudioContent added in v1.1.2

type AudioContent struct {
	Type        ContentType `json:"type"`
	Data        string      `json:"data"`     // Base64 编码的音频数据
	MimeType    string      `json:"mimeType"` // 例如: audio/wav, audio/mp3
	Annotations *Annotation `json:"annotations,omitempty"`
}

AudioContent 音频内容 (MCP 2025-06-18)

func NewAudioContent added in v1.1.2

func NewAudioContent(data, mimeType string) AudioContent

NewAudioContent 创建音频内容 (MCP 2025-06-18)

func (AudioContent) GetType added in v1.1.2

func (ac AudioContent) GetType() ContentType

func (*AudioContent) WithAnnotations added in v1.1.2

func (ac *AudioContent) WithAnnotations(annotations *Annotation) *AudioContent

type CallToolParams

type CallToolParams struct {
	Meta      map[string]any `json:"_meta,omitempty"`
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments"`
}

type CallToolRequest

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

type CallToolResult

type CallToolResult struct {
	Content           []Content      `json:"content"`
	IsError           bool           `json:"isError,omitempty"`
	StructuredContent any            `json:"structuredContent,omitempty"` // MCP 2025-06-18
	Meta              map[string]any `json:"_meta,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 CancelledNotificationParams added in v1.1.6

type CancelledNotificationParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
	// 要取消的请求 ID
	RequestID any `json:"requestId"`
	// 可选的取消原因描述
	Reason string `json:"reason,omitempty"`
}

CancelledNotificationParams 取消请求通知参数

type ClientCapabilities

type ClientCapabilities struct {
	Roots        *RootsCapability       `json:"roots,omitempty"`
	Sampling     *SamplingCapability    `json:"sampling,omitempty"`
	Elicitation  *ElicitationCapability `json:"elicitation,omitempty"`
	Experimental map[string]interface{} `json:"experimental,omitempty"`
}

type ClientInfo

type ClientInfo struct {
	Name       string `json:"name"`
	Title      string `json:"title,omitempty"`
	Version    string `json:"version"`
	WebsiteURL string `json:"websiteUrl,omitempty"`
	Icons      []Icon `json:"icons,omitempty"`
}

type CompleteRequest added in v1.1.2

type CompleteRequest struct {
	Ref      map[string]any     `json:"ref"`               // 引用 (PromptReference 或 ResourceReference)
	Argument CompletionArgument `json:"argument"`          // 要补全的参数
	Context  *CompletionContext `json:"context,omitempty"` // 可选的上下文
}

CompleteRequest 补全请求 (completion/complete)

type CompleteResult added in v1.1.2

type CompleteResult struct {
	Completion CompletionResult `json:"completion"` // 补全结果
}

CompleteResult 补全响应

type CompletionArgument added in v1.1.2

type CompletionArgument struct {
	Name  string `json:"name"`  // 参数名称
	Value string `json:"value"` // 当前值
}

CompletionArgument 补全参数

type CompletionCapability added in v1.1.2

type CompletionCapability struct{}

CompletionCapability 补全能力声明

type CompletionContext added in v1.1.2

type CompletionContext struct {
	Arguments map[string]string `json:"arguments,omitempty"` // 已解析的参数映射
}

CompletionContext 补全上下文

type CompletionReference added in v1.1.2

type CompletionReference interface {
	GetType() ReferenceType
}

CompletionReference 补全引用 (PromptReference 或 ResourceReference)

func UnmarshalCompletionReference added in v1.1.2

func UnmarshalCompletionReference(data map[string]any) (CompletionReference, error)

UnmarshalCompletionReference 反序列化补全引用

type CompletionResult added in v1.1.2

type CompletionResult struct {
	Values  []string `json:"values"`          // 补全建议列表 (最多 100 个)
	Total   *int     `json:"total,omitempty"` // 可选: 总匹配数
	HasMore bool     `json:"hasMore"`         // 是否有更多结果
}

CompletionResult 补全结果

func NewCompletionResult added in v1.1.2

func NewCompletionResult(values []string, hasMore bool) CompletionResult

NewCompletionResult 创建补全结果

func NewCompletionResultWithTotal added in v1.1.2

func NewCompletionResultWithTotal(values []string, total int, hasMore bool) CompletionResult

NewCompletionResultWithTotal 创建带总数的补全结果

type Content

type Content interface {
	GetType() ContentType
}

func UnmarshalContent

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

type ContentType

type ContentType string
const (
	ContentTypeText         ContentType = "text"
	ContentTypeImage        ContentType = "image"
	ContentTypeAudio        ContentType = "audio"         // MCP 2025-06-18
	ContentTypeResourceLink ContentType = "resource_link" // MCP 2025-06-18
	ContentTypeResource     ContentType = "resource"      // MCP 2025-06-18: Embedded Resource
)

type CreateMessageParams added in v1.2.0

type CreateMessageParams = CreateMessageRequest

CreateMessageParams 是 CreateMessageRequest 的别名,用于保持一致性

type CreateMessageRequest added in v1.1.1

type CreateMessageRequest struct {
	Meta             map[string]any         `json:"_meta,omitempty"`
	Messages         []SamplingMessage      `json:"messages"`
	ModelPreferences *ModelPreferences      `json:"modelPreferences,omitempty"`
	SystemPrompt     string                 `json:"systemPrompt,omitempty"`
	IncludeContext   IncludeContext         `json:"includeContext,omitempty"`
	Temperature      *float64               `json:"temperature,omitempty"` // 0.0-1.0
	MaxTokens        int                    `json:"maxTokens"`             // 必需
	StopSequences    []string               `json:"stopSequences,omitempty"`
	Metadata         map[string]interface{} `json:"metadata,omitempty"`
}

CreateMessageRequest 创建消息请求 (服务器发起的LLM采样)

func NewCreateMessageRequest added in v1.1.1

func NewCreateMessageRequest(messages []SamplingMessage, maxTokens int) *CreateMessageRequest

NewCreateMessageRequest 创建消息请求

func (*CreateMessageRequest) Validate added in v1.1.1

func (cmr *CreateMessageRequest) Validate() error

Validate 验证创建消息请求

func (*CreateMessageRequest) WithIncludeContext added in v1.1.1

func (cmr *CreateMessageRequest) WithIncludeContext(context IncludeContext) *CreateMessageRequest

WithIncludeContext 设置上下文包含选项

func (*CreateMessageRequest) WithMetadata added in v1.1.1

func (cmr *CreateMessageRequest) WithMetadata(metadata map[string]interface{}) *CreateMessageRequest

WithMetadata 设置元数据

func (*CreateMessageRequest) WithModelPreferences added in v1.1.1

func (cmr *CreateMessageRequest) WithModelPreferences(prefs *ModelPreferences) *CreateMessageRequest

WithModelPreferences 设置模型偏好

func (*CreateMessageRequest) WithStopSequences added in v1.1.1

func (cmr *CreateMessageRequest) WithStopSequences(sequences ...string) *CreateMessageRequest

WithStopSequences 设置停止序列

func (*CreateMessageRequest) WithSystemPrompt added in v1.1.1

func (cmr *CreateMessageRequest) WithSystemPrompt(prompt string) *CreateMessageRequest

WithSystemPrompt 设置系统提示

func (*CreateMessageRequest) WithTemperature added in v1.1.1

func (cmr *CreateMessageRequest) WithTemperature(temp float64) *CreateMessageRequest

WithTemperature 设置温度 (0.0-1.0)

type CreateMessageResult added in v1.1.1

type CreateMessageResult struct {
	Role       Role       `json:"role"`
	Content    Content    `json:"content"`
	Model      string     `json:"model"`
	StopReason StopReason `json:"stopReason"`
}

func NewCreateMessageResult added in v1.1.1

func NewCreateMessageResult(role Role, content Content, model string, stopReason StopReason) *CreateMessageResult

NewCreateMessageResult 创建消息结果

type ElicitationAction added in v1.1.1

type ElicitationAction string
const (
	ElicitationActionAccept  ElicitationAction = "accept"
	ElicitationActionDecline ElicitationAction = "decline"
	ElicitationActionCancel  ElicitationAction = "cancel"
)

type ElicitationCapability added in v1.1.1

type ElicitationCapability struct{}

Elicitation 能力声明

type ElicitationCreateParams added in v1.1.1

type ElicitationCreateParams struct {
	Message         string     `json:"message"`
	RequestedSchema JSONSchema `json:"requestedSchema"`
}

func NewElicitationCreateParams added in v1.1.1

func NewElicitationCreateParams(message string, schema JSONSchema) *ElicitationCreateParams

type ElicitationResult added in v1.1.1

type ElicitationResult struct {
	Action  ElicitationAction `json:"action"`
	Content interface{}       `json:"content,omitempty"`
}

func NewElicitationAccept added in v1.1.1

func NewElicitationAccept(content interface{}) *ElicitationResult

func NewElicitationCancel added in v1.1.1

func NewElicitationCancel() *ElicitationResult

func NewElicitationDecline added in v1.1.1

func NewElicitationDecline() *ElicitationResult

func NewElicitationResult added in v1.1.1

func NewElicitationResult(action ElicitationAction, content interface{}) *ElicitationResult

func (*ElicitationResult) IsAccepted added in v1.1.1

func (r *ElicitationResult) IsAccepted() bool

func (*ElicitationResult) IsCancelled added in v1.1.1

func (r *ElicitationResult) IsCancelled() bool

func (*ElicitationResult) IsDeclined added in v1.1.1

func (r *ElicitationResult) IsDeclined() bool

func (*ElicitationResult) MarshalJSON added in v1.1.1

func (r *ElicitationResult) MarshalJSON() ([]byte, error)

func (*ElicitationResult) UnmarshalJSON added in v1.1.1

func (r *ElicitationResult) UnmarshalJSON(data []byte) error

func (*ElicitationResult) Validate added in v1.1.1

func (r *ElicitationResult) Validate() error

type EmbeddedResourceContent added in v1.1.2

type EmbeddedResourceContent struct {
	Type     ContentType      `json:"type"`
	Resource ResourceContents `json:"resource"`
}

EmbeddedResourceContent 嵌入式资源 (MCP 2025-06-18)

func NewEmbeddedResourceContent added in v1.1.2

func NewEmbeddedResourceContent(resource ResourceContents) EmbeddedResourceContent

NewEmbeddedResourceContent 创建嵌入式资源内容 (MCP 2025-06-18)

func (EmbeddedResourceContent) GetType added in v1.1.2

func (erc EmbeddedResourceContent) GetType() ContentType

type EmptyResult added in v1.2.0

type EmptyResult struct{}

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"`
	Meta        map[string]any  `json:"_meta,omitempty"`
}

func NewGetPromptResult

func NewGetPromptResult(description string, messages ...PromptMessage) *GetPromptResult

type Icon added in v1.2.2

type Icon struct {
	// Source 指向图标资源的 URI (必需),可以是:
	// - HTTP/HTTPS URL 指向图像文件
	// - data URI 包含 base64 编码的图像数据
	Source string `json:"src"`
	// MIMEType 可选的 MIME 类型
	MIMEType string `json:"mimeType,omitempty"`
	// Sizes 可选的尺寸规范 (如 ["48x48"], ["any"] 用于 SVG 等可缩放格式)
	Sizes []string `json:"sizes,omitempty"`
	// Theme 可选的主题,如 "light" 或 "dark"
	Theme string `json:"theme,omitempty"`
}

Icon 图标定义,用于资源、工具、提示和实现的视觉标识

type ImageContent

type ImageContent struct {
	Type        ContentType `json:"type"`
	Data        string      `json:"data"`
	MimeType    string      `json:"mimeType"`
	Annotations *Annotation `json:"annotations,omitempty"` // MCP 2025-06-18
}

func NewImageContent

func NewImageContent(data, mimeType string) ImageContent

func (ImageContent) GetType

func (ic ImageContent) GetType() ContentType

func (*ImageContent) WithAnnotations added in v1.1.2

func (ic *ImageContent) WithAnnotations(annotations *Annotation) *ImageContent

type IncludeContext added in v1.1.1

type IncludeContext string

IncludeContext 上下文包含选项

const (
	IncludeContextNone       IncludeContext = "none"
	IncludeContextThisServer IncludeContext = "thisServer"
	IncludeContextAllServers IncludeContext = "allServers"
)

type InitializeParams added in v1.2.0

type InitializeParams struct {
	Meta            map[string]any     `json:"_meta,omitempty"`
	ProtocolVersion string             `json:"protocolVersion"`
	Capabilities    ClientCapabilities `json:"capabilities"`
	ClientInfo      ClientInfo         `json:"clientInfo"`
}

InitializeParams initialize 请求参数

type InitializeResult

type InitializeResult struct {
	ProtocolVersion string             `json:"protocolVersion"`
	Capabilities    ServerCapabilities `json:"capabilities"`
	ServerInfo      ServerInfo         `json:"serverInfo"`
	Instructions    string             `json:"instructions,omitempty"`
}

InitializeResult initialize 响应

type InitializedParams added in v1.2.0

type InitializedParams struct {
	Meta map[string]any `json:"_meta,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

func (*JSONRPCMessage) IsNotification

func (m *JSONRPCMessage) IsNotification() bool

type JSONSchema

type JSONSchema map[string]interface{}

func CreateBooleanElicitationSchema added in v1.1.1

func CreateBooleanElicitationSchema(propertyName, description string, defaultValue *bool, required bool) JSONSchema

CreateBooleanElicitationSchema creates a schema for requesting boolean input

func CreateElicitationSchema added in v1.1.1

func CreateElicitationSchema() JSONSchema

CreateElicitationSchema creates a common elicitation schema

func CreateEnumElicitationSchema added in v1.1.1

func CreateEnumElicitationSchema(propertyName, description string, options []string, optionNames []string, required bool) JSONSchema

CreateEnumElicitationSchema creates a schema for requesting enum selection

func CreateNumberElicitationSchema added in v1.1.1

func CreateNumberElicitationSchema(propertyName, description string, min, max *float64, required bool) JSONSchema

CreateNumberElicitationSchema creates a schema for requesting number input

func CreateStringElicitationSchema added in v1.1.1

func CreateStringElicitationSchema(propertyName, description string, required bool) JSONSchema

CreateStringElicitationSchema creates a schema for requesting string input

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 ListResourceTemplatesParams added in v1.2.2

type ListResourceTemplatesParams = ListResourceTemplatesRequest

ListResourceTemplatesParams is an alias for ListResourceTemplatesRequest

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 ListRootsParams added in v1.1.2

type ListRootsParams struct {
}

ListRootsParams 列出根目录的参数类型

type ListRootsRequest added in v1.1.2

type ListRootsRequest struct {
}

ListRootsRequest roots/list 请求

type ListRootsResult added in v1.1.2

type ListRootsResult struct {
	Roots []Root `json:"roots"`
}

ListRootsResult roots/list 响应

func NewListRootsResult added in v1.1.2

func NewListRootsResult(roots ...Root) *ListRootsResult

NewListRootsResult 创建根目录列表结果

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 LoggingLevel added in v1.1.6

type LoggingLevel string

LoggingLevel 日志级别 映射到 syslog 消息严重性,如 RFC-5424 中所述: https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1

const (
	LogLevelDebug     LoggingLevel = "debug"     // 调试级别消息
	LogLevelInfo      LoggingLevel = "info"      // 信息级别消息
	LogLevelNotice    LoggingLevel = "notice"    // 正常但重要的消息
	LogLevelWarning   LoggingLevel = "warning"   // 警告消息
	LogLevelError     LoggingLevel = "error"     // 错误消息
	LogLevelCritical  LoggingLevel = "critical"  // 严重错误消息
	LogLevelAlert     LoggingLevel = "alert"     // 需要立即采取行动
	LogLevelEmergency LoggingLevel = "emergency" // 系统不可用
)

type LoggingMessageParams added in v1.1.6

type LoggingMessageParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
	// 要记录的数据,例如字符串消息或对象
	// 允许任何 JSON 可序列化类型
	Data any `json:"data"`
	// 此日志消息的严重性级别
	Level LoggingLevel `json:"level"`
	// 发出此消息的日志记录器的可选名称
	Logger string `json:"logger,omitempty"`
}

LoggingMessageParams notifications/message 通知参数

type MCPError added in v1.1.1

type MCPError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

MCPError MCP特定错误类型

func NewMCPError added in v1.1.1

func NewMCPError(code int, message string, data interface{}) *MCPError

NewMCPError 创建新的MCP错误

func (*MCPError) Error added in v1.1.1

func (e *MCPError) Error() string

type ModelHint added in v1.1.1

type ModelHint struct {
	Name string `json:"name,omitempty"`
}

ModelHint 模型提示

func NewModelHint added in v1.1.1

func NewModelHint(name string) ModelHint

type ModelPreferences added in v1.1.1

type ModelPreferences struct {
	Hints                []ModelHint `json:"hints,omitempty"`
	CostPriority         *float64    `json:"costPriority,omitempty"`         // 0-1, 成本优先级
	SpeedPriority        *float64    `json:"speedPriority,omitempty"`        // 0-1, 速度优先级
	IntelligencePriority *float64    `json:"intelligencePriority,omitempty"` // 0-1, 智能优先级
}

ModelPreferences 模型偏好设置

func NewModelPreferences added in v1.1.1

func NewModelPreferences() *ModelPreferences

func (*ModelPreferences) Validate added in v1.1.1

func (mp *ModelPreferences) Validate() error

Validate 验证模型偏好设置

func (*ModelPreferences) WithCostPriority added in v1.1.1

func (mp *ModelPreferences) WithCostPriority(priority float64) *ModelPreferences

WithCostPriority 设置成本优先级 (0-1)

func (*ModelPreferences) WithHints added in v1.1.1

func (mp *ModelPreferences) WithHints(hints ...ModelHint) *ModelPreferences

WithHints 设置模型提示

func (*ModelPreferences) WithIntelligencePriority added in v1.1.1

func (mp *ModelPreferences) WithIntelligencePriority(priority float64) *ModelPreferences

WithIntelligencePriority 设置智能优先级 (0-1)

func (*ModelPreferences) WithSpeedPriority added in v1.1.1

func (mp *ModelPreferences) WithSpeedPriority(priority float64) *ModelPreferences

WithSpeedPriority 设置速度优先级 (0-1)

type PaginatedResult

type PaginatedResult struct {
	NextCursor *string `json:"nextCursor,omitempty"`
}

type PaginationParams

type PaginationParams struct {
	Cursor string `json:"cursor,omitempty"`
}

type PingParams added in v1.1.6

type PingParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
}

PingParams ping 请求参数 (空参数)

type ProgressNotificationParams added in v1.1.6

type ProgressNotificationParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
	// 进度令牌,用于关联此通知与正在进行的请求
	ProgressToken any `json:"progressToken"`
	// 当前进度值,每次进度更新时应该增加
	Progress float64 `json:"progress"`
	// 总进度值(如果已知),0 表示未知
	Total float64 `json:"total,omitempty"`
	// 可选的进度描述消息
	Message string `json:"message,omitempty"`
}

ProgressNotificationParams 进度通知参数

type Prompt

type Prompt struct {
	Name        string           `json:"name"`
	Title       string           `json:"title,omitempty"` // MCP 2025-06-18: 人类友好的标题
	Description string           `json:"description,omitempty"`
	Arguments   []PromptArgument `json:"arguments,omitempty"`
	Meta        map[string]any   `json:"_meta,omitempty"` // MCP 2025-06-18: 扩展元数据
}

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 PromptListChangedParams added in v1.2.0

type PromptListChangedParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
}

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 PromptReference added in v1.1.2

type PromptReference struct {
	Type ReferenceType `json:"type"` // 必须是 "ref/prompt"
	Name string        `json:"name"` // 提示名称
}

PromptReference 提示引用

func NewPromptReference added in v1.1.2

func NewPromptReference(name string) PromptReference

NewPromptReference 创建提示引用

func (PromptReference) GetType added in v1.1.2

func (p PromptReference) GetType() ReferenceType

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 ReferenceType added in v1.1.2

type ReferenceType string

ReferenceType 引用类型

const (
	ReferenceTypePrompt   ReferenceType = "ref/prompt"   // 提示引用
	ReferenceTypeResource ReferenceType = "ref/resource" // 资源引用
)

type Resource

type Resource struct {
	URI         string         `json:"uri"`
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	MimeType    string         `json:"mimeType,omitempty"`
	Meta        map[string]any `json:"_meta,omitempty"`
}

Resource 资源定义

func NewResource

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

type ResourceContents

type ResourceContents struct {
	URI         string      `json:"uri"`
	Title       string      `json:"title,omitempty"`
	MimeType    string      `json:"mimeType,omitempty"`
	Text        string      `json:"text,omitempty"`
	Blob        string      `json:"blob,omitempty"`
	Annotations *Annotation `json:"annotations,omitempty"`
}

ResourceContents 资源内容

func NewBlobResourceContents

func NewBlobResourceContents(uri, blob, mimeType string) ResourceContents

func NewTextResourceContents

func NewTextResourceContents(uri, text string) ResourceContents

type ResourceLinkContent added in v1.1.2

type ResourceLinkContent struct {
	Type        ContentType `json:"type"`
	URI         string      `json:"uri"`
	Name        string      `json:"name,omitempty"`
	Description string      `json:"description,omitempty"`
	MimeType    string      `json:"mimeType,omitempty"`
	Annotations *Annotation `json:"annotations,omitempty"`
}

ResourceLinkContent 资源链接 (MCP 2025-06-18)

func NewResourceLinkContent added in v1.1.2

func NewResourceLinkContent(uri string) ResourceLinkContent

NewResourceLinkContent 创建资源链接内容 (MCP 2025-06-18)

func NewResourceLinkContentWithDetails added in v1.1.2

func NewResourceLinkContentWithDetails(uri, name, description, mimeType string) ResourceLinkContent

NewResourceLinkContentWithDetails 创建带详细信息的资源链接 (MCP 2025-06-18)

func (ResourceLinkContent) GetType added in v1.1.2

func (rlc ResourceLinkContent) GetType() ContentType

func (*ResourceLinkContent) WithAnnotations added in v1.1.2

func (rlc *ResourceLinkContent) WithAnnotations(annotations *Annotation) *ResourceLinkContent

type ResourceListChangedParams added in v1.2.0

type ResourceListChangedParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
}

type ResourceReference added in v1.1.2

type ResourceReference struct {
	Type ReferenceType `json:"type"` // 必须是 "ref/resource"
	URI  string        `json:"uri"`  // 资源 URI (可能包含模板变量)
}

ResourceReference 资源引用

func NewResourceReference added in v1.1.2

func NewResourceReference(uri string) ResourceReference

NewResourceReference 创建资源引用

func (ResourceReference) GetType added in v1.1.2

func (r ResourceReference) GetType() ReferenceType

type ResourceTemplate

type ResourceTemplate struct {
	URITemplate string         `json:"uriTemplate"`
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	MimeType    string         `json:"mimeType,omitempty"`
	Meta        map[string]any `json:"_meta,omitempty"`
}

func NewResourceTemplate added in v1.1.2

func NewResourceTemplate(uriTemplate, name, description, mimeType string) ResourceTemplate

type ResourceTemplateListChangedParams added in v1.2.0

type ResourceTemplateListChangedParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
}

ResourceTemplateListChangedParams 资源模板列表变更通知参数

type ResourceTemplatesListChangedNotification added in v1.1.2

type ResourceTemplatesListChangedNotification struct{}

type ResourceUpdatedNotificationParams added in v1.1.6

type ResourceUpdatedNotificationParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
	// 更新的资源 URI
	URI string `json:"uri"`
}

ResourceUpdatedNotificationParams 资源更新通知参数

type ResourcesCapability

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

type ResourcesListChangedNotification

type ResourcesListChangedNotification struct{}

ResourcesListChangedNotification 资源变更通知

type Role

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

type Root added in v1.1.2

type Root struct {
	URI  string `json:"uri"`            // 根目录URI,必须是 file:// 协议
	Name string `json:"name,omitempty"` // 可选的人类可读名称
}

Root 根目录定义

func NewRoot added in v1.1.2

func NewRoot(uri, name string) Root

NewRoot 创建新的根目录定义

type RootsCapability

type RootsCapability struct {
	ListChanged bool `json:"listChanged,omitempty"`
}

type RootsListChangedNotification added in v1.1.2

type RootsListChangedNotification struct{}

RootsListChangedNotification 根目录列表变更通知

func NewRootsListChangedNotification added in v1.1.2

func NewRootsListChangedNotification() RootsListChangedNotification

NewRootsListChangedNotification 创建根目录列表变更通知

type RootsListChangedParams added in v1.2.0

type RootsListChangedParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
}

RootsListChangedParams 根目录列表变更通知参数

type SamplingCapability

type SamplingCapability struct{}

type SamplingMessage added in v1.1.1

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

SamplingMessage 采样消息

func NewSamplingMessage added in v1.1.1

func NewSamplingMessage(role Role, content Content) SamplingMessage

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"`
	Completion   *CompletionCapability  `json:"completions,omitempty"` // MCP 2025-06-18: 参数自动补全
	Experimental map[string]interface{} `json:"experimental,omitempty"`
}

type ServerInfo

type ServerInfo struct {
	Name       string `json:"name"`
	Title      string `json:"title,omitempty"`
	Version    string `json:"version"`
	WebsiteURL string `json:"websiteUrl,omitempty"`
	Icons      []Icon `json:"icons,omitempty"`
}

type SetLoggingLevelParams added in v1.1.6

type SetLoggingLevelParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
	// 客户端希望从服务器接收的日志级别
	// 服务器应该发送此级别及更高级别(即更严重)的所有日志到客户端
	Level LoggingLevel `json:"level"`
}

SetLoggingLevelParams logging/setLevel 请求参数

type StopReason added in v1.1.1

type StopReason string
const (
	StopReasonEndTurn      StopReason = "endTurn"
	StopReasonMaxTokens    StopReason = "maxTokens"
	StopReasonStopSequence StopReason = "stopSequence"
	StopReasonToolUse      StopReason = "toolUse"
)

type SubscribeParams added in v1.1.6

type SubscribeParams struct {
	URI string `json:"uri"`
}

SubscribeParams resources/subscribe 请求参数

type TextContent

type TextContent struct {
	Type        ContentType `json:"type"`
	Text        string      `json:"text"`
	Annotations *Annotation `json:"annotations,omitempty"` // MCP 2025-06-18
}

func NewTextContent

func NewTextContent(text string) TextContent

func (TextContent) GetType

func (tc TextContent) GetType() ContentType

func (*TextContent) WithAnnotations added in v1.1.2

func (tc *TextContent) WithAnnotations(annotations *Annotation) *TextContent

WithAnnotations 为内容添加注解 (MCP 2025-06-18)

type Tool

type Tool struct {
	Name         string         `json:"name"`
	Title        string         `json:"title,omitempty"` // MCP 2025-06-18: 人类友好的标题
	Description  string         `json:"description,omitempty"`
	InputSchema  JSONSchema     `json:"inputSchema"`
	OutputSchema JSONSchema     `json:"outputSchema,omitempty"` // MCP 2025-06-18
	Meta         map[string]any `json:"_meta,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 ToolListChangedParams added in v1.2.0

type ToolListChangedParams struct {
	Meta map[string]any `json:"_meta,omitempty"`
}

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

type UnsubscribeParams added in v1.1.6

type UnsubscribeParams struct {
	URI string `json:"uri"`
}

UnsubscribeParams resources/unsubscribe 请求参数

Jump to

Keyboard shortcuts

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