mcp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mcp 提供 MCP(Model Context Protocol)客户端 → ToolDefinition 桥(任务 S13)。

对齐上游:packages 的 MCP 客户端接缝 + (原生 MCP 协议 spec)

设计要点:

  • MCPTransport 抽象 JSON-RPC 上承载的传输方式(stdio / SSE……),生产可替换;
  • MCPClient 按 MCP 协议初始化并列出工具(tools/list)与调用工具(tools/call);
  • ToolBridge 把 MCP 服务器暴露的工具自动映射为 pkg/tools.Tool(M48 强校验子集 schema
  • Execute 包装),从而「工具自动出现在 ToolRegistry 并能正常调用」。

publicname.go 复刻官方 mcp-client/tools 的公共名推导:MCP 工具的稳定身份是 (server, raw),模型面公共名为 mcp__<server>__<raw>;当字符替换或截断到函数名 约束(64 字符、[A-Za-z0-9_-])时追加身份哈希,避免不同身份塌缩为同名。

Index

Constants

View Source
const MaxPublicNameLength = 64

MaxPublicNameLength 是函数名长度上限。

Variables

This section is empty.

Functions

func MapSchema

func MapSchema(m map[string]any) (*tools.JsonSchemaNode, error)

MapSchema 把通用 JSON Schema map 映射为强校验子集 JsonSchemaNode(M48 子集)。 仅映射子集支持的关键字:type/properties/required/items/enum/description/const/default。 遇到不支持的(如 anyOf)返回错误(fail-closed)。

func PublicToolName

func PublicToolName(serverName, rawName string) string

PublicToolName 由 (serverName, rawName) 确定性推导模型面公共名。

Types

type Bridge

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

Bridge 把 MCP 服务器工具映射为 pkg/tools.Tool。

func NewBridge

func NewBridge(client *Client) *Bridge

NewBridge 创建桥接器。

func (*Bridge) ToTool

func (b *Bridge) ToTool(schema MCPSchema) (*tools.Tool, error)

ToTool 将一条 MCP 工具 schema 映射为等价 tool(Execute 包装 CallTool)。

func (*Bridge) ToTools

func (b *Bridge) ToTools(schemas []MCPSchema) ([]*tools.Tool, error)

ToolRegistry 把已列出的工具批量注册为 *tools.Tool。

type Client

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

Client 是 MCP 客户端:初始化 → 列工具 → 调工具。

func NewClient

func NewClient(t Transport) *Client

NewClient 创建 MCP 客户端。

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, input map[string]any) (string, error)

CallTool 调用 MCP 工具并返回文本结果。

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context) error

Initialize 执行 MCP 初始化握手。

func (*Client) ListTools

func (c *Client) ListTools(ctx context.Context) ([]MCPSchema, error)

ListTools 列出 MCP 服务器暴露的全部工具。

type MCPSchema

type MCPSchema struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	InputSchema map[string]any `json:"inputSchema"` // JSON Schema(object 形式)
}

MCPSchema 是一个由 MCP 服务器暴露的工具定义。

type Request

type Request struct {
	ID     uint64         `json:"id"`
	Method string         `json:"method"`
	Params map[string]any `json:"params,omitempty"`
}

Request 是一条 JSON-RPC 请求。

type Response

type Response struct {
	ID     uint64          `json:"id"`
	Result json.RawMessage `json:"result,omitempty"`
	Error  json.RawMessage `json:"error,omitempty"`
}

Response 是一条 JSON-RPC 响应(result 原样保留)。

type Transport

type Transport interface {
	// RoundTrip 发送一条请求,返回对应响应。
	RoundTrip(ctx context.Context, req Request) (Response, error)
}

Transport 封装 MCP JSON-RPC 往返(stdio / SSE 等实现)。

Jump to

Keyboard shortcuts

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