improve error handling for tools and server methods (#311)
- Fix tool error handling to properly distinguish between protocol errors and tool execution errors - Return structured JSON-RPC errors directly for protocol-level issues - Embed regular tool errors in CallToolResult as per MCP specification - Improve server error responses for unknown prompts and tools - Add comprehensive tests for both structured and regular error handling
This commit is contained in:
parent
8e6ab130d8
commit
719f68d0bf
1 changed files with 113 additions and 5 deletions
|
|
@ -328,8 +328,11 @@ func (s *Server) getPrompt(ctx context.Context, req *ServerRequest[*GetPromptPar
|
|||
prompt, ok := s.prompts.get(req.Params.Name)
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
// TODO: surface the error code over the wire, instead of flattening it into the string.
|
||||
return nil, fmt.Errorf("%s: unknown prompt %q", jsonrpc2.ErrInvalidParams, req.Params.Name)
|
||||
// Return a proper JSON-RPC error with the correct error code
|
||||
return nil, &jsonrpc2.WireError{
|
||||
Code: CodeInvalidParams,
|
||||
Message: fmt.Sprintf("unknown prompt %q", req.Params.Name),
|
||||
}
|
||||
}
|
||||
return prompt.handler(ctx, req.Session, req.Params)
|
||||
}
|
||||
|
|
@ -353,7 +356,10 @@ func (s *Server) callTool(ctx context.Context, req *ServerRequest[*CallToolParam
|
|||
st, ok := s.tools.get(req.Params.Name)
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s: unknown tool %q", jsonrpc2.ErrInvalidParams, req.Params.Name)
|
||||
return nil, &jsonrpc2.WireError{
|
||||
Code: CodeInvalidParams,
|
||||
Message: fmt.Sprintf("unknown tool %q", req.Params.Name),
|
||||
}
|
||||
}
|
||||
// TODO: if handler returns nil content, it will serialize as null.
|
||||
// Add a test and fix.
|
||||
|
|
|
|||
Loading…
Reference in a new issue