Help us shape the future of Go! Take the 2026 Go Developer Survey

mcp

package
v0.32.22 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package mcp 实现 PigeonBox 的 Model Context Protocol (MCP) server。

传输:Streamable HTTP(POST 单端点 + JSON 响应体)。协议层由**官方 SDK** github.com/modelcontextprotocol/go-sdk 承载(2026-10-22 换掉自研 JSON-RPC 实现):协议版本协商、JSON-RPC 帧、工具注册、错误语义全部交给 SDK,默认支持 全部已知版本(最新 2026-07-28;握手期旧版 2025-11-25 / 2025-06-18 / 2025-03-26 向下兼容,客户端报什么版本就协商什么版本)。legacy 文档中的裸 TCP 方案不符合 MCP 规范,本实现为标准重做。

认证与作用域(2026-10-12 访客快速分享,对标上游 v2.8 MCP quick share 并超出): 默认路由挂 AdminMiddleware(管理员 JWT)全量工具;PB_MCP_ALLOW_GUEST=true 时 改挂可选认证,按调用者身份分三级作用域——游客(未登录)仅 share_text/share_file 快速分享(与网页匿名寄件同链路、同限流、同审核/白名单),登录用户 += create_request_code/preview_share,管理员 = 全量。身份由 transport 层经 WithCaller 注入 ctx(官方 SDK 把 http.Request 的 ctx 透传到工具 handler)。

