What changed: GitHub's remote MCP server (api.githubcopilot.com/mcp/)
points to https://github.com/login/oauth, whose metadata has no
registration_endpoint, so magpie's sign-in, which always registered
itself (DCR), could never sign in to it. A server now carries an
optional OAuth app, `oauth: {clientId, clientSecret, callbackPort}`,
Claude Code's names for --client-id/--client-secret/--callback-port
(oauth.clientId in its .claude.json). mcpauth.StartWith signs in as
that app without registering: PKCE S256, a loopback callback
http://127.0.0.1:<port>/callback (any port unless callbackPort is set),
client_secret_post with a secret (basic only when the server lists
basic alone), "none" without one.
Without an app, a server with no registration endpoint now fails with
mcpauth.ErrNoRegistration, which says to make an OAuth app with the
vendor (GitHub's under Settings > Developer settings > OAuth Apps, with
http://127.0.0.1/callback) and give it under "OAuth app" in the editor.
The editor opens those fields with Client ID focused when that error
comes back. Filling them makes Sign in read "Save and sign in", and
Cancel asks before dropping them, as the earlier Cancel fix does.
GitHub answers token errors with 200 and {"error": ...}; tokenRequest
now reads a 2xx body with `error` as the error, and bad_refresh_token /
incorrect_client_credentials count as a dead sign-in as
invalid_grant/invalid_client do.
Secrets: the client secret sits in library.json like header and env
values. A backup without keys blanks it; WithSecrets puts this
computer's secret back only for the same client ID. No agent file is
given the app. The TUI editor, which doesn't show it, keeps it on save.
check() keeps it only on an HTTP server, requires a client ID when a
secret or port is given, and a port 1-65535.
Run (worktree at 98b5a02d1 + the two earlier commits):
- go test -race ./internal/mcpauth/... (new github_app_test.go: an
httptest fake built from GitHub's real PRM and AS metadata bytes:
no app -> ErrNoRegistration, sign-in as the app, wrong secret, fixed
callback port, bad refresh token)
- go test -tags nogui ./internal/library ./internal/tui ./internal/gui;
go test ./internal/gateway -run MCP
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
- GUI, Chromium and WebKit, 1100/390, en zh zh-TW ja de:
mcp-oauth-app (new, 22/22), mcp-editor-cancel, mcp-signin,
mcp-signin-health, library-mcp-health, i18n-keys - all pass
- Broken by hand, each went red: no 2xx error parse, GitHub codes out
of dead(), app ignored in StartWith, secret not blanked / not matched
by client ID in carry, check() without the non-HTTP rule; in the GUI,
oauth left out of the save, and the failed hook not opening the fields.
- Sandbox HOME, rebuilt magpie web, against the real GitHub server:
Sign in showed the new error and opened the fields; Save and sign in
saved the app and produced a real github.com/login/oauth/authorize
URL with the app's client_id, S256 and a 127.0.0.1 redirect, which
GitHub answered with its login page. .claude.json didn't hold the app.
Not run: a real GitHub sign-in to completion. There's no real OAuth app
to use, and I didn't make one on the owner's account. Claude Code's
callback says localhost where magpie's says 127.0.0.1, so an app made
for Claude Code needs its callback URL changed; that isn't bridged.
115 lines
3.4 KiB
Go
115 lines
3.4 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/mcpauth"
|
|
)
|
|
|
|
// A remote server that asks for an OAuth sign-in is signed in to once, in
|
|
// magpie, and every agent given it reaches it through the gateway's
|
|
// /mcp/<name>, which adds magpie's token (#615): the agents are given that
|
|
// address in place of the server's. A server magpie isn't signed in to is
|
|
// given as it is, and the agent signs in itself as before.
|
|
|
|
func init() {
|
|
mcpauth.Changed = func(string) { _, _ = Sync() }
|
|
// an agent beyond loopback is given a relayed server with the LAN key
|
|
// (through), which is the one there is no longer
|
|
access.LANChanged = func() {
|
|
if relaysAny() {
|
|
_, _ = Sync()
|
|
}
|
|
}
|
|
}
|
|
|
|
// relaysAny says whether any of the library's servers is given through the
|
|
// gateway: one magpie is signed in to.
|
|
func relaysAny() bool {
|
|
mu.Lock()
|
|
l, err := load()
|
|
mu.Unlock()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return slices.ContainsFunc(l.MCP, func(s *Server) bool { return s.Transport == "http" && mcpauth.SignedIn(s.Name, s.URL) })
|
|
}
|
|
|
|
// through is the server as an agent that reaches the gateway at base is
|
|
// given it. Only a streamable HTTP server is relayed: an SSE one's stream
|
|
// names the address to post to, which would have to be rewritten.
|
|
func through(s *Server, base string) *Server {
|
|
if s.Transport != "http" || !mcpauth.SignedIn(s.Name, s.URL) {
|
|
return s
|
|
}
|
|
c := *s
|
|
c.URL = strings.TrimRight(base, "/") + "/mcp/" + url.PathEscape(s.Name)
|
|
c.Headers = nil
|
|
for k, v := range s.Headers {
|
|
// magpie's token goes in its place
|
|
if strings.EqualFold(k, "Authorization") {
|
|
continue
|
|
}
|
|
if c.Headers == nil {
|
|
c.Headers = map[string]string{}
|
|
}
|
|
c.Headers[k] = v
|
|
}
|
|
if k := agent.KeyAt(base); k != gateway.Token {
|
|
// from another machine (a WSL distro under NAT) the gateway refuses
|
|
// a call without an enabled key, with a 401 the agent would take
|
|
// for the server asking it to sign in; the relay drops this
|
|
// Authorization for magpie's token
|
|
if c.Headers == nil {
|
|
c.Headers = map[string]string{}
|
|
}
|
|
c.Headers["Authorization"] = "Bearer " + k
|
|
}
|
|
return &c
|
|
}
|
|
|
|
// gatewayOf is the gateway's address as the target's agent reaches it.
|
|
func gatewayOf(t *Target) string {
|
|
if t.Agent != nil && t.Agent.Gateway != nil {
|
|
return t.Agent.Gateway()
|
|
}
|
|
return gateway.URL()
|
|
}
|
|
|
|
var errNotHTTP = errors.New("magpie signs in only to a server reached over streamable HTTP")
|
|
|
|
// SignInServer starts signing magpie in to the library's server called
|
|
// name: the State's URL is the page to open in a browser.
|
|
func SignInServer(ctx context.Context, name string) (mcpauth.State, error) {
|
|
mu.Lock()
|
|
l, err := load()
|
|
mu.Unlock()
|
|
if err != nil {
|
|
return mcpauth.State{}, err
|
|
}
|
|
s := l.server(name)
|
|
if s == nil {
|
|
return mcpauth.State{}, fmt.Errorf("no server called %s", name)
|
|
}
|
|
if s.Transport != "http" {
|
|
return mcpauth.State{}, errNotHTTP
|
|
}
|
|
var app *mcpauth.App
|
|
if s.OAuth != nil {
|
|
app = &mcpauth.App{ClientID: s.OAuth.ClientID, ClientSecret: s.OAuth.ClientSecret, CallbackPort: s.OAuth.CallbackPort}
|
|
}
|
|
return mcpauth.StartWith(ctx, s.Name, s.URL, app)
|
|
}
|
|
|
|
// SignOutServer forgets magpie's sign-in to the server called name, and
|
|
// gives the agents the server's own address again.
|
|
func SignOutServer(name string) error { return mcpauth.SignOut(name) }
|