Documentation
¶
Index ¶
- Constants
- type DefaultMCPServer
- func (s *DefaultMCPServer) CallTool(ctx context.Context, name string, args map[string]any) (any, error)
- func (s *DefaultMCPServer) Close() error
- func (s *DefaultMCPServer) DeleteResource(uri string) error
- func (s *DefaultMCPServer) GetPrompt(ctx context.Context, name string, vars map[string]string) (string, error)
- func (s *DefaultMCPServer) GetResource(ctx context.Context, uri string) (*Resource, error)
- func (s *DefaultMCPServer) GetServerInfo() ServerInfo
- func (s *DefaultMCPServer) HandleMessage(ctx context.Context, msg *MCPMessage) (*MCPMessage, error)
- func (s *DefaultMCPServer) ListPrompts(ctx context.Context) ([]PromptTemplate, error)
- func (s *DefaultMCPServer) ListResources(ctx context.Context) ([]Resource, error)
- func (s *DefaultMCPServer) ListTools(ctx context.Context) ([]ToolDefinition, error)
- func (s *DefaultMCPServer) RegisterPrompt(prompt *PromptTemplate) error
- func (s *DefaultMCPServer) RegisterResource(resource *Resource) error
- func (s *DefaultMCPServer) RegisterTool(tool *ToolDefinition, handler ToolHandler) error
- func (s *DefaultMCPServer) Serve(ctx context.Context, transport Transport) error
- func (s *DefaultMCPServer) SetLogLevel(level string) error
- func (s *DefaultMCPServer) SubscribeResource(ctx context.Context, uri string) (<-chan Resource, error)
- func (s *DefaultMCPServer) UnregisterPrompt(name string) error
- func (s *DefaultMCPServer) UnregisterTool(name string) error
- func (s *DefaultMCPServer) UpdateResource(resource *Resource) error
- type MCPClient
- type MCPError
- type MCPMessage
- type MCPServer
- type PromptExample
- type PromptTemplate
- type Resource
- type ResourceType
- type ServerCapabilities
- type ServerInfo
- type ToolDefinition
- type ToolHandler
- type Transport
Constants ¶
const ( ErrorCodeParseError = -32700 ErrorCodeInvalidRequest = -32600 ErrorCodeMethodNotFound = -32601 ErrorCodeInvalidParams = -32602 ErrorCodeInternalError = -32603 )
标准错误码
const MCPVersion = "2024-11-05"
MCPVersion MCP 协议版本
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultMCPServer ¶
type DefaultMCPServer struct {
// contains filtered or unexported fields
}
DefaultMCPServer 默认 MCP 服务器实现
func NewMCPServer ¶
func NewMCPServer(name, version string, logger *zap.Logger) *DefaultMCPServer
NewMCPServer 创建 MCP 服务器
func (*DefaultMCPServer) CallTool ¶
func (s *DefaultMCPServer) CallTool(ctx context.Context, name string, args map[string]any) (any, error)
CallTool 调用工具(带 30 秒超时控制)
func (*DefaultMCPServer) DeleteResource ¶
func (s *DefaultMCPServer) DeleteResource(uri string) error
DeleteResource 删除资源
func (*DefaultMCPServer) GetPrompt ¶
func (s *DefaultMCPServer) GetPrompt(ctx context.Context, name string, vars map[string]string) (string, error)
GetPrompt 获取渲染后的提示词
func (*DefaultMCPServer) GetResource ¶
GetResource 获取资源
func (*DefaultMCPServer) GetServerInfo ¶
func (s *DefaultMCPServer) GetServerInfo() ServerInfo
GetServerInfo 获取服务器信息
func (*DefaultMCPServer) HandleMessage ¶ added in v1.0.0
func (s *DefaultMCPServer) HandleMessage(ctx context.Context, msg *MCPMessage) (*MCPMessage, error)
HandleMessage dispatches an incoming JSON-RPC 2.0 request to the appropriate server method and returns a JSON-RPC 2.0 response. Notifications (messages without an ID) return nil response and nil error.
func (*DefaultMCPServer) ListPrompts ¶
func (s *DefaultMCPServer) ListPrompts(ctx context.Context) ([]PromptTemplate, error)
ListPrompts 列出所有提示词模板
func (*DefaultMCPServer) ListResources ¶
func (s *DefaultMCPServer) ListResources(ctx context.Context) ([]Resource, error)
ListResources 列出所有资源
func (*DefaultMCPServer) ListTools ¶
func (s *DefaultMCPServer) ListTools(ctx context.Context) ([]ToolDefinition, error)
ListTools 列出所有工具
func (*DefaultMCPServer) RegisterPrompt ¶
func (s *DefaultMCPServer) RegisterPrompt(prompt *PromptTemplate) error
RegisterPrompt 注册提示词模板
func (*DefaultMCPServer) RegisterResource ¶
func (s *DefaultMCPServer) RegisterResource(resource *Resource) error
RegisterResource 注册资源
func (*DefaultMCPServer) RegisterTool ¶
func (s *DefaultMCPServer) RegisterTool(tool *ToolDefinition, handler ToolHandler) error
RegisterTool 注册工具
func (*DefaultMCPServer) Serve ¶ added in v1.0.0
func (s *DefaultMCPServer) Serve(ctx context.Context, transport Transport) error
Serve runs the MCP server message loop over the given transport. It receives messages, dispatches them via HandleMessage, and sends responses back. The loop exits when the context is cancelled or the transport returns an error.
func (*DefaultMCPServer) SetLogLevel ¶
func (s *DefaultMCPServer) SetLogLevel(level string) error
SetLogLevel 设置日志级别
func (*DefaultMCPServer) SubscribeResource ¶
func (s *DefaultMCPServer) SubscribeResource(ctx context.Context, uri string) (<-chan Resource, error)
SubscribeResource 订阅资源更新
func (*DefaultMCPServer) UnregisterPrompt ¶
func (s *DefaultMCPServer) UnregisterPrompt(name string) error
UnregisterPrompt 注销提示词模板
func (*DefaultMCPServer) UnregisterTool ¶
func (s *DefaultMCPServer) UnregisterTool(name string) error
UnregisterTool 注销工具
func (*DefaultMCPServer) UpdateResource ¶
func (s *DefaultMCPServer) UpdateResource(resource *Resource) error
UpdateResource 更新资源
type MCPClient ¶
type MCPClient interface {
// 连接管理
Connect(ctx context.Context, serverURL string) error
Disconnect(ctx context.Context) error
IsConnected() bool
// 服务器交互
GetServerInfo(ctx context.Context) (*ServerInfo, error)
// 资源操作
ListResources(ctx context.Context) ([]Resource, error)
ReadResource(ctx context.Context, uri string) (*Resource, error)
// 工具操作
ListTools(ctx context.Context) ([]ToolDefinition, error)
CallTool(ctx context.Context, name string, args map[string]any) (any, error)
// 提示词操作
ListPrompts(ctx context.Context) ([]PromptTemplate, error)
GetPrompt(ctx context.Context, name string, vars map[string]string) (string, error)
}
MCPClient MCP 客户端接口
type MCPError ¶
type MCPError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
MCPError MCP 错误
type MCPMessage ¶
type MCPMessage struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params map[string]any `json:"params,omitempty"`
Result any `json:"result,omitempty"`
Error *MCPError `json:"error,omitempty"`
}
MCPMessage MCP 消息(JSON-RPC 2.0)
func NewMCPError ¶
func NewMCPError(id any, code int, message string, data any) *MCPMessage
NewMCPError 创建 MCP 错误响应
func NewMCPRequest ¶
func NewMCPRequest(id any, method string, params map[string]any) *MCPMessage
NewMCPRequest 创建 MCP 请求
func (*MCPMessage) MarshalJSON ¶
func (m *MCPMessage) MarshalJSON() ([]byte, error)
MarshalJSON 自定义 JSON 序列化
type MCPServer ¶
type MCPServer interface {
// 服务器信息
GetServerInfo() ServerInfo
// 资源管理
ListResources(ctx context.Context) ([]Resource, error)
GetResource(ctx context.Context, uri string) (*Resource, error)
SubscribeResource(ctx context.Context, uri string) (<-chan Resource, error)
// 工具管理
ListTools(ctx context.Context) ([]ToolDefinition, error)
CallTool(ctx context.Context, name string, args map[string]any) (any, error)
// 提示词管理
ListPrompts(ctx context.Context) ([]PromptTemplate, error)
GetPrompt(ctx context.Context, name string, vars map[string]string) (string, error)
// 日志
SetLogLevel(level string) error
}
MCPServer MCP 服务器接口
type PromptExample ¶
type PromptExample struct {
Variables map[string]string `json:"variables"`
Output string `json:"output"`
}
PromptExample 提示词示例
type PromptTemplate ¶
type PromptTemplate struct {
Name string `json:"name"`
Description string `json:"description"`
Template string `json:"template"`
Variables []string `json:"variables"`
Examples []PromptExample `json:"examples,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
PromptTemplate MCP 提示词模板
func (*PromptTemplate) RenderPrompt ¶
func (p *PromptTemplate) RenderPrompt(vars map[string]string) (string, error)
RenderPrompt 渲染提示词模板
func (*PromptTemplate) Validate ¶
func (p *PromptTemplate) Validate() error
ValidatePromptTemplate 验证提示词模板
type Resource ¶
type Resource struct {
URI string `json:"uri"` // 资源 URI
Name string `json:"name"` // 资源名称
Description string `json:"description"` // 资源描述
Type ResourceType `json:"type"` // 资源类型
MimeType string `json:"mimeType"` // MIME 类型
Content any `json:"content"` // 资源内容
Metadata map[string]any `json:"metadata"` // 元数据
Size int64 `json:"size"` // 资源大小(字节)
CreatedAt time.Time `json:"createdAt"` // 创建时间
UpdatedAt time.Time `json:"updatedAt"` // 更新时间
}
Resource MCP 资源
type ResourceType ¶
type ResourceType string
ResourceType 资源类型
const ( ResourceTypeText ResourceType = "text" ResourceTypeImage ResourceType = "image" ResourceTypeFile ResourceType = "file" ResourceTypeData ResourceType = "data" ResourceTypeStream ResourceType = "stream" )
type ServerCapabilities ¶
type ServerCapabilities struct {
Resources bool `json:"resources"`
Tools bool `json:"tools"`
Prompts bool `json:"prompts"`
Logging bool `json:"logging"`
Sampling bool `json:"sampling"`
}
ServerCapabilities 服务器能力
type ServerInfo ¶
type ServerInfo struct {
Name string `json:"name"`
Version string `json:"version"`
ProtocolVersion string `json:"protocolVersion"`
Capabilities ServerCapabilities `json:"capabilities"`
Metadata map[string]any `json:"metadata,omitempty"`
}
ServerInfo 服务器信息
type ToolDefinition ¶
type ToolDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]any `json:"inputSchema"` // JSON Schema
OutputSchema map[string]any `json:"outputSchema,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
ToolDefinition MCP 工具定义
func FromLLMToolSchema ¶
func FromLLMToolSchema(schema types.ToolSchema) (ToolDefinition, error)
FromLLMToolSchema 从 LLM 工具 Schema 创建 MCP 工具定义
func (*ToolDefinition) ToLLMToolSchema ¶
func (t *ToolDefinition) ToLLMToolSchema() types.ToolSchema
ToLLMToolSchema 将 MCP 工具定义转换为 LLM 工具 Schema
func (*ToolDefinition) Validate ¶
func (t *ToolDefinition) Validate() error
ValidateToolDefinition 验证工具定义
type ToolHandler ¶
ToolHandler 工具处理函数