mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
feat(mcp): probe servers live with mcp check --live
The static check can say a variable is set and a command is on PATH, but not whether the server actually starts and speaks MCP, which is the other half of "synced, but the Agent does not see it". --live runs after the static checks and only for servers without a static error. A stdio server is started with the current environment plus its env and fromEnv values, probed with server/discover and, for servers before 2026-07-28, the initialize handshake, then stopped: stdin closed, then SIGTERM and SIGKILL to its process group. A remote server gets a Streamable HTTP POST with its resolved headers; a 401 reports the WWW-Authenticate resource metadata as a warning and never signs in. Each probe has one timeout (--timeout, default 10s), a few run at once, and configured values are redacted from every message. Without --live nothing is started and no request is sent, and the dashboard API stays static. Refs #141
This commit is contained in:
@@ -325,7 +325,8 @@ func printMCPHelp() {
|
||||
|
||||
Commands:
|
||||
add [name] Guided setup, or --url URL / -- command args...
|
||||
check [name...] Verify variables, commands, hosts and sync state (--no-dns)
|
||||
check [name...] Verify variables, commands, hosts and sync state (--no-dns);
|
||||
--live also starts or calls each server [--timeout 10s]
|
||||
edit [name] Interactive editor, or update --url / --target / -- command
|
||||
import [name] Import --from <client> or --file <JSON/TOML/YAML file>
|
||||
list Browse connections and per-client sync status (default)
|
||||
|
||||
@@ -3,29 +3,54 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"skillshare/internal/mcp"
|
||||
"skillshare/internal/theme"
|
||||
"skillshare/internal/ui"
|
||||
)
|
||||
|
||||
// runMCPCheck is read-only: it writes no file and no operation log entry.
|
||||
const mcpCheckUsage = "skillshare mcp check [name...] [--json] [--no-dns] [--live [--timeout <duration>]]"
|
||||
|
||||
// runMCPCheck is read-only: it writes no file and no operation log entry. Only --live
|
||||
// starts servers or sends requests to them.
|
||||
func runMCPCheck(service *mcp.Service, args []string) error {
|
||||
var opts mcp.CheckOptions
|
||||
opts := mcp.CheckOptions{ClientVersion: version}
|
||||
asJSON := false
|
||||
for _, arg := range args {
|
||||
switch arg {
|
||||
case "--json":
|
||||
timeout := ""
|
||||
for i := 0; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
switch {
|
||||
case arg == "--json":
|
||||
asJSON = true
|
||||
case "--no-dns":
|
||||
case arg == "--no-dns":
|
||||
opts.SkipDNS = true
|
||||
default:
|
||||
if strings.HasPrefix(arg, "-") {
|
||||
return fmt.Errorf("unknown mcp check argument %q; usage: skillshare mcp check [name...] [--json] [--no-dns]", arg)
|
||||
case arg == "--live":
|
||||
opts.Live = true
|
||||
case arg == "--timeout":
|
||||
if i+1 >= len(args) {
|
||||
return fmt.Errorf("--timeout requires a duration such as 10s")
|
||||
}
|
||||
i++
|
||||
timeout = args[i]
|
||||
case strings.HasPrefix(arg, "--timeout="):
|
||||
timeout = strings.TrimPrefix(arg, "--timeout=")
|
||||
case strings.HasPrefix(arg, "-"):
|
||||
return fmt.Errorf("unknown mcp check argument %q; usage: %s", arg, mcpCheckUsage)
|
||||
default:
|
||||
opts.Names = append(opts.Names, arg)
|
||||
}
|
||||
}
|
||||
if timeout != "" {
|
||||
if !opts.Live {
|
||||
return fmt.Errorf("--timeout only applies with --live")
|
||||
}
|
||||
d, err := time.ParseDuration(timeout)
|
||||
if err != nil || d <= 0 {
|
||||
return fmt.Errorf("--timeout needs a positive duration such as 10s or 1m, got %q", timeout)
|
||||
}
|
||||
opts.Timeout = d
|
||||
}
|
||||
report, err := service.Check(opts)
|
||||
if err != nil {
|
||||
if asJSON {
|
||||
|
||||
+13
-1
@@ -32,6 +32,8 @@ type CheckServer struct {
|
||||
Project string `json:"project,omitempty"`
|
||||
OK bool `json:"ok"`
|
||||
Findings []CheckFinding `json:"findings"`
|
||||
// Live is what the server reported about itself; only CheckOptions.Live probes it.
|
||||
Live *CheckLive `json:"live,omitempty"`
|
||||
}
|
||||
|
||||
type CheckSummary struct {
|
||||
@@ -53,6 +55,13 @@ type CheckOptions struct {
|
||||
LookupEnv func(string) (string, bool)
|
||||
LookPath func(string) (string, error)
|
||||
LookupHost func(context.Context, string) ([]string, error)
|
||||
// Live starts each stdio server and sends requests to each remote one, after the
|
||||
// static checks; nothing else in a check does either.
|
||||
Live bool
|
||||
// Timeout bounds each live probe; zero means DefaultLiveTimeout.
|
||||
Timeout time.Duration
|
||||
// ClientVersion is the clientInfo version a live probe sends.
|
||||
ClientVersion string
|
||||
}
|
||||
|
||||
// dnsTimeout bounds each host lookup, the only network access a check makes.
|
||||
@@ -60,7 +69,7 @@ const dnsTimeout = 3 * time.Second
|
||||
|
||||
// Check verifies the source's servers, including those under each mcp.projects root, without
|
||||
// starting them or sending requests: referenced variables are set, commands resolve, hosts
|
||||
// resolve, and the plan has each entry in sync.
|
||||
// resolve, and the plan has each entry in sync. Only opts.Live then probes the servers.
|
||||
func (s *Service) Check(opts CheckOptions) (*CheckReport, error) {
|
||||
if opts.LookupEnv == nil {
|
||||
opts.LookupEnv = os.LookupEnv
|
||||
@@ -172,6 +181,9 @@ func (s *Service) Check(opts CheckOptions) (*CheckReport, error) {
|
||||
result := byKey[checkKey{c.Root, c.Name}]
|
||||
result.Findings = append(result.Findings, syncFinding(c))
|
||||
}
|
||||
if opts.Live {
|
||||
s.checkLive(source, report, opts)
|
||||
}
|
||||
for i := range report.Servers {
|
||||
result := &report.Servers[i]
|
||||
result.OK = true
|
||||
|
||||
@@ -0,0 +1,745 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A live probe speaks MCP revision 2026-07-28 and falls back to the initialize handshake of
|
||||
// 2025-11-25 for servers that predate server/discover, as the spec's backward-compatibility
|
||||
// rules describe.
|
||||
const (
|
||||
liveProtocolVersion = "2026-07-28"
|
||||
legacyProtocolVersion = "2025-11-25"
|
||||
// DefaultLiveTimeout bounds each server's probe unless CheckOptions.Timeout says otherwise.
|
||||
DefaultLiveTimeout = 10 * time.Second
|
||||
liveConcurrency = 4
|
||||
// liveGrace is how long a stopped server gets before the next, harder signal.
|
||||
liveGrace = 500 * time.Millisecond
|
||||
liveMaxMessage = 16 << 20
|
||||
liveMaxPages = 50
|
||||
liveStderrLines = 5
|
||||
)
|
||||
|
||||
// CheckLive is what a server said about itself when probed. Every field is self-reported.
|
||||
type CheckLive struct {
|
||||
ProtocolVersion string `json:"protocolVersion"`
|
||||
ServerInfo LiveServerInfo `json:"serverInfo"`
|
||||
Tools int `json:"tools"`
|
||||
}
|
||||
|
||||
type LiveServerInfo struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// checkLive probes, a few at a time, each selected server that passed the static checks.
|
||||
func (s *Service) checkLive(source *Source, report *CheckReport, opts CheckOptions) {
|
||||
timeout := opts.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = DefaultLiveTimeout
|
||||
}
|
||||
slots := make(chan struct{}, liveConcurrency)
|
||||
var wg sync.WaitGroup
|
||||
for i := range report.Servers {
|
||||
result := &report.Servers[i]
|
||||
server := checkKey{result.Project, result.Name}.server(source)
|
||||
if reason := liveSkipReason(server, result.Findings); reason != "" {
|
||||
result.Findings = append(result.Findings, CheckFinding{Level: "info", Check: "live", Message: reason})
|
||||
continue
|
||||
}
|
||||
p := newLiveProbe(server, result.Project, timeout, opts)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
slots <- struct{}{}
|
||||
defer func() { <-slots }()
|
||||
live, finding := p.run()
|
||||
result.Live = live
|
||||
result.Findings = append(result.Findings, finding)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func liveSkipReason(server Server, findings []CheckFinding) string {
|
||||
if server.Disabled {
|
||||
return "not probed live: a disabled entry has no server to start"
|
||||
}
|
||||
for _, f := range findings {
|
||||
if f.Level == "error" {
|
||||
return "not probed live: fix the errors above first"
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type liveProbe struct {
|
||||
server Server
|
||||
dir string
|
||||
timeout time.Duration
|
||||
client LiveServerInfo
|
||||
env map[string]string
|
||||
headers http.Header
|
||||
// secrets are the resolved env, header and token values, longest first.
|
||||
secrets []string
|
||||
}
|
||||
|
||||
func newLiveProbe(server Server, root string, timeout time.Duration, opts CheckOptions) *liveProbe {
|
||||
p := &liveProbe{server: server, timeout: timeout, env: map[string]string{}, headers: http.Header{}}
|
||||
if root != "" {
|
||||
p.dir, _ = expandHome(root)
|
||||
}
|
||||
p.client = LiveServerInfo{Name: "skillshare", Version: opts.ClientVersion}
|
||||
if p.client.Version == "" {
|
||||
p.client.Version = "dev"
|
||||
}
|
||||
resolve := func(v Value) string {
|
||||
if v.FromEnv == "" {
|
||||
return v.Literal
|
||||
}
|
||||
value, _ := opts.LookupEnv(v.FromEnv)
|
||||
return value
|
||||
}
|
||||
// A value shorter than four characters is no credential, and hiding every "1" or
|
||||
// "on" would garble the rest of the message.
|
||||
keep := func(value string) {
|
||||
if len(value) >= 4 && !slices.Contains(p.secrets, value) {
|
||||
p.secrets = append(p.secrets, value)
|
||||
}
|
||||
}
|
||||
for key, v := range server.Env {
|
||||
p.env[key] = resolve(v)
|
||||
keep(p.env[key])
|
||||
}
|
||||
for key, v := range server.Headers {
|
||||
value := resolve(v)
|
||||
p.headers.Set(key, value)
|
||||
keep(value)
|
||||
}
|
||||
if server.BearerToken != nil {
|
||||
token := resolve(*server.BearerToken)
|
||||
p.headers.Set("Authorization", "Bearer "+token)
|
||||
keep(token)
|
||||
}
|
||||
slices.SortFunc(p.secrets, func(a, b string) int { return len(b) - len(a) })
|
||||
return p
|
||||
}
|
||||
|
||||
// redact removes every configured value from text a server or the network produced.
|
||||
func (p *liveProbe) redact(text string) string {
|
||||
for _, secret := range p.secrets {
|
||||
text = strings.ReplaceAll(text, secret, "[redacted]")
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (p *liveProbe) run() (*CheckLive, CheckFinding) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
|
||||
defer cancel()
|
||||
var live *CheckLive
|
||||
var err error
|
||||
if p.server.Command != "" {
|
||||
live, err = p.stdio(ctx)
|
||||
} else {
|
||||
live, err = p.http(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
var auth *authRequiredError
|
||||
if errors.As(err, &auth) {
|
||||
f := CheckFinding{Level: "warning", Check: "live", Message: "sign-in required (HTTP 401); skillshare does not sign in"}
|
||||
if auth.metadata != "" {
|
||||
f.Subject = p.redact(auth.metadata)
|
||||
f.Message = "sign-in required (HTTP 401): resource metadata at " + f.Subject + "; skillshare does not sign in"
|
||||
}
|
||||
return nil, f
|
||||
}
|
||||
return nil, CheckFinding{Level: "error", Check: "live", Message: p.redact("live probe failed: " + err.Error())}
|
||||
}
|
||||
live.ProtocolVersion = p.redact(live.ProtocolVersion)
|
||||
live.ServerInfo.Name = p.redact(live.ServerInfo.Name)
|
||||
live.ServerInfo.Version = p.redact(live.ServerInfo.Version)
|
||||
who := strings.TrimSpace(live.ServerInfo.Name + " " + live.ServerInfo.Version)
|
||||
if who == "" {
|
||||
who = "unnamed server"
|
||||
}
|
||||
return live, CheckFinding{Level: "info", Check: "live", Subject: live.ServerInfo.Name,
|
||||
Message: fmt.Sprintf("responds: %s, protocol %s, %d tool(s)", who, live.ProtocolVersion, live.Tools)}
|
||||
}
|
||||
|
||||
// rpcConn sends JSON-RPC over one transport. legacy switches it to a 2025-11-25 session.
|
||||
type rpcConn interface {
|
||||
call(ctx context.Context, method string, params any) (json.RawMessage, error)
|
||||
notify(ctx context.Context, method string, params any) error
|
||||
legacy(protocolVersion string)
|
||||
}
|
||||
|
||||
type rpcMessage struct {
|
||||
ID json.RawMessage `json:"id,omitempty"`
|
||||
Method string `json:"method,omitempty"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *rpcError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type rpcError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data json.RawMessage `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (e *rpcError) Error() string { return fmt.Sprintf("JSON-RPC error %d: %s", e.Code, e.Message) }
|
||||
|
||||
// modern reports a code from the range the MCP specification reserves, which only a
|
||||
// 2026-07-28 or later server sends; any other error to server/discover means a legacy server.
|
||||
func (e *rpcError) modern() bool { return e.Code <= -32020 && e.Code >= -32099 }
|
||||
|
||||
func rpcRequest(id int, method string, params any) map[string]any {
|
||||
return map[string]any{"jsonrpc": "2.0", "id": id, "method": method, "params": params}
|
||||
}
|
||||
|
||||
func (p *liveProbe) modernParams(cursor string) map[string]any {
|
||||
params := map[string]any{"_meta": map[string]any{
|
||||
"io.modelcontextprotocol/protocolVersion": liveProtocolVersion,
|
||||
"io.modelcontextprotocol/clientInfo": p.client,
|
||||
"io.modelcontextprotocol/clientCapabilities": map[string]any{},
|
||||
}}
|
||||
if cursor != "" {
|
||||
params["cursor"] = cursor
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
// callResult calls method and decodes a complete result into out.
|
||||
func callResult(ctx context.Context, conn rpcConn, method string, params, out any) error {
|
||||
raw, err := conn.call(ctx, method, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var kind struct {
|
||||
ResultType string `json:"resultType"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &kind); err != nil {
|
||||
return fmt.Errorf("%s returned an invalid result", method)
|
||||
}
|
||||
// Servers before 2026-07-28 leave resultType out; that means complete.
|
||||
if kind.ResultType != "" && kind.ResultType != "complete" {
|
||||
return fmt.Errorf("%s returned resultType %q", method, kind.ResultType)
|
||||
}
|
||||
if err := json.Unmarshal(raw, out); err != nil {
|
||||
return fmt.Errorf("%s returned an invalid result", method)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handshake asks the server who it is and counts its tools. discoverWait, when set, is how
|
||||
// long server/discover may go unanswered before the server counts as legacy (stdio).
|
||||
func (p *liveProbe) handshake(ctx context.Context, conn rpcConn, discoverWait time.Duration, fallback func(error) bool) (*CheckLive, error) {
|
||||
live, err := p.discover(ctx, conn, discoverWait, fallback)
|
||||
if err == nil && live == nil {
|
||||
live, err = p.initialize(ctx, conn)
|
||||
}
|
||||
if err != nil && ctx.Err() != nil {
|
||||
return nil, fmt.Errorf("no answer within %s", p.timeout)
|
||||
}
|
||||
return live, err
|
||||
}
|
||||
|
||||
// discover returns nil, nil when the server predates server/discover.
|
||||
func (p *liveProbe) discover(ctx context.Context, conn rpcConn, wait time.Duration, fallback func(error) bool) (*CheckLive, error) {
|
||||
dctx := ctx
|
||||
if wait > 0 {
|
||||
var cancel context.CancelFunc
|
||||
dctx, cancel = context.WithTimeout(ctx, wait)
|
||||
defer cancel()
|
||||
}
|
||||
var result struct {
|
||||
SupportedVersions []string `json:"supportedVersions"`
|
||||
Capabilities map[string]json.RawMessage `json:"capabilities"`
|
||||
Meta struct {
|
||||
ServerInfo LiveServerInfo `json:"io.modelcontextprotocol/serverInfo"`
|
||||
} `json:"_meta"`
|
||||
}
|
||||
err := callResult(dctx, conn, "server/discover", p.modernParams(""), &result)
|
||||
var rerr *rpcError
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.As(err, &rerr) && rerr.modern():
|
||||
var data struct {
|
||||
Supported []string `json:"supported"`
|
||||
}
|
||||
if rerr.Code == -32022 && json.Unmarshal(rerr.Data, &data) == nil && len(data.Supported) > 0 {
|
||||
return nil, unsupportedVersions(data.Supported)
|
||||
}
|
||||
return nil, err
|
||||
case errors.As(err, &rerr), fallback(err):
|
||||
return nil, nil
|
||||
case wait > 0 && dctx.Err() != nil && ctx.Err() == nil:
|
||||
return nil, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
if !slices.Contains(result.SupportedVersions, liveProtocolVersion) {
|
||||
return nil, unsupportedVersions(result.SupportedVersions)
|
||||
}
|
||||
live := &CheckLive{ProtocolVersion: liveProtocolVersion, ServerInfo: result.Meta.ServerInfo}
|
||||
if _, ok := result.Capabilities["tools"]; ok {
|
||||
live.Tools, err = countTools(ctx, conn, p.modernParams)
|
||||
}
|
||||
return live, err
|
||||
}
|
||||
|
||||
func unsupportedVersions(versions []string) error {
|
||||
return fmt.Errorf("server supports protocol version(s) %s; skillshare probes %s or the initialize handshake", strings.Join(versions, ", "), liveProtocolVersion)
|
||||
}
|
||||
|
||||
func (p *liveProbe) initialize(ctx context.Context, conn rpcConn) (*CheckLive, error) {
|
||||
conn.legacy("")
|
||||
var result struct {
|
||||
ProtocolVersion string `json:"protocolVersion"`
|
||||
ServerInfo LiveServerInfo `json:"serverInfo"`
|
||||
Capabilities map[string]json.RawMessage `json:"capabilities"`
|
||||
}
|
||||
params := map[string]any{"protocolVersion": legacyProtocolVersion, "capabilities": map[string]any{}, "clientInfo": p.client}
|
||||
if err := callResult(ctx, conn, "initialize", params, &result); err != nil {
|
||||
return nil, fmt.Errorf("initialize: %w", err)
|
||||
}
|
||||
conn.legacy(result.ProtocolVersion)
|
||||
if err := conn.notify(ctx, "notifications/initialized", map[string]any{}); err != nil {
|
||||
return nil, fmt.Errorf("notifications/initialized: %w", err)
|
||||
}
|
||||
live := &CheckLive{ProtocolVersion: result.ProtocolVersion, ServerInfo: result.ServerInfo}
|
||||
if _, ok := result.Capabilities["tools"]; ok {
|
||||
var err error
|
||||
live.Tools, err = countTools(ctx, conn, func(cursor string) map[string]any {
|
||||
if cursor == "" {
|
||||
return map[string]any{}
|
||||
}
|
||||
return map[string]any{"cursor": cursor}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return live, nil
|
||||
}
|
||||
|
||||
func countTools(ctx context.Context, conn rpcConn, params func(cursor string) map[string]any) (int, error) {
|
||||
count, cursor := 0, ""
|
||||
for range liveMaxPages {
|
||||
var page struct {
|
||||
Tools []json.RawMessage `json:"tools"`
|
||||
NextCursor string `json:"nextCursor"`
|
||||
}
|
||||
if err := callResult(ctx, conn, "tools/list", params(cursor), &page); err != nil {
|
||||
return 0, fmt.Errorf("tools/list: %w", err)
|
||||
}
|
||||
count += len(page.Tools)
|
||||
if page.NextCursor == "" {
|
||||
break
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// stdio starts the server, probes it over stdin/stdout, and always stops it again.
|
||||
func (p *liveProbe) stdio(ctx context.Context) (*CheckLive, error) {
|
||||
command, err := expandHome(p.server.Command)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd := exec.Command(command, p.server.Args...)
|
||||
cmd.Dir = p.dir
|
||||
cmd.Env = os.Environ()
|
||||
for _, key := range sortedKeys(p.env) {
|
||||
cmd.Env = append(cmd.Env, key+"="+p.env[key])
|
||||
}
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Plain pipes, so waiting for the server never waits for a descendant holding its output.
|
||||
outR, outW, err := os.Pipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer outR.Close()
|
||||
errR, errW, err := os.Pipe()
|
||||
if err != nil {
|
||||
outW.Close()
|
||||
return nil, err
|
||||
}
|
||||
cmd.Stdout, cmd.Stderr = outW, errW
|
||||
startInGroup(cmd)
|
||||
err = cmd.Start()
|
||||
outW.Close()
|
||||
errW.Close()
|
||||
if err != nil {
|
||||
errR.Close()
|
||||
return nil, fmt.Errorf("could not start %s: %w", p.server.Command, err)
|
||||
}
|
||||
stderr := &tailBuffer{}
|
||||
stderrDone := make(chan struct{})
|
||||
go func() {
|
||||
io.Copy(stderr, errR)
|
||||
errR.Close()
|
||||
close(stderrDone)
|
||||
}()
|
||||
exited := make(chan struct{})
|
||||
var waitErr error
|
||||
go func() {
|
||||
waitErr = cmd.Wait()
|
||||
close(exited)
|
||||
}()
|
||||
conn := newStdioConn(stdin, outR, exited, &waitErr)
|
||||
live, err := p.handshake(ctx, conn, p.timeout/3, func(error) bool { return false })
|
||||
stopGroup(cmd, stdin, exited)
|
||||
close(conn.stop)
|
||||
if err != nil {
|
||||
select {
|
||||
case <-stderrDone:
|
||||
case <-time.After(liveGrace):
|
||||
}
|
||||
if lines := stderr.lastLines(liveStderrLines); lines != "" {
|
||||
err = fmt.Errorf("%w; stderr: %s", err, lines)
|
||||
}
|
||||
}
|
||||
return live, err
|
||||
}
|
||||
|
||||
// stopGroup closes stdin, then terminates, then kills the server and everything it started.
|
||||
func stopGroup(cmd *exec.Cmd, stdin io.Closer, exited <-chan struct{}) {
|
||||
stdin.Close()
|
||||
select {
|
||||
case <-exited:
|
||||
case <-time.After(liveGrace):
|
||||
terminateGroup(cmd)
|
||||
select {
|
||||
case <-exited:
|
||||
case <-time.After(liveGrace):
|
||||
killGroup(cmd)
|
||||
<-exited
|
||||
}
|
||||
}
|
||||
// Descendants may outlive the server itself.
|
||||
killGroup(cmd)
|
||||
}
|
||||
|
||||
type stdioConn struct {
|
||||
stdin io.Writer
|
||||
msgs chan rpcMessage
|
||||
stop chan struct{}
|
||||
exited <-chan struct{}
|
||||
waitErr *error
|
||||
nextID int
|
||||
}
|
||||
|
||||
func newStdioConn(stdin io.Writer, stdout io.Reader, exited <-chan struct{}, waitErr *error) *stdioConn {
|
||||
c := &stdioConn{stdin: stdin, msgs: make(chan rpcMessage), stop: make(chan struct{}), exited: exited, waitErr: waitErr}
|
||||
go func() {
|
||||
defer close(c.msgs)
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
scanner.Buffer(make([]byte, 64<<10), liveMaxMessage)
|
||||
for scanner.Scan() {
|
||||
var msg rpcMessage
|
||||
// Only responses matter; lines that are not JSON-RPC are skipped.
|
||||
if json.Unmarshal(scanner.Bytes(), &msg) != nil || len(msg.ID) == 0 || msg.Method != "" {
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case c.msgs <- msg:
|
||||
case <-c.stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *stdioConn) write(message any) error {
|
||||
data, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := c.stdin.Write(append(data, '\n')); err != nil {
|
||||
return c.exitError()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *stdioConn) call(ctx context.Context, method string, params any) (json.RawMessage, error) {
|
||||
c.nextID++
|
||||
id := strconv.Itoa(c.nextID)
|
||||
if err := c.write(rpcRequest(c.nextID, method, params)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case msg, ok := <-c.msgs:
|
||||
if !ok {
|
||||
return nil, c.exitError()
|
||||
}
|
||||
if string(msg.ID) != id {
|
||||
continue
|
||||
}
|
||||
if msg.Error != nil {
|
||||
return nil, msg.Error
|
||||
}
|
||||
return msg.Result, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *stdioConn) notify(_ context.Context, method string, params any) error {
|
||||
return c.write(map[string]any{"jsonrpc": "2.0", "method": method, "params": params})
|
||||
}
|
||||
|
||||
func (c *stdioConn) legacy(string) {}
|
||||
|
||||
// exitError describes a server that stopped answering, with its exit status once known.
|
||||
func (c *stdioConn) exitError() error {
|
||||
select {
|
||||
case <-c.exited:
|
||||
if *c.waitErr != nil {
|
||||
return fmt.Errorf("server exited early (%v)", *c.waitErr)
|
||||
}
|
||||
return errors.New("server exited early")
|
||||
case <-time.After(liveGrace):
|
||||
return errors.New("server closed its output")
|
||||
}
|
||||
}
|
||||
|
||||
// tailBuffer keeps the end of a server's stderr.
|
||||
type tailBuffer struct {
|
||||
mu sync.Mutex
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (b *tailBuffer) Write(p []byte) (int, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.data = append(b.data, p...)
|
||||
if extra := len(b.data) - 64<<10; extra > 0 {
|
||||
b.data = b.data[extra:]
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (b *tailBuffer) lastLines(n int) string {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
var lines []string
|
||||
for _, line := range strings.Split(string(b.data), "\n") {
|
||||
if line = strings.TrimSpace(line); line != "" {
|
||||
if len(line) > 200 {
|
||||
line = line[:200] + "…"
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
}
|
||||
if len(lines) > n {
|
||||
lines = lines[len(lines)-n:]
|
||||
}
|
||||
return strings.Join(lines, " | ")
|
||||
}
|
||||
|
||||
// http probes a Streamable HTTP endpoint. It never follows an authorization challenge.
|
||||
func (p *liveProbe) http(ctx context.Context) (*CheckLive, error) {
|
||||
conn := &httpConn{client: &http.Client{}, url: p.server.URL, headers: p.headers, protocol: liveProtocolVersion}
|
||||
defer conn.close(ctx)
|
||||
return p.handshake(ctx, conn, 0, func(err error) bool {
|
||||
var status *httpStatusError
|
||||
return errors.As(err, &status) && status.fallsBack()
|
||||
})
|
||||
}
|
||||
|
||||
type authRequiredError struct{ metadata string }
|
||||
|
||||
func (e *authRequiredError) Error() string { return "sign-in required" }
|
||||
|
||||
type httpStatusError struct{ code int }
|
||||
|
||||
func (e *httpStatusError) Error() string {
|
||||
return fmt.Sprintf("HTTP %d %s", e.code, http.StatusText(e.code))
|
||||
}
|
||||
|
||||
// fallsBack reports the statuses a legacy server answers an unknown modern request with.
|
||||
func (e *httpStatusError) fallsBack() bool {
|
||||
return e.code == http.StatusBadRequest || e.code == http.StatusNotFound || e.code == http.StatusMethodNotAllowed
|
||||
}
|
||||
|
||||
type httpConn struct {
|
||||
client *http.Client
|
||||
url string
|
||||
headers http.Header
|
||||
protocol string
|
||||
session string
|
||||
nextID int
|
||||
}
|
||||
|
||||
func (c *httpConn) legacy(protocolVersion string) { c.protocol = protocolVersion }
|
||||
|
||||
func (c *httpConn) post(ctx context.Context, method string, message any) (*http.Response, error) {
|
||||
body, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for key, values := range c.headers {
|
||||
req.Header[key] = values
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json, text/event-stream")
|
||||
req.Header.Set("Mcp-Method", method)
|
||||
if c.protocol != "" {
|
||||
req.Header.Set("MCP-Protocol-Version", c.protocol)
|
||||
}
|
||||
if c.session != "" {
|
||||
req.Header.Set("Mcp-Session-Id", c.session)
|
||||
}
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if session := resp.Header.Get("Mcp-Session-Id"); session != "" {
|
||||
c.session = session
|
||||
}
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
resp.Body.Close()
|
||||
return nil, &authRequiredError{metadata: resourceMetadata(resp.Header.Values("WWW-Authenticate"))}
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *httpConn) call(ctx context.Context, method string, params any) (json.RawMessage, error) {
|
||||
c.nextID++
|
||||
resp, err := c.post(ctx, method, rpcRequest(c.nextID, method, params))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body := io.LimitReader(resp.Body, liveMaxMessage)
|
||||
status := &httpStatusError{code: resp.StatusCode}
|
||||
var msg *rpcMessage
|
||||
switch {
|
||||
case resp.StatusCode >= 200 && resp.StatusCode < 300:
|
||||
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") {
|
||||
msg, err = readSSE(body, strconv.Itoa(c.nextID))
|
||||
} else {
|
||||
msg = &rpcMessage{}
|
||||
err = json.NewDecoder(body).Decode(msg)
|
||||
}
|
||||
if err != nil || (msg.Error == nil && msg.Result == nil) {
|
||||
return nil, fmt.Errorf("%s: the response is not a JSON-RPC response", method)
|
||||
}
|
||||
case status.fallsBack():
|
||||
// A modern server explains a rejection in a JSON-RPC error body.
|
||||
msg = &rpcMessage{}
|
||||
if json.NewDecoder(body).Decode(msg) != nil || msg.Error == nil {
|
||||
return nil, status
|
||||
}
|
||||
default:
|
||||
return nil, status
|
||||
}
|
||||
if msg.Error != nil {
|
||||
return nil, msg.Error
|
||||
}
|
||||
return msg.Result, nil
|
||||
}
|
||||
|
||||
func (c *httpConn) notify(ctx context.Context, method string, params any) error {
|
||||
resp, err := c.post(ctx, method, map[string]any{"jsonrpc": "2.0", "method": method, "params": params})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return &httpStatusError{code: resp.StatusCode}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// close ends a 2025-11-25 session, which a stateless server never opens.
|
||||
func (c *httpConn) close(ctx context.Context) {
|
||||
if c.session == "" || ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.url, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for key, values := range c.headers {
|
||||
req.Header[key] = values
|
||||
}
|
||||
req.Header.Set("Mcp-Session-Id", c.session)
|
||||
if resp, err := c.client.Do(req); err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// readSSE returns the response with the given id from a request's event stream.
|
||||
func readSSE(r io.Reader, id string) (*rpcMessage, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
scanner.Buffer(make([]byte, 64<<10), liveMaxMessage)
|
||||
var data []string
|
||||
dispatch := func() *rpcMessage {
|
||||
defer func() { data = nil }()
|
||||
var msg rpcMessage
|
||||
if len(data) == 0 || json.Unmarshal([]byte(strings.Join(data, "\n")), &msg) != nil {
|
||||
return nil
|
||||
}
|
||||
if string(msg.ID) == id && msg.Method == "" {
|
||||
return &msg
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSuffix(scanner.Text(), "\r")
|
||||
switch {
|
||||
case line == "":
|
||||
if msg := dispatch(); msg != nil {
|
||||
return msg, nil
|
||||
}
|
||||
case strings.HasPrefix(line, "data:"):
|
||||
data = append(data, strings.TrimPrefix(strings.TrimPrefix(line, "data:"), " "))
|
||||
}
|
||||
}
|
||||
if msg := dispatch(); msg != nil {
|
||||
return msg, nil
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New("the event stream ended without a response")
|
||||
}
|
||||
|
||||
var resourceMetadataParam = regexp.MustCompile(`resource_metadata="([^"]*)"|resource_metadata=([^,\s]+)`)
|
||||
|
||||
// resourceMetadata is the Protected Resource Metadata URL a 401 challenge points to.
|
||||
func resourceMetadata(challenges []string) string {
|
||||
for _, challenge := range challenges {
|
||||
if m := resourceMetadataParam.FindStringSubmatch(challenge); m != nil {
|
||||
return m[1] + m[2]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const liveSecret = "s3cr3t-live-value"
|
||||
|
||||
// TestMCPLiveHelperServer is not a test: the live probe tests start this test binary as an
|
||||
// MCP server, selected by MCP_LIVE_HELPER, speaking the mode given after --.
|
||||
func TestMCPLiveHelperServer(t *testing.T) {
|
||||
if os.Getenv("MCP_LIVE_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
mode := os.Args[len(os.Args)-1]
|
||||
if marker := os.Getenv("MCP_LIVE_MARKER"); marker != "" {
|
||||
_ = os.WriteFile(marker, []byte("started"), 0600)
|
||||
}
|
||||
if os.Getenv("API_TOKEN") != liveSecret {
|
||||
fmt.Fprintln(os.Stderr, "API_TOKEN was not passed")
|
||||
os.Exit(4)
|
||||
}
|
||||
switch mode {
|
||||
case "crash":
|
||||
fmt.Fprintf(os.Stderr, "starting\nfatal: token %s rejected\n", os.Getenv("API_TOKEN"))
|
||||
os.Exit(3)
|
||||
case "hang":
|
||||
signal.Ignore(syscall.SIGTERM)
|
||||
if os.Getenv("MCP_LIVE_CHILD") == "" {
|
||||
child := exec.Command(os.Args[0], os.Args[1:]...)
|
||||
child.Env = append(os.Environ(), "MCP_LIVE_CHILD=1")
|
||||
if err := child.Start(); err != nil {
|
||||
os.Exit(5)
|
||||
}
|
||||
_ = os.WriteFile(os.Getenv("MCP_LIVE_PIDS"), fmt.Appendf(nil, "%d %d", os.Getpid(), child.Process.Pid), 0600)
|
||||
}
|
||||
select {}
|
||||
}
|
||||
out := json.NewEncoder(os.Stdout)
|
||||
reply := func(id json.RawMessage, result any) {
|
||||
_ = out.Encode(map[string]any{"jsonrpc": "2.0", "id": id, "result": result})
|
||||
}
|
||||
initialized := false
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
var msg struct {
|
||||
ID json.RawMessage `json:"id"`
|
||||
Method string `json:"method"`
|
||||
Params struct {
|
||||
Cursor string `json:"cursor"`
|
||||
} `json:"params"`
|
||||
}
|
||||
if json.Unmarshal(scanner.Bytes(), &msg) != nil {
|
||||
os.Exit(6)
|
||||
}
|
||||
switch {
|
||||
case mode == "modern" && msg.Method == "server/discover":
|
||||
reply(msg.ID, map[string]any{"resultType": "complete", "supportedVersions": []string{"2026-07-28"}, "capabilities": map[string]any{"tools": map[string]any{}},
|
||||
"_meta": map[string]any{"io.modelcontextprotocol/serverInfo": map[string]string{"name": "helper", "version": "1.2.3"}}})
|
||||
case mode == "modern" && msg.Method == "tools/list" && msg.Params.Cursor == "":
|
||||
reply(msg.ID, map[string]any{"resultType": "complete", "tools": []map[string]string{{"name": "a"}}, "nextCursor": "page2"})
|
||||
case mode == "modern" && msg.Method == "tools/list":
|
||||
reply(msg.ID, map[string]any{"resultType": "complete", "tools": []map[string]string{{"name": "b"}}})
|
||||
case msg.Method == "initialize":
|
||||
reply(msg.ID, map[string]any{"protocolVersion": "2025-11-25", "capabilities": map[string]any{"tools": map[string]any{}}, "serverInfo": map[string]string{"name": "legacy", "version": "0.9"}})
|
||||
case msg.Method == "notifications/initialized":
|
||||
initialized = true
|
||||
case msg.Method == "tools/list" && initialized:
|
||||
reply(msg.ID, map[string]any{"tools": []map[string]string{{"name": "a"}, {"name": "b"}, {"name": "c"}}})
|
||||
case mode == "silent":
|
||||
// Some legacy servers never answer a request they do not know.
|
||||
default:
|
||||
_ = out.Encode(map[string]any{"jsonrpc": "2.0", "id": msg.ID, "error": map[string]any{"code": -32601, "message": "Method not found"}})
|
||||
}
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// liveConfig declares one stdio server that runs the helper above in mode.
|
||||
func liveConfig(mode string, env map[string]string) string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "mcp:\n targets: [claude]\n servers:\n fake:\n command: %q\n args: [%q, \"--\", %q]\n env:\n MCP_LIVE_HELPER: \"1\"\n API_TOKEN: {fromEnv: LIVE_TOKEN}\n", os.Args[0], "-test.run=^TestMCPLiveHelperServer$", mode)
|
||||
for _, key := range sortedKeys(env) {
|
||||
fmt.Fprintf(&b, " %s: %q\n", key, env[key])
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func liveCheck(timeout time.Duration, env map[string]string) CheckOptions {
|
||||
opts := offlineCheck()
|
||||
opts.Live, opts.Timeout = true, timeout
|
||||
opts.LookPath = exec.LookPath
|
||||
opts.LookupEnv = func(name string) (string, bool) {
|
||||
value, ok := env[name]
|
||||
return value, ok
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func liveOf(t *testing.T, report *CheckReport) (*CheckLive, CheckFinding) {
|
||||
t.Helper()
|
||||
for _, f := range report.Servers[0].Findings {
|
||||
if f.Check == "live" {
|
||||
return report.Servers[0].Live, f
|
||||
}
|
||||
}
|
||||
t.Fatalf("no live finding: %+v", report.Servers[0].Findings)
|
||||
return nil, CheckFinding{}
|
||||
}
|
||||
|
||||
func assertNoSecret(t *testing.T, report *CheckReport) {
|
||||
t.Helper()
|
||||
data, _ := json.Marshal(report)
|
||||
if strings.Contains(string(data), liveSecret) {
|
||||
t.Fatalf("report leaks a configured value: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
var tokenEnv = map[string]string{"LIVE_TOKEN": liveSecret}
|
||||
|
||||
func TestLiveStdioDiscover(t *testing.T) {
|
||||
report, err := checkService(t, liveConfig("modern", nil)).Check(liveCheck(10*time.Second, tokenEnv))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
live, f := liveOf(t, report)
|
||||
want := CheckLive{ProtocolVersion: "2026-07-28", ServerInfo: LiveServerInfo{Name: "helper", Version: "1.2.3"}, Tools: 2}
|
||||
if f.Level != "info" || live == nil || *live != want || !report.Servers[0].OK {
|
||||
t.Fatalf("live = %+v, finding = %+v", live, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveStdioFallsBackToInitialize(t *testing.T) {
|
||||
report, err := checkService(t, liveConfig("legacy", nil)).Check(liveCheck(10*time.Second, tokenEnv))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
live, f := liveOf(t, report)
|
||||
want := CheckLive{ProtocolVersion: "2025-11-25", ServerInfo: LiveServerInfo{Name: "legacy", Version: "0.9"}, Tools: 3}
|
||||
if live == nil || *live != want {
|
||||
t.Fatalf("live = %+v, finding = %+v", live, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveStdioFallsBackWhenDiscoverIsUnanswered(t *testing.T) {
|
||||
report, err := checkService(t, liveConfig("silent", nil)).Check(liveCheck(3*time.Second, tokenEnv))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if live, f := liveOf(t, report); live == nil || live.ProtocolVersion != "2025-11-25" {
|
||||
t.Fatalf("live = %+v, finding = %+v", live, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveStdioExitReportsRedactedStderr(t *testing.T) {
|
||||
report, err := checkService(t, liveConfig("crash", nil)).Check(liveCheck(10*time.Second, tokenEnv))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, f := liveOf(t, report)
|
||||
if f.Level != "error" || !strings.Contains(f.Message, "exit status 3") || !strings.Contains(f.Message, "token [redacted] rejected") || report.Summary.Errors != 1 {
|
||||
t.Fatalf("finding = %+v", f)
|
||||
}
|
||||
assertNoSecret(t, report)
|
||||
}
|
||||
|
||||
func TestLiveStdioSpawnFailure(t *testing.T) {
|
||||
s := checkService(t, "mcp:\n targets: [claude]\n servers:\n gone:\n command: /no/such/mcp-server\n")
|
||||
opts := liveCheck(time.Second, nil)
|
||||
// The static check passes; starting the command is what fails.
|
||||
opts.LookPath = func(name string) (string, error) { return name, nil }
|
||||
report, err := s.Check(opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, f := liveOf(t, report); f.Level != "error" || !strings.Contains(f.Message, "could not start /no/such/mcp-server") {
|
||||
t.Fatalf("finding = %+v", f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveAbsentStartsNothing(t *testing.T) {
|
||||
marker := filepath.Join(t.TempDir(), "started")
|
||||
opts := liveCheck(time.Second, tokenEnv)
|
||||
opts.Live = false
|
||||
report, err := checkService(t, liveConfig("modern", map[string]string{"MCP_LIVE_MARKER": marker})).Check(opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(marker); !os.IsNotExist(err) {
|
||||
t.Fatal("a check without Live started the server")
|
||||
}
|
||||
if report.Servers[0].Live != nil || subjectOf(report.Servers[0].Findings, "live") != "" || hasFinding(report.Servers[0].Findings, "info", "live", "") {
|
||||
t.Fatalf("unexpected live result: %+v", report.Servers[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveSkipsServerWithStaticError(t *testing.T) {
|
||||
marker := filepath.Join(t.TempDir(), "started")
|
||||
report, err := checkService(t, liveConfig("modern", map[string]string{"MCP_LIVE_MARKER": marker})).Check(liveCheck(time.Second, nil))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, f := liveOf(t, report); f.Level != "info" || !strings.Contains(f.Message, "not probed live") {
|
||||
t.Fatalf("finding = %+v", f)
|
||||
}
|
||||
if _, err := os.Stat(marker); !os.IsNotExist(err) {
|
||||
t.Fatal("a server with a missing variable was started")
|
||||
}
|
||||
}
|
||||
|
||||
// liveHTTP serves a check config for one remote server backed by handler.
|
||||
func liveHTTP(t *testing.T, handler http.HandlerFunc) (*CheckReport, CheckFinding) {
|
||||
t.Helper()
|
||||
ts := httptest.NewServer(handler)
|
||||
defer ts.Close()
|
||||
s := checkService(t, fmt.Sprintf("mcp:\n targets: [claude]\n servers:\n remote:\n url: %s/mcp\n bearerToken: {fromEnv: LIVE_TOKEN}\n headers:\n X-Team: {fromEnv: LIVE_TEAM}\n", ts.URL))
|
||||
report, err := s.Check(liveCheck(5*time.Second, map[string]string{"LIVE_TOKEN": liveSecret, "LIVE_TEAM": "team-" + liveSecret}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertNoSecret(t, report)
|
||||
_, f := liveOf(t, report)
|
||||
return report, f
|
||||
}
|
||||
|
||||
type httpRequest struct {
|
||||
ID json.RawMessage `json:"id"`
|
||||
Method string `json:"method"`
|
||||
}
|
||||
|
||||
func decodeRequest(t *testing.T, r *http.Request) httpRequest {
|
||||
var req httpRequest
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
t.Errorf("invalid request body %s", body)
|
||||
}
|
||||
if r.Header.Get("Authorization") != "Bearer "+liveSecret || r.Header.Get("X-Team") != "team-"+liveSecret || r.Header.Get("Mcp-Method") != req.Method {
|
||||
t.Errorf("%s: missing or wrong headers: %v", req.Method, r.Header)
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
func TestLiveHTTPDiscoverJSON(t *testing.T) {
|
||||
report, f := liveHTTP(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
req := decodeRequest(t, r)
|
||||
if r.Header.Get("MCP-Protocol-Version") != "2026-07-28" {
|
||||
t.Errorf("missing protocol version header")
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
result := map[string]any{"resultType": "complete", "tools": []any{map[string]string{"name": "x"}}}
|
||||
if req.Method == "server/discover" {
|
||||
result = map[string]any{"resultType": "complete", "supportedVersions": []string{"2026-07-28"}, "capabilities": map[string]any{"tools": map[string]any{}},
|
||||
"_meta": map[string]any{"io.modelcontextprotocol/serverInfo": map[string]string{"name": "remote", "version": "2"}}}
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"jsonrpc": "2.0", "id": req.ID, "result": result})
|
||||
})
|
||||
want := CheckLive{ProtocolVersion: "2026-07-28", ServerInfo: LiveServerInfo{Name: "remote", Version: "2"}, Tools: 1}
|
||||
if f.Level != "info" || report.Servers[0].Live == nil || *report.Servers[0].Live != want {
|
||||
t.Fatalf("live = %+v, finding = %+v", report.Servers[0].Live, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveHTTPSSEResponse(t *testing.T) {
|
||||
report, f := liveHTTP(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
req := decodeRequest(t, r)
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fmt.Fprint(w, ": keep-alive\n\nevent: message\ndata: {\"jsonrpc\":\"2.0\",\"method\":\"notifications/progress\",\"params\":{}}\n\n")
|
||||
result, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": req.ID, "result": map[string]any{"supportedVersions": []string{"2026-07-28"}, "capabilities": map[string]any{},
|
||||
"_meta": map[string]any{"io.modelcontextprotocol/serverInfo": map[string]string{"name": "sse", "version": "1"}}}})
|
||||
fmt.Fprintf(w, "event: message\ndata: %s\n\n", result)
|
||||
})
|
||||
if f.Level != "info" || report.Servers[0].Live == nil || report.Servers[0].Live.ServerInfo.Name != "sse" || report.Servers[0].Live.Tools != 0 {
|
||||
t.Fatalf("live = %+v, finding = %+v", report.Servers[0].Live, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveHTTPFallsBackToInitialize(t *testing.T) {
|
||||
report, f := liveHTTP(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodDelete {
|
||||
return
|
||||
}
|
||||
req := decodeRequest(t, r)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch {
|
||||
case req.Method == "initialize":
|
||||
w.Header().Set("Mcp-Session-Id", "session-1")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"jsonrpc": "2.0", "id": req.ID, "result": map[string]any{"protocolVersion": "2025-06-18", "capabilities": map[string]any{"tools": map[string]any{}}, "serverInfo": map[string]string{"name": "old", "version": "1"}}})
|
||||
case r.Header.Get("Mcp-Session-Id") != "session-1":
|
||||
http.Error(w, "Bad Request: No valid session ID provided", http.StatusBadRequest)
|
||||
case req.Method == "notifications/initialized":
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
case r.Header.Get("MCP-Protocol-Version") != "2025-06-18":
|
||||
http.Error(w, "wrong protocol version", http.StatusBadRequest)
|
||||
default:
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"jsonrpc": "2.0", "id": req.ID, "result": map[string]any{"tools": []any{map[string]string{"name": "x"}, map[string]string{"name": "y"}}}})
|
||||
}
|
||||
})
|
||||
want := CheckLive{ProtocolVersion: "2025-06-18", ServerInfo: LiveServerInfo{Name: "old", Version: "1"}, Tools: 2}
|
||||
if report.Servers[0].Live == nil || *report.Servers[0].Live != want {
|
||||
t.Fatalf("live = %+v, finding = %+v", report.Servers[0].Live, f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveHTTPUnauthorizedIsWarning(t *testing.T) {
|
||||
calls := 0
|
||||
report, f := liveHTTP(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
w.Header().Set("WWW-Authenticate", `Bearer resource_metadata="https://auth.example.com/.well-known/oauth-protected-resource", scope="files:read"`)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
})
|
||||
if f.Level != "warning" || f.Subject != "https://auth.example.com/.well-known/oauth-protected-resource" || !strings.Contains(f.Message, "sign-in required") || !report.Servers[0].OK || calls != 1 {
|
||||
t.Fatalf("finding = %+v, calls = %d", f, calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveHTTPServerErrorIsError(t *testing.T) {
|
||||
report, f := liveHTTP(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "boom "+r.Header.Get("Authorization"), http.StatusInternalServerError)
|
||||
})
|
||||
if f.Level != "error" || !strings.Contains(f.Message, "HTTP 500") || report.Servers[0].OK {
|
||||
t.Fatalf("finding = %+v", f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//go:build !windows
|
||||
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// startInGroup gives the server its own process group, so stopping it reaches its children.
|
||||
func startInGroup(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
}
|
||||
|
||||
func terminateGroup(cmd *exec.Cmd) { _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM) }
|
||||
|
||||
func killGroup(cmd *exec.Cmd) { _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) }
|
||||
@@ -0,0 +1,56 @@
|
||||
//go:build !windows
|
||||
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// processGone treats a zombie as gone: a container's init may never reap an orphan.
|
||||
func processGone(pid int) bool {
|
||||
if err := syscall.Kill(pid, 0); err != nil {
|
||||
return true
|
||||
}
|
||||
stat, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
|
||||
if err != nil {
|
||||
return os.IsNotExist(err)
|
||||
}
|
||||
fields := strings.Fields(string(stat[strings.LastIndexByte(string(stat), ')')+1:]))
|
||||
return len(fields) > 0 && fields[0] == "Z"
|
||||
}
|
||||
|
||||
func TestLiveStdioTimeoutKillsProcessGroup(t *testing.T) {
|
||||
pids := filepath.Join(t.TempDir(), "pids")
|
||||
start := time.Now()
|
||||
report, err := checkService(t, liveConfig("hang", map[string]string{"MCP_LIVE_PIDS": pids})).Check(liveCheck(1500*time.Millisecond, tokenEnv))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, f := liveOf(t, report); f.Level != "error" || !strings.Contains(f.Message, "no answer within 1.5s") {
|
||||
t.Fatalf("finding = %+v", f)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > 5*time.Second {
|
||||
t.Fatalf("the probe took %s", elapsed)
|
||||
}
|
||||
data, err := os.ReadFile(pids)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var server, child int
|
||||
if _, err := fmt.Sscanf(string(data), "%d %d", &server, &child); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for !(processGone(server) && processGone(child)) {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("server %d or its child %d is still running", server, child)
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build windows
|
||||
|
||||
package mcp
|
||||
|
||||
import "os/exec"
|
||||
|
||||
func startInGroup(*exec.Cmd) {}
|
||||
|
||||
// Windows has no signals to escalate through; the server is terminated at once.
|
||||
func terminateGroup(cmd *exec.Cmd) { _ = cmd.Process.Kill() }
|
||||
|
||||
func killGroup(cmd *exec.Cmd) { _ = cmd.Process.Kill() }
|
||||
@@ -1,5 +1,6 @@
|
||||
// Package mcp manages portable MCP declarations and native client configuration.
|
||||
// It never starts servers, resolves credentials, or performs network requests.
|
||||
// It never starts servers, resolves credentials, or performs network requests, except
|
||||
// for the live probe that `mcp check --live` asks for explicitly.
|
||||
package mcp
|
||||
|
||||
import (
|
||||
|
||||
@@ -95,3 +95,64 @@ func TestMCPCheckProjects(t *testing.T) {
|
||||
r.AssertOutputContains(t, "docs (project ~/work/app)")
|
||||
r.AssertOutputContains(t, "2 server(s) checked: 1 error(s)")
|
||||
}
|
||||
|
||||
// tinyMCPServer answers server/discover and tools/list, one JSON-RPC message per line.
|
||||
const tinyMCPServer = `#!/bin/sh
|
||||
while IFS= read -r line; do
|
||||
id=$(printf '%s\n' "$line" | sed -n 's/.*"id":\([0-9][0-9]*\).*/\1/p')
|
||||
case "$line" in
|
||||
*'"server/discover"'*) printf '{"jsonrpc":"2.0","id":%s,"result":{"resultType":"complete","supportedVersions":["2026-07-28"],"capabilities":{"tools":{}},"_meta":{"io.modelcontextprotocol/serverInfo":{"name":"tiny","version":"0.1.0"}}}}\n' "$id" ;;
|
||||
*'"tools/list"'*) printf '{"jsonrpc":"2.0","id":%s,"result":{"resultType":"complete","tools":[{"name":"echo","inputSchema":{"type":"object"}}]}}\n' "$id" ;;
|
||||
esac
|
||||
done
|
||||
`
|
||||
|
||||
func TestMCPCheckLive(t *testing.T) {
|
||||
sb := testutil.NewSandbox(t)
|
||||
defer sb.Cleanup()
|
||||
script := filepath.Join(sb.Home, "tiny-mcp.sh")
|
||||
if err := os.WriteFile(script, []byte(tinyMCPServer), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sb.WriteConfig("targets: {}\nmcp:\n targets: [claude]\n servers:\n tiny:\n command: sh\n args: [" + script + "]\n down:\n url: http://127.0.0.1:1/mcp\n")
|
||||
|
||||
r := sb.RunCLI("mcp", "check", "tiny", "--json", "-g")
|
||||
r.AssertSuccess(t)
|
||||
if strings.Contains(r.Stdout, `"live"`) {
|
||||
t.Fatalf("a check without --live probed the server:\n%s", r.Stdout)
|
||||
}
|
||||
|
||||
r = sb.RunCLI("mcp", "check", "--live", "--timeout", "5s", "--json", "--no-dns", "-g")
|
||||
r.AssertExitCode(t, 1)
|
||||
var report struct {
|
||||
Servers []struct {
|
||||
Name string `json:"name"`
|
||||
OK bool `json:"ok"`
|
||||
Live *struct {
|
||||
ProtocolVersion string `json:"protocolVersion"`
|
||||
ServerInfo struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"serverInfo"`
|
||||
Tools int `json:"tools"`
|
||||
} `json:"live"`
|
||||
} `json:"servers"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(r.Stdout), &report); err != nil {
|
||||
t.Fatalf("invalid JSON: %v\n%s", err, r.Stdout)
|
||||
}
|
||||
down, tiny := report.Servers[0], report.Servers[1]
|
||||
if tiny.Live == nil || tiny.Live.ServerInfo.Name != "tiny" || tiny.Live.ProtocolVersion != "2026-07-28" || tiny.Live.Tools != 1 || !tiny.OK {
|
||||
t.Fatalf("tiny: %+v", tiny)
|
||||
}
|
||||
if down.Live != nil || down.OK {
|
||||
t.Fatalf("an unreachable server must fail: %+v", down)
|
||||
}
|
||||
|
||||
r = sb.RunCLI("mcp", "check", "tiny", "--live", "-g")
|
||||
r.AssertSuccess(t)
|
||||
r.AssertOutputContains(t, "responds: tiny 0.1.0, protocol 2026-07-28, 1 tool(s)")
|
||||
|
||||
r = sb.RunCLI("mcp", "check", "--timeout", "5s", "-g")
|
||||
r.AssertFailure(t)
|
||||
r.AssertAnyOutputContains(t, "--timeout only applies with --live")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user