-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhandler_test.go
More file actions
215 lines (174 loc) · 5.68 KB
/
Copy pathhandler_test.go
File metadata and controls
215 lines (174 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Tencent is pleased to support the open source community by making trpc-mcp-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-mcp-go is licensed under the Apache License Version 2.0.
package mcp
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewMCPHandler(t *testing.T) {
// Create handler
handler := newMCPHandler()
// Verify object created successfully
assert.NotNil(t, handler)
assert.NotNil(t, handler.toolManager)
assert.NotNil(t, handler.lifecycleManager)
}
func TestMCPHandler_WithOptions(t *testing.T) {
// Create custom components
toolManager := newToolManager()
lifecycleManager := newLifecycleManager(Implementation{
Name: "Test-Server",
Version: "1.0.0",
})
// Create handler with options
handler := newMCPHandler(
withToolManager(toolManager),
withLifecycleManager(lifecycleManager),
)
// Verify options applied
assert.Equal(t, toolManager, handler.toolManager)
assert.Equal(t, lifecycleManager, handler.lifecycleManager)
}
func TestMCPHandler_Initialize(t *testing.T) {
// Create handler
toolManager := newToolManager()
lifecycleManager := newLifecycleManager(Implementation{
Name: "Test-Server",
Version: "1.0.0",
})
handler := newMCPHandler(
withToolManager(toolManager),
withLifecycleManager(lifecycleManager),
)
// Create initialization request
request := NewInitializeRequest(
ProtocolVersion_2024_11_05,
Implementation{
Name: "Test-Client",
Version: "1.0.0",
},
ClientCapabilities{
Roots: &RootsCapability{
ListChanged: true,
},
Sampling: &SamplingCapability{},
},
)
// Create session
session := newSession()
// Process request
ctx := context.Background()
resp, err := handler.handleRequest(ctx, request, session)
// Verify results
require.NoError(t, err)
assert.NotNil(t, resp)
// Verify protocol version in session
protocolVersion, ok := session.GetData("protocolVersion")
require.True(t, ok)
assert.Equal(t, ProtocolVersion_2024_11_05, protocolVersion)
}
func TestMCPHandler_UnknownMethod(t *testing.T) {
// Create handler
handler := newMCPHandler()
// Create request with unknown method
req := newJSONRPCRequest(1, "unknown/method", nil)
// Create session
session := newSession()
// Process request
ctx := context.Background()
resp, err := handler.handleRequest(ctx, req, session)
// Updated test expectation: for unknown methods, the handler now may return a JSONRPCError response instead of an error
// This might be due to internal implementation changes
assert.Nil(t, err)
assert.NotNil(t, resp)
// Check if a JSONRPCError was returned
errorResp, ok := resp.(*JSONRPCError)
assert.True(t, ok, "Expected JSONRPCError response")
assert.Equal(t, -32601, errorResp.Error.Code)
assert.Equal(t, "method not found", errorResp.Error.Message)
}
// handleMockTool handles the mock tool
func handleMockTool(ctx context.Context, req *CallToolRequest) (*CallToolResult, error) {
return &CallToolResult{
Content: []Content{
NewTextContent("Mock tool response"),
},
}, nil
}
func TestMCPHandler_ToolsList(t *testing.T) {
// Create handler
handler := newMCPHandler()
// Register test tool
tool := NewMockTool("test-tool", "Test Tool", map[string]interface{}{})
handler.toolManager.registerTool(tool, handleMockTool)
// Create session and set protocol version
session := newSession()
session.SetData("protocolVersion", ProtocolVersion_2025_03_26)
// Create list tools request
req := newJSONRPCRequest(1, MethodToolsList, nil)
// Process request
ctx := context.Background()
resp, err := handler.handleRequest(ctx, req, session)
// Verify results
require.NoError(t, err)
assert.NotNil(t, resp)
// Print actual response type for debugging
t.Logf("Response type: %T", resp)
// Check if it's a JSONRPCResponse type
if jsonRPCResp, ok := resp.(*JSONRPCResponse); ok {
t.Logf("It's a JSONRPCResponse with JSONRPC: %s, ID: %d", jsonRPCResp.JSONRPC, jsonRPCResp.ID)
// Check the Result field
if result, ok := jsonRPCResp.Result.(*ListToolsResult); ok {
assert.NotNil(t, result.Tools)
assert.Len(t, result.Tools, 1)
assert.Equal(t, "test-tool", result.Tools[0].Name)
assert.Equal(t, "Test Tool", result.Tools[0].Description)
return
}
// Check if Result is of another type
t.Logf("Result type: %T", jsonRPCResp.Result)
}
// Check if it's a ListToolsResult type
if result, ok := resp.(ListToolsResult); ok {
assert.NotNil(t, result.Tools)
assert.Len(t, result.Tools, 1)
assert.Equal(t, "test-tool", result.Tools[0].Name)
assert.Equal(t, "Test Tool", result.Tools[0].Description)
return
}
// If it's neither, print detailed type information for debugging
t.Errorf("Unexpected response type: %T", resp)
}
func TestMCPHandler_ToolsCall(t *testing.T) {
// Create handler
handler := newMCPHandler()
// Register test tool
tool := NewMockTool("test-tool", "Test Tool", map[string]interface{}{})
handler.toolManager.registerTool(tool, handleMockTool)
// Create session
session := newSession()
session.SetData("protocolVersion", ProtocolVersion_2024_11_05)
// Create call tool request
req := newJSONRPCRequest(1, MethodToolsCall, map[string]interface{}{
"name": "test-tool",
"arguments": map[string]interface{}{
"param1": "value1",
},
})
// Process request
ctx := context.Background()
resp, err := handler.handleRequest(ctx, req, session)
// Verify results
require.NoError(t, err)
assert.NotNil(t, resp)
// Updated test expectation: the response might now be a CallToolResult or another direct response type
result, ok := resp.(*CallToolResult)
assert.True(t, ok, "Expected CallToolResult response")
assert.NotNil(t, result.Content)
assert.Len(t, result.Content, 1)
}