Ophis automatically generates JSON schemas for MCP tools from Cobra commands.
- Name: Command path with underscores (
kubectl_get_pods) - Description: From command's Long, Short, and Example fields
- Input Schema: Generated from flags and arguments
- Output Schema: Standard format (stdout, stderr, exitCode)
Each flag becomes a property with:
Type mapping:
bool→booleanint,uint→integerfloat→numberstring→stringstring→<JSON schema>if the flag has an annotation calledjsonschemawith a value that is the JSON string representation of the schemastringSlice,intSlice→arrayduration,ip,ipNet→stringwith pattern validation
Flags marked as required (via cmd.MarkFlagRequired()) are included in the schema's required array. Default values are included in the schema, except for empty strings ("") and empty arrays ([]).
Example:
{
"flags": {
"type": "object",
"properties": {
"namespace": {
"type": "string",
"description": "Kubernetes namespace",
"default": "default"
},
"replicas": {
"type": "integer",
"default": 3
},
"labels": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["namespace"]
}
}Example showing JSON schema:
type SomeJsonObject struct {
Foo string
Bar int
FooBar struct {
Baz string
}
}
// generate schema for our object
aJsonObjSchema, err := jsonschema.For[SomeJsonObject](nil)
if err != nil {
// do something better than this in prod
panic(err)
}
bytes, err := aJsonObjSchema.MarshalJSON()
if err != nil {
// do something better than this in prod
panic(err)
}
// now create flag that has a json schema that represents a json object
cmd.Flags().String("a_json_obj", "", "Some JSON Object")
jsonobj := cmd.Flags().Lookup("a_json_obj")
jsonobj.Annotations = make(map[string][]string)
jsonobj.Annotations["jsonschema"] = []string{string(bytes)}Positional arguments are a string array:
{
"args": {
"type": "array",
"description": "Positional arguments\nUsage: [NAME] [flags]",
"items": { "type": "string" }
}
}{
"type": "object",
"properties": {
"stdout": { "type": "string" },
"stderr": { "type": "string" },
"exitCode": { "type": "integer" }
}
}./my-cli mcp tools # Creates mcp-tools.json