mcp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Implements an MCP server based on the following specification: https://modelcontextprotocol.io/specification/2025-03-26/basic/lifecycle

Index

Constants

View Source
const (
	RPCVersion      = "2.0"
	ProtocolVersion = "2025-06-18"

	// Message types
	MessageTypeInitialize    = "initialize"
	MessageTypePing          = "ping"
	MessageTypeListTools     = "tools/list"
	MessageTypeCallTool      = "tools/call"
	MessageTypeListResources = "resources/list"
	MessageTypeListPrompts   = "prompts/list"

	// Notification types
	NotificationTypeInitialize = "notifications/initialized"

	// Error codes
	ErrorCodeMethodNotFound    = -32601
	ErrorCodeInvalidParameters = -32602
	ErrorInternalError         = -32603
)

Variables

This section is empty.

Functions

func WithToolkit

func WithToolkit(v *tool.Toolkit) opt.Opt

WithToolkit sets the toolkit for the MCP server

Types

type Content

type Content struct {
	Type     string `json:"type"` // "text", "image", "audio", "resource_link", "resource"
	Text     string `json:"text,omitempty"`
	Data     string `json:"data,omitempty"`
	MimeType string `json:"mimeType,omitempty"`
	URI      string `json:"uri,omitempty"`
	Name     string `json:"name,omitempty"`
	Resource any    `json:"resource,omitempty"`
}

Content represents a single piece of content in a tool result

type Error

type Error struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

func NewError

func NewError(code int, message string, data ...any) *Error

func (Error) Error

func (e Error) Error() string

type Handler

type Handler func(context.Context, any, json.RawMessage) (any, error)

type Request

type Request struct {
	Version string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	ID      any             `json:"id,omitempty"` // string or number for non-notifications
	Payload json.RawMessage `json:"params,omitempty"`
}

type RequestList

type RequestList struct {
	Cursor string `json:"cursor,omitempty"`
}

type RequestToolCall

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

type Response

type Response struct {
	Version string `json:"jsonrpc"`
	ID      any    `json:"id,omitempty"` // string or number
	Result  any    `json:"result,omitempty"`
	Err     *Error `json:"error,omitempty"`
}

type ResponseInitialize

type ResponseInitialize struct {
	Capabilities struct {
		Prompts   map[string]any `json:"prompts"`
		Tools     map[string]any `json:"tools"`
		Resources map[string]any `json:"resources"`
	} `json:"capabilities"`
	ServerInfo struct {
		Name    string `json:"name"`
		Version string `json:"version"`
	} `json:"serverInfo"`
	Version string `json:"protocolVersion"`
}

type ResponseListPrompts

type ResponseListPrompts struct {
	Prompts    []any  `json:"prompts"`
	NextCursor string `json:"nextCursor,omitempty"`
}

type ResponseListResources

type ResponseListResources struct {
	Resources  []any  `json:"resources"`
	NextCursor string `json:"nextCursor,omitempty"`
}

type ResponseListTools

type ResponseListTools struct {
	Tools      []*Tool `json:"tools"`
	NextCursor string  `json:"nextCursor,omitempty"`
}

type ResponseToolCall

type ResponseToolCall struct {
	Content []*Content `json:"content"`
	Error   bool       `json:"isError,omitempty"`
}

type Server

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

func New

func New(name, version string, opts ...opt.Opt) (*Server, error)

Create a new MCP server with the given name and version

func (*Server) HandlerFunc

func (server *Server) HandlerFunc(method string, fn Handler)

HandlerFunc registers (or removes) a handler for a method

func (*Server) RunStdio

func (server *Server) RunStdio(ctx context.Context, r io.Reader, w io.Writer) error

Implements an MCP server with standard input and output, and run in the foreground until the context is done.

type Tool

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

Tool represents an MCP tool definition with schema

Jump to

Keyboard shortcuts

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