runtime

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 15 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

Index

Constants

View Source
const DefaultMaxRecursionDepth = 3

DefaultMaxRecursionDepth is the number of times a recursive message type is expanded in a generated schema before it is replaced with a JSON-string placeholder. EncodeMessage uses the same value so the JSON it emits matches the schema the model was given. The gen package mirrors this constant.

View Source
const DiscriminatorKey = "which"

DiscriminatorKey is the property name of the oneof discriminator emitted in the nested wrapper object. It carries the protojson name of the field that is set within the oneof. The gen package mirrors this constant when rendering schemas, and DecodeArguments/EncodeMessage read and write it at runtime.

Variables

This section is empty.

Functions

func DecodeArguments

func DecodeArguments(md protoreflect.MessageDescriptor, args map[string]any) error

DecodeArguments rewrites model-supplied tool-call arguments in place so that protojson can unmarshal them into a proto message. It is the inverse of the two schema shapes the generator emits that protojson does not understand:

  • oneof discriminated wrappers: a oneof renders as a nested object {"which":"<member>", "<member>":<value>, ...}. This lifts the named member to its native sibling key so protojson sees a normal oneof.
  • recursion-depth placeholders: a message nested beyond MaxRecursionDepth renders as a JSON-string. This parses that string back to an object.

Everything else passes straight through to protojson untouched. Errors are phrased to be model-readable: a failed tool call is returned to the model for one-turn self-correction, so the message names the fix.

func EncodeMessage

func EncodeMessage(msg proto.Message) (json.RawMessage, error)

EncodeMessage marshals a proto message to the model-facing JSON shape: it runs protojson, then rewraps each set oneof into its discriminated object and stringifies any subtree nested beyond DefaultMaxRecursionDepth so the output matches the tool's generated output schema. It is the encode-side inverse of DecodeArguments.

func UnmarshalArguments

func UnmarshalArguments(args map[string]any, msg proto.Message) error

UnmarshalArguments unmarshals tool-call arguments, already rewritten by DecodeArguments, into msg. A failure is returned as an *ArgumentsError.

When a single field is to blame, the error names that field, the JSON shape it expects, and the shape that was sent.

Types

type ArgumentsError

type ArgumentsError struct {
	// Field is the path of the offending field as named in the tool's input
	// schema, e.g. "page_size" or "items[2].name". It is empty when no single
	// field is to blame.
	Field string
	// Description says what is wrong: with Field when it is set (e.g. `must
	// be an integer, but got the string "ten".`), otherwise with the
	// arguments as a whole.
	Description string
}

ArgumentsError is the reason tool-call arguments could not be decoded into the request message. Its Error text is phrased for the model, so it can see what was wrong and call the tool again with corrected arguments.

Use ConnectError to report it like any other failed call: as an INVALID_ARGUMENT connect error rendered by HandleError.

func (*ArgumentsError) ConnectError

func (e *ArgumentsError) ConnectError() *connect.Error

ConnectError returns e as an INVALID_ARGUMENT connect error with e's text as its message. When a single field is to blame, the error carries a google.rpc.BadRequest detail with one field violation for it.

func (*ArgumentsError) Error

func (e *ArgumentsError) Error() string

type CallToolRequest

type CallToolRequest struct {
	// Arguments is the JSON of the call's arguments as the client sent it,
	// undecoded, so that the handler can enforce a size limit before decoding.
	// It is empty when the client sent no arguments.
	Arguments json.RawMessage
	// Header holds the headers of the HTTP request that carried the tool
	// call, or nil when the MCP transport is not HTTP (e.g. stdio).
	Header http.Header
}

CallToolRequest carries the arguments of an MCP tool call.

type CallToolResult

type CallToolResult struct {
	Text              string
	StructuredContent any
	IsError           bool
}

CallToolResult is the response from a tool handler.

func HandleError

func HandleError(err error) (*CallToolResult, error)

