9
9
"github.com/metoro-io/mcp-golang/internal/protocol"
10
10
"github.com/metoro-io/mcp-golang/internal/tools"
11
11
"github.com/metoro-io/mcp-golang/transport"
12
+ "github.com/pkg/errors"
12
13
"reflect"
13
14
"sort"
14
15
"strings"
@@ -319,19 +320,19 @@ func createWrappedPromptHandler(userHandler any) func(baseGetPromptRequestParams
319
320
// Unmarshal the JSON into the correct type
320
321
err := json .Unmarshal (arguments .Arguments , & unmarshaledArguments )
321
322
if err != nil {
322
- return newPromptResponseSentError (fmt . Errorf ( "failed to unmarshal arguments: %w" , err ))
323
+ return newPromptResponseSentError (errors . Wrap ( err , "failed to unmarshal arguments" ))
323
324
}
324
325
325
326
// Need to dereference the unmarshaled arguments
326
327
of := reflect .ValueOf (unmarshaledArguments )
327
328
if of .Kind () != reflect .Ptr || ! of .Elem ().CanInterface () {
328
- return newPromptResponseSentError (fmt . Errorf ( "arguments must be a struct" ))
329
+ return newPromptResponseSentError (errors . Wrap ( err , "arguments must be a struct" ))
329
330
}
330
331
// Call the handler with the typed arguments
331
332
output := handlerValue .Call ([]reflect.Value {of .Elem ()})
332
333
333
334
if len (output ) != 2 {
334
- return newPromptResponseSentError (fmt .Errorf ("handler must return exactly two values, got %d" , len (output )))
335
+ return newPromptResponseSentError (errors . New ( fmt .Sprintf ("handler must return exactly two values, got %d" , len (output ) )))
335
336
}
336
337
337
338
if ! output [0 ].CanInterface () {
@@ -437,40 +438,40 @@ func createWrappedToolHandler(userHandler any) func(baseCallToolRequestParams) *
437
438
return func (arguments baseCallToolRequestParams ) * toolResponseSent {
438
439
// Instantiate a struct of the type of the arguments
439
440
if ! reflect .New (argumentType ).CanInterface () {
440
- return newToolResponseSentError (fmt .Errorf ("arguments must be a struct" ))
441
+ return newToolResponseSentError (errors . Wrap ( fmt .Errorf ("arguments must be a struct" ), "failed to create argument struct" ))
441
442
}
442
443
unmarshaledArguments := reflect .New (argumentType ).Interface ()
443
444
444
445
// Unmarshal the JSON into the correct type
445
446
err := json .Unmarshal (arguments .Arguments , & unmarshaledArguments )
446
447
if err != nil {
447
- return newToolResponseSentError (fmt . Errorf ( "failed to unmarshal arguments: %w" , err ))
448
+ return newToolResponseSentError (errors . Wrap ( err , "failed to unmarshal arguments" ))
448
449
}
449
450
450
451
// Need to dereference the unmarshaled arguments
451
452
of := reflect .ValueOf (unmarshaledArguments )
452
453
if of .Kind () != reflect .Ptr || ! of .Elem ().CanInterface () {
453
- return newToolResponseSentError (fmt .Errorf ("arguments must be a struct" ))
454
+ return newToolResponseSentError (errors . Wrap ( fmt .Errorf ("arguments must be a struct" ), "failed to dereference arguments " ))
454
455
}
455
456
// Call the handler with the typed arguments
456
457
output := handlerValue .Call ([]reflect.Value {of .Elem ()})
457
458
458
459
if len (output ) != 2 {
459
- return newToolResponseSentError (fmt .Errorf ("handler must return exactly two values, got %d" , len (output )))
460
+ return newToolResponseSentError (errors . Wrap ( fmt .Errorf ("handler must return exactly two values, got %d" , len (output )), "invalid handler return" ))
460
461
}
461
462
462
463
if ! output [0 ].CanInterface () {
463
- return newToolResponseSentError (fmt .Errorf ("handler must return a struct, got %s" , output [0 ].Type ().Name ()))
464
+ return newToolResponseSentError (errors . Wrap ( fmt .Errorf ("handler must return a struct, got %s" , output [0 ].Type ().Name ()), "invalid handler return" ))
464
465
}
465
466
tool := output [0 ].Interface ()
466
467
if ! output [1 ].CanInterface () {
467
- return newToolResponseSentError (fmt .Errorf ("handler must return an error, got %s" , output [1 ].Type ().Name ()))
468
+ return newToolResponseSentError (errors . Wrap ( fmt .Errorf ("handler must return an error, got %s" , output [1 ].Type ().Name ()), "invalid handler return" ))
468
469
}
469
470
errorOut := output [1 ].Interface ()
470
471
if errorOut == nil {
471
472
return newToolResponseSent (tool .(* ToolResponse ))
472
473
}
473
- return newToolResponseSentError (errorOut .(error ))
474
+ return newToolResponseSentError (errors . Wrap ( errorOut .(error ), "handler returned an error" ))
474
475
}
475
476
}
476
477
@@ -514,9 +515,13 @@ func (s *Server) handleListTools(request *transport.BaseJSONRPCRequest, _ protoc
514
515
Cursor * string `json:"cursor"`
515
516
}
516
517
var params toolRequestParams
517
- err := json .Unmarshal (request .Params , & params )
518
- if err != nil {
519
- return nil , fmt .Errorf ("failed to unmarshal arguments: %w" , err )
518
+ if request .Params == nil {
519
+ params = toolRequestParams {}
520
+ } else {
521
+ err := json .Unmarshal (request .Params , & params )
522
+ if err != nil {
523
+ return nil , errors .Wrap (err , "failed to unmarshal arguments" )
524
+ }
520
525
}
521
526
522
527
// Order by name for pagination
@@ -534,7 +539,7 @@ func (s *Server) handleListTools(request *transport.BaseJSONRPCRequest, _ protoc
534
539
// Base64 decode the cursor
535
540
c , err := base64 .StdEncoding .DecodeString (* params .Cursor )
536
541
if err != nil {
537
- return nil , fmt . Errorf ( "failed to decode cursor: %w" , err )
542
+ return nil , errors . Wrap ( err , "failed to decode cursor" )
538
543
}
539
544
cString := string (c )
540
545
// Iterate through the tools until we find an entry > the cursor
@@ -585,7 +590,7 @@ func (s *Server) handleToolCalls(req *transport.BaseJSONRPCRequest, _ protocol.R
585
590
// Instantiate a struct of the type of the arguments
586
591
err := json .Unmarshal (req .Params , & params )
587
592
if err != nil {
588
- return nil , fmt . Errorf ( "failed to unmarshal arguments: %w" , err )
593
+ return nil , errors . Wrap ( err , "failed to unmarshal arguments" )
589
594
}
590
595
591
596
var toolToUse * tool
@@ -598,11 +603,10 @@ func (s *Server) handleToolCalls(req *transport.BaseJSONRPCRequest, _ protocol.R
598
603
})
599
604
600
605
if toolToUse == nil {
601
- return nil , fmt . Errorf ( "unknown tool: %s" , req .Method )
606
+ return nil , errors . Wrapf ( err , "unknown tool: %s" , req .Method )
602
607
}
603
608
return toolToUse .Handler (params ), nil
604
609
}
605
-
606
610
func (s * Server ) generateCapabilities () serverCapabilities {
607
611
t := false
608
612
return serverCapabilities {
@@ -623,15 +627,14 @@ func (s *Server) generateCapabilities() serverCapabilities {
623
627
}(),
624
628
}
625
629
}
626
-
627
630
func (s * Server ) handleListPrompts (request * transport.BaseJSONRPCRequest , extra protocol.RequestHandlerExtra ) (transport.JsonRpcBody , error ) {
628
631
type promptRequestParams struct {
629
632
Cursor * string `json:"cursor"`
630
633
}
631
634
var params promptRequestParams
632
635
err := json .Unmarshal (request .Params , & params )
633
636
if err != nil {
634
- return nil , fmt . Errorf ( "failed to unmarshal arguments: %w" , err )
637
+ return nil , errors . Wrap ( err , "failed to unmarshal arguments" )
635
638
}
636
639
637
640
// Order by name for pagination
@@ -649,7 +652,7 @@ func (s *Server) handleListPrompts(request *transport.BaseJSONRPCRequest, extra
649
652
// Base64 decode the cursor
650
653
c , err := base64 .StdEncoding .DecodeString (* params .Cursor )
651
654
if err != nil {
652
- return nil , fmt . Errorf ( "failed to decode cursor: %w" , err )
655
+ return nil , errors . Wrap ( err , "failed to decode cursor" )
653
656
}
654
657
cString := string (c )
655
658
// Iterate through the prompts until we find an entry > the cursor
@@ -694,7 +697,7 @@ func (s *Server) handleListResources(request *transport.BaseJSONRPCRequest, extr
694
697
var params resourceRequestParams
695
698
err := json .Unmarshal (request .Params , & params )
696
699
if err != nil {
697
- return nil , fmt . Errorf ( "failed to unmarshal arguments: %w" , err )
700
+ return nil , errors . Wrap ( err , "failed to unmarshal arguments" )
698
701
}
699
702
700
703
// Order by URI for pagination
@@ -712,7 +715,7 @@ func (s *Server) handleListResources(request *transport.BaseJSONRPCRequest, extr
712
715
// Base64 decode the cursor
713
716
c , err := base64 .StdEncoding .DecodeString (* params .Cursor )
714
717
if err != nil {
715
- return nil , fmt . Errorf ( "failed to decode cursor: %w" , err )
718
+ return nil , errors . Wrap ( err , "failed to decode cursor" )
716
719
}
717
720
cString := string (c )
718
721
// Iterate through the resources until we find an entry > the cursor
@@ -760,7 +763,7 @@ func (s *Server) handlePromptCalls(req *transport.BaseJSONRPCRequest, extra prot
760
763
// Instantiate a struct of the type of the arguments
761
764
err := json .Unmarshal (req .Params , & params )
762
765
if err != nil {
763
- return nil , fmt . Errorf ( "failed to unmarshal arguments: %w" , err )
766
+ return nil , errors . Wrap ( err , "failed to unmarshal arguments" )
764
767
}
765
768
766
769
var promptToUse * prompt
@@ -773,7 +776,7 @@ func (s *Server) handlePromptCalls(req *transport.BaseJSONRPCRequest, extra prot
773
776
})
774
777
775
778
if promptToUse == nil {
776
- return nil , fmt . Errorf ( "unknown prompt: %s" , req .Method )
779
+ return nil , errors . Wrapf ( err , "unknown prompt: %s" , req .Method )
777
780
}
778
781
return promptToUse .Handler (params ), nil
779
782
}
@@ -783,7 +786,7 @@ func (s *Server) handleResourceCalls(req *transport.BaseJSONRPCRequest, extra pr
783
786
// Instantiate a struct of the type of the arguments
784
787
err := json .Unmarshal (req .Params , & params )
785
788
if err != nil {
786
- return nil , fmt . Errorf ( "failed to unmarshal arguments: %w" , err )
789
+ return nil , errors . Wrap ( err , "failed to unmarshal arguments" )
787
790
}
788
791
789
792
var resourceToUse * resource
@@ -796,7 +799,7 @@ func (s *Server) handleResourceCalls(req *transport.BaseJSONRPCRequest, extra pr
796
799
})
797
800
798
801
if resourceToUse == nil {
799
- return nil , fmt . Errorf ( "unknown prompt: %s" , req .Method )
802
+ return nil , errors . Wrapf ( err , "unknown prompt: %s" , req .Method )
800
803
}
801
804
return resourceToUse .Handler (), nil
802
805
}
0 commit comments