Documentation
¶
Overview ¶
Package mcp — Model Context Protocol (MCP) 服务端实现 提供 JSON-RPC 2.0 协议处理,允许外部 LLM 应用通过标准 MCP 协议操作 edgeCore
Index ¶
- Constants
- func IsVersionSupported(v string) bool
- func RegisterCapabilityTools(srv *MCPServer, rt *capability.Runtime, writeGate func() *CallToolResult) int
- func SupportedVersions() []string
- func ToolNameFromCapability(capabilityID string) string
- type CallToolParams
- type CallToolResult
- type ClientCapabilities
- type ClientInfo
- type ContentBlock
- type GetPromptParams
- type GetPromptResult
- type InitializeParams
- type InitializeResult
- type InputSchema
- type JSONRPCError
- type JSONRPCNotification
- type JSONRPCRequest
- type JSONRPCResponse
- type ListPromptsResult
- type ListResourcesResult
- type ListToolsResult
- type LoggingCapability
- type MCPServer
- func (s *MCPServer) GetPromptNames() []string
- func (s *MCPServer) GetPrompts() []Prompt
- func (s *MCPServer) GetResourceURIs() []string
- func (s *MCPServer) GetResources() []Resource
- func (s *MCPServer) GetToolNames() []string
- func (s *MCPServer) GetTools() []Tool
- func (s *MCPServer) HandleMessage(raw json.RawMessage) *JSONRPCResponse
- func (s *MCPServer) IsInitialized() bool
- func (s *MCPServer) RegisterPrompt(prompt Prompt)
- func (s *MCPServer) RegisterResource(resource Resource, handler ResourceHandler)
- func (s *MCPServer) RegisterTool(tool Tool, handler ToolHandler)
- type Prompt
- type PromptArgument
- type PromptMessage
- type PromptsCapability
- type PropertyDef
- type ReadResourceParams
- type ReadResourceResult
- type Resource
- type ResourceContent
- type ResourceHandler
- type ResourcesCapability
- type RootsCapability
- type SamplingCapability
- type ServerCapabilities
- type ServerInfo
- type Tool
- type ToolHandler
- type ToolsCapability
Constants ¶
const ( MCPVersion = "2024-11-05" MCPVersionV25 = "2025-11-25" ProtocolName = "mcp" ServerName = "edgeCore-MCP-Server" ServerVersion = "1.0.0" // JSON-RPC 标准错误码 ErrParseError = -32700 ErrInvalidRequest = -32600 ErrMethodNotFound = -32601 ErrInvalidParams = -32602 ErrInternalError = -32603 )
const CapabilityToolPrefix = "ean_"
CapabilityToolPrefix namespaces auto-generated EAN tools so they do not collide with hand-written JSON-RPC tools (list_channels, create_device, etc.).
Variables ¶
This section is empty.
Functions ¶
func RegisterCapabilityTools ¶
func RegisterCapabilityTools(srv *MCPServer, rt *capability.Runtime, writeGate func() *CallToolResult) int
RegisterCapabilityTools registers Capability→Tool handlers on an MCP server. Tool calls are dispatched through capability.Runtime.Invoke (not raw JSON-RPC business handlers), keeping MCP protocol concerns in this package. writeGate, when non-nil, is invoked before write/admin capabilities; a non-nil result short-circuits the call (used for MCP full-access confirmation).
func ToolNameFromCapability ¶
ToolNameFromCapability converts a Capability ID to an MCP tool name. Example: modbus_tcp.read_point → ean_modbus_tcp_read_point
Types ¶
type CallToolParams ¶
type CallToolParams struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments,omitempty"`
}
CallToolParams tools/call 请求参数
type CallToolResult ¶
type CallToolResult struct {
Content []ContentBlock `json:"content"`
IsError bool `json:"isError,omitempty"`
}
CallToolResult tools/call 响应
type ClientCapabilities ¶
type ClientCapabilities struct {
Roots *RootsCapability `json:"roots,omitempty"`
Sampling *SamplingCapability `json:"sampling,omitempty"`
}
ClientCapabilities 客户端能力声明
type ClientInfo ¶
ClientInfo 客户端信息
type ContentBlock ¶
type ContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Data string `json:"data,omitempty"`
MimeType string `json:"mimeType,omitempty"`
URI string `json:"uri,omitempty"`
Name string `json:"name,omitempty"`
}
ContentBlock 内容块(文本/图片/资源)
type GetPromptParams ¶
type GetPromptParams struct {
Name string `json:"name"`
Arguments map[string]string `json:"arguments,omitempty"`
}
GetPromptParams prompts/get 请求参数
type GetPromptResult ¶
type GetPromptResult struct {
Description string `json:"description,omitempty"`
Messages []PromptMessage `json:"messages"`
}
GetPromptResult prompts/get 响应
type InitializeParams ¶
type InitializeParams struct {
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 InputSchema ¶
type InputSchema struct {
Type string `json:"type"`
Properties map[string]PropertyDef `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
}
InputSchema JSON Schema 输入定义
type JSONRPCError ¶
type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
JSONRPCError JSON-RPC 错误
type JSONRPCNotification ¶
type JSONRPCNotification struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
JSONRPCNotification 标准 JSON-RPC 通知(无 id,无需响应)
type JSONRPCRequest ¶
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id,omitempty"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
JSONRPCRequest 标准 JSON-RPC 请求
type JSONRPCResponse ¶
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
}
JSONRPCResponse 标准 JSON-RPC 响应
func JSONRPCErrorResponse ¶
func JSONRPCErrorResponse(id any, code int, message string) JSONRPCResponse
JSONRPCErrorResponse 创建 JSON-RPC 错误响应
func JSONRPCSuccessResponse ¶
func JSONRPCSuccessResponse(id any, result any) JSONRPCResponse
JSONRPCSuccessResponse 创建 JSON-RPC 成功响应
type ListPromptsResult ¶
type ListPromptsResult struct {
Prompts []Prompt `json:"prompts"`
}
ListPromptsResult prompts/list 响应
type ListResourcesResult ¶
type ListResourcesResult struct {
Resources []Resource `json:"resources"`
}
ListResourcesResult resources/list 响应
type ListToolsResult ¶
type ListToolsResult struct {
Tools []Tool `json:"tools"`
}
ListToolsResult tools/list 响应
type MCPServer ¶
type MCPServer struct {
// contains filtered or unexported fields
}
── MCPServer MCP 协议引擎 ──
func (*MCPServer) GetPromptNames ¶
GetPromptNames 返回所有提示词名称
func (*MCPServer) GetResourceURIs ¶
GetResourceURIs 返回所有资源 URI
func (*MCPServer) GetResources ¶
GetResources 返回所有已注册资源
func (*MCPServer) HandleMessage ¶
func (s *MCPServer) HandleMessage(raw json.RawMessage) *JSONRPCResponse
HandleMessage 处理 JSON-RPC 消息,返回响应(nil 表示通知无需响应)
func (*MCPServer) IsInitialized ¶
IsInitialized 检查是否已完成初始化握手
func (*MCPServer) RegisterPrompt ¶
RegisterPrompt 注册提示词模板
func (*MCPServer) RegisterResource ¶
func (s *MCPServer) RegisterResource(resource Resource, handler ResourceHandler)
RegisterResource 注册资源及其处理函数
func (*MCPServer) RegisterTool ¶
func (s *MCPServer) RegisterTool(tool Tool, handler ToolHandler)
RegisterTool 注册工具及其处理函数
type Prompt ¶
type Prompt struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Arguments []PromptArgument `json:"arguments,omitempty"`
}
Prompt MCP 提示词定义
type PromptArgument ¶
type PromptArgument struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
}
PromptArgument 提示词参数
type PromptMessage ¶
type PromptMessage struct {
Role string `json:"role"`
Content ContentBlock `json:"content"`
}
PromptMessage 提示词消息
type PromptsCapability ¶
type PromptsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
PromptsCapability 提示词能力
type PropertyDef ¶
type PropertyDef struct {
Type string `json:"type"`
Description string `json:"description"`
Enum []string `json:"enum,omitempty"`
Default any `json:"default,omitempty"`
}
PropertyDef 属性定义
type ReadResourceParams ¶
type ReadResourceParams struct {
URI string `json:"uri"`
}
ReadResourceParams resources/read 请求参数
type ReadResourceResult ¶
type ReadResourceResult struct {
Contents []ResourceContent `json:"contents"`
}
ReadResourceResult resources/read 响应
type Resource ¶
type Resource struct {
URI string `json:"uri"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
Resource MCP 资源定义
type ResourceContent ¶
type ResourceContent struct {
URI string `json:"uri"`
MimeType string `json:"mimeType,omitempty"`
Text string `json:"text,omitempty"`
Blob string `json:"blob,omitempty"`
}
ResourceContent 资源内容
type ResourceHandler ¶
type ResourceHandler func(uri string) (*ReadResourceResult, error)
── ResourceHandler 资源读取函数签名 ──
type ResourcesCapability ¶
type ResourcesCapability struct {
Subscribe bool `json:"subscribe,omitempty"`
ListChanged bool `json:"listChanged,omitempty"`
}
ResourcesCapability 资源能力
type RootsCapability ¶
type RootsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
RootsCapability roots 能力
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"`
}
ServerCapabilities 服务端能力声明
type ServerInfo ¶
ServerInfo 服务端信息
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema InputSchema `json:"inputSchema"`
}
Tool MCP 工具定义
func CapabilityToTool ¶
func CapabilityToTool(cap capability.Capability) Tool
CapabilityToTool maps an EAN Capability Descriptor to an MCP Tool definition.
type ToolHandler ¶
type ToolHandler func(args json.RawMessage) (*CallToolResult, error)
── ToolHandler 工具执行函数签名 ── 接收参数 JSON,返回结果文本
type ToolsCapability ¶
type ToolsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
ToolsCapability 工具能力