Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-6034: fix(cursor): normalize MCP approval server shapes (#6776)
Wu Changming84dc02cd65ff
diff --git a/server/internal/daemon/execenv/cursor_mcp.go b/server/internal/daemon/execenv/cursor_mcp.goindex f8e6c2de7e..7daa39ccd8 100644--- a/server/internal/daemon/execenv/cursor_mcp.go+++ b/server/internal/daemon/execenv/cursor_mcp.go@@ -31,6 +31,20 @@ type cursorMcpConfigFile struct {McpServers map[string]json.RawMessage `json:"mcpServers"`}+type cursorStdioMcpApprovalServer struct {+ Type json.RawMessage `json:"type,omitempty"`+ Command json.RawMessage `json:"command"`+ Args json.RawMessage `json:"args,omitempty"`+ Env json.RawMessage `json:"env,omitempty"`+ Cwd json.RawMessage `json:"cwd,omitempty"`+}++type cursorRemoteMcpApprovalServer struct {+ Type json.RawMessage `json:"type,omitempty"`+ URL json.RawMessage `json:"url"`+ Headers json.RawMessage `json:"headers,omitempty"`+}+// prepareCursorMcpConfig writes the Cursor-native MCP sidecars for agents that// have an explicit managed mcp_config saved. A nil/null mcp_config means "let// Cursor behave normally", so no .cursor/mcp.json or CURSOR_DATA_DIR is created.@@ -239,9 +253,9 @@ func cursorMcpApprovalKeys(projectRoot string, servers map[string]json.RawMessagapprovals := make([]string, 0, len(names))for _, name := range names {- compact := &bytes.Buffer{}- if err := json.Compact(compact, servers[name]); err != nil {- return nil, fmt.Errorf("compact mcp_servers.%s: %w", name, err)+ server, err := marshalCursorMcpApprovalServer(servers[name])+ if err != nil {+ return nil, fmt.Errorf("marshal mcp_servers.%s for cursor approval: %w", name, err)}pathJSON, err := json.Marshal(projectRoot)if err != nil {@@ -250,7 +264,7 @@ func cursorMcpApprovalKeys(projectRoot string, servers map[string]json.RawMessagpayload := []byte(`{"path":`)payload = append(payload, pathJSON...)payload = append(payload, []byte(`,"server":`)...)- payload = append(payload, compact.Bytes()...)+ payload = append(payload, server...)payload = append(payload, '}')sum := sha256.Sum256(payload)@@ -259,6 +273,50 @@ func cursorMcpApprovalKeys(projectRoot string, servers map[string]json.RawMessagreturn approvals, nil}+func marshalCursorMcpApprovalServer(raw json.RawMessage) ([]byte, error) {+ compact := &bytes.Buffer{}+ if err := json.Compact(compact, raw); err != nil {+ return nil, err+ }++ var fields map[string]json.RawMessage+ if err := json.Unmarshal(compact.Bytes(), &fields); err != nil {+ return nil, err+ }+ if command, isStdio := fields["command"]; isStdio {+ // Cursor normalizes stdio servers to this exact field set and order+ // before hashing; unknown fields are dropped and absent fields omitted.+ return marshalJSONStringifyCompatible(cursorStdioMcpApprovalServer{+ Type: fields["type"],+ Command: command,+ Args: fields["args"],+ Env: fields["env"],+ Cwd: fields["cwd"],+ })+ }+ if url, isRemote := fields["url"]; isRemote {+ // Remote servers use a separate normalized shape before hashing.+ return marshalJSONStringifyCompatible(cursorRemoteMcpApprovalServer{+ Type: fields["type"],+ URL: url,+ Headers: fields["headers"],+ })+ }+ return compact.Bytes(), nil+}++// marshalJSONStringifyCompatible serializes a value without Go's default HTML+// escaping so approval payload bytes match Cursor's JSON.stringify output.+func marshalJSONStringifyCompatible(value any) ([]byte, error) {+ buf := &bytes.Buffer{}+ enc := json.NewEncoder(buf)+ enc.SetEscapeHTML(false)+ if err := enc.Encode(value); err != nil {+ return nil, err+ }+ return bytes.TrimSuffix(buf.Bytes(), []byte("\n")), nil+}+func cursorProjectRoot(workDir string) string {if workDir == "" {return workDirdiff --git a/server/internal/daemon/execenv/cursor_mcp_test.go b/server/internal/daemon/execenv/cursor_mcp_test.goindex 337969dd29..14f811132e 100644--- a/server/internal/daemon/execenv/cursor_mcp_test.go+++ b/server/internal/daemon/execenv/cursor_mcp_test.go@@ -23,6 +23,68 @@ func TestCursorMcpApprovalKeyMatchesCursorAgent(t *testing.T) {}}+func TestCursorMcpApprovalKeyMatchesCursorAgentWithReorderedStdioConfig(t *testing.T) {+ t.Parallel()++ keys, err := cursorMcpApprovalKeys("/tmp/work", map[string]json.RawMessage{+ "fetch": json.RawMessage(`{ "args": [ "mcp-server-fetch" ], "command": "uvx", "env": { "TOKEN": "x" } }`),+ })+ if err != nil {+ t.Fatalf("cursorMcpApprovalKeys: %v", err)+ }+ want := []string{"fetch-4c7232835349773b"}+ if !reflect.DeepEqual(keys, want) {+ t.Fatalf("approval keys = %v, want %v", keys, want)+ }+}++func TestCursorMcpApprovalKeyMatchesJSONStringifyWithHTMLCharacters(t *testing.T) {+ t.Parallel()++ keys, err := cursorMcpApprovalKeys("/tmp/work", map[string]json.RawMessage{+ "fetch": json.RawMessage(`{"command":"uvx","args":["mcp"],"env":{"URL":"https://x.test?a=1&b=2"}}`),+ })+ if err != nil {+ t.Fatalf("cursorMcpApprovalKeys: %v", err)+ }+ want := []string{"fetch-37ab92a3e088d73a"}+ if !reflect.DeepEqual(keys, want) {+ t.Fatalf("approval keys = %v, want %v", keys, want)+ }+}++func TestCursorMcpApprovalKeyMatchesCursorAgentWithStdioTypeAndCwd(t *testing.T) {+ t.Parallel()++ keys, err := cursorMcpApprovalKeys("/private/tmp/work", map[string]json.RawMessage{+ "withtypecwd": json.RawMessage(`{"enabled":true,"cwd":"/tmp/probe","timeout":30,"args":["mcp"],"command":"uvx","type":"stdio"}`),+ })+ if err != nil {+ t.Fatalf("cursorMcpApprovalKeys: %v", err)+ }+ // Captured from cursor-agent 2026.08.04-aaa8809 via mcp enable.+ want := []string{"withtypecwd-ba71d2f70913528e"}+ if !reflect.DeepEqual(keys, want) {+ t.Fatalf("approval keys = %v, want %v", keys, want)+ }+}++func TestCursorMcpApprovalKeyMatchesCursorAgentForRemoteServer(t *testing.T) {+ t.Parallel()++ keys, err := cursorMcpApprovalKeys("/private/tmp/work", map[string]json.RawMessage{+ "remote": json.RawMessage(`{"enabled":true,"headers":{"Authorization":"Bearer x"},"timeout":30,"url":"https://mcp.example.com","type":"http"}`),+ })+ if err != nil {+ t.Fatalf("cursorMcpApprovalKeys: %v", err)+ }+ // Captured from cursor-agent 2026.08.04-aaa8809 via mcp enable.+ want := []string{"remote-abc2ffe78532b37e"}+ if !reflect.DeepEqual(keys, want) {+ t.Fatalf("approval keys = %v, want %v", keys, want)+ }+}+func TestPrepareCursorMcpConfigWritesProjectConfigAndApprovals(t *testing.T) {t.Parallel()