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
+18-22Lines changed: 18 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -154,7 +154,10 @@ public function getMultipleContent(): array
154
154
155
155
#### Error Handling
156
156
157
-
**Tools SHOULD throw `ToolCallException` for errors that occur during tool execution to be communicated to the client.**
157
+
Tool handlers can throw any exception, but the type determines how it's handled:
158
+
159
+
-**`ToolCallException`**: Converted to JSON-RPC response with `CallToolResult` where `isError: true`, allowing the LLM to see the error message and self-correct
160
+
-**Any other exception**: Converted to JSON-RPC error response, but with a generic error message
158
161
159
162
```php
160
163
use Mcp\Exception\ToolCallException;
@@ -180,11 +183,8 @@ public function processFile(string $filename): string
180
183
}
181
184
```
182
185
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.
186
+
**Recommendation**: Use `ToolCallException`when you want to communicate specific errors to clients. Any other exception will still be converted to JSON-RPC compliant errors but with generic error messages.
184
187
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
188
188
189
189
## Resources
190
190
@@ -304,7 +304,10 @@ public function getMultipleResources(): array
304
304
305
305
#### Error Handling
306
306
307
-
**Resource handlers should ONLY throw `ResourceReadException` for any error that occurs during resource reading.**
307
+
Resource handlers can throw any exception, but the type determines how it's handled:
308
+
309
+
-**`ResourceReadException`**: Converted to JSON-RPC error response with the actual exception message
310
+
-**Any other exception**: Converted to JSON-RPC error response, but with a generic error message
308
311
309
312
```php
310
313
use Mcp\Exception\ResourceReadException;
@@ -324,11 +327,7 @@ public function getFile(string $path): string
324
327
}
325
328
```
326
329
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
330
+
**Recommendation**: Use `ResourceReadException` when you want to communicate specific errors to clients. Any other exception will still be converted to JSON-RPC compliant errors but with generic error messages.
332
331
333
332
334
333
## Resource Templates
@@ -464,14 +463,19 @@ public function explicitMessages(): array
464
463
}
465
464
```
466
465
466
+
467
+
The SDK automatically validates that all messages have valid roles and converts the result into the appropriate MCP prompt message format.
468
+
467
469
#### Valid Message Roles
468
470
469
471
-**`user`**: User input or questions
470
472
-**`assistant`**: Assistant responses/system
471
473
472
474
#### Error Handling
473
475
474
-
**Prompt handlers should ONLY throw `PromptGetException` for any error that occurs during prompt generation.**
476
+
Prompt handlers can throw any exception, but the type determines how it's handled:
477
+
-**`PromptGetException`**: Converted to JSON-RPC error response with the actual exception message
478
+
-**Any other exception**: Converted to JSON-RPC error response, but with a generic error message
475
479
476
480
```php
477
481
use Mcp\Exception\PromptGetException;
@@ -493,19 +497,11 @@ public function generatePrompt(string $topic, string $style): array
493
497
}
494
498
```
495
499
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
-
502
-
The SDK automatically validates that all messages have valid roles and converts the result into the appropriate MCP prompt message format.
500
+
**Recommendation**: Use `PromptGetException` when you want to communicate specific errors to clients. Any other exception will still be converted to JSON-RPC compliant errors but with generic error messages.
503
501
504
502
## Completion Providers
505
503
506
-
Completion providers help MCP clients offer auto-completion suggestions for Resource Templates and Prompts. Unlike Tools
507
-
and static Resources (which can be listed via `tools/list` and `resources/list`), Resource Templates and Prompts have
508
-
dynamic parameters that benefit from completion hints.
504
+
Completion providers help MCP clients offer auto-completion suggestions for Resource Templates and Prompts. Unlike Tools and static Resources (which can be listed via `tools/list` and `resources/list`), Resource Templates and Prompts have dynamic parameters that benefit from completion hints.
0 commit comments