stdio

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package stdio provides the stdio interface for the MCP server.

Index

Constants

View Source
const (
	JSONRPCVersion = "2.0"

	// Error codes
	ParseErrorCode     = -32700
	InvalidParamsCode  = -32602
	MethodNotFoundCode = -32601
	InternalErrorCode  = -32603
)

Constants for JSON-RPC

Variables

This section is empty.

Functions

func ServeStdio

func ServeStdio(server *rest.MCPServer, opts ...StdioOption) error

ServeStdio is a convenience function that creates and starts a StdioServer with os.Stdin and os.Stdout. It sets up signal handling for graceful shutdown on SIGTERM and SIGINT. Returns an error if the server encounters any issues during operation.

Types

type MessageProcessor

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

MessageProcessor handles JSON-RPC message processing

func NewMessageProcessor

func NewMessageProcessor(server *rest.MCPServer, logger *logging.Logger) *MessageProcessor

NewMessageProcessor creates a new message processor with registered handlers

func (*MessageProcessor) Process

func (p *MessageProcessor) Process(ctx context.Context, message string) (interface{}, error)

Process processes a JSON-RPC message and returns a response

func (*MessageProcessor) RegisterHandler

func (p *MessageProcessor) RegisterHandler(method string, handler MethodHandler)

RegisterHandler registers a method handler

type MethodHandler

type MethodHandler interface {
	Handle(ctx context.Context, params interface{}, id interface{}) (interface{}, *domain.JSONRPCError)
}

MethodHandler defines the interface for JSON-RPC method handlers

type MethodHandlerFunc

type MethodHandlerFunc func(ctx context.Context, params interface{}, id interface{}) (interface{}, *domain.JSONRPCError)

MethodHandlerFunc is a function type that implements MethodHandler

func (MethodHandlerFunc) Handle

func (f MethodHandlerFunc) Handle(ctx context.Context, params interface{}, id interface{}) (interface{}, *domain.JSONRPCError)

Handle calls the handler function

type StdioContextFunc

type StdioContextFunc func(ctx context.Context) context.Context

StdioContextFunc is a function that takes an existing context and returns a potentially modified context. This can be used to inject context values from environment variables, for example.

type StdioOption

type StdioOption func(*StdioServer)

StdioOption defines a function type for configuring StdioServer

func WithErrorLogger

func WithErrorLogger(stdLogger *log.Logger) StdioOption

WithErrorLogger is kept for backwards compatibility It will create a custom logger that wraps the standard log.Logger

func WithLogger

func WithLogger(logger *logging.Logger) StdioOption

WithLogger sets the logger for the server

func WithStdioContextFunc

func WithStdioContextFunc(fn StdioContextFunc) StdioOption

WithContextFunc sets a function that will be called to customize the context to the server. Note that the stdio server uses the same context for all requests, so this function will only be called once per server instance.

func WithToolHandler

func WithToolHandler(toolName string, handler func(ctx context.Context, params map[string]interface{}, session *domain.ClientSession) (interface{}, error)) StdioOption

WithToolHandler registers a custom handler function for a specific tool. This allows you to override the default tool handling behavior.

type StdioServer

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

StdioServer wraps a MCPServer and handles stdio communication. It provides a simple way to create command-line MCP servers that communicate via standard input/output streams using JSON-RPC messages.

func NewStdioServer

func NewStdioServer(server *rest.MCPServer, opts ...StdioOption) *StdioServer

NewStdioServer creates a new stdio server wrapper around an MCPServer. It initializes the server with a default logger that logs to stderr.

func (*StdioServer) Listen

func (s *StdioServer) Listen(ctx context.Context, stdin io.Reader, stdout io.Writer) error

Listen starts listening for JSON-RPC messages on the provided input and writes responses to the provided output. It runs until the context is canceled or an error occurs. Returns an error if there are issues with reading input or writing output.

Jump to

Keyboard shortcuts

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