Files
magpie/internal/library/mcp_check.go
T
yetone 14b10f1166 library: an MCP server signed in to in Claude Code, in WSL too, says so instead of "needs sign-in" (CyberCaius on Discord)
CyberCaius signed in to a remote MCP server with Claude Code's /mcp in
WSL, and the Library still said 需要登录. The check connects as magpie,
without the agent's tokens, so an OAuth server answers it 401, and magpie
read no agent's own sign-in anywhere: not on Windows, not in a distro.

The check now reads each Claude Code target's .credentials.json, beside
the settings.json the target already has (readOwnSignIns,
mcp_agent_signin.go). A distro's Claude Code is a target under the
distro's home (wslTargetOf), so its file is read there through
\\wsl.localhost with no new setting. An mcpOAuth entry of the server's
name and URL with an access or refresh token, as Claude Code 2.1.310
counts one, names the agent in ServerHealth.AgentSignIn; the row says
"signed in in Claude Code (Ubuntu)" with the ok dot, and the tooltip says
magpie can't list its tools without signing in itself. An entry without
a token, another URL by the same name, and a file that can't be read or
parsed name nobody, so the row stays "needs sign-in". The tokens are
never sent or kept. A stopped distro has no target and isn't started.
On a Mac only the file is read, never the Keychain. The sign-ins are part
of the check's cache key, so a sign-in in the agent since isn't answered
from the session's earlier check. Strings in zh, zh-TW, ja and de.

Run:
- TestCheckNamesAgentsSignedInThemselves (internal/library): agent.FakeWSL
  forces the Windows+WSL switch (Ubuntu running, its home a temp root),
  sandbox HOME/USERPROFILE/XDG_CONFIG_HOME, CLAUDE_CONFIG_DIR cleared, a
  real httptest server answering 401 with WWW-Authenticate.
- Broken by hand, each red with a FAIL naming the behavior:
  - the old behavior (the reader names nobody): reading only this
    computer's Claude folder (claudeDir()) instead of the target's:
    "signed in in claude@wsl:Ubuntu, the check says {State:auth ...
    AgentSignIn:[]}".
  - counting an entry with no token: "a sign-in with no token counted".
  - leaving the sign-ins out of healthKey: "signed in on both sides, the
    check says ... AgentSignIn:[claude@wsl:Ubuntu]" (stale cache).
- library-mcp-health-wsl.test.cjs, chromium and webkit, en/zh/zh-TW/ja/de:
  red on the old client.js (expected "signed in in Claude Code (Ubuntu)",
  got "needs sign-in"), 12 of 12 after.
- gofmt -l (nothing); go vet on darwin, GOOS=linux -tags nogui and
  GOOS=windows for ./internal/library ./internal/gui; go build darwin,
  GOOS=windows and GOOS=linux -tags nogui ./...; go test -tags nogui
  -count=1 -timeout 45m ./internal/library ./internal/gui: ok (187s, 63s).
- GUI: library-mcp-health, library-mcp-health-wsl, mcp-signin-health,
  mcp-signin, mcp-oauth-app, library-mcp-tools, library-mcp-tool-switch,
  library-mcp-unshown, library-items, i18n-keys, gui-zh-tw, gui-ja,
  gui-de: 161 of 161 pass.
These ran first at a base before origin/main 1f589e3f4, then again at
this head (on 1f589e3f4): vet, builds, internal/library ok (149s), the 13
GUI files 161 of 161. internal/gui is red at this head on
TestCoreScriptBudget alone: app.js is 1321476 bytes, over 1321336. It is
the same on origin/main 1f589e3f4, from 2a8a17746 (this change doesn't
touch app.js, and its size is main's); #3367. The budget isn't raised.

Claude Code's file format was read from its 2.1.310 binary (td's key,
the token save's serverName/serverUrl/accessToken/refreshToken, $Ci's
"neither token is no sign-in").

Not tried: a real /mcp sign-in in Claude Code in WSL on the Windows box
(ssh to 10.0.0.132 timed out), and the full ./... suite. Not covered:
Codex's own MCP sign-ins (keyring, or a .credentials.json fallback whose
format wasn't checked against its source), #3508.
2026-10-25 00:03:52 +08:00

