mcp

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package mcp 是 Flux 的 MCP(Model Context Protocol)接入层。

主线二 阶段 A(consume):Flux 作为 MCP client,连上 MCP server,把它的工具 接进 flux/tool.Registry,让 planner 像调本地工具一样调远端工具。

范围(M1 阶段,故意收窄):

  • 仅 stdio transport(子进程 + 换行分隔 JSON-RPC);HTTP/SSE 只预留 Transport 接口位。
  • 仅 tools 切面:initialize / tools/list / tools/call。 不做 resources / prompts / sampling / roots / progress / cancellation。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterAll

func RegisterAll(ctx context.Context, client *Client, reg *tool.Registry, prefix string) ([]string, error)

RegisterAll 列出 server 的所有工具并注册进 registry(带可选前缀防与本地工具撞名, 如 filesystem 的 write_file 会撞本地 write_file)。返回注册后的名字列表。

Types

type CallToolResult

type CallToolResult struct {
	Content []ContentBlock `json:"content"`
	IsError bool           `json:"isError"`
}

CallToolResult 是 tools/call 的返回。

func (*CallToolResult) Text

func (r *CallToolResult) Text() string

Text 把所有 text 块拼起来 —— 适配 tool.Result.Data 时用。

type Client

type Client struct {
	ServerName      string
	ServerVersion   string
	ProtocolVersion string
	// contains filtered or unexported fields
}

Client 是一个 MCP server 的会话(tools 切面)。

func NewClient

func NewClient(ctx context.Context, t Transport) (*Client, error)

NewClient 用一个已有 Transport 构造 Client(便于将来接 HTTP/SSE,或测试注入假 transport)。

func NewStdioClient

func NewStdioClient(ctx context.Context, command string, args, env []string) (*Client, error)

NewStdioClient 启动一个 stdio MCP server 子进程并完成 initialize 握手。

c, err := mcp.NewStdioClient(ctx, "npx",
    []string{"-y", "@modelcontextprotocol/server-filesystem", dir}, nil)

func (*Client) CallTool

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

CallTool 调用一个工具。注意:工具内部失败由 CallToolResult.IsError 表达, 不是这里返回的 error —— error 仅代表传输/协议层失败。

func (*Client) Close

func (c *Client) Close() error

func (*Client) ListTools

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

ListTools 返回 server 暴露的工具(M1 不处理分页 cursor —— filesystem 等小 server 一页够)。

type ContentBlock

type ContentBlock struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

ContentBlock:MCP 的内容块(text/image/resource...)。M1 只重点用 text; 其余类型保留 Raw,等主线二 C 阶段做完整 content 映射(诚实标注的有损点)。

type RPCError

type RPCError struct {
	Code    int
	Message string
}

RPCError 是上游 server 返回的 JSON-RPC error,暴露给调用方。

func (*RPCError) Error

func (e *RPCError) Error() string

type Server

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

Server 把一个 flux/tool.Registry 暴露成 MCP server —— stage A(consume) 的对称面。 让 Flux 的工具能被 Claude Code / Codex 等 MCP 客户端调用。

范围同 consume:仅 stdio、仅 tools 切面(initialize / tools/list / tools/call)。 请求串行处理(一个 Execute 跑完再读下一条)—— M1 够用,不引入并发。

func NewServer

func NewServer(reg *tool.Registry) *Server

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, in io.Reader, out io.Writer) error

Serve 在 in/out 上跑 JSON-RPC 循环,直到 in 关闭(EOF)或 ctx 取消。

func (*Server) ServeStdio

func (s *Server) ServeStdio(ctx context.Context) error

ServeStdio = Serve(ctx, os.Stdin, os.Stdout),给真实子进程入口(cmd/flux-mcp)用。

type ToolAdapter

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

ToolAdapter 把一个 MCP 工具包装成 flux/tool.Tool,使其能注册进 tool.Registry, 让 planner 像调本地工具一样调它。

名字双轨:name 是注册/展示给 LLM 的名字(可带前缀防撞),serverName 是调 server 用的原名。

func (*ToolAdapter) Definition

func (a *ToolAdapter) Definition() tool.ToolDefinition

Definition 直供 MCP 原生定义(JSON Schema 原样)。这是阶段 C 把旧 RawInputSchema 旁路 转正后的正式出口:DefinitionOf 见到 DefinedTool 即直接采用,不走 DataSchema 合成。

func (*ToolAdapter) Description

func (a *ToolAdapter) Description() string

func (*ToolAdapter) Execute

func (a *ToolAdapter) Execute(ctx context.Context, input map[string]any, _ tool.ToolEmitter) (*tool.Result, error)

Execute 转译到 MCP tools/call。

关键语义(与本地 compile 工具一致):MCP 工具内部错误(CallToolResult.IsError) 是给 planner 看的**反馈**,不是 run 终结错误——所以 Success 仍为 true,把 content+isError 放进 Data。只有传输/协议层失败才返回 Go error(真基础设施故障)。

TODO(主线二 C):MCP content 是 text/image/resource 块数组,这里压成 {content:text,isError} 是有损的;多模态/资源块需要在 C 阶段做完整 content 映射。

func (*ToolAdapter) InputSchema

func (a *ToolAdapter) InputSchema() tool.DataSchema

InputSchema 把 MCP 的 JSON Schema 有损压成 DataSchema(仅为满足 tool.Tool 接口 / 兼容仍读 DataSchema 的旧消费者)。需要无损定义的消费者请用 tool.DefinitionOf。

func (*ToolAdapter) Mode

func (a *ToolAdapter) Mode() tool.ExecutionMode

func (*ToolAdapter) Name

func (a *ToolAdapter) Name() string

func (*ToolAdapter) OutputSchema

func (a *ToolAdapter) OutputSchema() tool.DataSchema

OutputSchema:MCP tools/list 不带 output schema,返回空。

type ToolInfo

type ToolInfo struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	InputSchema json.RawMessage `json:"inputSchema"`
}

ToolInfo 是 server 暴露的一个工具的元数据。 InputSchema 原样保留 JSON Schema —— 这正是要喂给 LLM 的形状(MCP 自带,无需翻译)。

type Transport

type Transport interface {
	Call(ctx context.Context, method string, params any) (json.RawMessage, error)
	Notify(ctx context.Context, method string, params any) error
	Close() error
}

Transport 是 JSON-RPC 消息的承载层。Client 只依赖这个接口; 现已实现 stdio,HTTP/SSE 将来加一个实现即可,Client 不动(预留位)。

Jump to

Keyboard shortcuts

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