feat(mcp): add SSE transport support and related configuration options
Some checks failed
CI / build-and-test (push) Failing after -30m37s

This commit is contained in:
2026-04-05 15:57:34 +02:00
parent 1958eaca01
commit 5f48a197e8
10 changed files with 276 additions and 6 deletions

View File

@@ -46,7 +46,28 @@ type ToolSet struct {
Describe *tools.DescribeTool
}
// Handlers groups the HTTP handlers produced for an MCP server instance.
type Handlers struct {
// StreamableHTTP is the primary MCP handler (always non-nil).
StreamableHTTP http.Handler
// SSE is the SSE transport handler; nil when SSEPath is empty.
// SSE is the de facto transport for MCP over the internet and is required by most hosted MCP clients.
SSE http.Handler
}
// New builds the StreamableHTTP MCP handler. It is a convenience wrapper
// around NewHandlers for callers that only need the primary transport.
func New(cfg config.MCPConfig, logger *slog.Logger, toolSet ToolSet, onSessionClosed func(string)) (http.Handler, error) {
h, err := NewHandlers(cfg, logger, toolSet, onSessionClosed)
if err != nil {
return nil, err
}
return h.StreamableHTTP, nil
}
// NewHandlers builds MCP HTTP handlers for both transports.
// SSE is nil when cfg.SSEPath is empty.
func NewHandlers(cfg config.MCPConfig, logger *slog.Logger, toolSet ToolSet, onSessionClosed func(string)) (Handlers, error) {
instructions := cfg.Instructions
if instructions == "" {
instructions = string(amcsllm.MemoryInstructions)
@@ -77,7 +98,7 @@ func New(cfg config.MCPConfig, logger *slog.Logger, toolSet ToolSet, onSessionCl
registerDescribeTools,
} {
if err := register(server, logger, toolSet); err != nil {
return nil, err
return Handlers{}, err
}
}
@@ -89,9 +110,19 @@ func New(cfg config.MCPConfig, logger *slog.Logger, toolSet ToolSet, onSessionCl
opts.EventStore = newCleanupEventStore(mcp.NewMemoryEventStore(nil), onSessionClosed)
}
return mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
return server
}, opts), nil
h := Handlers{
StreamableHTTP: mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
return server
}, opts),
}
if strings.TrimSpace(cfg.SSEPath) != "" {
h.SSE = mcp.NewSSEHandler(func(*http.Request) *mcp.Server {
return server
}, nil)
}
return h, nil
}
// buildServerIcons returns icon definitions referencing the server's own /images/icon.png endpoint.