1033 lines
28 KiB
Go

package library
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"slices"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/mcpauth"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/wslrun"
)
// The Library page says of each server whether it works, found the only
// way that tells: by connecting to it as an agent would. A command is
// started, asked to initialize and to list its tools, and ended; a URL is
// asked the same over HTTP. Nothing is written anywhere: the check only
// reads the library, and what it found is kept in memory for the session.
// ServerHealth is what a check of one server found.
type ServerHealth struct {
// State is ok (it answered, with Tools tools), auth (it asks for a
// sign-in or a key) or error (Why says what went wrong)
State string `json:"state"`
Tools int `json:"tools"`
// ToolList is each tool's name and what it says it does, as tools/list
// gave them, for the page to list (nf1qaz2w on Discord); at most
// maxToolList, while Tools counts every one
ToolList []Tool `json:"toolList,omitempty"`
// Why is the kind of error, for the page to put in its own words:
// notfound, start, exited, timeout, http, refused, unreachable,
// protocol, novar (a ${NAME} magpie's environment hasn't: Detail names
// them), wsldown (a command only agents in WSL start, whose distros,
// Detail, aren't running: it wasn't checked)
Why string `json:"why,omitempty"`
Code int `json:"code,omitempty"` // the exit code, or the HTTP status
Detail string `json:"detail,omitempty"` // the last line it wrote to stderr, or the error's text
// OAuth is a refusal that named a sign-in (WWW-Authenticate), which
// magpie can do itself for a streamable HTTP server
OAuth bool `json:"oauth,omitempty"`
// SignIn is magpie's sign-in to a streamable HTTP server as the check
// left it: one whose renewed token the server refused has run out
// since, and the page's tag says so (新v学员 on Discord)
SignIn *mcpauth.Status `json:"signIn,omitempty"`
// AgentSignIn are the agents signed in to a server that refused
// magpie's check on their own (Claude Code's /mcp, mcp_agent_signin.go),
// by id: they use it, though the check, without their tokens, was asked
// to sign in (CyberCaius on Discord)
AgentSignIn []string `json:"agentSignIn,omitempty"`
// Step is what a server that timed out was asked and didn't answer
// (initialize, notifications/initialized, tools/list, or connect: an
// SSE server's stream), and Waited how long it was waited for, in ms
Step string `json:"step,omitempty"`
Waited int64 `json:"waited,omitempty"`
// Checked are where a command was started, in order, for one that agents
// in WSL have (mcp_check_wsl.go): "" for this machine, a distro's name
// for its agents there. A failure is the last one's; none for a
// command only this machine's agents start.
Checked []string `json:"checked,omitempty"`
At int64 `json:"at"` // when it was checked, unix ms
}
// Each step of a check (initialize, notifications/initialized, tools/list)
// has a budget of its own, not one for the whole check: a remote server
// that is slow but healthy took 0.6-12s to initialize and 38-53s to list
// its tools (#1467, xiaozhu1337), which a 15s budget for all of it always
// called "no answer". A command gets less: it runs on this machine, though
// npx fetching a package the first time can take a while.
var (
stdioStep = 30 * time.Second
remoteStep = 60 * time.Second
)
// checkAtOnce is how many servers are checked together, each a process or
// a few requests.
const checkAtOnce = 4
const mcpProtocol = "2025-06-18"
var checkClient = &http.Client{Transport: checkTransport()}
func checkTransport() http.RoundTripper {
t := http.DefaultTransport.(*http.Transport).Clone()
t.Proxy = netproxy.Func
return netproxy.Dispatch(t)
}
// checked is the last check of each server, by name, with what the
// server was then: an edited server, or one signed in to since, is checked
// again.
var checked = struct {
sync.Mutex
m map[string]checkedServer
}{m: map[string]checkedServer{}}
type checkedServer struct {
key string
h ServerHealth
}
func healthKey(s *Server, own ownSignIns) string {
c := *s
c.Name, c.Agents = "", nil
b, _ := json.Marshal(c)
st := mcpauth.StatusOf(s.Name, s.URL)
// and where a command is started, which its agents decide
var sides []string
if s.Transport == "stdio" {
sides = checkSides(s)
}
// and the agents signed in to it themselves: one signed in since
// isn't left as "needs sign-in"
return fmt.Sprintf("%s|%v|%v|%d|%q|%q", b, st.SignedIn, st.Dead, st.At, sides, own.of(s))
}
// CheckServers checks the library's servers called names (every one when
// there are none). One checked already this session, as it is now, isn't
// started again unless fresh.
func CheckServers(ctx context.Context, names []string, fresh bool) (map[string]ServerHealth, error) {
mu.Lock()
l, err := load()
mu.Unlock()
if err != nil {
return nil, err
}
var list []*Server
if len(names) == 0 {
list = l.MCP
} else {
for _, n := range names {
if s := l.server(n); s != nil {
list = append(list, s)
}
}
}
own := readOwnSignIns()
out := map[string]ServerHealth{}
var outMu sync.Mutex
var wg sync.WaitGroup
slots := make(chan struct{}, checkAtOnce)
for _, s := range list {
key := healthKey(s, own)
checked.Lock()
c, ok := checked.m[s.Name]
checked.Unlock()
if ok && c.key == key && !fresh {
outMu.Lock()
out[s.Name] = c.h
outMu.Unlock()
continue
}
wg.Add(1)
go func(s *Server) {
defer wg.Done()
slots <- struct{}{}
defer func() { <-slots }()
h := checkServer(ctx, s, own)
// as the server is now: a sign-in the check found run out
// isn't one to check it again for
key := healthKey(s, own)
// a check cut short by the page going away isn't one, and a
// server that didn't answer in time is asked again next time:
// a slow server may answer then (#1467)
if ctx.Err() == nil {
checked.Lock()
if h.Why == "timeout" {
// nor is an answer from before kept in its place
delete(checked.m, s.Name)
} else {
checked.m[s.Name] = checkedServer{key, h}
}
checked.Unlock()
}
outMu.Lock()
out[s.Name] = h
outMu.Unlock()
}(s)
}
wg.Wait()
return out, nil
}
// CheckServer connects to one server and asks it for its tools.
func CheckServer(ctx context.Context, s *Server) ServerHealth {
return checkServer(ctx, s, nil)
}
// checkServer is CheckServer with the agents' own sign-ins read already,
// once for every server checked together; nil reads them when a server
// asks for a sign-in.
func checkServer(ctx context.Context, s *Server, own ownSignIns) ServerHealth {
each := remoteStep
if s.Transport == "stdio" {
each = stdioStep
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
st := &checkSteps{step: "initialize", each: each, timer: time.AfterFunc(each, cancel)}
defer st.timer.Stop()
ctx = context.WithValue(ctx, stepsKey{}, st)
var h ServerHealth
switch s.Transport {
case "stdio":
h = checkStdio(ctx, s)
case "sse":
h = checkSSE(ctx, s)
default:
h = checkHTTP(ctx, s)
}
h.At = time.Now().UnixMilli()
if s.Transport == "http" {
// only a server magpie is signed in to has one to report
if st := mcpauth.StatusOf(s.Name, s.URL); st.SignedIn || st.Dead {
h.SignIn = &st
}
}
if h.State == "auth" && s.Transport != "stdio" {
if own == nil {
own = readOwnSignIns()
}
h.AgentSignIn = own.of(s)
}
return h
}
func failed(why, detail string) ServerHealth {
return ServerHealth{State: "error", Why: why, Detail: detail}
}
// checkSteps is the step a check is at, and the timer that ends the check
// when the step takes longer than each.
type checkSteps struct {
mu sync.Mutex
step string
each time.Duration
timer *time.Timer
}
type stepsKey struct{}
// stepTo starts the check's next step, with a budget of its own.
func stepTo(ctx context.Context, step string) {
st, _ := ctx.Value(stepsKey{}).(*checkSteps)
if st == nil || ctx.Err() != nil {
return
}
st.mu.Lock()
defer st.mu.Unlock()
st.step = step
st.timer.Reset(st.each)
}
// timedOut is a server that didn't answer a step in time; the page says
// which step, and how long it waited.
func timedOut(ctx context.Context) ServerHealth {
h := failed("timeout", "")
if st, _ := ctx.Value(stepsKey{}).(*checkSteps); st != nil {
st.mu.Lock()
h.Step, h.Waited = st.step, st.each.Milliseconds()
st.mu.Unlock()
}
return h
}
// rpcMsg is a JSON-RPC message from the server: a reply has an id.
type rpcMsg struct {
ID json.RawMessage `json:"id"`
Method string `json:"method"`
Result json.RawMessage `json:"result"`
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
func (m *rpcMsg) is(id int) bool { return string(bytes.TrimSpace(m.ID)) == fmt.Sprint(id) }
func request(id int, method string, params any) []byte {
m := map[string]any{"jsonrpc": "2.0", "id": id, "method": method}
if params != nil {
m["params"] = params
}
b, _ := json.Marshal(m)
return b
}
func initializeReq() []byte {
return request(1, "initialize", map[string]any{
"protocolVersion": mcpProtocol,
"capabilities": map[string]any{},
"clientInfo": map[string]any{"name": "magpie", "version": gateway.Version},
})
}
var initializedNote = []byte(`{"jsonrpc":"2.0","method":"notifications/initialized"}`)
// Tool is one tool a server listed: its name, and the first paragraph of
// its description, cut short.
type Tool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
// What of the tools a check keeps: every tool's name up to maxToolList,
// a description up to maxToolDesc runes, and up to maxToolPages pages of
// tools/list, each with a step's budget of its own.
const (
maxToolList = 500
maxToolName = 128
maxToolDesc = 500
maxToolPages = 10
)
// clip is s with its whitespace runs made one space, cut at n runes.
func clip(s string, n int) string {
s = strings.Join(strings.Fields(s), " ")
if r := []rune(s); len(r) > n {
return strings.TrimSpace(string(r[:n])) + "…"
}
return s
}
// toolDesc is the first paragraph of a tool's description: the rest is
// usually how to call it, which the agent reads, not the person.
func toolDesc(d string) string {
d = strings.TrimSpace(strings.ReplaceAll(d, "\r\n", "\n"))
if i := strings.Index(d, "\n\n"); i >= 0 {
d = d[:i]
}
return clip(d, maxToolDesc)
}
// toolsOf is the health one reply to tools/list says, and the cursor of
// the page after it: a server with no tools may not know the method at all.
func toolsOf(m *rpcMsg) (ServerHealth, string) {
if m.Error != nil {
if m.Error.Code == -32601 {
return ServerHealth{State: "ok"}, ""
}
return failed("protocol", "tools/list: "+m.Error.Message), ""
}
var r struct {
Tools []struct {
Name string `json:"name"`
Title string `json:"title"`
Description string `json:"description"`
} `json:"tools"`
NextCursor string `json:"nextCursor"`
}
if err := json.Unmarshal(m.Result, &r); err != nil {
return failed("protocol", "tools/list: "+err.Error()), ""
}
h := ServerHealth{State: "ok", Tools: len(r.Tools)}
for _, t := range r.Tools {
if len(h.ToolList) == maxToolList {
break
}
d := toolDesc(t.Description)
if d == "" {
d = clip(t.Title, maxToolDesc)
}
h.ToolList = append(h.ToolList, Tool{Name: clip(t.Name, maxToolName), Description: d})
}
return h, r.NextCursor
}
// listTools asks for the server's tools, a page at a time while it names
// a next one, up to maxToolPages. ask sends a request and reads the reply
// to its id. A page after the first that is refused ends the list there.
func listTools(ctx context.Context, ask func(id int, req []byte) (*rpcMsg, *ServerHealth)) ServerHealth {
var all ServerHealth
cursor := ""
for page := 0; page < maxToolPages; page++ {
stepTo(ctx, "tools/list")
var params any
if cursor != "" {
params = map[string]any{"cursor": cursor}
}
id := 2 + page
m, bad := ask(id, request(id, "tools/list", params))
if bad != nil {
return *bad
}
h, next := toolsOf(m)
if h.State != "ok" {
if page == 0 {
return h
}
break
}
all.State = "ok"
all.Tools += h.Tools
for _, t := range h.ToolList {
if len(all.ToolList) < maxToolList {
all.ToolList = append(all.ToolList, t)
}
}
if next == "" || next == cursor {
break
}
cursor = next
}
return all
}
// initError is what an error reply to initialize says.
func initError(m *rpcMsg) *ServerHealth {
if m.Error == nil {
return nil
}
h := failed("protocol", "initialize: "+m.Error.Message)
return &h
}
// ---- a command ---------------------------------------------------------------
// tail keeps the end of what a server writes to stderr: its last line is
// usually why it stopped.
type tail struct {
mu sync.Mutex
buf []byte
}
func (t *tail) Write(p []byte) (int, error) {
t.mu.Lock()
defer t.mu.Unlock()
t.buf = append(t.buf, p...)
if len(t.buf) > 8192 {
t.buf = t.buf[len(t.buf)-8192:]
}
return len(p), nil
}
func (t *tail) last() string {
t.mu.Lock()
defer t.mu.Unlock()
lines := strings.Split(strings.TrimSpace(string(t.buf)), "\n")
l := strings.TrimSpace(lines[len(lines)-1])
if len(l) > 300 {
l = l[:300] + "…"
}
return l
}
// checkStdio starts the command where the agents given it start it
// (checkSides), each place in turn; the first failure is the answer.
func checkStdio(ctx context.Context, s *Server) ServerHealth {
sides := checkSides(s)
if len(sides) == 1 && sides[0] == "" {
return checkStdioAt(ctx, s, "")
}
var done, down []string
var first *ServerHealth
for _, d := range sides {
if len(done) > 0 {
stepTo(ctx, "initialize")
}
h := checkStdioAt(ctx, s, d)
if h.Why == "wsldown" {
down = append(down, d)
continue
}
done = append(done, d)
h.Checked = slices.Clone(done)
if h.State != "ok" {
return h
}
if first == nil {
first = &h
}
}
if first == nil {
return failed("wsldown", strings.Join(down, ", "))
}
first.Checked = done
return *first
}
// checkStdioAt starts the command on this machine, or in WSL distro.
func checkStdioAt(ctx context.Context, s *Server, distro string) ServerHealth {
// started as the agents start it: in the environment magpie has, with
// the server's own variables over it
env, unset := expandAll(s.Env)
if len(unset) > 0 {
return failed("novar", strings.Join(unset, ", "))
}
var cmd *exec.Cmd
var t wslrun.Tool
stop := func() {}
if distro == "" {
cmd = proc.Command(s.Command, s.Args...)
} else {
var err error
t, err = wslrun.In(distro, s.Command)
switch {
case errors.Is(err, wslrun.ErrDown):
return failed("wsldown", distro)
case errors.Is(err, wslrun.ErrNotFound):
return failed("notfound", s.Command)
case err != nil:
return failed("start", "wsl.exe -d "+distro+": "+err.Error())
}
// its own context, ended with the check, which ends the run inside
// the distro too (killing wsl.exe alone leaves it there); not the
// check's, whose timeout is told as one rather than as an exit
var rctx context.Context
rctx, stop = context.WithCancel(context.WithoutCancel(ctx))
cmd = wslCommand(rctx, t, s.Args...)
}
defer stop()
// Agents start MCP servers from a project or home directory. A magpie
// launched from a shortcut (Windows Start menu) often has C:\Windows\system32
// as its own cwd, which is not writable; inherit that and a server that
// writes into "." dies as "exited (1)" with no log. Pin the check to HOME.
cmd.Env = os.Environ()
if d := home(); d != "" {
cmd.Dir = d
// and PWD with it, as exec does itself only when Env is nil: a
// shell-script server that reads $PWD would otherwise see magpie's
// own folder while it runs in home (Windows doesn't use PWD)
if runtime.GOOS != "windows" && distro == "" {
cmd.Env = append(cmd.Env, "PWD="+d)
}
}
var names []string
for k, v := range env {
cmd.Env = append(cmd.Env, k+"="+v)
names = append(names, k)
}
if distro != "" {
// a run in the distro is given the server's own variables, by
// WSLENV, and none of Windows' others
slices.Sort(names)
cmd.Env = t.Env(cmd.Env, names...)
}
stdin, err := cmd.StdinPipe()
if err != nil {
return failed("start", err.Error())
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return failed("start", err.Error())
}
errs := &tail{}
cmd.Stderr = errs
tree, err := proc.StartTree(cmd)
if err != nil {
if errors.Is(err, exec.ErrNotFound) || errors.Is(err, os.ErrNotExist) {
return failed("notfound", s.Command)
}
return failed("start", err.Error())
}
var exitErr error
exited := make(chan struct{})
go func() { exitErr = tree.Wait(); close(exited) }()
// the server ends with the check, and whatever it started with it
defer func() {
stdin.Close()
stop()
tree.Kill()
select {
case <-exited:
case <-time.After(3 * time.Second):
}
}()
msgs := make(chan *rpcMsg, 16)
go func() {
defer close(msgs)
sc := bufio.NewScanner(stdout)
sc.Buffer(make([]byte, 64*1024), 16<<20)
for sc.Scan() {
var m rpcMsg
// a line that isn't JSON-RPC is a server logging to the wrong
// stream; agents pass over it too
if json.Unmarshal(sc.Bytes(), &m) != nil {
continue
}
select {
case msgs <- &m:
case <-ctx.Done():
return
}
}
}()
send := func(b []byte) error {
_, err := stdin.Write(append(b, '\n'))
return err
}
// a reply, or why there is none: the process ended or time ran out. A
// process that ended may still have lines to read, the reply among
// them, so its output is read to the end first.
out, gone, ended := msgs, exited, false
await := func(id int) (*rpcMsg, *ServerHealth) {
for {
select {
case m, ok := <-out:
if !ok {
out = nil
if ended {
h := exitHealth(exitErr, errs)
return nil, &h
}
continue
}
if m.is(id) {
return m, nil
}
case <-gone:
gone, ended = nil, true
if out == nil {
h := exitHealth(exitErr, errs)
return nil, &h
}
case <-ctx.Done():
h := timedOut(ctx)
h.Detail = errs.last()
return nil, &h
}
}
}
if err := send(initializeReq()); err != nil {
if _, h := await(1); h != nil {
return *h
}
return failed("start", err.Error())
}
m, h := await(1)
if h != nil {
return *h
}
if h := initError(m); h != nil {
return *h
}
_ = send(initializedNote)
return listTools(ctx, func(id int, req []byte) (*rpcMsg, *ServerHealth) {
_ = send(req)
return await(id)
})
}
func exitHealth(err error, errs *tail) ServerHealth {
h := failed("exited", errs.last())
var ee *exec.ExitError
if errors.As(err, &ee) {
h.Code = ee.ExitCode()
}
return h
}
// ---- a URL -------------------------------------------------------------------
// remoteHeaders are the server's own headers, with magpie's token when it
// is signed in to it: the agents reach it with that too.
func remoteHeaders(ctx context.Context, s *Server) (http.Header, *ServerHealth) {
h := http.Header{}
headers, unset := expandAll(s.Headers)
if len(unset) > 0 {
f := failed("novar", strings.Join(unset, ", "))
return nil, &f
}
for k, v := range headers {
h.Set(k, v)
}
if s.Transport == "http" && mcpauth.SignedIn(s.Name, s.URL) {
tok, err := mcpauth.Token(ctx, s.Name)
if err != nil {
return nil, &ServerHealth{State: "auth", OAuth: true, Detail: err.Error()}
}
h.Set("Authorization", "Bearer "+tok)
}
return h, nil
}
// netHealth puts a failed request in the page's words.
func netHealth(ctx context.Context, err error) ServerHealth {
if ctx.Err() != nil || errors.Is(err, context.DeadlineExceeded) {
return timedOut(ctx)
}
if errors.Is(err, connectionRefused) {
return failed("refused", err.Error())
}
var ue *url.Error
if errors.As(err, &ue) {
err = ue.Err
}
var dns *net.DNSError
if errors.As(err, &dns) {
return failed("unreachable", "can't find "+dns.Name)
}
return failed("unreachable", err.Error())
}
// refused is the health of a 401 or 403: the server wants a sign-in, or a
// key it wasn't given.
func refused(res *http.Response) ServerHealth {
return ServerHealth{State: "auth", Code: res.StatusCode, OAuth: res.Header.Get("WWW-Authenticate") != ""}
}
func statusHealth(res *http.Response) ServerHealth {
h := failed("http", res.Status)
h.Code = res.StatusCode
return h
}
func checkHTTP(ctx context.Context, s *Server) ServerHealth {
hdr, bad := remoteHeaders(ctx, s)
if bad != nil {
return *bad
}
// magpie's own token, when it is signed in: one the server refuses is
// renewed once and asked again with, as the gateway's relay does, and
// one refused just after it was renewed is a sign-in that has run out
tok := ""
if s.Transport == "http" && mcpauth.SignedIn(s.Name, s.URL) {
tok = strings.TrimPrefix(hdr.Get("Authorization"), "Bearer ")
}
renewed := false
session, version := "", mcpProtocol
post := func(body []byte) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.URL, bytes.NewReader(body))
if err != nil {
return nil, err
}
for k, v := range hdr {
req.Header[k] = v
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
if session != "" {
req.Header.Set("Mcp-Session-Id", session)
req.Header.Set("MCP-Protocol-Version", version)
}
return checkClient.Do(req)
}
// call posts a request and reads its reply, as JSON or from an event
// stream
var call func(id int, body []byte) (*rpcMsg, *ServerHealth)
call = func(id int, body []byte) (*rpcMsg, *ServerHealth) {
res, err := post(body)
if err != nil {
h := netHealth(ctx, err)
return nil, &h
}
defer res.Body.Close()
if res.StatusCode == 401 && tok != "" {
if renewed {
mcpauth.Rejected(s.Name, tok)
} else {
renewed = true
io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))
fresh, err := mcpauth.Renew(ctx, s.Name, tok)
if err != nil {
return nil, &ServerHealth{State: "auth", Code: 401, OAuth: true, Detail: err.Error()}
}
tok = fresh
hdr.Set("Authorization", "Bearer "+fresh)
return call(id, body)
}
}
if res.StatusCode == 401 || res.StatusCode == 403 {
h := refused(res)
return nil, &h
}
if res.StatusCode/100 != 2 {
h := statusHealth(res)
return nil, &h
}
if sid := res.Header.Get("Mcp-Session-Id"); sid != "" {
session = sid
}
m, err := replyOf(res, id)
if err != nil {
if ctx.Err() != nil {
h := timedOut(ctx)
return nil, &h
}
h := failed("protocol", err.Error())
return nil, &h
}
return m, nil
}
m, h := call(1, initializeReq())
if h != nil {
return *h
}
if h := initError(m); h != nil {
return *h
}
var init struct {
ProtocolVersion string `json:"protocolVersion"`
}
if json.Unmarshal(m.Result, &init) == nil && init.ProtocolVersion != "" {
version = init.ProtocolVersion
}
if session != "" {
defer endSession(s.URL, hdr, session)
}
stepTo(ctx, "notifications/initialized")
if res, err := post(initializedNote); err == nil {
io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))
res.Body.Close()
} else if ctx.Err() != nil {
return timedOut(ctx)
}
return listTools(ctx, call)
}
// endSession tells the server the check is done with its session, as a
// client leaving should; one that doesn't take it is no matter.
func endSession(u string, hdr http.Header, session string) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
if err != nil {
return
}
for k, v := range hdr {
req.Header[k] = v
}
req.Header.Set("Mcp-Session-Id", session)
if res, err := checkClient.Do(req); err == nil {
res.Body.Close()
}
}
// replyOf is the reply to id in a response: its JSON body, or the event
// stream's message that carries it.
func replyOf(res *http.Response, id int) (*rpcMsg, error) {
ct := res.Header.Get("Content-Type")
if strings.HasPrefix(ct, "text/event-stream") {
done := make(chan struct{})
defer close(done)
for e := range sseEvents(res.Body, done) {
if e.name != "" && e.name != "message" {
continue
}
var m rpcMsg
if json.Unmarshal([]byte(e.data), &m) == nil && m.is(id) {
return &m, nil
}
}
return nil, errors.New("the event stream ended without a reply")
}
b, err := io.ReadAll(io.LimitReader(res.Body, 16<<20))
if err != nil {
return nil, err
}
var m rpcMsg
if err := json.Unmarshal(b, &m); err != nil {
return nil, fmt.Errorf("not an MCP reply (%s): %s", ct, snippet(b))
}
return &m, nil
}
func snippet(b []byte) string {
s := strings.Join(strings.Fields(string(b)), " ")
if len(s) > 120 {
s = s[:120] + "…"
}
return s
}
type sseEvent struct{ name, data string }
// sseEvents reads an event stream's events until it ends.
// done ends the reading once the events are no longer wanted.
func sseEvents(r io.Reader, done <-chan struct{}) <-chan sseEvent {
out := make(chan sseEvent)
go func() {
defer close(out)
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 64*1024), 16<<20)
var e sseEvent
var data []string
for sc.Scan() {
l := strings.TrimSuffix(sc.Text(), "\r")
switch {
case l == "":
if len(data) > 0 {
e.data = strings.Join(data, "\n")
select {
case out <- e:
case <-done:
return
}
}
e, data = sseEvent{}, nil
case strings.HasPrefix(l, "event:"):
e.name = strings.TrimSpace(l[6:])
case strings.HasPrefix(l, "data:"):
data = append(data, strings.TrimPrefix(l[5:], " "))
}
}
}()
return out
}
// checkSSE checks a server on the older SSE transport: its stream names
// where to post, and the replies come back on the stream.
func checkSSE(ctx context.Context, s *Server) ServerHealth {
hdr, bad := remoteHeaders(ctx, s)
if bad != nil {
return *bad
}
stepTo(ctx, "connect")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL, nil)
if err != nil {
return failed("unreachable", err.Error())
}
for k, v := range hdr {
req.Header[k] = v
}
req.Header.Set("Accept", "text/event-stream")
res, err := checkClient.Do(req)
if err != nil {
return netHealth(ctx, err)
}
defer res.Body.Close()
if res.StatusCode == 401 || res.StatusCode == 403 {
return refused(res)
}
if res.StatusCode/100 != 2 {
return statusHealth(res)
}
events := sseEvents(res.Body, ctx.Done())
next := func() (sseEvent, *ServerHealth) {
select {
case e, ok := <-events:
if !ok {
h := timedOut(ctx)
if ctx.Err() == nil {
h = failed("protocol", "the event stream ended")
}
return e, &h
}
return e, nil
case <-ctx.Done():
h := timedOut(ctx)
return sseEvent{}, &h
}
}
var endpoint *url.URL
for endpoint == nil {
e, h := next()
if h != nil {
return *h
}
if e.name == "endpoint" {
base, _ := url.Parse(s.URL)
endpoint, err = base.Parse(strings.TrimSpace(e.data))
if err != nil {
return failed("protocol", "endpoint: "+err.Error())
}
}
}
post := func(body []byte) *ServerHealth {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(body))
if err != nil {
h := failed("protocol", err.Error())
return &h
}
for k, v := range hdr {
req.Header[k] = v
}
req.Header.Set("Content-Type", "application/json")
res, err := checkClient.Do(req)
if err != nil {
h := netHealth(ctx, err)
return &h
}
io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))
res.Body.Close()
if res.StatusCode == 401 || res.StatusCode == 403 {
h := refused(res)
return &h
}
if res.StatusCode/100 != 2 {
h := statusHealth(res)
return &h
}
return nil
}
await := func(id int) (*rpcMsg, *ServerHealth) {
for {
e, h := next()
if h != nil {
return nil, h
}
var m rpcMsg
if json.Unmarshal([]byte(e.data), &m) == nil && m.is(id) {
return &m, nil
}
}
}
stepTo(ctx, "initialize")
if h := post(initializeReq()); h != nil {
return *h
}
m, h := await(1)
if h != nil {
return *h
}
if h := initError(m); h != nil {
return *h
}
stepTo(ctx, "notifications/initialized")
if h := post(initializedNote); h != nil && h.Why == "timeout" {
return *h
}
return listTools(ctx, func(id int, req []byte) (*rpcMsg, *ServerHealth) {
if h := post(req); h != nil {
return nil, h
}
return await(id)
})
}