Files
MrAlders0n f93983cea0 feat(mcp): add an optional read-only MCP endpoint
Serves the public read API to AI assistants at /mcp. Off by default;
private when mcp.keys are set, public otherwise. Tools call the REST
handlers in-process, so validation, guards and errors match REST.
2026-12-02 12:24:54 -05:00

100 lines
3.8 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package mcp
import (
"context"
"encoding/json"
"strconv"
"strings"
)
func apiTools() []*tool {
list := &tool{
name: "beacon_list_operations", title: "List API operations",
desc: "The full set of public read operations behind this server, for the rare question the other tools do not cover. Without operation it lists IDs and summaries; with one it returns that operation's parameters. Run one with beacon_call_operation.",
args: []arg{
{name: "group", kind: "string", desc: "Only operations in this group, e.g. nodes, stats or routes."},
{name: "operation", kind: "string", desc: "An operation ID, to see its parameters."},
},
run: func(_ context.Context, s *service, a args) (*envelope, error) {
if id := a.str("operation"); id != "" {
op, ok := s.ops[id]
if !ok {
return &envelope{Status: "not_found", Summary: "There is no operation " + quote(id) + ". Call beacon_list_operations with no arguments to see them."}, nil
}
return &envelope{Data: op, Summary: op.ID + ": " + op.Summary}, nil
}
group := strings.ToLower(a.str("group"))
rows := []map[string]any{}
for _, id := range sortedOperationIDs(s.ops) {
if group != "" && !strings.HasPrefix(id, group+".") {
continue
}
rows = append(rows, map[string]any{"id": id, "summary": s.ops[id].Summary})
}
if len(rows) == 0 {
return &envelope{Status: "not_found", Summary: "No operations in group " + quote(group) + "."}, nil
}
return &envelope{Data: rows, Summary: strconv.Itoa(len(rows)) + " operation(s). Pass one as operation to see its parameters."}, nil
},
}
// call_operation takes a free-form object, so it declares its own schema.
call := &tool{
name: "beacon_call_operation", title: "Call an API operation",
desc: "Run one public read operation by ID with its documented parameters, and get the raw response (epoch-millisecond times, no trimming). Prefer the specific tools; use this only when none fits. See beacon_list_operations for IDs and parameters. Lists are capped at 50 rows.",
args: []arg{
{name: "operation", kind: "string", required: true, desc: "Operation ID from beacon_list_operations, e.g. nodes.neighbors."},
{name: "parameters", kind: "object", desc: "The operation's path and query parameters by name."},
},
run: func(ctx context.Context, s *service, a args) (*envelope, error) {
op, ok := s.ops[a.str("operation")]
if !ok {
return &envelope{Status: "not_found", Summary: "There is no operation " + quote(a.str("operation")) + ". Call beacon_list_operations to see them."}, nil
}
params, _ := a["parameters"].(map[string]any)
path, query, err := op.build(params)
if err != nil {
return nil, err
}
raw, err := s.get(ctx, path, query)
if err != nil {
return nil, err
}
sh, err := shape(raw, true)
if err != nil {
return nil, err
}
env := &envelope{Scope: map[string]any{"operation": op.ID}, Summary: op.Summary}
data := sh.value
// A paged answer keeps its own cursor fields; only a bare list is cut here.
if list, page := truncateList(data, maxOperationLimit); page != nil {
data, env.Page = list, page
env.Notes = append(env.Notes, "The list was cut at "+strconv.Itoa(maxOperationLimit)+" rows. Narrow the filters to see the rest.")
}
env.Data = data
if sh.userText {
env.Notes = append(env.Notes, untrustedNote)
}
return env, nil
},
}
return []*tool{list, call}
}
// objectArg reads a free-form object argument.
func objectArg(v any) (map[string]any, bool) {
switch x := v.(type) {
case map[string]any:
return x, true
case string:
// Some clients send nested objects as JSON text.
out := map[string]any{}
if json.Unmarshal([]byte(x), &out) == nil {
return out, true
}
}
return nil, false
}