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

mcp

package
v0.18.0 Latest Latest
Warning

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

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

Documentation

Overview

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

传输:Streamable HTTP(POST 单端点,JSON-RPC 2.0;与 MCP 规范对齐, Claude Desktop / 任意标准 MCP 客户端可直接接入)。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,管理员 = 全量。

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

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 提取;游客=零值)。

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 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) Handle

func (s *Service) Handle(ctx context.Context, body []byte, caller Caller) (int, []byte)

Handle 处理一次 JSON-RPC POST。 返回 (httpStatus, responseBody);通知类请求返回 (202, "")。 caller 为 transport 层提取的调用者身份(游客=零值),驱动工具作用域过滤。

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 实例适配器)

func (*Service) SetMaxFileSize

func (s *Service) SetMaxFileSize(n int64)

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

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) (code, fullURL string, err error)
	// ShareBytes 内存内容建文件分享(MCP 通道:落存储 + 配额/审核/白名单链路)。
	ShareBytes(ctx context.Context, opts FileShareOpts) (code, fullURL string, err error)
	// ShareFiles 取分享的子文件清单。
	ShareFiles(ctx context.Context, code string) ([]ShareFileInfo, error)
}

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

type ShareFileInfo

type ShareFileInfo struct {
	Name string
	Size int64
}

ShareFileInfo 分享子文件清单项。

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