toolschema

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 20 Imported by: ? 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 toolschema derives MCP tool definitions and their JSON schemas from protobuf descriptors. It works at runtime with any protoreflect descriptor.

Index

Constants

View Source
const DiscriminatorKey = toolcodec.DiscriminatorKey

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. A oneof member field literally named this is rejected at generation time (see messageSchema). It mirrors toolcodec.DiscriminatorKey so schema generation and the toolcodec transform stay in lockstep.

Variables

This section is empty.

Functions

func Base36String

func Base36String(b []byte) string

Base36String encodes a byte slice as a base-36 string.

func CheckValidateRules

func CheckValidateRules(md protoreflect.MessageDescriptor) error

CheckValidateRules reports the first (buf.validate) rule on md, or on a message reachable from it, that this binary cannot decode. Such a rule arrives as unknown fields: a predefined rule whose Go package is not linked into the binary, or a rule newer than the protovalidate Go module in use. It cannot be put in the schema, and rather than leaving it out silently connectmcp fails registration.

func CleanComment

func CleanComment(comment string) string

CleanComment removes tool-specific comment prefixes (buf:lint, @ignore-comment).

func ConfiguredFieldDescription

func ConfiguredFieldDescription(fd protoreflect.FieldDescriptor) (string, bool)

ConfiguredFieldDescription returns the (connectmcp.v1.field_description) option of a field, without surrounding whitespace, and whether it has one that is not empty.

func ConfiguredToolDescription

func ConfiguredToolDescription(method protoreflect.MethodDescriptor) (string, bool)

ConfiguredToolDescription returns the (connectmcp.v1.tool_description) option on the method and whether it is set at all. The connectmcp package exposes exactly the methods that carry it.

func ConfiguredToolName

func ConfiguredToolName(method protoreflect.MethodDescriptor) (string, bool)

ConfiguredToolName returns the (connectmcp.v1.tool_name) option on the method and whether it is set at all. Presence is reported separately from the value so an explicitly authored empty string is distinguishable from an absent option — the generator must reject the former. The value is returned as authored; callers decide how to handle an invalid one (the generator fails the build, the runtime registration path falls back to the derived name).

func ExtractValidateConstraints

func ExtractValidateConstraints(fd protoreflect.FieldDescriptor) map[string]any

ExtractValidateConstraints reads the (buf.validate.field) rules of a field and returns the JSON Schema keywords to merge into the schema of that field (see the comment at the top of this file).

The keywords describe the field as a whole. For a repeated field they are those of the array, with the rules of its items under "items"; for a map field the rules of its keys and values are under "propertyNames" and "additionalProperties". Rules without a keyword are in "description", and example values in "examples". The result is empty, never nil, for a field without rules.

func ExtractValidateMessageConstraints

func ExtractValidateMessageConstraints(md protoreflect.MessageDescriptor) map[string]any

ExtractValidateMessageConstraints reads the (buf.validate.message) rules of a message and returns the JSON Schema keywords to merge into the schema of that message. Rules across fields have no JSON Schema keyword, so they are lines in "description". The result is empty, never nil, for a message without rules.

func FieldSchema

func FieldSchema(fd protoreflect.FieldDescriptor, opts SchemaOptions) map[string]any

FieldSchema generates a JSON schema for a single protobuf field descriptor.

func IsFieldRequired

func IsFieldRequired(fd protoreflect.FieldDescriptor) bool

IsFieldRequired reports whether a field must be set: it carries the REQUIRED field behavior or (buf.validate.field).required.

func KindToType

func KindToType(kind protoreflect.Kind) string

KindToType converts a protobuf field kind to a JSON Schema type string.

func MangleHeadIfTooLong

func MangleHeadIfTooLong(name string, maxLen int) string

MangleHeadIfTooLong truncates a tool name if it exceeds maxLen, using a hash prefix + the tail of the name (most specific part).

func MessageSchema

func MessageSchema(md protoreflect.MessageDescriptor, opts SchemaOptions) map[string]any

MessageSchema generates a JSON schema for a protobuf message descriptor. This is the main entry point for schema generation and can be used both at codegen time and at runtime with protoreflect.

func ToolAnnotationsForMethod

func ToolAnnotationsForMethod(method protoreflect.MethodDescriptor) (*mcpserver.ToolAnnotations, error)

ToolAnnotationsForMethod returns the MCP tool annotations of a method, or nil when it has none.

Whether the tool has side effects follows from the standard idempotency_level option, the declaration Connect uses for GET requests:

  • NO_SIDE_EFFECTS: readOnlyHint, idempotentHint and destructiveHint false;
  • IDEMPOTENT: idempotentHint;
  • unset: no hints.

The (connectmcp.v1.tool_annotations) option adds the title, destructiveHint and openWorldHint. A destructive_hint of true on a NO_SIDE_EFFECTS method contradicts idempotency_level: the error reports it, and the returned annotations then hold the hints derived from idempotency_level alone, so a contradictory hint is never published. connectmcp rejects such a method at registration.

func ToolForMethod

func ToolForMethod(method protoreflect.MethodDescriptor, description string) mcpserver.Tool

ToolForMethod generates the MCP tool definition for a given RPC method descriptor (input and output JSON schemas plus name, description and annotations, see ToolAnnotationsForMethod).

func ToolForMethodWithOptions added in v0.0.8

func ToolForMethodWithOptions(method protoreflect.MethodDescriptor, description string, opts SchemaOptions) mcpserver.Tool

ToolForMethodWithOptions is ToolForMethod with the schemas generated with opts.

func ToolNameForMethod

func ToolNameForMethod(method protoreflect.MethodDescriptor) string

ToolNameForMethod resolves the MCP tool name for a method: the (connectmcp.v1.tool_name) option when set and valid, otherwise the derived full-name-based form, hash-truncated to 64 chars. Invalid configured names fall back to the derived form here so a provider-rejected name is never published; connectmcp additionally rejects them at registration so they are caught at startup.

func ValidateToolName

func ValidateToolName(name string) error

ValidateToolName reports whether a configured tool name is usable on every LLM provider.

Types

type FieldOptions added in v0.0.8

type FieldOptions struct {
	// Description is what the model reads about the field.
	Description string
	// Required lists the field in its message's "required", as the REQUIRED
	// field behavior does; it cannot make a required field optional.
	Required bool
}

FieldOptions are the Go form of a field's (connectmcp.v1.field_description) option and REQUIRED field behavior.

type SchemaOptions

type SchemaOptions struct {
	// MaxRecursionDepth controls how many times a recursive message type
	// can be expanded before being replaced with a JSON-string placeholder.
	// Zero uses the default (3). OpenAI documents a 5-level nesting limit
	// (though it's not strictly enforced), so the default keeps total depth
	// manageable while still giving LLMs useful field-level detail.
	MaxRecursionDepth int

	// Fields gives fields, by full name, what their own options would: for
	// fields of protos you do not own. A Description is used only for a field
	// without the (connectmcp.v1.field_description) option.
	Fields map[protoreflect.FullName]FieldOptions
}

SchemaOptions controls JSON schema generation behavior.

Jump to

Keyboard shortcuts

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