Documentation
¶
Index ¶
- type CallToolRequest
- type Connection
- type GetPromptRequest
- type PromptHandler
- type ReadResourceRequest
- type ResourceHandler
- type Server
- func (s *Server) AddPrompt(p *protocol.Prompt, h PromptHandler)
- func (s *Server) AddResource(r *protocol.Resource, h ResourceHandler)
- func (s *Server) AddResourceTemplate(t *protocol.ResourceTemplate, h ResourceHandler)
- func (s *Server) AddTool(t *protocol.Tool, h ToolHandler)
- func (s *Server) Connect(ctx context.Context, t transport.Transport, opts *ServerSessionOptions) (*ServerSession, error)
- func (s *Server) HandleMessage(ctx context.Context, msg *protocol.JSONRPCMessage) (*protocol.JSONRPCMessage, error)
- func (s *Server) RemovePrompt(name string)
- func (s *Server) RemoveResource(uri string)
- func (s *Server) RemoveResourceTemplate(uriTemplate string)
- func (s *Server) RemoveTool(name string)
- func (s *Server) Run(ctx context.Context, t transport.Transport) error
- type ServerOptions
- type ServerSession
- func (ss *ServerSession) Close() error
- func (ss *ServerSession) CreateMessage(ctx context.Context, params *protocol.CreateMessageParams) (*protocol.CreateMessageResult, error)
- func (ss *ServerSession) Elicit(ctx context.Context, params *protocol.ElicitationCreateParams) (*protocol.ElicitationResult, error)
- func (ss *ServerSession) ID() string
- func (ss *ServerSession) InitializeParams() *protocol.InitializeParams
- func (ss *ServerSession) ListRoots(ctx context.Context) (*protocol.ListRootsResult, error)
- func (ss *ServerSession) Log(ctx context.Context, params *protocol.LoggingMessageParams) error
- func (ss *ServerSession) NotifyProgress(ctx context.Context, params *protocol.ProgressNotificationParams) error
- func (ss *ServerSession) Ping(ctx context.Context) error
- func (ss *ServerSession) Wait() error
- type ServerSessionOptions
- type ServerSessionState
- type ToolHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallToolRequest ¶ added in v1.2.0
type CallToolRequest struct {
// Session 当前会话
Session *ServerSession
// Params 原始参数
Params *protocol.CallToolParams
}
CallToolRequest 表示工具调用请求,允许工具处理函数发送通知
type Connection ¶ added in v1.2.0
type Connection interface {
// SendNotification 发送通知到客户端
SendNotification(ctx context.Context, method string, params interface{}) error
// SendRequest 发送请求到客户端并等待响应
SendRequest(ctx context.Context, method string, params interface{}, result interface{}) error
Close() error
SessionID() string
}
Connection 表示底层传输连接
type GetPromptRequest ¶ added in v1.2.0
type GetPromptRequest struct {
Session *ServerSession
Params *protocol.GetPromptParams
}
type PromptHandler ¶
type PromptHandler func(ctx context.Context, req *GetPromptRequest) (*protocol.GetPromptResult, error)
type ReadResourceRequest ¶ added in v1.2.0
type ReadResourceRequest struct {
Session *ServerSession
Params *protocol.ReadResourceParams
}
type ResourceHandler ¶
type ResourceHandler func(ctx context.Context, req *ReadResourceRequest) (*protocol.ReadResourceResult, error)
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server MCP服务器实例, 可以服务一个或多个 MCP 会话
func NewServer ¶
func NewServer(impl *protocol.ServerInfo, opts *ServerOptions) *Server
func (*Server) AddPrompt ¶ added in v1.2.0
func (s *Server) AddPrompt(p *protocol.Prompt, h PromptHandler)
func (*Server) AddResource ¶ added in v1.2.0
func (s *Server) AddResource(r *protocol.Resource, h ResourceHandler)
func (*Server) AddResourceTemplate ¶ added in v1.2.0
func (s *Server) AddResourceTemplate(t *protocol.ResourceTemplate, h ResourceHandler)
func (*Server) Connect ¶ added in v1.2.0
func (s *Server) Connect(ctx context.Context, t transport.Transport, opts *ServerSessionOptions) (*ServerSession, error)
Connect 通过给定的 transport 连接 MCP 服务器并开始处理消息
它返回一个连接对象,可用于终止连接(使用 Close)或等待客户端终止(使用 Wait)
func (*Server) HandleMessage ¶
func (s *Server) HandleMessage(ctx context.Context, msg *protocol.JSONRPCMessage) (*protocol.JSONRPCMessage, error)
HandleMessage 实现 SSE Handler 接口 (用于向后兼容)
func (*Server) RemovePrompt ¶ added in v1.2.0
func (*Server) RemoveResource ¶ added in v1.2.0
func (*Server) RemoveResourceTemplate ¶ added in v1.2.0
func (*Server) RemoveTool ¶ added in v1.2.0
type ServerOptions ¶ added in v1.2.0
type ServerOptions struct {
// 可选的客户端指令
Instructions string
// 初始化处理函数
InitializedHandler func(context.Context, *ServerSession)
// 进度通知处理函数
ProgressNotificationHandler func(context.Context, *ServerSession, *protocol.ProgressNotificationParams)
// 补全处理函数
CompletionHandler func(context.Context, *protocol.CompleteRequest) (*protocol.CompleteResult, error)
// 日志级别设置处理函数
LoggingSetLevelHandler func(context.Context, *ServerSession, protocol.LoggingLevel) error
// 资源订阅/取消订阅处理函数
SubscribeHandler func(context.Context, *protocol.SubscribeParams) error
UnsubscribeHandler func(context.Context, *protocol.UnsubscribeParams) error
}
type ServerSession ¶ added in v1.2.0
type ServerSession struct {
// contains filtered or unexported fields
}
ServerSession 表示一个服务器会话, 每个客户端连接对应一个 ServerSession
func (*ServerSession) Close ¶ added in v1.2.0
func (ss *ServerSession) Close() error
func (*ServerSession) CreateMessage ¶ added in v1.2.0
func (ss *ServerSession) CreateMessage(ctx context.Context, params *protocol.CreateMessageParams) (*protocol.CreateMessageResult, error)
CreateMessage 发送采样请求到客户端
func (*ServerSession) Elicit ¶ added in v1.2.0
func (ss *ServerSession) Elicit(ctx context.Context, params *protocol.ElicitationCreateParams) (*protocol.ElicitationResult, error)
Elicit 发送 elicitation 请求到客户端,请求用户输入
func (*ServerSession) ID ¶ added in v1.2.0
func (ss *ServerSession) ID() string
func (*ServerSession) InitializeParams ¶ added in v1.2.0
func (ss *ServerSession) InitializeParams() *protocol.InitializeParams
InitializeParams 返回初始化参数
func (*ServerSession) ListRoots ¶ added in v1.2.0
func (ss *ServerSession) ListRoots(ctx context.Context) (*protocol.ListRootsResult, error)
ListRoots 列出客户端根目录
func (*ServerSession) Log ¶ added in v1.2.0
func (ss *ServerSession) Log(ctx context.Context, params *protocol.LoggingMessageParams) error
Log 发送日志消息到客户端
func (*ServerSession) NotifyProgress ¶ added in v1.2.0
func (ss *ServerSession) NotifyProgress(ctx context.Context, params *protocol.ProgressNotificationParams) error
NotifyProgress 发送进度通知到客户端
func (*ServerSession) Ping ¶ added in v1.2.0
func (ss *ServerSession) Ping(ctx context.Context) error
Ping 发送 ping 请求到客户端
func (*ServerSession) Wait ¶ added in v1.2.0
func (ss *ServerSession) Wait() error
Wait 等待会话结束, 返回会话结束的错误
type ServerSessionOptions ¶ added in v1.2.0
type ServerSessionOptions struct {
State *ServerSessionState
// contains filtered or unexported fields
}
type ServerSessionState ¶ added in v1.2.0
type ServerSessionState struct {
// InitializeParams 来自 initialize 请求的参数
InitializeParams *protocol.InitializeParams
// InitializedParams 来自 notifications/initialized 的参数
InitializedParams *protocol.InitializedParams
// LogLevel 日志级别
LogLevel protocol.LoggingLevel
}
ServerSessionState 会话状态
type ToolHandler ¶
type ToolHandler func(ctx context.Context, req *CallToolRequest) (*protocol.CallToolResult, error)
ToolHandler 工具处理函数 接收 CallToolRequest,可以通过 req.Session 发送通知
Click to show internal directories.
Click to hide internal directories.