5e0417306b
Refresh called alive() with the manager lock held, so a slow health check blocked every other server. It now snapshots the candidates and asks outside the lock. A server that cannot be dialled was retried every minute forever, which for a misconfigured stdio block means re-exec'ing a process 1440 times a day. Dials now back off from one minute to thirty. An allow_private fetcher followed redirects. A LAN MCP endpoint could answer a POST with a redirect to 169.254.169.254 and the guard would go there, because allow_private is what turns the address check off. Redirects are refused outright on that door. The tool catalogue was trimmed by taking the first max_tools entries of whatever order the server sent, so the server chose which of its tools Maven proposed. Over the cap without allow_tools now contributes nothing: refusing is honest, silently keeping the server's pick is not. Descriptions are server-written text that lands in the router prompt and on /tools, so they are capped too. A server block with enabled false was skipped by validation, so a typo in a block written dark surfaced only on the day it was switched on. All blocks are shape-checked now. Configured static headers carry the bearer token a real remote server needs, and host_interval bounds how fast one endpoint is polled. Found in review of #70.
124 lines
4.0 KiB
Go
124 lines
4.0 KiB
Go
package config
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestMCPAbsentIsOff(t *testing.T) {
|
|
c, err := Load(writeConfig(t, `{}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.MCP != nil {
|
|
t.Error("no mcp block ⇒ nil")
|
|
}
|
|
if got := c.MCPServers(); got != nil {
|
|
t.Errorf("MCPServers() = %+v, want nil", got)
|
|
}
|
|
}
|
|
|
|
// A described-but-not-enabled server must not be wired. This is how a block can
|
|
// sit in the config file, reviewed, before it is switched on.
|
|
func TestMCPDisabledServerIsOff(t *testing.T) {
|
|
c, err := Load(writeConfig(t, `{"mcp":{"servers":[
|
|
{"name":"vikunja","url":"http://localhost:9100/mcp","allow_private":true}]}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := c.MCPServers(); len(got) != 0 {
|
|
t.Errorf("MCPServers() = %+v", got)
|
|
}
|
|
}
|
|
|
|
// A block with no servers at all is the same as no block.
|
|
func TestMCPEmptyBlockNormalisesToNil(t *testing.T) {
|
|
c, err := Load(writeConfig(t, `{"mcp":{"servers":[]}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.MCP != nil {
|
|
t.Errorf("mcp = %+v, want nil", c.MCP)
|
|
}
|
|
}
|
|
|
|
// A server that is written but not switched on is still shape-checked. The
|
|
// review the config layer can give is the point of writing a block dark, and
|
|
// skipping it meant a typo only surfaced on the day it was enabled.
|
|
func TestMCPDisabledServerIsStillValidated(t *testing.T) {
|
|
cases := map[string]string{
|
|
"both": `{"mcp":{"servers":[{"name":"a","command":"x","url":"http://a.test"}]}}`,
|
|
"bare host": `{"mcp":{"servers":[{"name":"a","url":"a.test"}]}}`,
|
|
"no name": `{"mcp":{"servers":[{"command":"x"}]}}`,
|
|
"duplicates": `{"mcp":{"servers":[{"name":"a","command":"x"},{"name":"a","command":"y"}]}}`,
|
|
}
|
|
for name, body := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
if _, err := Load(writeConfig(t, body)); err == nil {
|
|
t.Fatal("a dark server with a typo must fail at startup")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Headers carry a bearer token to a real remote server.
|
|
func TestMCPServerHeaders(t *testing.T) {
|
|
c, err := Load(writeConfig(t, `{"mcp":{"servers":[
|
|
{"name":"remote","url":"https://mcp.example.test/mcp","enabled":true,
|
|
"headers":{"Authorization":"Bearer sekret"}}]}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := c.MCPServers()
|
|
if len(got) != 1 || got[0].Headers["Authorization"] != "Bearer sekret" {
|
|
t.Fatalf("headers not mapped: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestMCPEnabledServerMapping(t *testing.T) {
|
|
c, err := Load(writeConfig(t, `{"mcp":{
|
|
"timeout":"5s",
|
|
"servers":[
|
|
{"name":"vikunja","url":"http://localhost:9100/mcp","allow_private":true,
|
|
"allow_tools":["list_tasks"],"max_tools":3,"enabled":true},
|
|
{"name":"files","command":"mcp-server-fs","args":["/srv"],"timeout":"1s","enabled":true},
|
|
{"name":"off","command":"nope"}
|
|
]}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := c.MCPServers()
|
|
if len(got) != 2 {
|
|
t.Fatalf("servers = %+v", got)
|
|
}
|
|
if got[0].Name != "vikunja" || !got[0].AllowPrivate || got[0].MaxTools != 3 ||
|
|
len(got[0].AllowTools) != 1 || got[0].Timeout != 5*time.Second {
|
|
t.Errorf("vikunja mapped wrong: %+v", got[0])
|
|
}
|
|
if got[1].Command != "mcp-server-fs" || len(got[1].Args) != 1 || got[1].Timeout != time.Second {
|
|
t.Errorf("files mapped wrong: %+v", got[1])
|
|
}
|
|
// allow_private is per server and must not leak to the other one.
|
|
if got[1].AllowPrivate {
|
|
t.Error("allow_private leaked between servers")
|
|
}
|
|
}
|
|
|
|
func TestMCPBadServerFailsAtStartup(t *testing.T) {
|
|
cases := map[string]string{
|
|
"no name": `{"mcp":{"servers":[{"command":"x","enabled":true}]}}`,
|
|
"both": `{"mcp":{"servers":[{"name":"a","command":"x","url":"http://a.test","enabled":true}]}}`,
|
|
"neither": `{"mcp":{"servers":[{"name":"a","enabled":true}]}}`,
|
|
"bad scheme": `{"mcp":{"servers":[{"name":"a","url":"unix:///run/x.sock","enabled":true}]}}`,
|
|
"duplicate": `{"mcp":{"servers":[{"name":"a","command":"x","enabled":true},{"name":"a","command":"y","enabled":true}]}}`,
|
|
"spacey name": `{"mcp":{"servers":[{"name":"a b","command":"x","enabled":true}]}}`,
|
|
}
|
|
for name, body := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
if _, err := Load(writeConfig(t, body)); err == nil {
|
|
t.Fatal("want a startup error")
|
|
}
|
|
})
|
|
}
|
|
}
|