codegen

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package codegen contains the MCP generator built on top of Goa evaluation and Goa's JSON-RPC codegen.

Codegen Philosophy

MCP code generation is intentionally fail-fast and transport-closed: MCP-enabled services either generate a pure MCP surface or generation fails. The package derives all per-run state from the evaluated Goa roots, then builds a synthetic MCP service expression and lets Goa generate the JSON-RPC transport/client code that MCP needs.

Where MCP needs behavior beyond Goa's standard JSON-RPC generator (tool/resource/prompt adapters, MCP-specific clients, helper packages), this package emits dedicated files around Goa's output. The generator still rewrites a narrow set of example/CLI sections because Goa does not yet expose smaller hooks for that scaffolding; the docs should stay honest about that coupling until the underlying hook surface improves.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error)

Generate orchestrates MCP code generation for services that declare MCP configuration in the DSL. It composes Goa service and JSON-RPC generators and adds adapter/client helpers.

func ModifyExampleFiles

func ModifyExampleFiles(_ string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error)

ModifyExampleFiles patches example CLI wiring to target the MCP adapter client and replaces the default MCP stub factory to return the adapter-wrapped service. It avoids touching HTTP server signatures or example mains.

func PrepareExample

func PrepareExample(_ string, roots []eval.Root) error

PrepareExample augments the original roots so the Goa example generator includes the MCP JSON-RPC server without manual cmd edits. It runs the same pure-MCP contract validation as Generate so example scaffolding cannot mask invalid MCP mappings.

func PrepareServices

func PrepareServices(_ string, roots []eval.Root) error

PrepareServices validates the full pure-MCP generation contract before filtering HTTP transport generation for MCP-enabled services. Callers may use Generate directly, but when PrepareServices is part of the pipeline it guarantees invalid MCP designs fail before any transport pruning happens.

Types

type AdapterData

type AdapterData struct {
	ServiceName         string
	ServiceGoName       string
	MCPServiceName      string
	MCPName             string
	MCPVersion          string
	ProtocolVersion     string
	Package             string
	MCPPackage          string
	ServiceJSONRPCAlias string
	ImportPath          string
	Tools               []*ToolAdapter
	Resources           []*ResourceAdapter
	StaticPrompts       []*StaticPromptAdapter
	DynamicPrompts      []*DynamicPromptAdapter
	Notifications       []*NotificationAdapter
	Subscriptions       []*SubscriptionAdapter
	// Streaming flags derived from original service DSL
	ToolsCallStreaming bool
	// Derived flags
	HasWatchableResources bool
	NeedsMCPClient        bool
	NeedsOriginalClient   bool
	NeedsQueryFormatting  bool

	Register     *RegisterData
	ClientCaller *ClientCallerData
}

AdapterData holds the data for generating the adapter

type AnnotationMetaEntry

type AnnotationMetaEntry struct {
	Key    string
	Values []string
}

AnnotationMetaEntry stores one generated tool annotation entry.

type ClientCallerData

type ClientCallerData struct {
	MCPImportPath string
}

type DefaultField

type DefaultField struct {
	Name    string
	GoName  string
	Literal string
	Kind    string
}

DefaultField describes a top-level payload field default assignment.

type DynamicPromptAdapter

type DynamicPromptAdapter struct {
	Name               string
	Description        string
	OriginalMethodName string
	HasPayload         bool
	PayloadType        string
	ResultType         string
	// Arguments describes prompt arguments derived from the payload (dynamic prompts)
	Arguments []PromptArg
	// ExampleArguments contains a minimal valid JSON for prompt arguments
	ExampleArguments string
}

DynamicPromptAdapter represents a dynamic prompt adapter

type NotificationAdapter

type NotificationAdapter struct {
	Name               string
	Description        string
	OriginalMethodName string
	HasMessage         bool
	MessagePointer     bool
	HasData            bool
}

NotificationAdapter represents a notification mapping

type PromptArg

type PromptArg struct {
	Name        string
	Description string
	Required    bool
}

PromptArg is a lightweight representation for generating PromptArgument values

type PromptMessageAdapter

type PromptMessageAdapter struct {
	Role    string
	Content string
}

PromptMessageAdapter represents a prompt message

type RegisterData

type RegisterData struct {
	Package            string
	HelperName         string
	ServiceName        string
	SuiteName          string
	SuiteQualifiedName string
	Description        string
	Tools              []RegisterTool
}

RegisterData drives generation of runtime registration helpers.

type RegisterTool

type RegisterTool struct {
	ID            string
	QualifiedName string
	Description   string
	Meta          []AnnotationMetaEntry
	PayloadType   string
	ResultType    string
	InputSchema   string
	ExampleArgs   string
}

RegisterTool represents a single tool entry in the helper file.

type ResourceAdapter

type ResourceAdapter struct {
	Name               string
	Description        string
	URI                string
	MimeType           string
	OriginalMethodName string
	HasPayload         bool
	HasResult          bool
	PayloadType        string
	ResultType         string
	QueryFields        []*ResourceQueryField
	Watchable          bool
}

ResourceAdapter represents a resource adapter

type ResourceQueryField

type ResourceQueryField struct {
	QueryKey       string
	GuardExpr      string
	ValueExpr      string
	CollectionExpr string
	FormatKind     string
	Repeated       bool
}

ResourceQueryField describes one statically known query parameter binding for a resource payload field.

type ServiceMethodMapping

type ServiceMethodMapping struct {
	ToolMethods          map[string]string
	ResourceMethods      map[string]string
	DynamicPromptMethods map[string]string
}

ServiceMethodMapping maps MCP operations to original service methods.

type StaticPromptAdapter

type StaticPromptAdapter struct {
	Name        string
	Description string
	Messages    []*PromptMessageAdapter
}

StaticPromptAdapter represents a static prompt

type SubscriptionAdapter

type SubscriptionAdapter struct {
	ResourceName       string
	ResourceURI        string
	OriginalMethodName string
}

SubscriptionAdapter represents a subscription mapping

type ToolAdapter

type ToolAdapter struct {
	Name               string
	Description        string
	OriginalMethodName string
	Meta               []AnnotationMetaEntry
	AnnotationsJSON    string
	HasPayload         bool
	HasResult          bool
	PayloadType        string
	ResultType         string
	InputSchema        string
	IsStreaming        bool
	StreamInterface    string
	StreamEventType    string
	// Simple validations (top-level only)
	RequiredFields []string
	EnumFields     map[string][]string
	EnumFieldsPtr  map[string]bool
	DefaultFields  []DefaultField
	// ExampleArguments contains a minimal valid JSON for tool arguments
	ExampleArguments string
}

ToolAdapter represents a tool adapter

Jump to

Keyboard shortcuts

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