Profpatsch/users/Profpatsch/varlink-mcp-bridge/translator.go
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main

import (
	"encoding/json"
	"fmt"

	"github.com/varlink/go/varlink/idl"
)

// translateVarlinkTypeToJSONSchema converts a varlink type to a JSON Schema
func translateVarlinkTypeToJSONSchema(t *idl.Type) (map[string]any, error) {
	schema := make(map[string]any)

	switch t.Kind {
	case idl.TypeBool:
		schema["type"] = "boolean"

	case idl.TypeInt:
		schema["type"] = "integer"

	case idl.TypeFloat:
		schema["type"] = "number"

	case idl.TypeString:
		schema["type"] = "string"

	case idl.TypeObject:
		// Arbitrary JSON object (like map[string]interface{})
		schema["type"] = "object"
		schema["additionalProperties"] = true

	case idl.TypeArray:
		schema["type"] = "array"
		if t.ElementType != nil {
			items, err := translateVarlinkTypeToJSONSchema(t.ElementType)
			if err != nil {
				return nil, err
			}
			schema["items"] = items
		}

	case idl.TypeMaybe:
		// Maybe wraps another type - unwrap it and mark as optional
		// The optional nature is handled at the field level
		if t.ElementType != nil {
			return translateVarlinkTypeToJSONSchema(t.ElementType)
		}

	case idl.TypeStruct:
		schema["type"] = "object"
		properties := make(map[string]any)
		required := []string{}

		for _, field := range t.Fields {
			fieldSchema, err := translateVarlinkTypeToJSONSchema(field.Type)
			if err != nil {
				return nil, err
			}
			properties[field.Name] = fieldSchema

			// If field is not Maybe (optional), add to required list
			if field.Type.Kind != idl.TypeMaybe {
				required = append(required, field.Name)
			}
		}

		schema["properties"] = properties
		if len(required) > 0 {
			schema["required"] = required
		}

	case idl.TypeMap:
		schema["type"] = "object"
		if t.ElementType != nil {
			additionalProps, err := translateVarlinkTypeToJSONSchema(t.ElementType)
			if err != nil {
				return nil, err
			}
			schema["additionalProperties"] = additionalProps
		}

	case idl.TypeEnum:
		// Enum is represented as string with enum constraint
		schema["type"] = "string"
		// TODO: could add "enum" field with values if we extract them

	default:
		// Named types (aliases) - for simplicity treat as string
		if t.Alias != "" {
			schema["type"] = "string"
		} else {
			return nil, fmt.Errorf("unsupported varlink type kind: %v", t.Kind)
		}
	}

	return schema, nil
}

// buildInputSchemaForMethod creates a JSON Schema for a varlink method's input parameters
func buildInputSchemaForMethod(method *idl.Method) (json.RawMessage, error) {
	schema := make(map[string]any)
	schema["type"] = "object"

	if method.In != nil && len(method.In.Fields) > 0 {
		properties := make(map[string]any)
		required := []string{}

		for _, field := range method.In.Fields {
			fieldSchema, err := translateVarlinkTypeToJSONSchema(field.Type)
			if err != nil {
				return nil, err
			}
			properties[field.Name] = fieldSchema

			// Add to required if not Maybe (optional)
			if field.Type.Kind != idl.TypeMaybe {
				required = append(required, field.Name)
			}
		}

		schema["properties"] = properties
		if len(required) > 0 {
			schema["required"] = required
		}
	} else {
		// No parameters
		schema["properties"] = make(map[string]any)
	}

	return json.Marshal(schema)
}

// sanitizeToolName returns the Varlink method name as the MCP tool name
// No transformation is needed - MCP tool names are arbitrary strings
func sanitizeToolName(interfaceName, methodName string) string {
	return methodName
}