Files
kolaenteandTink e115c375f2 feat(mcp): derive tool specs from the v2 OpenAPI document
Tools are built from the finished document: path and query parameters plus
the JSON body become one input schema, update tools take their schema from
the sibling PUT because AutoPatch strips $ref, and an explicit allow-list
decides which operations are typed tools, catalog actions, or hidden.
2026-09-15 11:41:34 +02:00

59 lines
1.8 KiB
Go

// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package mcp
import (
"encoding/json"
"errors"
)
func decodeArgs(spec *toolSpec, raw json.RawMessage) (map[string]json.RawMessage, error) {
instance := map[string]any{}
if len(raw) > 0 {
if err := json.Unmarshal(raw, &instance); err != nil || instance == nil {
return nil, errors.New("arguments must be a JSON object")
}
}
args := map[string]json.RawMessage{}
if len(raw) > 0 {
if err := json.Unmarshal(raw, &args); err != nil {
return nil, errors.New("arguments must be a JSON object")
}
}
// A null parameter means unset, which the parameter schema itself does not allow.
for name, v := range instance {
if v == nil && spec.params[name] != nil {
delete(instance, name)
delete(args, name)
}
}
if err := spec.resolved.Validate(instance); err != nil {
return nil, err
}
return args, nil
}
func decodeToolArgs(spec *toolSpec, raw json.RawMessage, dst any) error {
if _, err := decodeArgs(spec, raw); err != nil {
return err
}
if len(raw) == 0 {
return nil
}
return json.Unmarshal(raw, dst)
}