Documentation
¶
Overview ¶
Package handler provides a configurable MCP handler implementation.
It wires protocol handlers from the github.com/eberle1080/mcp-protocol/handler package with optional middleware such as:
- Transport (HTTP, HTTP-SSE, Streaming, STDIO)
- OAuth2 / OIDC authorization
- CORS handling
- Structured logging
Callers typically construct a handler via `server.New` and then expose it over HTTP or stdio:
s, _ := handler.New(handler.WithNewHandler(myImpl)) log.Fatal(s.HTTP(ctx, ":4981").ListenAndServe())
Index ¶
- Constants
- func ChainMiddlewareHandlers(h http.Handler, mws ...Middleware) http.Handler
- func ToolArgumentsJSON(ctx context.Context, request *schema.CallToolRequest) (json.RawMessage, bool)
- type Adapter
- func (a *Adapter) CallTool(ctx context.Context, params *schema.CallToolRequestParams, ...) (*schema.CallToolResult, error)
- func (a *Adapter) Complete(ctx context.Context, params *schema.CompleteRequestParams, ...) (*schema.CompleteResult, error)
- func (a *Adapter) CreateMessage(ctx context.Context, params *schema.CreateMessageRequestParams, ...) (*schema.CreateMessageResult, error)
- func (a *Adapter) Discover(ctx context.Context, options ...client.RequestOption) (*schema.DiscoverResult, error)
- func (a *Adapter) Elicit(ctx context.Context, params *schema.ElicitRequestParams, ...) (*schema.ElicitResult, error)
- func (a *Adapter) GetPrompt(ctx context.Context, params *schema.GetPromptRequestParams, ...) (*schema.GetPromptResult, error)
- func (a *Adapter) GetSkill(ctx context.Context, uri string, options ...client.RequestOption) (*schema.GetSkillResult, error)
- func (a *Adapter) Initialize(ctx context.Context, options ...client.RequestOption) (*schema.InitializeResult, error)
- func (a *Adapter) ListPrompts(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListPromptsResult, error)
- func (a *Adapter) ListResourceTemplates(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListResourceTemplatesResult, error)
- func (a *Adapter) ListResources(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListResourcesResult, error)
- func (a *Adapter) ListRoots(ctx context.Context, params *schema.ListRootsRequestParams, ...) (*schema.ListRootsResult, error)
- func (a *Adapter) ListSkills(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListSkillsResult, error)
- func (a *Adapter) ListTools(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListToolsResult, error)
- func (a *Adapter) Ping(ctx context.Context, params *schema.PingRequestParams, ...) (*schema.PingResult, error)
- func (a *Adapter) ReadResource(ctx context.Context, params *schema.ReadResourceRequestParams, ...) (*schema.ReadResourceResult, error)
- func (a *Adapter) ReadResourceDirectory(ctx context.Context, uri string, cursor *string, ...) (*schema.ReadResourceDirectoryResult, error)
- func (a *Adapter) SetLevel(ctx context.Context, params *schema.SetLevelRequestParams, ...) (*schema.SetLevelResult, error)
- func (a *Adapter) Subscribe(ctx context.Context, params *schema.SubscribeRequestParams, ...) (*schema.SubscribeResult, error)
- func (a *Adapter) Unsubscribe(ctx context.Context, params *schema.UnsubscribeRequestParams, ...) (*schema.UnsubscribeResult, error)
- type Client
- func (c *Client) CreateMessage(ctx context.Context, ...) (*schema.CreateMessageResult, *jsonrpc.Error)
- func (c *Client) Elicit(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ElicitRequest]) (*schema.ElicitResult, *jsonrpc.Error)
- func (c *Client) Implements(method string) bool
- func (c *Client) Init(ctx context.Context, capabilities *schema.ClientCapabilities)
- func (c *Client) ListRoots(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListRootsRequest]) (*schema.ListRootsResult, *jsonrpc.Error)
- func (c *Client) NextRequestId() uint64
- type Cors
- type Handler
- func (h *Handler) CallTool(ctx context.Context, request *jsonrpc.Request) (*schema.CallToolResult, *jsonrpc.Error)
- func (h *Handler) Cancel(ctx context.Context, notification *jsonrpc.Notification) *jsonrpc.Error
- func (h *Handler) CancelOperation(id int)
- func (h *Handler) CancelTask(ctx context.Context, request *jsonrpc.Request) (*tasks.AckResult, *jsonrpc.Error)
- func (h *Handler) Close()
- func (h *Handler) Complete(ctx context.Context, request *jsonrpc.Request) (*schema.CompleteResult, *jsonrpc.Error)
- func (h *Handler) Discover(ctx context.Context) (*schema.DiscoverResult, *jsonrpc.Error)
- func (h *Handler) GetPrompt(ctx context.Context, request *jsonrpc.Request) (*schema.GetPromptResult, *jsonrpc.Error)
- func (h *Handler) GetSkill(ctx context.Context, request *jsonrpc.Request) (*schema.GetSkillResult, *jsonrpc.Error)
- func (h *Handler) GetTask(ctx context.Context, request *jsonrpc.Request) (*tasks.GetTaskResult, *jsonrpc.Error)
- func (h *Handler) Initialize(ctx context.Context, request *jsonrpc.Request) (*schema.InitializeResult, *jsonrpc.Error)
- func (h *Handler) ListPrompts(ctx context.Context, request *jsonrpc.Request) (*schema.ListPromptsResult, *jsonrpc.Error)
- func (h *Handler) ListResourceTemplates(ctx context.Context, request *jsonrpc.Request) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
- func (h *Handler) ListResources(ctx context.Context, request *jsonrpc.Request) (*schema.ListResourcesResult, *jsonrpc.Error)
- func (h *Handler) ListSkills(ctx context.Context, request *jsonrpc.Request) (*schema.ListSkillsResult, *jsonrpc.Error)
- func (h *Handler) ListTools(ctx context.Context, request *jsonrpc.Request) (*schema.ListToolsResult, *jsonrpc.Error)
- func (h *Handler) Listen(ctx context.Context, request *jsonrpc.Request) (*schema.SubscriptionsListenResult, *jsonrpc.Error)
- func (h *Handler) OnNotification(ctx context.Context, notification *jsonrpc.Notification)
- func (h *Handler) Ping(ctx context.Context, request *jsonrpc.Request) (*schema.PingResult, *jsonrpc.Error)
- func (h *Handler) ReadResource(ctx context.Context, request *jsonrpc.Request) (*schema.ReadResourceResult, *jsonrpc.Error)
- func (h *Handler) ReadResourceDirectory(ctx context.Context, request *jsonrpc.Request) (*schema.ReadResourceDirectoryResult, *jsonrpc.Error)
- func (h *Handler) Serve(parent context.Context, request *jsonrpc.Request, response *jsonrpc.Response)
- func (h *Handler) SetLevel(ctx context.Context, request *jsonrpc.Request) (*schema.SetLevelResult, *jsonrpc.Error)
- func (h *Handler) Subscribe(ctx context.Context, request *jsonrpc.Request) (*schema.SubscribeResult, *jsonrpc.Error)
- func (h *Handler) Unsubscribe(ctx context.Context, request *jsonrpc.Request) (*schema.UnsubscribeResult, *jsonrpc.Error)
- func (h *Handler) UpdateTask(ctx context.Context, request *jsonrpc.Request) (*tasks.AckResult, *jsonrpc.Error)
- type Logger
- func (l *Logger) Alert(ctx context.Context, data interface{}) error
- func (l *Logger) Critical(ctx context.Context, data interface{}) error
- func (l *Logger) Debug(ctx context.Context, data interface{}) error
- func (l *Logger) Emergency(ctx context.Context, data interface{}) error
- func (l *Logger) Error(ctx context.Context, data interface{}) error
- func (l *Logger) Info(ctx context.Context, data interface{}) error
- func (l *Logger) Logger(name string) logger.Logger
- func (l *Logger) Notice(ctx context.Context, data interface{}) error
- func (l *Logger) Warning(ctx context.Context, data interface{}) error
- type Middleware
- type Option
- func WithAuthorizer(authorizer Middleware) Option
- func WithBFFAuthStore(store streamauth.Store) Option
- func WithCORS(cors *Cors) Option
- func WithCustomHTTPHandler(path string, handler http.HandlerFunc) Option
- func WithEndpointAddress(addr string) Option
- func WithImplementation(implementation schema.Implementation) Option
- func WithInstructions(instructions string) Option
- func WithJRPCAuthorizer(authorizer auth.JRPCAuthorizer) Option
- func WithLoggerName(name string) Option
- func WithMaxRequestBodyBytes(limit int64) Option
- func WithNewHandler(newHandler server.NewHandler) Option
- func WithOnSessionClose(closed func(transport.Notifier)) Option
- func WithProtectedResourcesHandler(handler http.HandlerFunc) Option
- func WithProtocolVersion(version string) Option
- func WithRequestContext(prepare func(context.Context) (context.Context, error)) Option
- func WithRootRedirect(enable bool) Option
- func WithSSEMessageURI(uri string) Option
- func WithSSEURI(uri string) Option
- func WithSessionStore(store base.SessionStore) Option
- func WithStreamableURI(uri string) Option
- func WithTasks(options ...TaskOption) Option
- func WithToolProtocolErrors() Option
- type ProtocolRequest
- type Server
- func (s *Server) AsClient(ctx context.Context) client.Interface
- func (s *Server) CancelOperation(id int)
- func (s *Server) HTTP(_ context.Context, addr string) *http.Server
- func (s *Server) NewHandler(ctx context.Context, transport transport.Transport) transport.Handler
- func (s *Server) Stdio(ctx context.Context) *stdio.Server
- func (s *Server) UseStreamableHTTP(flag bool)
- type TaskOption
Constants ¶
const ( AllowOriginHeader = "Access-Control-Allow-Origin" AllowHeadersHeader = "Access-Control-Allow-Headers" AllowMethodsHeader = "Access-Control-Allow-Methods" AllControlRequestHeader = "Access-Control-Request-Method" AllowCredentialsHeader = "Access-Control-Allow-Credentials" ExposeHeadersHeader = "Access-Control-Expose-Headers" MaxAgeHeader = "Access-Control-Max-Age" Separator = ", " )
Variables ¶
This section is empty.
Functions ¶
func ChainMiddlewareHandlers ¶
func ChainMiddlewareHandlers(h http.Handler, mws ...Middleware) http.Handler
ChainMiddlewareHandlers chains multiple middleware handlers together
func ToolArgumentsJSON ¶
func ToolArgumentsJSON(ctx context.Context, request *schema.CallToolRequest) (json.RawMessage, bool)
ToolArgumentsJSON returns the original arguments JSON for this exact tool invocation after normal request decoding. Existing decoded Arguments and all authorization/dispatch behavior are unchanged. The returned bytes are owned by the caller. Omitted arguments return nil, true. Repeated arguments members and unrelated requests return nil, false.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter adapts a handler Handler to implement the client.Interface
func NewAdapter ¶
NewAdapter creates a new adapter for the given handler
func (*Adapter) CallTool ¶
func (a *Adapter) CallTool(ctx context.Context, params *schema.CallToolRequestParams, options ...client.RequestOption) (*schema.CallToolResult, error)
CallTool calls a tool
func (*Adapter) Complete ¶
func (a *Adapter) Complete(ctx context.Context, params *schema.CompleteRequestParams, options ...client.RequestOption) (*schema.CompleteResult, error)
Complete completes a request
func (*Adapter) CreateMessage ¶
func (a *Adapter) CreateMessage(ctx context.Context, params *schema.CreateMessageRequestParams, options ...client.RequestOption) (*schema.CreateMessageResult, error)
CreateMessage proxies "sampling/createMessage" to the underlying handler.
func (*Adapter) Discover ¶
func (a *Adapter) Discover(ctx context.Context, options ...client.RequestOption) (*schema.DiscoverResult, error)
Discover exposes the July stateless discovery operation for in-process clients.
func (*Adapter) Elicit ¶
func (a *Adapter) Elicit(ctx context.Context, params *schema.ElicitRequestParams, options ...client.RequestOption) (*schema.ElicitResult, error)
Elicit proxies "elicitation/create" to the underlying handler.
func (*Adapter) GetPrompt ¶
func (a *Adapter) GetPrompt(ctx context.Context, params *schema.GetPromptRequestParams, options ...client.RequestOption) (*schema.GetPromptResult, error)
GetPrompt gets a prompt
func (*Adapter) GetSkill ¶
func (a *Adapter) GetSkill(ctx context.Context, uri string, options ...client.RequestOption) (*schema.GetSkillResult, error)
func (*Adapter) Initialize ¶
func (a *Adapter) Initialize(ctx context.Context, options ...client.RequestOption) (*schema.InitializeResult, error)
Initialize initializes the client
func (*Adapter) ListPrompts ¶
func (a *Adapter) ListPrompts(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListPromptsResult, error)
ListPrompts lists prompts
func (*Adapter) ListResourceTemplates ¶
func (a *Adapter) ListResourceTemplates(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListResourceTemplatesResult, error)
ListResourceTemplates lists resource templates
func (*Adapter) ListResources ¶
func (a *Adapter) ListResources(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListResourcesResult, error)
ListResources lists resources
func (*Adapter) ListRoots ¶
func (a *Adapter) ListRoots(ctx context.Context, params *schema.ListRootsRequestParams, options ...client.RequestOption) (*schema.ListRootsResult, error)
ListRoots proxies "roots/list" to the underlying handler.
func (*Adapter) ListSkills ¶
func (a *Adapter) ListSkills(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListSkillsResult, error)
Native skills use the same request sequencing and handler dispatch as other in-process standard methods. Authentication remains in the request context.
func (*Adapter) ListTools ¶
func (a *Adapter) ListTools(ctx context.Context, cursor *string, options ...client.RequestOption) (*schema.ListToolsResult, error)
ListTools lists tools
func (*Adapter) Ping ¶
func (a *Adapter) Ping(ctx context.Context, params *schema.PingRequestParams, options ...client.RequestOption) (*schema.PingResult, error)
Ping pings the handler
func (*Adapter) ReadResource ¶
func (a *Adapter) ReadResource(ctx context.Context, params *schema.ReadResourceRequestParams, options ...client.RequestOption) (*schema.ReadResourceResult, error)
ReadResource reads a resource
func (*Adapter) ReadResourceDirectory ¶
func (a *Adapter) ReadResourceDirectory(ctx context.Context, uri string, cursor *string, options ...client.RequestOption) (*schema.ReadResourceDirectoryResult, error)
func (*Adapter) SetLevel ¶
func (a *Adapter) SetLevel(ctx context.Context, params *schema.SetLevelRequestParams, options ...client.RequestOption) (*schema.SetLevelResult, error)
SetLevel sets the logging level
func (*Adapter) Subscribe ¶
func (a *Adapter) Subscribe(ctx context.Context, params *schema.SubscribeRequestParams, options ...client.RequestOption) (*schema.SubscribeResult, error)
Subscribe subscribes to a resource
func (*Adapter) Unsubscribe ¶
func (a *Adapter) Unsubscribe(ctx context.Context, params *schema.UnsubscribeRequestParams, options ...client.RequestOption) (*schema.UnsubscribeResult, error)
Unsubscribe unsubscribes from a resource
type Client ¶
type Client struct {
transport.Transport
transport.Sequencer
// contains filtered or unexported fields
}
Client implements mcp-protocol/client.Operations for the handler side. It allows handler implementers to invoke client-side RPC methods over the same transport channel on which the original request arrived.
func (*Client) CreateMessage ¶
func (c *Client) CreateMessage(ctx context.Context, request *jsonrpc.TypedRequest[*schema.CreateMessageRequest]) (*schema.CreateMessageResult, *jsonrpc.Error)
CreateMessage creates a sampling message on the client side.
func (*Client) Elicit ¶
func (c *Client) Elicit(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ElicitRequest]) (*schema.ElicitResult, *jsonrpc.Error)
Elicit asks the client to elicit additional information from the user.
func (*Client) Implements ¶
func (*Client) Init ¶
func (c *Client) Init(ctx context.Context, capabilities *schema.ClientCapabilities)
func (*Client) ListRoots ¶
func (c *Client) ListRoots(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListRootsRequest]) (*schema.ListRootsResult, *jsonrpc.Error)
func (*Client) NextRequestId ¶
type Cors ¶
type Cors struct {
AllowCredentials *bool `yaml:"AllowCredentials,omitempty"`
AllowHeaders []string `yaml:"AllowHeaders,omitempty"`
AllowMethods []string `yaml:"AllowMethods,omitempty"`
AllowOrigins []string `yaml:"AllowOrigins,omitempty"`
ExposeHeaders []string `yaml:"ExposeHeaders,omitempty"`
MaxAge *int64 `yaml:"MaxAge,omitempty"`
}
type Handler ¶
type Handler struct {
transport.Notifier
*Logger
*Server
Initialized bool
// contains filtered or unexported fields
}
Handler represents handler
func (*Handler) CallTool ¶
func (h *Handler) CallTool(ctx context.Context, request *jsonrpc.Request) (*schema.CallToolResult, *jsonrpc.Error)
CallTool handles the tools/call method
func (*Handler) CancelOperation ¶
CancelOperation cancels only an operation received by this handler. A client cancellation notification cannot cancel another client's equal request ID.
func (*Handler) CancelTask ¶
func (h *Handler) CancelTask(ctx context.Context, request *jsonrpc.Request) (*tasks.AckResult, *jsonrpc.Error)
CancelTask handles tasks/cancel.
func (*Handler) Close ¶
func (h *Handler) Close()
Close ends this handler's owning transport session once. HTTP uses its native final-session hook; in-process adapters may explicitly close their handler.
func (*Handler) Complete ¶
func (h *Handler) Complete(ctx context.Context, request *jsonrpc.Request) (*schema.CompleteResult, *jsonrpc.Error)
Complete handles the completion/complete method
func (*Handler) GetPrompt ¶
func (h *Handler) GetPrompt(ctx context.Context, request *jsonrpc.Request) (*schema.GetPromptResult, *jsonrpc.Error)
GetPrompt handles the prompts/get method
func (*Handler) GetTask ¶
func (h *Handler) GetTask(ctx context.Context, request *jsonrpc.Request) (*tasks.GetTaskResult, *jsonrpc.Error)
GetTask handles tasks/get.
func (*Handler) Initialize ¶
func (h *Handler) Initialize(ctx context.Context, request *jsonrpc.Request) (*schema.InitializeResult, *jsonrpc.Error)
Initialize handles the legacy initialize method. July clients use Discover.
func (*Handler) ListPrompts ¶
func (h *Handler) ListPrompts(ctx context.Context, request *jsonrpc.Request) (*schema.ListPromptsResult, *jsonrpc.Error)
ListPrompts handles the prompts/list method
func (*Handler) ListResourceTemplates ¶
func (h *Handler) ListResourceTemplates(ctx context.Context, request *jsonrpc.Request) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
ListResourceTemplates handles the resources/templates/list method
func (*Handler) ListResources ¶
func (h *Handler) ListResources(ctx context.Context, request *jsonrpc.Request) (*schema.ListResourcesResult, *jsonrpc.Error)
ListResources handles the resources/list method
func (*Handler) ListSkills ¶
func (*Handler) ListTools ¶
func (h *Handler) ListTools(ctx context.Context, request *jsonrpc.Request) (*schema.ListToolsResult, *jsonrpc.Error)
ListTools handles the tools/list method
func (*Handler) Listen ¶
func (h *Handler) Listen(ctx context.Context, request *jsonrpc.Request) (*schema.SubscriptionsListenResult, *jsonrpc.Error)
Listen establishes the July notification stream, acknowledges the accepted filter first, and remains active until its request context is cancelled.
func (*Handler) OnNotification ¶
func (h *Handler) OnNotification(ctx context.Context, notification *jsonrpc.Notification)
OnNotification handles incoming JSON-RPC notifications
func (*Handler) Ping ¶
func (h *Handler) Ping(ctx context.Context, request *jsonrpc.Request) (*schema.PingResult, *jsonrpc.Error)
Ping handles the ping method
func (*Handler) ReadResource ¶
func (h *Handler) ReadResource(ctx context.Context, request *jsonrpc.Request) (*schema.ReadResourceResult, *jsonrpc.Error)
ReadResource handles the resources/read method
func (*Handler) ReadResourceDirectory ¶
func (h *Handler) ReadResourceDirectory(ctx context.Context, request *jsonrpc.Request) (*schema.ReadResourceDirectoryResult, *jsonrpc.Error)
ReadResourceDirectory dispatches SEP-2640's resources/directory/read to a handler that implements protocol.ResourceDirectories.
func (*Handler) Serve ¶
func (h *Handler) Serve(parent context.Context, request *jsonrpc.Request, response *jsonrpc.Response)
Serve handles incoming JSON-RPC requests
func (*Handler) SetLevel ¶
func (h *Handler) SetLevel(ctx context.Context, request *jsonrpc.Request) (*schema.SetLevelResult, *jsonrpc.Error)
SetLevel handles the logging/setLevel method
func (*Handler) Subscribe ¶
func (h *Handler) Subscribe(ctx context.Context, request *jsonrpc.Request) (*schema.SubscribeResult, *jsonrpc.Error)
Subscribe handles the resources/subscribe method
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
type Middleware ¶
Middleware is a function that takes an http.Handler and returns an http.Handler
type Option ¶
Option is a function that configures the handler.
func WithAuthorizer ¶
func WithAuthorizer(authorizer Middleware) Option
WithAuthorizer adds a new authorizer to the handler.
func WithBFFAuthStore ¶
func WithBFFAuthStore(store streamauth.Store) Option
WithBFFAuthStore binds an auth grant store to this HTTP listener. Its request context also binds the same store to the listener's auth.Service.
func WithCustomHTTPHandler ¶
func WithCustomHTTPHandler(path string, handler http.HandlerFunc) Option
WithCustomHTTPHandler adds a custom handler to the handler.
func WithEndpointAddress ¶
WithEndpointAddress sets the protocol version.
func WithImplementation ¶
func WithImplementation(implementation schema.Implementation) Option
WithImplementation sets the handler implementation.
func WithInstructions ¶
WithInstructions adds instructions to the handler.
func WithJRPCAuthorizer ¶
func WithJRPCAuthorizer(authorizer auth.JRPCAuthorizer) Option
WithJRPCAuthorizer adds a new JRPCAuthorizer to the handler.
func WithMaxRequestBodyBytes ¶
WithMaxRequestBodyBytes selects an explicit bounded HTTP request limit. The default remains 4 MiB. This never disables request-size enforcement.
func WithNewHandler ¶
func WithNewHandler(newHandler server.NewHandler) Option
WithNewHandler sets the new handler.
func WithOnSessionClose ¶
WithOnSessionClose observes final transport-session removal. A temporary streaming disconnect remains resumable and does not close the session.
func WithProtectedResourcesHandler ¶
func WithProtectedResourcesHandler(handler http.HandlerFunc) Option
func WithProtocolVersion ¶
WithProtocolVersion sets the protocol version for the handler.
func WithRequestContext ¶
WithRequestContext prepares an invocation context before method lookup, authorization and dispatch. It applies to every transport, including stdio.
func WithRootRedirect ¶
WithRootRedirect enables redirect from "/" to the active transport base URI.
func WithSSEMessageURI ¶
WithSSEMessageURI sets the message POST URI for SSE transport (default "/message").
func WithSSEURI ¶
WithSSEURI sets the base URI for the SSE endpoint (default "/sse").
func WithSessionStore ¶
func WithSessionStore(store base.SessionStore) Option
WithSessionStore sets a custom session store for both SSE and Streamable handlers. This allows using Redis or other persistent backends instead of in-memory storage.
func WithStreamableURI ¶
WithStreamableURI sets the base URI for the streamable HTTP endpoint (default "/mcp").
func WithTasks ¶
func WithTasks(options ...TaskOption) Option
WithTasks enables the Tasks extension (io.modelcontextprotocol/tasks, MCP 2026-07-28). The server declares it in server/discover and serves tasks/get, tasks/update and tasks/cancel, and a tool may answer a tools/call with a task by calling tasks.Start from the mcp-protocol tasks package. Tasks live in this process's memory: they do not survive a restart, and a deployment with several instances must route requests by their Mcp-Name header (the task ID).
func WithToolProtocolErrors ¶
func WithToolProtocolErrors() Option
WithToolProtocolErrors preserves JSON-RPC errors returned by tools/call. The default converts those errors into CallToolResult values for compatibility.
type ProtocolRequest ¶
type ProtocolRequest struct {
Version string
Capabilities schema.ClientCapabilities
ClientInfo *schema.Implementation
LogLevel *schema.LoggingLevel
}
ProtocolRequest describes the immutable July metadata attached to one MCP request. It must not be retained or reused for a later request.
func ProtocolRequestFromContext ¶
func ProtocolRequestFromContext(ctx context.Context) (*ProtocolRequest, bool)
ProtocolRequestFromContext returns the current request's July metadata.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents MCP protocol handler
func (*Server) AsClient ¶
AsClient returns a client.Interface implementation that uses this handler directly
func (*Server) CancelOperation ¶
func (*Server) HTTP ¶
HTTP creates and returns an HTTP handler with OAuth2 authorizer and SSE handlers.
func (*Server) NewHandler ¶
NewHandler creates a new handler instance
func (*Server) UseStreamableHTTP ¶
UseStreamableHTTP sets whether to use streamableHTTP or SSE for the HTTP handler.
type TaskOption ¶
type TaskOption func(*taskManager)
TaskOption configures the task runtime enabled by WithTasks.
func WithMaxTasks ¶
func WithMaxTasks(limit int) TaskOption
WithMaxTasks caps how many tasks the server keeps at once (default 10,000). At the cap, tasks.Start returns an error and the tool decides what to do.
func WithMaxTasksPerCaller ¶
func WithMaxTasksPerCaller(limit int) TaskOption
WithMaxTasksPerCaller caps how many tasks one caller (see WithTaskOwner) holds at once (default 1,000), so one caller cannot fill the store for everyone. Without authentication every caller is the same one.
func WithTaskOwner ¶
func WithTaskOwner(owner func(ctx context.Context) string) TaskOption
WithTaskOwner sets how a request's caller is identified. Every task is bound to the caller that created it, and is "not found" for any other. The default is a digest of the request's bearer token, which binds a task to the exact token; a server whose tokens are refreshed during a task should identify the caller by a stable subject instead. Without a token every caller is the same one, and a task's unguessable ID is all that protects it.
func WithTaskPollInterval ¶
func WithTaskPollInterval(interval time.Duration) TaskOption
WithTaskPollInterval sets the poll interval suggested to clients (default one second).
func WithTaskTTL ¶
func WithTaskTTL(ttl time.Duration) TaskOption
WithTaskTTL sets how long after creation a task is kept (default one hour). After it the task is discarded, and stopped if it is still running.
Source Files
¶
- adapter.go
- adapter_result.go
- adapter_skills.go
- cancellation.go
- client.go
- completion.go
- context.go
- cors.go
- doc.go
- handler.go
- handler_operations.go
- http.go
- initialize.go
- logger.go
- logging.go
- middleware.go
- option.go
- origin.go
- params.go
- prompts.go
- protocol.go
- protocol_runtime.go
- request_context.go
- resources.go
- server.go
- skills.go
- stdio.go
- subscription_notifications.go
- subscriptions.go
- tasks.go
- tasks_handler.go
- tool_arguments.go
- tools.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package auth exposes helpers that make it easy to protect an MCP server with OAuth2/OIDC.
|
Package auth exposes helpers that make it easy to protect an MCP server with OAuth2/OIDC. |
|
Package namespace provides a reusable, policy-aware namespace resolution utility for MCP servers and services.
|
Package namespace provides a reusable, policy-aware namespace resolution utility for MCP servers and services. |
|
Package oob provides minimal, generic primitives to manage out-of-band (OOB) interactions using typed pending entries bound to a namespace.
|
Package oob provides minimal, generic primitives to manage out-of-band (OOB) interactions using typed pending entries bound to a namespace. |