Company MCP servers are registered with ctl mcp, and tools granted with keys limits --tools
CI / test (push) Failing after 5m4s
CI / test (push) Failing after 5m4s
llmgw ctl mcp add|list|remove|test and /api/v1/mcp/servers keep the servers; a token given with --secret-file is kept in the store, sealed, and named by reference. keys limits --tools, and the console's limits form, grant tools as patterns. Documented in install.md and clients.md. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ca77637a71
commit
4453f469e1
16 files changed
+893
-9
No files matched your search
@@ -77,6 +77,7 @@ type API struct {
|
||||
train *TrainAdmin
|
||||
settings *Settings
|
||||
auth *AuthAdmin
|
||||
mcp MCPStore
|
||||
// restartAgents tells every connected agent to start again.
|
||||
restartAgents func(reason string) []string
|
||||
// revoke records a removed node, so its certificate is refused.
|
||||
@@ -131,6 +132,7 @@ func (a *API) Register(mux Mux) {
|
||||
a.registerGuard(mux)
|
||||
a.registerSettings(mux)
|
||||
a.registerAuth(mux)
|
||||
a.registerMCP(mux)
|
||||
mux.HandleFunc("POST /api/v1/agents/restart", a.restartAgentsHandler)
|
||||
mux.HandleFunc("GET /api/v1/audit", a.auditEvents)
|
||||
mux.HandleFunc("GET /api/v1/usage/export", a.usageExport)
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
package adminapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/mcpclient"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/secrets"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// MCPStore keeps the company's MCP servers and the secrets their headers
|
||||
// carry.
|
||||
type MCPStore interface {
|
||||
MCPServers(ctx context.Context) ([]store.MCPServer, error)
|
||||
MCPServer(ctx context.Context, name string) (store.MCPServer, error)
|
||||
PutMCPServer(ctx context.Context, m store.MCPServer) error
|
||||
DeleteMCPServer(ctx context.Context, name string) error
|
||||
SetClusterSecret(ctx context.Context, name, value string) error
|
||||
}
|
||||
|
||||
// SetMCP enables the endpoints for the company's MCP servers.
|
||||
func (a *API) SetMCP(st MCPStore) { a.mcp = st }
|
||||
|
||||
// MCPServerView is a server as the API shows it: header values only for the
|
||||
// headers sent as written, and for the others the reference read.
|
||||
type MCPServerView struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Headers map[string]string `json:"headers,omitempty"`
|
||||
HeaderRefs map[string]string `json:"header_refs,omitempty"`
|
||||
Enabled bool `json:"enabled"`
|
||||
TimeoutSeconds int `json:"timeout_seconds,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
CreatedBy string `json:"created_by,omitempty"`
|
||||
}
|
||||
|
||||
// MCPServerRequest adds a server or changes one. Secrets are header values
|
||||
// to keep in the store, sealed, each then read by reference; HeaderRefs name
|
||||
// secrets kept elsewhere (a file, env:, vault:).
|
||||
type MCPServerRequest struct {
|
||||
URL string `json:"url"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Headers map[string]string `json:"headers,omitempty"`
|
||||
HeaderRefs map[string]string `json:"header_refs,omitempty"`
|
||||
Secrets map[string]string `json:"secrets,omitempty"`
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
TimeoutSeconds int `json:"timeout_seconds,omitempty"`
|
||||
}
|
||||
|
||||
// MCPTestView is what the server said when asked: its tools, or why not.
|
||||
type MCPTestView struct {
|
||||
OK bool `json:"ok"`
|
||||
Tools []string `json:"tools,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func mcpServerView(m store.MCPServer) MCPServerView {
|
||||
return MCPServerView{Name: m.Name, URL: m.URL, Description: m.Description, Headers: m.Headers,
|
||||
HeaderRefs: m.HeaderRefs, Enabled: m.Enabled, TimeoutSeconds: int(m.Timeout / time.Second),
|
||||
CreatedAt: m.CreatedAt, CreatedBy: m.CreatedBy}
|
||||
}
|
||||
|
||||
func (a *API) registerMCP(mux Mux) {
|
||||
need := func(w http.ResponseWriter) bool {
|
||||
if a.mcp == nil {
|
||||
writeError(w, http.StatusNotImplemented, "this control plane has no store to keep MCP servers in")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
mux.HandleFunc("GET /api/v1/mcp/servers", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !need(w) {
|
||||
return
|
||||
}
|
||||
list, err := a.mcp.MCPServers(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
out := make([]MCPServerView, len(list))
|
||||
for i, m := range list {
|
||||
out[i] = mcpServerView(m)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
})
|
||||
mux.HandleFunc("PUT /api/v1/mcp/servers/{name}", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !need(w) {
|
||||
return
|
||||
}
|
||||
name := r.PathValue("name")
|
||||
var req MCPServerRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
m, err := a.mcpServerFrom(r.Context(), name, req, actorOf(r))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if err := a.mcp.PutMCPServer(r.Context(), m); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, mcpServerView(m))
|
||||
})
|
||||
mux.HandleFunc("DELETE /api/v1/mcp/servers/{name}", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !need(w) {
|
||||
return
|
||||
}
|
||||
switch err := a.mcp.DeleteMCPServer(r.Context(), r.PathValue("name")); {
|
||||
case errors.Is(err, store.ErrNotFound):
|
||||
writeError(w, http.StatusNotFound, "no MCP server "+r.PathValue("name"))
|
||||
case err != nil:
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
default:
|
||||
writeJSON(w, http.StatusOK, map[string]string{"removed": r.PathValue("name")})
|
||||
}
|
||||
})
|
||||
mux.HandleFunc("POST /api/v1/mcp/servers/{name}/test", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !need(w) {
|
||||
return
|
||||
}
|
||||
m, err := a.mcp.MCPServer(r.Context(), r.PathValue("name"))
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
writeError(w, http.StatusNotFound, "no MCP server "+r.PathValue("name"))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, testMCPServer(r.Context(), m))
|
||||
})
|
||||
}
|
||||
|
||||
// mcpServerFrom is the server a request makes, over the one of that name if
|
||||
// there is one: its secrets kept in the store, and named by reference.
|
||||
func (a *API) mcpServerFrom(ctx context.Context, name string, req MCPServerRequest, by string) (store.MCPServer, error) {
|
||||
if !store.ValidMCPServerName(name) {
|
||||
return store.MCPServer{}, errors.New("a server's name is lowercase letters, digits, - and _, starting with a letter: " +
|
||||
"its tools are NAME.tool")
|
||||
}
|
||||
m, err := a.mcp.MCPServer(ctx, name)
|
||||
switch {
|
||||
case errors.Is(err, store.ErrNotFound):
|
||||
m = store.MCPServer{Name: name, Enabled: true, CreatedBy: by}
|
||||
case err != nil:
|
||||
return m, err
|
||||
}
|
||||
if req.URL != "" {
|
||||
m.URL = req.URL
|
||||
}
|
||||
if u, err := url.Parse(m.URL); err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" {
|
||||
return m, errors.New("the server's url is an http:// or https:// address")
|
||||
}
|
||||
if req.Description != "" {
|
||||
m.Description = req.Description
|
||||
}
|
||||
if req.Headers != nil {
|
||||
m.Headers = req.Headers
|
||||
}
|
||||
if req.HeaderRefs != nil {
|
||||
m.HeaderRefs = req.HeaderRefs
|
||||
}
|
||||
if m.HeaderRefs == nil {
|
||||
m.HeaderRefs = map[string]string{}
|
||||
}
|
||||
for header, value := range req.Secrets {
|
||||
if strings.TrimSpace(header) == "" || value == "" {
|
||||
return m, errors.New("a secret header needs a name and a value")
|
||||
}
|
||||
key := "mcp/" + name + "/" + header
|
||||
if err := a.mcp.SetClusterSecret(ctx, key, value); err != nil {
|
||||
return m, err
|
||||
}
|
||||
m.HeaderRefs[header] = "store:" + key
|
||||
delete(m.Headers, header)
|
||||
}
|
||||
if req.Enabled != nil {
|
||||
m.Enabled = *req.Enabled
|
||||
}
|
||||
if req.TimeoutSeconds > 0 {
|
||||
m.Timeout = time.Duration(req.TimeoutSeconds) * time.Second
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// testMCPServer asks a server for its tools, with its credentials, as /mcp
|
||||
// would.
|
||||
func testMCPServer(ctx context.Context, m store.MCPServer) MCPTestView {
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
c := &mcpclient.Client{URL: m.URL, Header: func(ctx context.Context) (http.Header, error) {
|
||||
h := http.Header{}
|
||||
for k, v := range m.Headers {
|
||||
h.Set(k, v)
|
||||
}
|
||||
for k, ref := range m.HeaderRefs {
|
||||
v, err := secrets.ReadString(ctx, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.Set(k, v)
|
||||
}
|
||||
return h, nil
|
||||
}}
|
||||
tools, err := c.Tools(ctx)
|
||||
if err != nil {
|
||||
return MCPTestView{Error: err.Error()}
|
||||
}
|
||||
v := MCPTestView{OK: true}
|
||||
for _, t := range tools {
|
||||
v.Tools = append(v.Tools, m.Name+"."+t.Name)
|
||||
}
|
||||
sort.Strings(v.Tools)
|
||||
return v
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package adminapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/mcpclient/mcptest"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/secrets"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
type memMCP struct {
|
||||
mu sync.Mutex
|
||||
servers map[string]store.MCPServer
|
||||
secrets map[string]string
|
||||
}
|
||||
|
||||
func (m *memMCP) MCPServers(context.Context) ([]store.MCPServer, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
var out []store.MCPServer
|
||||
for _, s := range m.servers {
|
||||
out = append(out, s)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *memMCP) MCPServer(_ context.Context, name string) (store.MCPServer, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
s, ok := m.servers[name]
|
||||
if !ok {
|
||||
return s, store.ErrNotFound
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (m *memMCP) PutMCPServer(_ context.Context, s store.MCPServer) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.servers[s.Name] = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memMCP) DeleteMCPServer(_ context.Context, name string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if _, ok := m.servers[name]; !ok {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
delete(m.servers, name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memMCP) SetClusterSecret(_ context.Context, name, value string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.secrets[name] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestAnMCPServerIsRegisteredWithItsTokenKeptAsASecretAndTried(t *testing.T) {
|
||||
srv := mcptest.New()
|
||||
defer srv.Close()
|
||||
a, _ := setup(t, testModels())
|
||||
st := &memMCP{servers: map[string]store.MCPServer{}, secrets: map[string]string{}}
|
||||
a.SetMCP(st)
|
||||
secrets.SetStore(func(_ context.Context, name string) (string, error) {
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
return st.secrets[name], nil
|
||||
})
|
||||
t.Cleanup(func() { secrets.SetStore(nil) })
|
||||
|
||||
var v MCPServerView
|
||||
body := `{"url":"` + srv.URL + `","description":"tickets","headers":{"X-Org":"acme"},"secrets":{"Authorization":"Bearer t0ken"}}`
|
||||
if code := call(t, a, "PUT", "/api/v1/mcp/servers/jira", body, &v); code != 200 {
|
||||
t.Fatalf("add: %d", code)
|
||||
}
|
||||
// The token is in the store, named by reference; the row has none.
|
||||
if st.secrets["mcp/jira/Authorization"] != "Bearer t0ken" || v.HeaderRefs["Authorization"] != "store:mcp/jira/Authorization" ||
|
||||
strings.Contains(v.Headers["Authorization"], "t0ken") || !v.Enabled {
|
||||
t.Errorf("view %+v, secrets %v", v, st.secrets)
|
||||
}
|
||||
var tv MCPTestView
|
||||
if code := call(t, a, "POST", "/api/v1/mcp/servers/jira/test", "", &tv); code != 200 || !tv.OK || len(tv.Tools) != 3 || tv.Tools[1] != "jira.echo" {
|
||||
t.Errorf("test: %d %+v", code, tv)
|
||||
}
|
||||
if code := call(t, a, "PUT", "/api/v1/mcp/servers/Bad.Name", body, nil); code != 400 {
|
||||
t.Errorf("a name with a dot: %d", code)
|
||||
}
|
||||
if code := call(t, a, "PUT", "/api/v1/mcp/servers/x", `{"url":"ftp://x"}`, nil); code != 400 {
|
||||
t.Errorf("not an http address: %d", code)
|
||||
}
|
||||
// Switched off, kept; then removed.
|
||||
if code := call(t, a, "PUT", "/api/v1/mcp/servers/jira", `{"enabled":false}`, &v); code != 200 || v.Enabled || v.URL != srv.URL {
|
||||
t.Errorf("off: %d %+v", code, v)
|
||||
}
|
||||
if code := call(t, a, "DELETE", "/api/v1/mcp/servers/jira", "", nil); code != 200 {
|
||||
t.Errorf("remove: %d", code)
|
||||
}
|
||||
if code := call(t, a, "DELETE", "/api/v1/mcp/servers/jira", "", nil); code != 404 {
|
||||
t.Errorf("remove again: %d", code)
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,7 @@ var PathParams = map[string]string{
|
||||
"presets/{name}": "The preset's name, as `llmgw ctl model presets` lists it.",
|
||||
"nodes/{id}": "The node's ID, as `llmgw ctl nodes` lists it.",
|
||||
"secrets/{name}": "ldap-bind, oidc-client-secret or scim-token.",
|
||||
"servers/{name}": "The MCP server's name: its tools are NAME.tool in /mcp.",
|
||||
"jobs/{id}": "The job's ID, as starting it answered.",
|
||||
"runs/{id}": "The quality run's ID, as starting it answered.",
|
||||
"requests/{id}": "The request's ID: the x-request-id a client was answered with.",
|
||||
@@ -406,6 +407,16 @@ var Routes = []Route{
|
||||
{Method: "PUT", Path: "/api/v1/auth/secrets/{name}", Summary: "Keep ldap-bind, oidc-client-secret or scim-token in the store, for settings to name as store:auth/NAME",
|
||||
Request: SecretRequest{}, Response: map[string]string{}},
|
||||
|
||||
// The company's own MCP servers, served through /mcp.
|
||||
{Method: "GET", Path: "/api/v1/mcp/servers", Summary: "The company's MCP servers /mcp serves, and how each is reached",
|
||||
Response: []MCPServerView{}},
|
||||
{Method: "PUT", Path: "/api/v1/mcp/servers/{name}", Summary: "Add an MCP server, or change one: its address, headers and the secrets they carry",
|
||||
Request: MCPServerRequest{}, Response: MCPServerView{}},
|
||||
{Method: "DELETE", Path: "/api/v1/mcp/servers/{name}", Summary: "Remove an MCP server: its tools leave /mcp",
|
||||
Response: map[string]string{}},
|
||||
{Method: "POST", Path: "/api/v1/mcp/servers/{name}/test", Summary: "Ask an MCP server for its tools, with its credentials, as /mcp would",
|
||||
Response: MCPTestView{}},
|
||||
|
||||
// Usage, requests and the record of changes.
|
||||
{Method: "GET", Path: "/api/v1/usage", Summary: "What each group of the tenancy tree spent",
|
||||
Query: []Param{
|
||||
|
||||
Reference in new issue
Block a user