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 ¶
- func WithCaller(ctx context.Context, c Caller) context.Context
- type AdminAPI
- type Caller
- type FederationAPI
- type FederationResolve
- type FederationStatus
- type FileShareOpts
- type P2pAPI
- type P2pInviteInfo
- type P2pPeerInfo
- type PreviewAPI
- type PreviewReq
- type PreviewResult
- type RequestAPI
- type RequestCreate
- type Service
- func (s *Service) Handler() http.Handler
- func (s *Service) SetAdminService(svc AdminAPI)
- func (s *Service) SetBaseURL(base string)
- func (s *Service) SetFederationService(svc FederationAPI)
- func (s *Service) SetMaxFileSize(n int64)
- func (s *Service) SetP2pService(svc P2pAPI)
- func (s *Service) SetPreviewService(svc PreviewAPI)
- func (s *Service) SetRequestService(svc RequestAPI)
- func (s *Service) SetShareService(svc ShareAPI)
- func (s *Service) SetStorageService(svc StorageAPI)
- func (s *Service) ToolNames() []string
- type ShareAPI
- type ShareCreated
- type ShareFileInfo
- type StorageAPI
- type StorageStatusInfo
- type SystemStats
- type UserRow
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithCaller ¶ added in v0.27.0
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 {
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
Caller 调用者身份(transport 层从中间件注入的 ctx 提取;游客=零值)。
func CallerFromContext ¶ added in v0.27.0
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 ¶
FederationStatus 联邦状态工具所需事实。
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
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 ¶
NewService 创建 MCP service。依赖经 Set*Service 注入(bootstrap 装配; 未注入时对应工具返回明确错误而非静默降级)。
func (*Service) Handler ¶ added in v0.27.0
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 ¶
SetAdminService 注入 admin 域能力适配器(统计/用户/维护)
func (*Service) SetBaseURL ¶ added in v0.17.0
SetBaseURL 注入对外 base(server.base_url;空=按请求来源动态推断)
func (*Service) SetFederationService ¶
func (s *Service) SetFederationService(svc FederationAPI)
SetFederationService 注入 P2P 联邦适配器(未启用传 nil 实例适配器。 P1 后台热切换后为运行期写,与工具调用并发——须加锁)
func (*Service) SetMaxFileSize ¶
SetMaxFileSize 配置 MCP 单文件上限(字节;非正值回退默认)
func (*Service) SetP2pService ¶ added in v0.19.0
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 ¶
SetShareService 注入 share 域能力适配器(创建分享)
func (*Service) SetStorageService ¶
func (s *Service) SetStorageService(svc StorageAPI)
SetStorageService 注入存储能力(download_share_file)
type ShareAPI ¶
type ShareAPI interface {
CreateTextShare(ctx context.Context, text string, expireValue int, expireStyle string,
requireAuth bool, passwordHash string, userID *uint, ownerIP, customCode string) (ShareCreated, error)
ShareBytes(ctx context.Context, opts FileShareOpts) (ShareCreated, error)
ShareFiles(ctx context.Context, code string) ([]ShareFileInfo, error)
}
ShareAPI 分享创建/查询工具所需的 share 域能力。
type ShareCreated ¶ added in v0.24.1
type ShareCreated struct {
}
ShareCreated 分享创建结果:分享码(链接标识,8 位/自定义口令)+ 6 位取件码 (E2E 密文等未铸造场景为空)+ 完整分享链接。两码并列输出,杜绝把分享码 误标成取件码(2026-10-16 双码收敛:主推取件码)。
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 存储状态工具所需事实。