Skip to content

Commit 6fb53de

Browse files
committed
docs: fix unreachable code example and section placement in migration guide
Split the two-throw example into separate registerTool calls so both code paths are reachable, as suggested by @felixweinberger. Moved the section under Breaking Changes (H3) where it belongs structurally.
1 parent eafaf13 commit 6fb53de

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

docs/migration.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,28 @@ server.setRequestHandler('tools/call', async (request, ctx) => {
821821

822822
> **Note:** These task APIs are marked `@experimental` and may change without notice.
823823
824+
### Tool error sanitization
825+
826+
Tool handlers that `throw new Error('message')` will now return `"Internal error"` to clients instead of the raw error message. This prevents accidental leakage of server internals (hostnames, connection strings, stack traces).
827+
828+
To send a user-visible error message, use the new `ToolError` class:
829+
830+
```typescript
831+
import { ToolError } from '@modelcontextprotocol/server';
832+
833+
// Generic errors are sanitized -- client sees: "Internal error"
834+
server.registerTool('internal-tool', {}, async () => {
835+
throw new Error('DB connection failed at 10.0.0.5:5432');
836+
});
837+
838+
// ToolError messages pass through -- client sees: "Invalid country"
839+
server.registerTool('validated-tool', {}, async () => {
840+
throw new ToolError('Invalid country');
841+
});
842+
```
843+
844+
`ProtocolError` messages (SDK validation errors) are still passed through unchanged.
845+
824846
## Enhancements
825847

826848
### Automatic JSON Schema validator selection by runtime
@@ -870,26 +892,6 @@ import { AjvJsonSchemaValidator } from '@modelcontextprotocol/server';
870892
import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server/validators/cf-worker';
871893
```
872894

873-
## Tool error sanitization
874-
875-
Tool handlers that `throw new Error('message')` will now return `"Internal error"` to clients instead of the raw error message. This prevents accidental leakage of server internals (hostnames, connection strings, stack traces).
876-
877-
To send a user-visible error message, use the new `ToolError` class:
878-
879-
```typescript
880-
import { ToolError } from '@modelcontextprotocol/server';
881-
882-
server.registerTool('my-tool', {}, async () => {
883-
// Client sees: "Internal error"
884-
throw new Error('DB connection failed at 10.0.0.5:5432');
885-
886-
// Client sees: "Invalid country"
887-
throw new ToolError('Invalid country');
888-
});
889-
```
890-
891-
`ProtocolError` messages (SDK validation errors) are still passed through unchanged.
892-
893895
## Unchanged APIs
894896

895897
The following APIs are unchanged between v1 and v2 (only the import paths changed):

0 commit comments

Comments
 (0)