mcp

package
v0.2.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 20 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, get_trace / get_graph_snapshot / correlated_signals 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 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,
	svcGraph *graph.Graph,
	vectorIdx *vectordb.Index,
) *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.

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) 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) 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