工具集(对齐 legacy docs/mcp-server-guide.md + 2026-10-06 上传/下载/联邦扩展 + 2026-10-12 寄件码/预览工具 + Wave B' 直传四工具退役):

share_text / share_file / create_request_code / preview_share /
get_share / get_share_content / download_share_file /
list_shares / delete_share / get_system_status / get_storage_info /
list_users / cleanup_expired / federation_status / federation_resolve /
p2p_peers / p2p_invites / p2p_accept

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentDeliveringToolNames added in v0.32.7

func ContentDeliveringToolNames() []string

ContentDeliveringToolNames 返回**客观判定**为「对外交付内容」的工具名。

判据 = 工具出参结构里含内容字段(text / content_base64 / …),由**反射**从 toolSpec.Out 推导 ⇒ 与作者的 `Gate.ReadsShare` 标注**互为交叉验证**: 客观判定为交付内容、但没自标 Gate 的工具 ⇒ 取件闸门契约守卫会变红 (2026-10-25 审计 A2:`preview_share` 此前就是这样漏出契约表的 —— 它回传 文本摘要,出参里有 `Text`,却没标 ReadsShare)。

func ShareReadingToolNames added in v0.32.5

func ShareReadingToolNames() []string

ShareReadingToolNames 返回「对外交付分享内容/元信息」的工具名。

供取件闸门契约守卫(bootstrap/share_surface_contract_test.go)消费: 凡是读分享内容的工具都必须在契约表里声明「次数耗尽时的判定」并有行为测试 覆盖 ⇒ 新增读内容的工具而忘声明 ⇒ 守卫变红。

⚠️ 此集合只含**作者自标** `Gate.ReadsShare` 的工具 ⇒ 它**不能**作为守卫的 唯一发现机制(新增工具忘自标就漏出)⇒ 守卫另用 **ContentDeliveringToolNames()**(客观判据)交叉核对。

func WithCaller added in v0.27.0

func WithCaller(ctx context.Context, c Caller) context.Context

WithCaller 把调用者身份注入 context。

transport 层(Hertz handler)在进 net/http 桥接**之前**注入:官方 SDK 把 http.Request 的 ctx 一路透传到工具 handler(SDK 源码注释:"Pass req.Context() here, to allow middleware to add context values"), 因此工具层与中间件都能读到同一份身份。

Types

type AdminAPI

type AdminAPI interface {
	DeleteShareByID(ctx context.Context, id uint) error
	SystemStats(ctx context.Context) (*SystemStats, error)
	StorageStatus(ctx context.Context) (*StorageStatusInfo, error)
	Users(ctx context.Context, page, pageSize int) ([]UserRow, int64, error)
	CleanExpired(ctx context.Context) (count, freed int64, err error)
}

AdminAPI 系统维护类工具所需的 admin 域能力(bootstrap 注入全站唯一实例适配器)。

type Caller added in v0.17.0

type Caller struct {
	Admin  bool
	UserID uint
	IP     string
}

Caller 调用者身份(transport 层从中间件注入的 ctx 提取;游客=零值)。

func CallerFromContext added in v0.27.0

func CallerFromContext(ctx context.Context) Caller

CallerFromContext 读取调用者身份(缺失=游客零值)。

type FederationAPI

type FederationAPI interface {
	Status() FederationStatus
	Resolve(code string) (*FederationResolve, error)
}

FederationAPI P2P 联邦工具所需事实(未启用时 bootstrap 传 nil 实例适配器)。

type FederationResolve

type FederationResolve struct {
	NodeID    string
	URL       string
	Name      string
	ExpiresAt time.Time
	SizeHint  int64
}

FederationResolve 联邦路由解析结果。

type FederationStatus

type FederationStatus struct {
	Enabled    bool
	NodeID     string
	Healthy    bool
	Registries []string
}

FederationStatus 联邦状态工具所需事实。

type FileShareOpts

type FileShareOpts struct {
	FileName     string
	Content      []byte
	ExpireValue  int
	ExpireStyle  string
	PasswordHash string
	CustomCode   string
	UserID       *uint // 归属(游客=nil 匿名归因;登录用户配额记账)
	OwnerIP      string
}

FileShareOpts 文件分享创建参数(share_file 工具)。

type P2pAPI added in v0.19.0

type P2pAPI interface {
	// P2pPeers 在线名录(opt-in 可发现节点)。
	P2pPeers(ctx context.Context) ([]P2pPeerInfo, error)
	// P2pInvites 待收邀请箱快照。
	P2pInvites(ctx context.Context) ([]P2pInviteInfo, error)
	// P2pAccept 认领邀请(纯信封:出箱 + 返回信封)。
	P2pAccept(ctx context.Context, inviteID string) (P2pInviteInfo, error)
}

P2pAPI 联邦面窄接口(在线名录 / 待收邀请 / 认领信封)。

2026-10-22 Wave B':直传四工具(status/tasks/send/recv)随 Box 侧直传 编排层整体退役 —— 直传由独立应用 PigeonBeam 承担,Box 不再编排。 p2p_accept 退化为**纯信封**(出箱 + 返回信封),与 HTTP 面同语义。

type P2pInviteInfo added in v0.21.2

type P2pInviteInfo struct {
	ID        string `json:"id"`
	Code      string `json:"code"`
	FromName  string `json:"from_name"`
	Note      string `json:"note"`
	ExpiresIn int64  `json:"expires_in"`
}

P2pInviteInfo 待收邀请条目(P3 收件箱快照透传)。

type P2pPeerInfo added in v0.21.2

type P2pPeerInfo struct {
	NodeID      string `json:"node_id"`
	Name        string `json:"name"`
	URL         string `json:"url"`
	BoxPub      string `json:"box_pub"`
	OnlineSince int64  `json:"online_since"`
}

P2pPeerInfo 在线名录对端(零敏感五字段,p2pd 透传)。

type PreviewAPI added in v0.17.0

type PreviewAPI interface {
	Preview(ctx context.Context, req PreviewReq) (*PreviewResult, error)
}

PreviewAPI 预览工具所需的 preview 域能力(bootstrap 注入域适配器)。

type PreviewReq added in v0.17.0

type PreviewReq struct {
	Code     string
	UserID   *uint
	ClientIP string
	Password string
}

PreviewReq 预览请求(preview_share 工具;闸门/密码校验在 preview 域内执行)。

type PreviewResult added in v0.17.0

type PreviewResult struct {
	PreviewType string
	MimeType    string
	TextContent string
	FileSize    int64
	Width       int
	Height      int
	PageCount   int
	Duration    int
}

PreviewResult 预览结果(AI 友好的最小事实面)。

type RequestAPI added in v0.17.0

type RequestAPI interface {
	Create(ctx context.Context, userID uint, req RequestCreate) (token string, expiredAt time.Time, err error)
}

RequestAPI 寄件码工具所需的 request 域能力(bootstrap 注入域适配器)。

type RequestCreate added in v0.17.0

type RequestCreate struct {
	Title       string
	MaxFiles    int
	MaxBytes    int64
	ExpireValue int
	ExpireStyle string
}

RequestCreate 寄件码(反向收件投递链接)创建参数。

type Service

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

Service MCP server。adminSvc 提供统计/维护能力,shareSvc 提供分享创建, storageSvc 提供文件下载事实,fedSvc 提供联邦状态/路由(未启用时为 nil 适配器); storage/DAO 直查用于 get_share/get_share_content/list_shares 展示。

func NewService

func NewService(gormDB *gorm.DB, version string) *Service

NewService 创建 MCP service。依赖经 Set*Service 注入(bootstrap 装配; 未注入时对应工具返回明确错误而非静默降级)。

func (*Service) Handler added in v0.27.0

func (s *Service) Handler() http.Handler

Handler 返回 MCP 端点的 net/http 处理器(Streamable HTTP 传输)。

传输模式(对齐 MCP 规范):

  • 无会话(Stateless):不写 Mcp-Session-Id;GET/DELETE 按规范回 405 (无会话模式不提供 SSE 流与会话终结);
  • JSON 响应(JSONResponse):响应体 application/json(非 SSE 流)。

两者与本站历史行为线形一致(POST 单端点 + JSON 响应体);协议版本协商、 JSON-RPC 帧、工具注册与错误语义全部由官方 SDK 承担。

惰性构建(sync.Once):请求体上限依赖 SetMaxFileSize 注入的配置, 首次调用时构造可保证拿到最终值。

func (*Service) SetAdminService

func (s *Service) SetAdminService(svc AdminAPI)

SetAdminService 注入 admin 域能力适配器(统计/用户/维护)

func (*Service) SetBaseURL added in v0.17.0

func (s *Service) SetBaseURL(base string)

SetBaseURL 注入对外 base(server.base_url;空=按请求来源动态推断)

func (*Service) SetFederationService

func (s *Service) SetFederationService(svc FederationAPI)

SetFederationService 注入 P2P 联邦适配器(未启用传 nil 实例适配器。 P1 后台热切换后为运行期写,与工具调用并发——须加锁)

func (*Service) SetMaxFileSize

func (s *Service) SetMaxFileSize(n int64)

SetMaxFileSize 配置 MCP 单文件上限(字节;非正值回退默认)

func (*Service) SetP2pService added in v0.19.0

func (s *Service) SetP2pService(svc P2pAPI)

SetP2pService 注入联邦面适配器(在线名录/待收邀请/认领信封;未装配传 nil)。

func (*Service) SetPreviewService added in v0.17.0

func (s *Service) SetPreviewService(svc PreviewAPI)

SetPreviewService 注入 preview 域能力适配器(preview_share)

func (*Service) SetRequestService added in v0.17.0

func (s *Service) SetRequestService(svc RequestAPI)

SetRequestService 注入 request 域能力适配器(create_request_code)

func (*Service) SetShareService

func (s *Service) SetShareService(svc ShareAPI)

SetShareService 注入 share 域能力适配器(创建分享)

func (*Service) SetStorageService

func (s *Service) SetStorageService(svc StorageAPI)

SetStorageService 注入存储能力(download_share_file)

func (*Service) ToolNames

func (s *Service) ToolNames() []string

ToolNames 供测试/文档列出工具名

type ShareAPI

type ShareAPI interface {
	// CreateTextShare 创建文本分享。userID=nil 匿名归因。
	CreateTextShare(ctx context.Context, text string, expireValue int, expireStyle string,
		requireAuth bool, passwordHash string, userID *uint, ownerIP, customCode string) (ShareCreated, error)
	// ShareBytes 内存内容建文件分享(MCP 通道:落存储 + 配额/审核/白名单链路)。
	ShareBytes(ctx context.Context, opts FileShareOpts) (ShareCreated, error)
	// ShareFiles 取分享的子文件清单。
	ShareFiles(ctx context.Context, code string) ([]ShareFileInfo, error)
}

ShareAPI 分享创建/查询工具所需的 share 域能力。

type ShareCreated added in v0.24.1

type ShareCreated struct {
	Code         string
	PickupCode   string
	FullShareURL string
}

ShareCreated 分享创建结果:分享码(链接标识,8 位/自定义口令)+ 6 位取件码 (E2E 密文等未铸造场景为空)+ 完整分享链接。两码并列输出,杜绝把分享码 误标成取件码(2026-10-16 双码收敛:主推取件码)。

type ShareFileInfo

type ShareFileInfo struct {
	Name string
	Size int64
}

ShareFileInfo 分享子文件清单项。

type ShareGate added in v0.32.5

type ShareGate struct {
	// ReadsShare 本工具是否对外交付分享内容或元信息(true ⇒ 必须守次数闸)。
	ReadsShare bool
	// Exhausted 次数耗尽时的判定(错误文案/业务码口径);ReadsShare=false 时留空。
	Exhausted string
	// CoveredBy 覆盖该策略的行为测试函数名(守卫机械校验其真实存在)。
	CoveredBy string
}

ShareGate 单个工具的取件闸门策略。

func ShareGatePolicy added in v0.32.5

func ShareGatePolicy(tool string) *ShareGate

ShareGatePolicy 返回某工具的取件闸门策略(无策略返回 nil)。

type StorageAPI

type StorageAPI interface {
	GetFileSize(ctx context.Context, filePath string) (int64, error)
	GetFile(ctx context.Context, filePath string) ([]byte, error)
}

StorageAPI 下载工具所需存储事实(bootstrap 注入全站唯一实例)。

type StorageStatusInfo

type StorageStatusInfo struct {
	StorageType  string
	TotalSpace   int64
	UsedSpace    int64
	UsagePercent float64
	FileCount    int64
}

StorageStatusInfo 存储状态工具所需事实。

type SystemStats

type SystemStats struct {
	TotalFiles   int64
	TotalUsers   int64
	TotalSize    int64
	TodayUploads int64
	ExpiredFiles int64
}

SystemStats 系统状态工具所需统计事实。

type UserRow

type UserRow struct {
	ID       uint
	Username string
	Email    string
	Status   string
}

UserRow 用户列表工具所需最小身份字段。

Jump to

Keyboard shortcuts

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