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

mcpstdio

package
v3.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package mcpstdio is a small MCP client over a child process's stdio, for the programs under scripts/ that drive the built server: the smoke gate, the live driver and the evals. It speaks exactly the frames those need and nothing else, but it is one client, because hand-written JSON-RPC loops are places for the handshake to drift apart.

Every line the server writes to stdout must be a JSON-RPC frame; a line that is not is kept and reported by Stray, because a stray line on stdout is a protocol violation the client would otherwise skip.

Index

Constants

View Source
const CallTimeout = 2 * time.Minute

CallTimeout bounds one request when Config.Timeout is zero.

View Source
const Protocol = "2025-11-25"

Protocol is the revision Initialize asks for when none is given.

Variables

View Source
var ErrClosed = errors.New("the server closed the connection")

ErrClosed is the server closing stdout before a reply arrived.

Functions

func Encode

func Encode(args map[string]any) string

Encode renders arguments for a transcript heading.

func Environ

func Environ(base []string, extra ...string) []string

Environ is base without the server's settings, plus extra.

Types

type Config

type Config struct {
	// Args follow the binary on the command line.
	Args []string
	// Env is added to the environment, which is the parent's minus every
	// GITLAB_MCP_ variable. An MCP client passes command, args and env,
	// and nothing else.
	Env []string
	// Timeout bounds each request; zero means CallTimeout.
	Timeout time.Duration
}

Config says how to start the server.

type RPCError

type RPCError struct {
	Method  string
	Code    int
	Message string
}

RPCError is the server answering with an error object rather than a result. A tool call reports a refusal inside its result, but a resource read reports one as a JSON-RPC error, and a driver that could not tell the two apart would count every expected refusal as a broken connection.

func (*RPCError) Error

func (e *RPCError) Error() string

type Session

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

Session is one stdio conversation with the server.

func Start

func Start(binary string, cfg Config) (*Session, error)

Start launches the server.

func (*Session) Await

func (s *Session) Await(method string, id int) (map[string]any, error)

Await waits for the reply to id, which was sent with Send.

func (*Session) CallTool

func (s *Session) CallTool(name string, args map[string]any) (ToolResult, error)

CallTool runs one tool.

func (*Session) Close

func (s *Session) Close() error

Close closes stdin and waits for the server to exit, killing it after the timeout. It returns the exit error, if any. Both pipes are read to the end before Wait, as os/exec requires: Wait closes them, and a read still in progress would lose the last lines.

func (*Session) Initialize

func (s *Session) Initialize(protocol, clientName string) (map[string]any, error)

Initialize completes the handshake at protocol (Protocol when empty) and lists the tool surface. It returns the initialize result.

func (*Session) ListTools

func (s *Session) ListTools() ([]Tool, error)

ListTools lists every tool, following the cursor.

func (*Session) Notify

func (s *Session) Notify(method string, params any) error

Notify sends a notification, which has no reply.

func (*Session) OnCall

func (s *Session) OnCall(f func(tool string, args map[string]any))

OnCall registers a watcher for every tool call this session makes.

func (*Session) Options

func (s *Session) Options() map[string][]string

Options is the tool surface the server published at initialize: each registered tool and the option names its schema declares.

func (*Session) ReadResource

func (s *Session) ReadResource(uri string) (text, mime string, err error)

ReadResource reads one resource and returns its text and media type. A refusal comes back as an *RPCError.

func (*Session) Request

func (s *Session) Request(method string, params any) (map[string]any, error)

Request sends one request and waits for its reply, returning the result object. Frames the server sends on its own account — notifications, logs — are skipped.

func (*Session) Send

func (s *Session) Send(frame any) error

Send writes one frame as a line.

func (*Session) StderrTail

func (s *Session) StderrTail(n int) []string

StderrTail returns the last n log lines the server wrote.

func (*Session) Stray

func (s *Session) Stray() []string

Stray is every stdout line so far that was not a JSON-RPC frame.

type Tool

type Tool struct {
	Name    string
	Options []string
	Raw     map[string]any
}

Tool is one listed tool: its name, its input option names sorted, and the whole listing entry.

type ToolResult

type ToolResult struct {
	// Text is every text content block, joined.
	Text string
	// IsError is the tool refusing, as opposed to the protocol failing.
	IsError bool
	// Structured is structuredContent, raw; nil when absent.
	Structured json.RawMessage
}

ToolResult is one tool call's reply.

Jump to

Keyboard shortcuts

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