mcpkit

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mcpkit runs a stdio MCP server whose tools are read-only and answer with the JSON envelope of --format json, split into pages with an opaque cursor when an answer passes the size cap. Its prompts return one user message that tells the agent which tools to call.

Index

Constants

View Source
const MaxBytes = 50_000

MaxBytes is the default cap on the JSON text of one tool result. Claude Code saves larger text results to a file instead of showing them to the model.

Variables

View Source
var ErrArgument = errors.New("missing required argument")

ErrArgument is returned when a call lacks a required argument.

View Source
var ErrCursor = errors.New("unknown or expired cursor")

ErrCursor is returned for a cursor the server doesn't know, has dropped, or issued for another tool.

View Source
var ErrUnknownArgument = errors.New("unknown argument")

ErrUnknownArgument is returned when a prompt gets an argument it doesn't declare.

Functions

func AddPrompt added in v0.4.0

func AddPrompt(s *Server, p Prompt, f PromptFunc)

AddPrompt registers a prompt whose answer is one user message. A get with an unknown argument or without a required one fails before f runs, because the SDK checks neither. It panics when two arguments share a name.

func AddTool

func AddTool[In any](s *Server, t Tool, h Handler[In])

AddTool registers a tool as read-only, idempotent and not destructive. A call with a cursor gets the next page of an earlier result and never reaches h. It panics when In has no valid input schema, as mcp.AddTool does.

func ClientRoots

func ClientRoots(req *mcp.CallToolRequest) ([]string, error)

ClientRoots returns the local directories the client listed as its roots for a tool with Tool.Roots set, or nil when the client doesn't support roots. Claude Code lists its launch directory and the directories added with --add-dir.

func FilePath

func FilePath(uri, goos string) (string, error)

FilePath turns a file URI into a local path for goos: file:///home/a is /home/a, file:///C:/src is C:\src and file://host/share is \\host\share on Windows.

Types

type Handler

type Handler[In any] func(ctx context.Context, req *mcp.CallToolRequest, in In) (*Result, error)

Handler answers one call. Its error becomes a tool error, which the client shows to the model.

type Options

type Options struct {
	Name         string // server_info name
	Version      string // server_info version
	Instructions string // sent to the client at initialization
	Logger       *slog.Logger
	Clock        func() time.Time
	MaxBytes     int           // 0 means [MaxBytes]; below 0 turns the cap off
	CursorTTL    time.Duration // how long a split result stays after its last use; 0 means 15 minutes
	MaxSnapshots int           // split results kept at once, least recently used dropped first; 0 means 16
}

Options configures a Server.

type Pager

type Pager interface {
	Len() int
	Noun() string        // names the items in the page field, such as "entries"
	Order() []int        // item indexes in the order pages take them; nil means 0 to Len-1
	Data(keep []int) any // the data with only the items at keep, which is sorted
}

Pager splits an envelope's data into items, so a result over the cap can be sent in pages.

func SlicePager

func SlicePager[T any](items []T, noun string, rank func(T) int, data func([]T) any) Pager

SlicePager pages items. Items with a lower rank go to earlier pages; equal ranks keep their order. data builds the envelope data from a subset of items in their original order.

type Paging

type Paging struct {
	Cursor string `json:"cursor,omitempty" jsonschema:"The next_cursor of an earlier result of this tool. Other arguments are then ignored."` //nolint:lll // struct tag
}

Paging is embedded in the input of a tool whose result can be split.

type Prompt added in v0.4.0

type Prompt struct {
	Name        string
	Title       string
	Description string // one sentence
	Arguments   []PromptArgument
}

Prompt describes a prompt template that a client offers its user, such as a slash command.

type PromptArgument added in v0.4.0

type PromptArgument struct {
	Name        string
	Description string
	Required    bool
}

PromptArgument is one string argument of a Prompt.

type PromptFunc added in v0.4.0

type PromptFunc func(args map[string]string) (string, error)

PromptFunc returns the text of a prompt's user message. args holds the arguments the client set, trimmed, without blank ones.

type Result

type Result struct {
	Envelope render.Envelope
	Pager    Pager // nil when the data is never split
}

Result is a whole answer before it is split.

type Server

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

Server is an MCP server for read-only tools that answer with an envelope.

func NewServer

func NewServer(o Options) *Server

NewServer returns a server with no tools.

func (*Server) Connect

func (s *Server) Connect(ctx context.Context, t mcp.Transport) (*mcp.ServerSession, error)

Connect serves one client over t and returns at once; tests pass an in-memory transport.

func (*Server) Run

func (s *Server) Run(ctx context.Context, in io.Reader, out io.Writer) error

Run serves one client over in and out until the client disconnects or ctx is canceled. Neither stream is closed.

type Tool

type Tool struct {
	Name        string
	Title       string
	Description string // one sentence
	OpenWorld   bool   // the tool reaches the network
	Roots       bool   // ask the client for its roots before each call; read them with [ClientRoots]
}

Tool describes a read-only tool.

Jump to

Keyboard shortcuts

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