mcp

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthHeaderFromContext

func AuthHeaderFromContext(ctx context.Context) string

AuthHeaderFromContext 从 context 中取出 Authorization header 值。 若 context 中不存在则返回空字符串。

func ContextWithAuthHeader

func ContextWithAuthHeader(parent context.Context, authHeader string) context.Context

ContextWithAuthHeader 返回一个携带 Authorization header 值的子 context。 调用方通常是 HTTP 中间件,把客户端原始 Authorization 注入 context, 供下游的 MCP tool handler 透传到 outgoing HTTP 请求(如 AutoBridge 调用 gateway)。

func LoggingMiddleware

func LoggingMiddleware(next http.Handler) http.Handler

LoggingMiddleware 是简易的 HTTP 请求日志中间件, 记录 method、path、status 和 duration。

func NewAuthMiddleware

func NewAuthMiddleware(authFn AuthFunc, next http.Handler) http.Handler

NewAuthMiddleware 创建 HTTP 认证中间件。 若 authFn 为 nil 则直接放行(无认证模式); 否则在调用下游 handler 前执行认证,失败时返回 401 JSON 错误。

无论 authFn 是否 nil,本中间件都会通过 r.WithContext 把原始 Authorization header 注入到请求 context,使下游 AutoBridge tool handler 能透传给 gateway。 这样即便 SecurityConfig.Enable=false(authFn=nil),客户端携带的 Authorization 仍可被透传到 gateway,保证审计与鉴权链路完整。

Types

type AuthFunc

type AuthFunc func(r *http.Request) error

AuthFunc 是 MCP 认证回调函数签名。 验证成功返回 nil,失败返回 error。 pkg/cfg.SecurityConfig.VerifyHTTPRequest 满足此签名。

type Server

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

Server 封装 MCP Server 及其 HTTP handler

func NewServer

func NewServer(enable bool, transport string) (*Server, error)

NewServer 根据 MCP 配置参数创建 MCP Server 若 enable 为 false,返回 nil, nil

func (*Server) Close

func (s *Server) Close() error

Close 优雅关闭 MCP Server 的所有活跃 sessions 采用 best-effort 策略:遍历所有活跃 session 逐个关闭,收集第一个错误但继续关闭剩余 session。 SDK handler 无导出的 Close 方法,关闭 session 后 HTTP 连接会自然终止。

func (*Server) Handler

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

Handler 返回 MCP Server 的 HTTP handler

func (*Server) MCPServer

func (s *Server) MCPServer() *mcp.Server

MCPServer 返回底层 MCP Server 实例,供后续注册 Tools 使用

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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