HandleError converts an RPC error into a structured MCP tool error result: a JSON object with the error "code", "message" and, for connect errors carrying them, "details" (each rendered as protojson with an "@type").

It reveals exactly what connect-go's own server transports put on the wire for a handler error (see connecthttp's scrubHandlerError), so a server served as MCP discloses no more than it does over Connect, gRPC or gRPC-Web:

  • A locally authored *connect.Error anywhere in err's chain supplies the code, message and details. Text added by wrapping it (fmt.Errorf("...: %w", cerr)) and its WithCause cause are not shown.
  • A remote *connect.Error (one decoded from a peer, e.g. returned by a connect client) is reported as INTERNAL without a message: a peer's verdict is not the handler's own.
  • context.Canceled is CANCELED and context.DeadlineExceeded (or os.ErrDeadlineExceeded) is DEADLINE_EXCEEDED, without a message.
  • Any other error is UNKNOWN without a message.

Return connect.NewError(code, msg) to tell the model something; keep internals in err or in WithCause for logging.

func NewToolResultError

func NewToolResultError(text string) *CallToolResult

NewToolResultError creates an error text result.

func NewToolResultJSON

func NewToolResultJSON(jsonBytes []byte) *CallToolResult

NewToolResultJSON creates a successful result that carries the same JSON payload as both unstructured text content (for backward compatibility) and structured content (matching the tool's output schema). The caller is responsible for passing JSON-encoded bytes; no validation is performed. The slice is copied into StructuredContent so callers may safely reuse the buffer afterwards.

func NewToolResultText

func NewToolResultText(text string) *CallToolResult

NewToolResultText creates a successful text result.

type MCPServer

type MCPServer interface {
	AddTool(tool Tool, handler ToolHandler)
}

MCPServer is the minimal MCP server surface that tool registration (connectmcp.Register) programs against. The gosdk package adapts the official modelcontextprotocol/go-sdk server to it; other MCP libraries can be supported by implementing this one method.

type Tool

type Tool struct {
	Name            string
	Description     string
	RawInputSchema  json.RawMessage
	RawOutputSchema json.RawMessage
	// Annotations describe the tool to clients; nil when there are none.
	Annotations *ToolAnnotations
}

Tool describes an MCP tool independent of any MCP library.

type ToolAnnotations

type ToolAnnotations struct {
	// Title is the human-readable name that clients display instead of the
	// tool name.
	Title string
	// ReadOnlyHint tells that the tool does not modify its environment.
	ReadOnlyHint bool
	// DestructiveHint tells whether the tool may destroy or overwrite data
	// (true) or only adds to it (false); nil means MCP's default, true.
	// Meaningful only when ReadOnlyHint is false.
	DestructiveHint *bool
	// IdempotentHint tells that calling the tool again with the same
	// arguments has no additional effect. Meaningful only when ReadOnlyHint
	// is false.
	IdempotentHint bool
	// OpenWorldHint tells whether the tool interacts with an open world of
	// external entities (true) or a closed domain (false); nil means MCP's
	// default, true.
	OpenWorldHint *bool
}

ToolAnnotations are the display title and behavior hints of an MCP tool. They are hints for clients (e.g. which calls to confirm with the user), not guarantees. An unset hint takes MCP's default: not read-only, not idempotent, possibly destructive and open world.

func (*ToolAnnotations) IsZero

func (a *ToolAnnotations) IsZero() bool

IsZero reports whether a carries no title and no hints.

type ToolHandler

type ToolHandler func(ctx context.Context, request *CallToolRequest) (*CallToolResult, error)

ToolHandler is the callback invoked when an MCP client calls a tool.

Directories

Path Synopsis
Package gosdk provides an adapter from modelcontextprotocol/go-sdk to the runtime.MCPServer interface.
Package gosdk provides an adapter from modelcontextprotocol/go-sdk to the runtime.MCPServer interface.

Jump to

Keyboard shortcuts

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