fix(mcp): map tool-not-found to CodeNotFound via registry sentinel

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mathias Bergqvist
2026-05-04 20:59:15 +02:00
parent 93c5a6934b
commit c6c328e517
3 changed files with 33 additions and 4 deletions

View File

@@ -90,7 +90,7 @@ func (s *Server) handlePOST(w http.ResponseWriter, r *http.Request) {
out, err := s.opts.Registry.Dispatch(r.Context(), p.Name, p.Arguments)
if err != nil {
code := -32000
if errors.Is(err, ErrToolNotFound) {
if errors.Is(err, registry.ErrToolNotFound) {
code = CodeNotFound
}
writeJSON(w, http.StatusOK,
@@ -125,8 +125,6 @@ func (s *Server) handleGET(w http.ResponseWriter, r *http.Request) {
<-r.Context().Done()
}
var ErrToolNotFound = errors.New("tool not found")
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)

View File

@@ -117,3 +117,31 @@ func TestPostBodyTooLarge(t *testing.T) {
assert.NotEqual(t, http.StatusOK, rr.Code, "oversized body must not return 200")
assert.Equal(t, http.StatusBadRequest, rr.Code)
}
func TestToolsCallToolNotFound(t *testing.T) {
srv := newServer(t)
// Initialize to get a session ID.
init := postJSON(t, srv, map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{"protocolVersion": "2025-06-18"},
}, "")
sid := init.Header().Get("Mcp-Session-Id")
rr := postJSON(t, srv, map[string]any{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": map[string]any{"name": "nonexistent", "arguments": map[string]any{}},
}, sid)
require.Equal(t, http.StatusOK, rr.Code)
var resp map[string]any
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &resp))
rpcErr, ok := resp["error"].(map[string]any)
require.True(t, ok, "expected error field in response")
code := int(rpcErr["code"].(float64))
assert.Equal(t, -32002, code, "expected CodeNotFound (-32002) for missing tool")
assert.NotEmpty(t, rpcErr["message"])
}