Skip to content

Commit e9181a1

Browse files
fix(tray): path-escape server names in tray API requests (#1752)
# Pull Request ## Description Same class as MCP-1118, on the tray side. The core decodes a `%2F`-encoded `{id}` on `/api/v1/servers/{id}/*`, but `cmd/mcpproxy-tray/internal/api/client.go` builds its per-server endpoints with a raw `fmt.Sprintf("/api/v1/servers/%s/...", serverName)`. For an official-registry server (`io.github.owner/repo`) the slash splits the name across path segments, so the request hits a different route. **Trigger.** Reachable from the tray menu: Enable / Disable (`EnableServer`), Quarantine (`QuarantineServer`) and Sign in (`TriggerOAuthLogin`) via `internal/tray/managers.go` and `internal/tray/tray.go:1907` → `api.ServerAdapter`. `RestartServer` and `GetServerTools` use the same shape and are fixed in the same way. `buildURL` keeps the escape: `url.Parse` records it in `RawPath` and `ResolveReference` goes through `EscapedPath`, so `%2F` reaches the wire. The test checks this end to end. ## Testing - [x] I have tested these changes locally - [x] I have added/updated tests that prove my fix is effective or my feature works - [x] All existing tests pass `TestClientServerActions_EscapeSlashServerName` (`cmd/mcpproxy-tray/internal/api/server_path_escape_test.go`) calls all six methods against an `httptest` server and checks `r.URL.EscapedPath()`. ``` go test ./cmd/mcpproxy-tray/internal/api -count=1 # ok # with client.go reverted: go test ./cmd/mcpproxy-tray/internal/api -run TestClientServerActions -count=1 # FAIL, 6 of 6 enable: request path = "/api/v1/servers/io.github.owner/repo/enable", want ".../io.github.owner%2Frepo/enable" gofmt -l cmd/mcpproxy-tray/internal/api # clean ``` (Run with `SDKROOT=.../MacOSX26.5.sdk` because my local Command Line Tools linker cannot read the 27.0 SDK stubs; unrelated to this change.) Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent b05d78e commit e9181a1

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎cmd/mcpproxy-tray/internal/api/client.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ func (c *Client) GetServers() ([]Server, error) {
586586
func (c *Client) EnableServer(serverName string, enabled bool) error {
587587
var endpoint string
588588
if enabled {
589-
endpoint = fmt.Sprintf("/api/v1/servers/%s/enable", serverName)
589+
endpoint = fmt.Sprintf("/api/v1/servers/%s/enable", url.PathEscape(serverName))
590590
} else {
591-
endpoint = fmt.Sprintf("/api/v1/servers/%s/disable", serverName)
591+
endpoint = fmt.Sprintf("/api/v1/servers/%s/disable", url.PathEscape(serverName))
592592
}
593593

594594
resp, err := c.makeRequest("POST", endpoint, nil)
@@ -605,7 +605,7 @@ func (c *Client) EnableServer(serverName string, enabled bool) error {
605605

606606
// RestartServer restarts a server
607607
func (c *Client) RestartServer(serverName string) error {
608-
endpoint := fmt.Sprintf("/api/v1/servers/%s/restart", serverName)
608+
endpoint := fmt.Sprintf("/api/v1/servers/%s/restart", url.PathEscape(serverName))
609609

610610
resp, err := c.makeRequest("POST", endpoint, nil)
611611
if err != nil {
@@ -640,7 +640,7 @@ func (c *Client) ForceReconnectAllServers(reason string) error {
640640

641641
// TriggerOAuthLogin triggers OAuth login for a server
642642
func (c *Client) TriggerOAuthLogin(serverName string) error {
643-
endpoint := fmt.Sprintf("/api/v1/servers/%s/login", serverName)
643+
endpoint := fmt.Sprintf("/api/v1/servers/%s/login", url.PathEscape(serverName))
644644

645645
resp, err := c.makeRequest("POST", endpoint, nil)
646646
if err != nil {
@@ -713,7 +713,7 @@ func (c *Client) SetActiveProfile(name string) error {
713713

714714
// GetServerTools gets tools for a specific server
715715
func (c *Client) GetServerTools(serverName string) ([]Tool, error) {
716-
endpoint := fmt.Sprintf("/api/v1/servers/%s/tools", serverName)
716+
endpoint := fmt.Sprintf("/api/v1/servers/%s/tools", url.PathEscape(serverName))
717717

718718
resp, err := c.makeRequest("GET", endpoint, nil)
719719
if err != nil {
@@ -754,7 +754,7 @@ func (c *Client) GetServerTools(serverName string) ([]Tool, error) {
754754

755755
// QuarantineServer places a server in quarantine
756756
func (c *Client) QuarantineServer(serverName string) error {
757-
endpoint := fmt.Sprintf("/api/v1/servers/%s/quarantine", serverName)
757+
endpoint := fmt.Sprintf("/api/v1/servers/%s/quarantine", url.PathEscape(serverName))
758758

759759
resp, err := c.makeRequest("POST", endpoint, nil)
760760
if err != nil {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"go.uber.org/zap"
9+
)
10+
11+
// Official-registry server ids are namespace/name; the core decodes a
12+
// %2F-encoded {id} on /api/v1/servers/{id}/* (MCP-1118), so the tray has to
13+
// send one for the menu actions to reach the right server.
14+
func TestClientServerActions_EscapeSlashServerName(t *testing.T) {
15+
const (
16+
name = "io.github.owner/repo"
17+
enc = "/api/v1/servers/io.github.owner%2Frepo"
18+
)
19+
20+
var got string
21+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
got = r.URL.EscapedPath()
23+
w.Header().Set("Content-Type", "application/json")
24+
_, _ = w.Write([]byte(`{"success":true,"data":{}}`))
25+
}))
26+
defer server.Close()
27+
28+
client := &Client{baseURL: server.URL, httpClient: server.Client(), logger: zap.NewNop().Sugar()}
29+
30+
for _, tc := range []struct {
31+
name string
32+
call func() error
33+
want string
34+
}{
35+
{"enable", func() error { return client.EnableServer(name, true) }, enc + "/enable"},
36+
{"disable", func() error { return client.EnableServer(name, false) }, enc + "/disable"},
37+
{"restart", func() error { return client.RestartServer(name) }, enc + "/restart"},
38+
{"login", func() error { return client.TriggerOAuthLogin(name) }, enc + "/login"},
39+
{"tools", func() error { _, err := client.GetServerTools(name); return err }, enc + "/tools"},
40+
{"quarantine", func() error { return client.QuarantineServer(name) }, enc + "/quarantine"},
41+
} {
42+
got = ""
43+
_ = tc.call()
44+
if got != tc.want {
45+
t.Errorf("%s: request path = %q, want %q", tc.name, got, tc.want)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)