Implementation:1broseidon Ketch mcp errors
| Knowledge Sources | |
|---|---|
| Domains | MCP, Error_Handling |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
The MCP error taxonomy: every tool error starts with a stable `[kind]` prefix mirroring the CLI exit codes, so agents can tell "fix your input" from "retry later" from "configure something".
Description
MCP carries tool-handler errors back to the client as text content with `IsError=true` — there is no structured error-data field, so the prefix is the contract. The five kinds mirror `cmd/exit.go`: `[validation]` (exit 2, fix the input), `[not_found]` (exit 3, absent resource), `[upstream]` (exit 4, retry may help), `[precondition]` (exit 5, operator config), `[cancelled]` (exit 6, ctx cancel/deadline). upstreamErrf maps `context.Canceled`/`DeadlineExceeded` → `[cancelled]` and `docs.ErrNotFound`/`code.ErrRepoNotFound` → `[not_found]`. backendErrf maps constructor errors wrapping `ErrUnknownBackend` → `[validation]`, anything else → `[precondition]`.
Usage
Used by every MCP tool handler to classify errors before returning them. The CLI applies the same mapping in `cmd/exit.go` so the surfaces cannot diverge.
Code Reference
Source Location
- Repository: ketch
- File: mcp/errors.go, cmd/exit.go
Signature
const (
kindValidation = "validation"
kindNotFound = "not_found"
kindUpstream = "upstream"
kindPrecondition = "precondition"
kindCancelled = "cancelled"
)
// errf builds an error whose message starts with "[kind] ".
func errf(kind, format string, args ...any) error
// upstreamErrf wraps a backend/network failure; maps ctx cancel -> [cancelled],
// ErrNotFound/ErrRepoNotFound -> [not_found], else [upstream].
func upstreamErrf(err error, format string, args ...any) error
// backendErrf classifies a constructor error: ErrUnknownBackend -> [validation],
// else [precondition].
func backendErrf(err, unknown error) error
Import
import "github.com/1broseidon/ketch/mcp"
I/O Contract
Outputs (errf)
| Name | Type | Description |
|---|---|---|
| return | error | Message starts with "[kind] " |
Usage Examples
Classifying Tool Errors
searcher, err := search.NewFromConfig(cfg, backend, "")
if err != nil {
return mcp.backendErrf(err, search.ErrUnknownBackend) // [validation] or [precondition]
}
results, err := searcher.Search(ctx, query, limit)
if err != nil {
return mcp.upstreamErrf(err, "search failed") // [upstream]/[cancelled]/[not_found]
}
Direct Prefix
return mcp.errf(mcp.kindValidation, "query must not be empty")
// -> "[validation] query must not be empty"