mcp

package
v1.0.0-rc.21 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package mcp 是 pt-tools 的 MCP(Model Context Protocol)服务(路线图 M14):工具定义、权限检查与写操作的审计。

工具本身不碰数据库与下载器:经 Backend 在进程内调用 App API v1(web 实现),所以返回的字段、脱敏与 App API 完全一样。 传输在别处接:主端口的 streamable HTTP(/mcp,web 包)与 stdio 桥(pt-tools mcp,cmd 包)。

权限分两层:HTTP 层只放行有 mcp:read 或 mcp:write 的 API 令牌;工具分派时只读工具要 mcp:read,写工具要 mcp:write, 写工具还要 confirm=true。写工具的每次调用(包括被拒的)记操作审计,通道是 mcp,命令是工具名。

Index

Constants

View Source
const ChannelType = "mcp"

ChannelType 是 MCP 写工具在操作审计里的通道。

Variables

View Source
var ToolNames = []string{
	"list_tasks", "list_downloader_torrents", "get_downloader_stats", "search_torrents",
	"pause_torrent", "resume_torrent", "delete_torrent", "push_torrent",
	"get_site_userinfo", "check_updates",
	"explore_media", "list_subscriptions", "add_subscription",
}

ToolNames 是全部工具的名字(文档与测试用)。

Functions

func NewServer

func NewServer(d Deps) *sdk.Server

NewServer 建 MCP 服务,注册全部工具。

func TokenInfo

func TokenInfo(c Caller, expires time.Time) *auth.TokenInfo

TokenInfo 把令牌换成 go-sdk 的 TokenInfo(HTTP 层校验令牌以后放进请求,工具从 req.Extra.TokenInfo 取回来)。 expires 为零表示不过期。

Types

type Auditor

type Auditor interface {
	Record(ctx context.Context, e app.AuditEntry) error
}

Auditor 记操作审计(app.AuditService 实现了它)。

type Backend

type Backend interface {
	Call(ctx context.Context, c Caller, req Request) (Response, error)
}

Backend 在进程内调用 App API v1(web.Server 实现)。

type Caller

type Caller struct {
	TokenID uint
	Name    string
	Scopes  []string
}

Caller 是调用工具的 API 令牌。

func CallerFromRequest

func CallerFromRequest(req *sdk.CallToolRequest) (Caller, bool)

CallerFromRequest 从工具请求里取调用的令牌(HTTP 层放进去的 TokenInfo);没有时 ok 为假。

func (Caller) Has

func (c Caller) Has(scope string) bool

Has 报告令牌有没有这个权限范围(精确匹配)。

type Deps

type Deps struct {
	Backend Backend
	Audit   Auditor
	// ResolveURL 把种子下载地址解析成已知站点与种子编号(push_torrent 的 torrent_url);不认识时 ok 为假
	ResolveURL func(rawURL string) (site, torrentID string, ok bool)
	// Version 是 pt-tools 的版本(initialize 时报给客户端)
	Version string
	// Caller 从工具请求里取令牌,默认 CallerFromRequest(测试里换掉)
	Caller func(req *sdk.CallToolRequest) (Caller, bool)
}

Deps 是 MCP 服务的依赖。

type Request

type Request struct {
	Method string
	// Path 不带 /api/app/v1 前缀,可以带查询串
	Path string
	// Body 不为 nil 时按 JSON 编码
	Body any
	// Write 为真时按写接口调用(要 mcp:write)
	Write bool
}

Request 是一次对 App API v1 的进程内调用。

type Response

type Response struct {
	Status int
	Body   []byte
	// Outcome 是写接口记下的业务结果(推送被拦下、批量动作全失败这类回 200 的失败),用审计的写法
	Outcome string
}

Response 是 App API 的回应。

Jump to

Keyboard shortcuts

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