Help us shape the future of Go! Take the 2026 Go Developer Survey

mcp

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package mcp implements the HTTP Streamable MCP (Model Context Protocol) server over JSON-RPC 2.0 with SSE streaming, allowing any AI agent (Claude, GPT, Cursor, etc.) to discover and call OtelContext tools natively.

Index

Constants

View Source
const (

	// ErrServerOverloaded is the JSON-RPC error code we surface when the
	// server-wide concurrency cap is exceeded. JSON-RPC reserves -32000
	// to -32099 for server errors; we pick a stable code in that band so
	// agent clients can detect-and-back-off deterministically.
	ErrServerOverloaded = -32000
	// ErrCallTimeout is the JSON-RPC error code returned when a tool
	// invocation runs past defaultCallTimeout.
	ErrCallTimeout = -32001
)
View Source
const (
	ErrParseError     = -32700
	ErrInvalidRequest = -32600
	ErrMethodNotFound = -32601
	ErrInvalidParams  = -32602
	ErrInternalError  = -32603
)

Standard JSON-RPC error codes.

View Source
const MaxToolResponseBytes = 4 * 1024 * 1024

MaxToolResponseBytes caps the rendered length of any tool response. Without this, large in-memory GraphRAG dumps can produce 100MB+ JSON on adversarial input, OOM the process, and stall every concurrent MCP call until MCP_CALL_TIMEOUT_MS fires.

The cap is intentionally set well above any legitimate row-capped tool response (search_logs at 200 rows is typically <1 MB) so it triggers only on pathological cases. Operators hitting it should narrow their query time range or use pagination.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientInfo

type ClientInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

type ContentItem

type ContentItem struct {
	Type     string    `json:"type"` // "text" | "resource"
	Text     string    `json:"text,omitempty"`
	Resource *Resource `json:"resource,omitempty"`
}

type InitializeParams

type InitializeParams struct {
	ProtocolVersion string         `json:"protocolVersion"`
	Capabilities    map[string]any `json:"capabilities"`
	ClientInfo      ClientInfo     `json:"clientInfo"`
}

type InitializeResult

type InitializeResult struct {
	ProtocolVersion string         `json:"protocolVersion"`
	Capabilities    map[string]any `json:"capabilities"`
	ServerInfo      ServerInfo     `json:"serverInfo"`
}

type InputSchema

type InputSchema struct {
	Type       string              `json:"type"`
	Properties map[string]Property `json:"properties,omitempty"`
	Required   []string            `json:"required,omitempty"`
}

type JSONRPCRequest

type JSONRPCRequest struct {
	JSONRPC string `json:"jsonrpc"`
	ID      any    `json:"id"` // string | number | null
	Method  string `json:"method"`
	Params  any    `json:"params,omitempty"`
}

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string    `json:"jsonrpc"`
	ID      any       `json:"id"`
	Result  any       `json:"result,omitempty"`
	Error   *RPCError `json:"error,omitempty"`
}

type LogSummaryQuerier added in v0.6.3

type LogSummaryQuerier interface {
	QueryLogSummary(context.Context, aggregate.Query) (*aggregate.LogSummaryResult, error)
}

type PromQLQuerier added in v0.6.2

type PromQLQuerier interface {
	Query(context.Context, url.Values, bool) (json.RawMessage, error)
}

PromQLQuerier is implemented by the tenant-bound query client. The raw envelope preserves result types, sample strings, warnings and backend errors.

type Property

type Property struct {
	Type        string `json:"type"`
	Description string `json:"description,omitempty"`
}

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

type Resource

type Resource struct {
	URI      string `json:"uri"`
	MimeType string `json:"mimeType"`
	Text     string `json:"text"`
}

type SSEEvent

type SSEEvent struct {
	Event string
	Data  string
}

SSE event envelope.

type Server

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

Server is the HTTP Streamable MCP server. POST /mcp — JSON-RPC 2.0 request/response GET /mcp — SSE stream for real-time notifications OPTIONS /mcp — CORS preflight

func New

func New(
	defaultTenant string,
	repo *storage.Repository,
	metrics *telemetry.Metrics,
	topologyProvider topology.Provider,
) *Server

