20
20
use Mcp \Schema \Request \CallToolRequest ;
21
21
use Mcp \Schema \Result \CallToolResult ;
22
22
use Mcp \Server \RequestHandler \CallToolHandler ;
23
+ use Mcp \Server \Session \SessionInterface ;
23
24
use PHPUnit \Framework \MockObject \MockObject ;
24
25
use PHPUnit \Framework \TestCase ;
25
26
use Psr \Log \LoggerInterface ;
26
27
27
28
class CallToolHandlerTest extends TestCase
28
29
{
29
30
private CallToolHandler $ handler ;
30
- private ToolCallerInterface |MockObject $ toolExecutor ;
31
+ private ToolCallerInterface |MockObject $ toolCaller ;
31
32
private LoggerInterface |MockObject $ logger ;
33
+ private SessionInterface |MockObject $ session ;
32
34
33
35
protected function setUp (): void
34
36
{
35
- $ this ->toolExecutor = $ this ->createMock (ToolCallerInterface::class);
37
+ $ this ->toolCaller = $ this ->createMock (ToolCallerInterface::class);
36
38
$ this ->logger = $ this ->createMock (LoggerInterface::class);
39
+ $ this ->session = $ this ->createMock (SessionInterface::class);
37
40
38
41
$ this ->handler = new CallToolHandler (
39
- $ this ->toolExecutor ,
42
+ $ this ->toolCaller ,
40
43
$ this ->logger ,
41
44
);
42
45
}
@@ -53,7 +56,7 @@ public function testHandleSuccessfulToolCall(): void
53
56
$ request = $ this ->createCallToolRequest ('greet_user ' , ['name ' => 'John ' ]);
54
57
$ expectedResult = new CallToolResult ([new TextContent ('Hello, John! ' )]);
55
58
56
- $ this ->toolExecutor
59
+ $ this ->toolCaller
57
60
->expects ($ this ->once ())
58
61
->method ('call ' )
59
62
->with ($ request )
@@ -63,7 +66,7 @@ public function testHandleSuccessfulToolCall(): void
63
66
->expects ($ this ->never ())
64
67
->method ('error ' );
65
68
66
- $ response = $ this ->handler ->handle ($ request );
69
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
67
70
68
71
$ this ->assertInstanceOf (Response::class, $ response );
69
72
$ this ->assertEquals ($ request ->getId (), $ response ->id );
@@ -75,13 +78,13 @@ public function testHandleToolCallWithEmptyArguments(): void
75
78
$ request = $ this ->createCallToolRequest ('simple_tool ' , []);
76
79
$ expectedResult = new CallToolResult ([new TextContent ('Simple result ' )]);
77
80
78
- $ this ->toolExecutor
81
+ $ this ->toolCaller
79
82
->expects ($ this ->once ())
80
83
->method ('call ' )
81
84
->with ($ request )
82
85
->willReturn ($ expectedResult );
83
86
84
- $ response = $ this ->handler ->handle ($ request );
87
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
85
88
86
89
$ this ->assertInstanceOf (Response::class, $ response );
87
90
$ this ->assertSame ($ expectedResult , $ response ->result );
@@ -99,13 +102,13 @@ public function testHandleToolCallWithComplexArguments(): void
99
102
$ request = $ this ->createCallToolRequest ('complex_tool ' , $ arguments );
100
103
$ expectedResult = new CallToolResult ([new TextContent ('Complex result ' )]);
101
104
102
- $ this ->toolExecutor
105
+ $ this ->toolCaller
103
106
->expects ($ this ->once ())
104
107
->method ('call ' )
105
108
->with ($ request )
106
109
->willReturn ($ expectedResult );
107
110
108
- $ response = $ this ->handler ->handle ($ request );
111
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
109
112
110
113
$ this ->assertInstanceOf (Response::class, $ response );
111
114
$ this ->assertSame ($ expectedResult , $ response ->result );
@@ -116,7 +119,7 @@ public function testHandleToolNotFoundExceptionReturnsError(): void
116
119
$ request = $ this ->createCallToolRequest ('nonexistent_tool ' , ['param ' => 'value ' ]);
117
120
$ exception = new ToolNotFoundException ($ request );
118
121
119
- $ this ->toolExecutor
122
+ $ this ->toolCaller
120
123
->expects ($ this ->once ())
121
124
->method ('call ' )
122
125
->with ($ request )
@@ -133,7 +136,7 @@ public function testHandleToolNotFoundExceptionReturnsError(): void
133
136
],
134
137
);
135
138
136
- $ response = $ this ->handler ->handle ($ request );
139
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
137
140
138
141
$ this ->assertInstanceOf (Error::class, $ response );
139
142
$ this ->assertEquals ($ request ->getId (), $ response ->id );
@@ -146,7 +149,7 @@ public function testHandleToolExecutionExceptionReturnsError(): void
146
149
$ request = $ this ->createCallToolRequest ('failing_tool ' , ['param ' => 'value ' ]);
147
150
$ exception = new ToolCallException ($ request , new \RuntimeException ('Tool execution failed ' ));
148
151
149
- $ this ->toolExecutor
152
+ $ this ->toolCaller
150
153
->expects ($ this ->once ())
151
154
->method ('call ' )
152
155
->with ($ request )
@@ -163,7 +166,7 @@ public function testHandleToolExecutionExceptionReturnsError(): void
163
166
],
164
167
);
165
168
166
- $ response = $ this ->handler ->handle ($ request );
169
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
167
170
168
171
$ this ->assertInstanceOf (Error::class, $ response );
169
172
$ this ->assertEquals ($ request ->getId (), $ response ->id );
@@ -176,13 +179,13 @@ public function testHandleWithNullResult(): void
176
179
$ request = $ this ->createCallToolRequest ('null_tool ' , []);
177
180
$ expectedResult = new CallToolResult ([]);
178
181
179
- $ this ->toolExecutor
182
+ $ this ->toolCaller
180
183
->expects ($ this ->once ())
181
184
->method ('call ' )
182
185
->with ($ request )
183
186
->willReturn ($ expectedResult );
184
187
185
- $ response = $ this ->handler ->handle ($ request );
188
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
186
189
187
190
$ this ->assertInstanceOf (Response::class, $ response );
188
191
$ this ->assertSame ($ expectedResult , $ response ->result );
@@ -193,13 +196,13 @@ public function testHandleWithErrorResult(): void
193
196
$ request = $ this ->createCallToolRequest ('error_tool ' , []);
194
197
$ expectedResult = CallToolResult::error ([new TextContent ('Tool error occurred ' )]);
195
198
196
- $ this ->toolExecutor
199
+ $ this ->toolCaller
197
200
->expects ($ this ->once ())
198
201
->method ('call ' )
199
202
->with ($ request )
200
203
->willReturn ($ expectedResult );
201
204
202
- $ response = $ this ->handler ->handle ($ request );
205
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
203
206
204
207
$ this ->assertInstanceOf (Response::class, $ response );
205
208
$ this ->assertSame ($ expectedResult , $ response ->result );
@@ -208,7 +211,7 @@ public function testHandleWithErrorResult(): void
208
211
209
212
public function testConstructorWithDefaultLogger (): void
210
213
{
211
- $ handler = new CallToolHandler ($ this ->toolExecutor );
214
+ $ handler = new CallToolHandler ($ this ->toolCaller );
212
215
213
216
$ this ->assertInstanceOf (CallToolHandler::class, $ handler );
214
217
}
@@ -218,7 +221,7 @@ public function testHandleLogsErrorWithCorrectParameters(): void
218
221
$ request = $ this ->createCallToolRequest ('test_tool ' , ['key1 ' => 'value1 ' , 'key2 ' => 42 ]);
219
222
$ exception = new ToolCallException ($ request , new \RuntimeException ('Custom error message ' ));
220
223
221
- $ this ->toolExecutor
224
+ $ this ->toolCaller
222
225
->expects ($ this ->once ())
223
226
->method ('call ' )
224
227
->willThrowException ($ exception );
@@ -234,21 +237,21 @@ public function testHandleLogsErrorWithCorrectParameters(): void
234
237
],
235
238
);
236
239
237
- $ this ->handler ->handle ($ request );
240
+ $ this ->handler ->handle ($ request, $ this -> session );
238
241
}
239
242
240
243
public function testHandleWithSpecialCharactersInToolName (): void
241
244
{
242
245
$ request = $ this ->createCallToolRequest ('tool-with_special.chars ' , []);
243
246
$ expectedResult = new CallToolResult ([new TextContent ('Special tool result ' )]);
244
247
245
- $ this ->toolExecutor
248
+ $ this ->toolCaller
246
249
->expects ($ this ->once ())
247
250
->method ('call ' )
248
251
->with ($ request )
249
252
->willReturn ($ expectedResult );
250
253
251
- $ response = $ this ->handler ->handle ($ request );
254
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
252
255
253
256
$ this ->assertInstanceOf (Response::class, $ response );
254
257
$ this ->assertSame ($ expectedResult , $ response ->result );
@@ -264,13 +267,13 @@ public function testHandleWithSpecialCharactersInArguments(): void
264
267
$ request = $ this ->createCallToolRequest ('unicode_tool ' , $ arguments );
265
268
$ expectedResult = new CallToolResult ([new TextContent ('Unicode handled ' )]);
266
269
267
- $ this ->toolExecutor
270
+ $ this ->toolCaller
268
271
->expects ($ this ->once ())
269
272
->method ('call ' )
270
273
->with ($ request )
271
274
->willReturn ($ expectedResult );
272
275
273
- $ response = $ this ->handler ->handle ($ request );
276
+ $ response = $ this ->handler ->handle ($ request, $ this -> session );
274
277
275
278
$ this ->assertInstanceOf (Response::class, $ response );
276
279
$ this ->assertSame ($ expectedResult , $ response ->result );
@@ -284,7 +287,7 @@ private function createCallToolRequest(string $name, array $arguments): CallTool
284
287
return CallToolRequest::fromArray ([
285
288
'jsonrpc ' => '2.0 ' ,
286
289
'method ' => CallToolRequest::getMethod (),
287
- 'id ' => 'test-request- ' . uniqid (),
290
+ 'id ' => 'test-request- ' . uniqid (),
288
291
'params ' => [
289
292
'name ' => $ name ,
290
293
'arguments ' => $ arguments ,
0 commit comments