package mcp
import (
"encoding/json"
"errors"
"fmt"
)
var (
ErrParseError = errors .New ("parse error" )
ErrInvalidRequest = errors .New ("invalid request" )
ErrMethodNotFound = errors .New ("method not found" )
ErrInvalidParams = errors .New ("invalid params" )
ErrInternalError = errors .New ("internal error" )
ErrRequestInterrupted = errors .New ("request interrupted" )
ErrResourceNotFound = errors .New ("resource not found" )
)
type URLElicitationRequiredError struct {
Elicitations []ElicitationParams `json:"elicitations"`
}
func (e URLElicitationRequiredError ) Error () string {
return fmt .Sprintf ("URL elicitation required: %d elicitation(s) needed" , len (e .Elicitations ))
}
func (e URLElicitationRequiredError ) JSONRPCError () JSONRPCError {
return JSONRPCError {
JSONRPC : JSONRPC_VERSION ,
Error : JSONRPCErrorDetails {
Code : URL_ELICITATION_REQUIRED ,
Message : e .Error (),
Data : map [string ]any {
"elicitations" : e .Elicitations ,
},
},
}
}
type UnsupportedProtocolVersionError struct {
Version string
}
func (e UnsupportedProtocolVersionError ) Error () string {
return fmt .Sprintf ("unsupported protocol version: %q" , e .Version )
}
func (e URLElicitationRequiredError ) Is (target error ) bool {
_ , ok := target .(URLElicitationRequiredError )
return ok
}
func (e UnsupportedProtocolVersionError ) Is (target error ) bool {
_ , ok := target .(UnsupportedProtocolVersionError )
return ok
}
func IsUnsupportedProtocolVersion (err error ) bool {
_ , ok := err .(UnsupportedProtocolVersionError )
return ok
}
func (e *JSONRPCErrorDetails ) AsError () error {
var err error
switch e .Code {
case PARSE_ERROR :
err = ErrParseError
case INVALID_REQUEST :
err = ErrInvalidRequest
case METHOD_NOT_FOUND :
err = ErrMethodNotFound
case INVALID_PARAMS :
err = ErrInvalidParams
case INTERNAL_ERROR :
err = ErrInternalError
case REQUEST_INTERRUPTED :
err = ErrRequestInterrupted
case RESOURCE_NOT_FOUND :
err = ErrResourceNotFound
case URL_ELICITATION_REQUIRED :
if e .Data != nil {
if dataBytes , marshalErr := json .Marshal (e .Data ); marshalErr == nil {
var data struct {
Elicitations []ElicitationParams `json:"elicitations"`
}
if unmarshalErr := json .Unmarshal (dataBytes , &data ); unmarshalErr == nil {
return URLElicitationRequiredError {
Elicitations : data .Elicitations ,
}
}
}
}
return URLElicitationRequiredError {}
default :
return errors .New (e .Message )
}
if e .Message != "" && e .Message != err .Error() {
return fmt .Errorf ("%w: %s" , err , e .Message )
}
return err
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .