Skip to content

Commit 8f5bcf8

Browse files
committed
Add sentinel errors
1 parent 988dd4c commit 8f5bcf8

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

server/elicitation.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@ package server
22

33
import (
44
"context"
5-
"fmt"
5+
"errors"
66

77
"github.com/mark3labs/mcp-go/mcp"
88
)
99

10+
var (
11+
// ErrNoActiveSession is returned when there is no active session in the context
12+
ErrNoActiveSession = errors.New("no active session")
13+
// ErrElicitationNotSupported is returned when the session does not support elicitation
14+
ErrElicitationNotSupported = errors.New("session does not support elicitation")
15+
)
16+
1017
// RequestElicitation sends an elicitation request to the client.
1118
// The client must have declared elicitation capability during initialization.
1219
// The session must implement SessionWithElicitation to support this operation.
1320
func (s *MCPServer) RequestElicitation(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error) {
1421
session := ClientSessionFromContext(ctx)
1522
if session == nil {
16-
return nil, fmt.Errorf("no active session")
23+
return nil, ErrNoActiveSession
1724
}
1825

1926
// Check if the session supports elicitation requests
2027
if elicitationSession, ok := session.(SessionWithElicitation); ok {
2128
return elicitationSession.RequestElicitation(ctx, request)
2229
}
2330

24-
return nil, fmt.Errorf("session does not support elicitation")
31+
return nil, ErrElicitationNotSupported
2532
}

server/elicitation_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"context"
5+
"errors"
56
"testing"
67

78
"github.com/mark3labs/mcp-go/mcp"
@@ -75,9 +76,8 @@ func TestMCPServer_RequestElicitation_NoSession(t *testing.T) {
7576
t.Error("expected error when no session available")
7677
}
7778

78-
expectedError := "no active session"
79-
if err.Error() != expectedError {
80-
t.Errorf("expected error %q, got %q", expectedError, err.Error())
79+
if !errors.Is(err, ErrNoActiveSession) {
80+
t.Errorf("expected ErrNoActiveSession, got %v", err)
8181
}
8282
}
8383

@@ -105,9 +105,8 @@ func TestMCPServer_RequestElicitation_SessionDoesNotSupportElicitation(t *testin
105105
t.Error("expected error when session doesn't support elicitation")
106106
}
107107

108-
expectedError := "session does not support elicitation"
109-
if err.Error() != expectedError {
110-
t.Errorf("expected error %q, got %q", expectedError, err.Error())
108+
if !errors.Is(err, ErrElicitationNotSupported) {
109+
t.Errorf("expected ErrElicitationNotSupported, got %v", err)
111110
}
112111
}
113112

@@ -176,7 +175,7 @@ func TestRequestElicitation(t *testing.T) {
176175
name string
177176
session ClientSession
178177
request mcp.ElicitationRequest
179-
expectedError string
178+
expectedError error
180179
expectedType mcp.ElicitationResponseType
181180
}{
182181
{
@@ -234,7 +233,7 @@ func TestRequestElicitation(t *testing.T) {
234233
RequestedSchema: map[string]any{"type": "object"},
235234
},
236235
},
237-
expectedError: "session does not support elicitation",
236+
expectedError: ErrElicitationNotSupported,
238237
},
239238
}
240239

@@ -245,9 +244,9 @@ func TestRequestElicitation(t *testing.T) {
245244

246245
result, err := server.RequestElicitation(ctx, tt.request)
247246

248-
if tt.expectedError != "" {
247+
if tt.expectedError != nil {
249248
require.Error(t, err)
250-
assert.Contains(t, err.Error(), tt.expectedError)
249+
assert.True(t, errors.Is(err, tt.expectedError), "expected %v, got %v", tt.expectedError, err)
251250
return
252251
}
253252

0 commit comments

Comments
 (0)