xonecas.io
60255
Zn

Zoea-Nova / commit ed04983

fix: intercept and improve session error messages

Rewrites server error messages to prevent claim→login loops:
- session_required: Guides mysis to check recent tool results
- session_invalid: Clarifies when session truly expired

Addresses SESSION_ID_ERROR_MESSAGE_AUDIT_2026-02-07.md Phase 2

xonecas2026-02-07 ed0498369a5af7076e4cbe99d5052813b9a0b46f parent b325473

2 files changed

internal/mcp/proxy.go+40−3
@@ -140,15 +140,26 @@ func (p *Proxy) CallTool(ctx context.Context, caller CallerContext, name string,
140140
141141 result, err := p.callUpstreamWithRetry(ctx, name, args)
142142
143- if accountStore != nil && result != nil && !result.IsError {
144- p.interceptAuthTools(name, arguments, result)
143+ if result != nil {
144+ if !result.IsError && accountStore != nil {
145+ p.interceptAuthTools(name, arguments, result)
146+ }
147+ if result.IsError {
148+ // Rewrite error messages to guide myses toward correct behavior
149+ for i := range result.Content {
150+ if result.Content[i].Type == "text" {
151+ result.Content[i].Text = p.rewriteSessionError(result.Content[i].Text)
152+ }
153+ }
154+ }
145155 }
146156
147157 return result, err
148158 }
149159
160+ errorMsg := fmt.Sprintf("tool not found: %s", name)
150161 return &ToolResult{
151- Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("tool not found: %s", name)}},
162+ Content: []ContentBlock{{Type: "text", Text: p.rewriteSessionError(errorMsg)}},
152163 IsError: true,
153164 }, nil
154165 }
@@ -335,6 +346,32 @@ func findStringFieldAtPath(payload interface{}, path ...string) (string, bool) {
335346 return "", false
336347 }
337348
349+// rewriteSessionError improves session-related error messages to guide myses
350+// toward correct behavior instead of causing claim→login loops.
351+func (p *Proxy) rewriteSessionError(errorMsg string) string {
352+ // Handle session_required errors
353+ if strings.Contains(errorMsg, "session_required") {
354+ // Original: "You must provide a session_id. Get one by calling login() or register() first."
355+ // Problem: Tells mysis to login again even if they already have session_id
356+ return strings.Replace(errorMsg,
357+ "Get one by calling login() or register() first.",
358+ "Check your recent tool results for session_id from login/register and use it as a parameter.",
359+ 1)
360+ }
361+
362+ // Handle session_invalid errors
363+ if strings.Contains(errorMsg, "session_invalid") {
364+ // Original: "Call login() again to get a new session_id."
365+ // This is actually correct - session truly expired
366+ // But add clarity about when this happens
367+ if strings.Contains(errorMsg, "Session not found or expired") {
368+ return errorMsg + " This means your session truly expired (server restart, timeout, or duplicate login)."
369+ }
370+ }
371+
372+ return errorMsg
373+}
374+
338375 // Initialize initializes the upstream connection if available.
339376 func (p *Proxy) Initialize(ctx context.Context) error {
340377 if p.upstream == nil {
internal/mcp/proxy_test.go+74−0
@@ -3,6 +3,7 @@ package mcp
33 import (
44 "context"
55 "encoding/json"
6+ "strings"
67 "testing"
78 )
89
@@ -149,3 +150,76 @@ func TestProxyAuthInterceptionLogout(t *testing.T) {
149150 t.Fatalf("expected account released for pilot, got %+v", accounts.released)
150151 }
151152 }
153+
154+func TestRewriteSessionError_SessionRequired(t *testing.T) {
155+ proxy := &Proxy{}
156+
157+ original := "Error: session_required: You must provide a session_id. Get one by calling login() or register() first."
158+ rewritten := proxy.rewriteSessionError(original)
159+
160+ if strings.Contains(rewritten, "Get one by calling login()") {
161+ t.Error("Should have replaced the instruction to login again")
162+ }
163+
164+ if !strings.Contains(rewritten, "Check your recent tool results") {
165+ t.Error("Should instruct to check recent tool results")
166+ }
167+}
168+
169+func TestRewriteSessionError_SessionInvalid(t *testing.T) {
170+ proxy := &Proxy{}
171+
172+ original := "Error: session_invalid: Session not found or expired. Call login() again to get a new session_id."
173+ rewritten := proxy.rewriteSessionError(original)
174+
175+ if !strings.Contains(rewritten, "session truly expired") {
176+ t.Error("Should add clarification about true expiration")
177+ }
178+}
179+
180+func TestRewriteSessionError_Other(t *testing.T) {
181+ proxy := &Proxy{}
182+
183+ original := "Error: some_other_error: This is not a session error."
184+ rewritten := proxy.rewriteSessionError(original)
185+
186+ if rewritten != original {
187+ t.Error("Should not modify non-session errors")
188+ }
189+}
190+
191+func TestProxyRewritesUpstreamErrors(t *testing.T) {
192+ // Simulate upstream returning a session_required error
193+ upstream := &mockUpstream{
194+ result: &ToolResult{
195+ Content: []ContentBlock{{
196+ Type: "text",
197+ Text: "Error: session_required: You must provide a session_id. Get one by calling login() or register() first.",
198+ }},
199+ IsError: true,
200+ },
201+ }
202+ proxy := NewProxy(upstream)
203+
204+ result, err := proxy.CallTool(context.Background(), CallerContext{}, "mine", json.RawMessage(`{}`))
205+ if err != nil {
206+ t.Fatalf("CallTool() error: %v", err)
207+ }
208+
209+ if !result.IsError {
210+ t.Fatal("Expected error result")
211+ }
212+
213+ if len(result.Content) == 0 {
214+ t.Fatal("Expected content in error result")
215+ }
216+
217+ rewrittenText := result.Content[0].Text
218+ if strings.Contains(rewrittenText, "Get one by calling login()") {
219+ t.Error("Error message should have been rewritten")
220+ }
221+
222+ if !strings.Contains(rewrittenText, "Check your recent tool results") {
223+ t.Error("Error message should contain guidance to check tool results")
224+ }
225+}