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

mcp

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package mcp implements a minimal, dependency-free Model Context Protocol server over stdio (newline-delimited JSON-RPC 2.0). It supports exactly the lifecycle and methods the `volcano docs` tool surface needs — initialize, notifications/initialized, ping, tools/list, tools/call — and nothing more.

The protocol loop is exposed as Serve(ctx, reader, writer, info, handler) so it can be driven directly with in-memory streams in tests, without spawning a subprocess. Domain logic lives entirely behind the Handler interface.

Index

Constants

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternalError  = -32603
)

JSON-RPC 2.0 standard error codes.

View Source
const ProtocolVersion = "2025-06-18"

ProtocolVersion is the MCP spec revision this server advertises.

Variables

This section is empty.

Functions

func Serve

func Serve(ctx context.Context, r io.Reader, w io.Writer, info ServerInfo, h Handler) error

Serve runs the JSON-RPC 2.0 stdio loop until EOF, a fatal read error, or context cancellation. It writes only protocol frames to w; callers must keep all diagnostics on a separate stream (stderr).

Types

type Content

type Content struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

Content is a single content item in a tool result (text only here).

type Error

type Error struct {
	Code    int
	Message string
}

Error is a protocol-level error a Handler may return to produce a JSON-RPC error response (e.g. unknown tool, invalid arguments). Domain failures should instead be returned as a ToolResult with IsError=true.

func (*Error) Error

func (e *Error) Error() string

type Handler

type Handler interface {
	Tools() []Tool
	Call(ctx context.Context, name string, args json.RawMessage) (ToolResult, error)
}

Handler supplies the tool surface backing the server.

type ServerInfo

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

ServerInfo identifies this server to the client during initialize.

type Tool

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

Tool describes one callable tool advertised via tools/list.

type ToolResult

type ToolResult struct {
	Content           []Content `json:"content"`
	StructuredContent any       `json:"structuredContent,omitempty"`
	IsError           bool      `json:"isError,omitempty"`
}

ToolResult is the payload returned from a successful tools/call. IsError signals a domain-level failure (the call completed but the operation failed), as distinct from a protocol error.

func TextResult

func TextResult(v any, isError bool) (ToolResult, error)

TextResult builds a ToolResult carrying one JSON text content item plus the same value as structuredContent.

Jump to

Keyboard shortcuts

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