mcp

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: GPL-2.0, GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mcp — Model Context Protocol (MCP) 服务端实现 提供 JSON-RPC 2.0 协议处理,允许外部 LLM 应用通过标准 MCP 协议操作 edgeCore

Index

Constants

View Source
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
)
View Source
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 IsVersionSupported

func IsVersionSupported(v string) bool

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

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 SupportedVersions

func SupportedVersions() []string

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

func ToolNameFromCapability

func ToolNameFromCapability(capabilityID string) string

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 响应

func NewErrorResult

func NewErrorResult(errMsg string) *CallToolResult

NewErrorResult 创建错误结果

func NewSuccessResult

func NewSuccessResult(text string) *CallToolResult

NewSuccessResult 创建成功结果

type ClientCapabilities

type ClientCapabilities struct {
	Roots    *RootsCapability    `json:"roots,omitempty"`
	Sampling *SamplingCapability `json:"sampling,omitempty"`
}

ClientCapabilities 客户端能力声明

type ClientInfo

type ClientInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

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 内容块(文本/图片/资源)

func NewTextContent

func NewTextContent(text string) ContentBlock

NewTextContent 创建文本内容块

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 LoggingCapability

type LoggingCapability struct{}

LoggingCapability 日志能力

type MCPServer

type MCPServer struct {
	// contains filtered or unexported fields
}

── MCPServer MCP 协议引擎 ──

func NewMCPServer

func NewMCPServer(name, version string) *MCPServer

NewMCPServer 创建 MCP 服务端实例

func (*MCPServer) GetPromptNames

func (s *MCPServer) GetPromptNames() []string

GetPromptNames 返回所有提示词名称

func (*MCPServer) GetPrompts

func (s *MCPServer) GetPrompts() []Prompt

GetPrompts 返回所有已注册提示词

func (*MCPServer) GetResourceURIs

func (s *MCPServer) GetResourceURIs() []string

GetResourceURIs 返回所有资源 URI

func (*MCPServer) GetResources

func (s *MCPServer) GetResources() []Resource

GetResources 返回所有已注册资源

func (*MCPServer) GetToolNames

func (s *MCPServer) GetToolNames() []string

GetToolNames 返回所有工具名称

func (*MCPServer) GetTools

func (s *MCPServer) GetTools() []Tool

GetTools 返回所有已注册工具

func (*MCPServer) HandleMessage

func (s *MCPServer) HandleMessage(raw json.RawMessage) *JSONRPCResponse

HandleMessage 处理 JSON-RPC 消息,返回响应(nil 表示通知无需响应)

func (*MCPServer) IsInitialized

func (s *MCPServer) IsInitialized() bool

IsInitialized 检查是否已完成初始化握手

func (*MCPServer) RegisterPrompt

func (s *MCPServer) RegisterPrompt(prompt Prompt)

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 SamplingCapability

type SamplingCapability struct{}

SamplingCapability sampling 能力

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

type ServerInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

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 工具能力

Jump to

Keyboard shortcuts

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