README
¶
funcschema
A code generation tool that creates Model Context Protocol (MCP) Tool definitions from Go functions.
Purpose
funcschema bridges the gap between Go functions and MCP-compatible tool definitions by automatically generating:
- JSON Schema definitions for function inputs and outputs
chat.Toolimplementations that expose those functions to LLM providers- Wrapper logic that marshals/unmarshals JSON and maps Go errors into MCP responses
This enables Go functions to be exposed as tools that can be invoked by AI agents and other MCP-compatible systems.
Usage
go run . -func <FunctionName> -input <source.go>
This generates a <functionname>_tool.go file containing a <FunctionName>Tool value that implements chat.Tool. The generated type includes:
- Tool definition metadata (name, description, JSON schema)
- A
Call(context.Context, string) stringmethod that bridges between JSON input and your Go function - An internal result wrapper that includes the tool's JSON schema + error propagation
Function Requirements
Functions must follow this pattern:
// With arguments
func MyFunction(ctx context.Context, req RequestStruct) (ResultStruct, error) {
// ...
}
// Without arguments (context-only)
func MyFunction(ctx context.Context) (ResultStruct, error) {
// ...
}
Requirements:
- First parameter must be
context.Context - Optional second parameter must be a named struct type (not a pointer or anonymous inline struct)
- Functions must return either
(ResultStruct, error)or justerror - The result struct can contain any fields you need; the generator wraps it with an error pointer automatically
- Function must be standalone (not a method)
Features
JSON Schema Generation
- Generates OpenAI-compatible JSON schemas
- Handles complex Go types: structs, arrays, maps, pointers
- Respects JSON struct tags for field naming
- Treats pointer fields as nullable (using
["type", "null"]format) - All fields are marked as required for OpenAI compatibility
Tool Naming
- Automatically converts Go function names from CamelCase to snake_case
- Example:
DatasetGetbecomesdataset_getin the tool definition
Wrapper Functions
The generated chat.Tool implementation:
- Accepts
context.Contextand a JSON string via itsCallmethod - Unmarshals JSON into the request struct (if applicable)
- Calls the original function and captures the returned
(result, error) - Wraps the Go result with an internal struct that adds an optional
errorfield for MCP responses - Marshals the wrapped result to JSON before returning
Example
Given this function:
type GetDataRequest struct {
DatasetId string `json:"datasetId"`
Limit int `json:"limit"`
}
type GetDataResult struct {
Data []string `json:"data"`
}
func GetData(ctx context.Context, req GetDataRequest) (GetDataResult, error) {
// Implementation
return GetDataResult{Data: []string{"foo"}}, nil
}
Running funcschema -func GetData -input data.go generates:
- MCP tool definition with name
get_data - Input schema matching
GetDataRequeststructure - Output schema matching
GetDataResultstructure plus anerrorfield var GetDataTool chat.Tool = getDataTool{}which you can register directly with anychat.Chat
OpenAI Compatibility
The tool follows OpenAI's JSON Schema subset limitations:
- No
$refreferences - No
oneOffor complex types (usesanyOfinstead) - All fields in
requiredarrays - Simple nullable types use
["type", "null"]format
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.