Skip to content

Commit 2742932

Browse files
docs: clarify error handling behavior for all element types
1 parent ba16cc4 commit 2742932

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

docs/mcp-elements.md

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ public function getMultipleContent(): array
154154

155155
#### Error Handling
156156

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
158161

159162
```php
160163
use Mcp\Exception\ToolCallException;
@@ -180,11 +183,8 @@ public function processFile(string $filename): string
180183
}
181184
```
182185

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.
184187

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
188188

189189
## Resources
190190

@@ -304,7 +304,10 @@ public function getMultipleResources(): array
304304

305305
#### Error Handling
306306

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
308311

309312
```php
310313
use Mcp\Exception\ResourceReadException;
@@ -324,11 +327,7 @@ public function getFile(string $path): string
324327
}
325328
```
326329

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.
332331

333332

334333
## Resource Templates
@@ -464,14 +463,19 @@ public function explicitMessages(): array
464463
}
465464
```
466465

466+
467+
The SDK automatically validates that all messages have valid roles and converts the result into the appropriate MCP prompt message format.
468+
467469
#### Valid Message Roles
468470

469471
- **`user`**: User input or questions
470472
- **`assistant`**: Assistant responses/system
471473

472474
#### Error Handling
473475

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
475479

476480
```php
477481
use Mcp\Exception\PromptGetException;
@@ -493,19 +497,11 @@ public function generatePrompt(string $topic, string $style): array
493497
}
494498
```
495499

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.
503501

504502
## Completion Providers
505503

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.
509505

510506
### Completion Provider Types
511507

0 commit comments

Comments
 (0)