You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/mcp-elements.md
+38-15Lines changed: 38 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -154,31 +154,37 @@ public function getMultipleContent(): array
154
154
155
155
#### Error Handling
156
156
157
-
Tools can throw exceptions which are automatically converted to proper JSON-RPC error responses:
157
+
**Tools should ONLY throw `ToolCallException` for any error that occurs during tool execution.**
158
158
159
159
```php
160
+
use Mcp\Exception\ToolCallException;
161
+
160
162
#[McpTool]
161
163
public function divideNumbers(float $a, float $b): float
162
164
{
163
165
if ($b === 0.0) {
164
-
throw new \InvalidArgumentException('Division by zero is not allowed');
166
+
throw new ToolCallException('Division by zero is not allowed');
165
167
}
166
-
168
+
167
169
return $a / $b;
168
170
}
169
171
170
172
#[McpTool]
171
173
public function processFile(string $filename): string
172
174
{
173
175
if (!file_exists($filename)) {
174
-
throw new \InvalidArgumentException("File not found: {$filename}");
176
+
throw new ToolCallException("File not found: {$filename}");
175
177
}
176
-
178
+
177
179
return file_get_contents($filename);
178
180
}
179
181
```
180
182
181
-
The SDK will convert these exceptions into appropriate JSON-RPC error responses that MCP clients can understand.
183
+
**Critical Rule:** For tool handlers, **ALWAYS** throw `ToolCallException` for any error condition - validation errors, file not found, processing errors, etc. Do not use generic PHP exceptions.
184
+
185
+
**Error Handling Behavior:**
186
+
-**`ToolCallException`**: Converted to `CallToolResult` with `isError: true`, allowing the LLM to see the error message and self-correct
187
+
-**Any other exception**: Treated as internal server error and returns a generic error message to client
182
188
183
189
## Resources
184
190
@@ -298,24 +304,33 @@ public function getMultipleResources(): array
298
304
299
305
#### Error Handling
300
306
301
-
Resource handlers can throw exceptions for error cases:
307
+
**Resource handlers should ONLY throw `ResourceReadException` for any error that occurs during resource reading.**
302
308
303
309
```php
310
+
use Mcp\Exception\ResourceReadException;
311
+
304
312
#[McpResource(uri: 'file://{path}')]
305
313
public function getFile(string $path): string
306
314
{
307
315
if (!file_exists($path)) {
308
-
throw new \InvalidArgumentException("File not found: {$path}");
316
+
throw new ResourceReadException("File not found: {$path}");
309
317
}
310
-
318
+
311
319
if (!is_readable($path)) {
312
-
throw new \RuntimeException("File not readable: {$path}");
320
+
throw new ResourceReadException("File not readable: {$path}");
313
321
}
314
-
322
+
315
323
return file_get_contents($path);
316
324
}
317
325
```
318
326
327
+
**Critical Rule:** For resource handlers, **ALWAYS** throw `ResourceReadException` for any error condition - file not found, permission errors, data format issues, etc. Do not use generic PHP exceptions.
328
+
329
+
**Error Handling Behavior:**
330
+
-**`ResourceReadException`**: Converted to JSON-RPC error response with the actual exception message
331
+
-**Any other exception**: Treated as internal server error and returns a generic error message to client
332
+
333
+
319
334
## Resource Templates
320
335
321
336
Resource templates are **dynamic resources** that use parameterized URIs with variables. They follow all the same rules
@@ -456,26 +471,34 @@ public function explicitMessages(): array
456
471
457
472
#### Error Handling
458
473
459
-
Prompt handlers can throw exceptions for invalid inputs:
474
+
**Prompt handlers should ONLY throw `PromptGetException` for any error that occurs during prompt generation.**
460
475
461
476
```php
477
+
use Mcp\Exception\PromptGetException;
478
+
462
479
#[McpPrompt]
463
480
public function generatePrompt(string $topic, string $style): array
464
481
{
465
482
$validStyles = ['casual', 'formal', 'technical'];
466
-
483
+
467
484
if (!in_array($style, $validStyles)) {
468
-
throw new \InvalidArgumentException(
485
+
throw new PromptGetException(
469
486
"Invalid style '{$style}'. Must be one of: " . implode(', ', $validStyles)
470
487
);
471
488
}
472
-
489
+
473
490
return [
474
491
['role' => 'user', 'content' => "Write about {$topic} in a {$style} style"]
475
492
];
476
493
}
477
494
```
478
495
496
+
**Critical Rule:** For prompt handlers, **ALWAYS** throw `PromptGetException` for any error condition - invalid parameters, data validation errors, template processing issues, etc. Do not use generic PHP exceptions.
497
+
498
+
**Error Handling Behavior:**
499
+
-**`PromptGetException`**: Converted to JSON-RPC error response with the actual exception message
500
+
-**Any other exception**: Treated as internal server error and returns a generic error message to client
501
+
479
502
The SDK automatically validates that all messages have valid roles and converts the result into the appropriate MCP prompt message format.
0 commit comments