New creates a new MCP server. defaultTenant is the fallback tenant applied to header-less MCP requests; an empty string falls back to storage.DefaultTenantID. Required at construction time so production startup cannot accidentally drop cfg.DefaultTenant — a missing argument is a compile error rather than a silent regression.

The vectordb-backed semantic similarity argument was removed on 2026-05-24 when find_similar_logs was cut from the MCP surface and the vectordb package was deleted.

func (*Server) Handler

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

Handler returns an http.Handler for the MCP server with CORS applied. Works correctly when mounted with http.StripPrefix.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches by HTTP method — no path routing needed.

func (*Server) SetAggregateMode added in v0.5.0

func (s *Server) SetAggregateMode(on bool)

SetAggregateMode makes tool results carry coverage body metadata. Call it with true only in AGGREGATE_MODE=aggregate.

func (*Server) SetCacheTTL

func (s *Server) SetCacheTTL(d time.Duration)

SetCacheTTL overrides the result-cache lifetime. <= 0 disables caching for the whitelisted GraphRAG tools.

func (*Server) SetCallLimit

func (s *Server) SetCallLimit(maxConcurrent int)

SetCallLimit configures the maximum number of concurrent tools/call invocations. <= 0 disables the cap (legacy behavior).

Startup-only: this swaps the underlying channel reference without quiescing in-flight callers. An already-running call will release into the OLD channel when it completes, leaving the NEW semaphore one slot short until process restart. Call exactly once during construction (main.go does); never from a request-handling goroutine.

func (*Server) SetCallTimeout

func (s *Server) SetCallTimeout(d time.Duration)

SetCallTimeout overrides the per-invocation deadline. A zero or negative value disables the timeout (handlers run until they return on their own).

func (*Server) SetDefaultTenant

func (s *Server) SetDefaultTenant(t string)

SetDefaultTenant overrides the fallback tenant at runtime. Empty strings are ignored so callers can pass through optional config without clobbering the constructor-provided value.

func (*Server) SetGraphRAG

func (s *Server) SetGraphRAG(g *graphrag.GraphRAG)

SetGraphRAG wires the GraphRAG instance for advanced query tools.

func (*Server) SetHistoryRetention added in v0.6.3

func (s *Server) SetHistoryRetention(d time.Duration)

SetHistoryRetention sets the aggregate investigation horizon at startup.

func (*Server) SetLogSummaryQuerier added in v0.6.3

func (s *Server) SetLogSummaryQuerier(q LogSummaryQuerier)

SetLogSummaryQuerier injects the aggregate engine at startup in aggregate mode.

func (*Server) SetPromQLQuerier added in v0.6.2

func (s *Server) SetPromQLQuerier(q PromQLQuerier, tenant string)

SetPromQLQuerier binds the client to its configured tenant at startup. Requests for any other tenant cannot reach that client.

func (*Server) SetRawRetention added in v0.6.3

func (s *Server) SetRawRetention(d time.Duration)

SetRawRetention sets the raw exemplar search horizon without changing global retention defaults. Call at startup with the effective raw retention.

func (*Server) SetTraceHorizon added in v0.6.3

func (s *Server) SetTraceHorizon(d time.Duration)

SetTraceHorizon sets the configured GraphRAG trace TTL used in coverage notes.

func (*Server) Stats

func (s *Server) Stats() Stats

Stats returns a snapshot of the server-wide counters. Safe to call from any goroutine; values are best-effort point-in-time.

type ServerInfo

type ServerInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

type Stats

type Stats struct {
	InFlight      int64
	CallsServiced int64
	CacheHits     int64
	Overloaded    int64
	TimedOut      int64
	CacheSize     int
}

Stats returns counters used by tests and observability.

type Tool

type Tool struct {
	Name        string      `json:"name"`
	Description string      `json:"description"`
	InputSchema InputSchema `json:"inputSchema"`
}

type ToolCallParams

type ToolCallParams struct {
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments,omitempty"`
}

type ToolCallResult

type ToolCallResult struct {
	Content []ContentItem `json:"content"`
	IsError bool          `json:"isError,omitempty"`
}

type ToolsListResult

type ToolsListResult struct {
	Tools []Tool `json:"tools"`
}

Jump to

Keyboard shortcuts

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