@@ -3,6 +3,7 @@ package errors
33import (
44 "context"
55 "fmt"
6+ "net/http"
67
78 "github.com/github/github-mcp-server/pkg/utils"
89 "github.com/google/go-github/v79/github"
@@ -44,10 +45,23 @@ func (e *GitHubGraphQLError) Error() string {
4445 return fmt .Errorf ("%s: %w" , e .Message , e .Err ).Error ()
4546}
4647
48+ type GitHubRawAPIError struct {
49+ Message string `json:"message"`
50+ Err error `json:"-"`
51+ }
52+
53+ func newGitHubRawAPIError (message string , err error ) * GitHubRawAPIError {
54+ return & GitHubRawAPIError {
55+ Message : message ,
56+ Err : err ,
57+ }
58+ }
59+
4760type GitHubErrorKey struct {}
4861type GitHubCtxErrors struct {
4962 api []* GitHubAPIError
5063 graphQL []* GitHubGraphQLError
64+ raw []* GitHubRawAPIError
5165}
5266
5367// ContextWithGitHubErrors updates or creates a context with a pointer to GitHub error information (to be used by middleware).
@@ -107,6 +121,15 @@ func addGitHubGraphQLErrorToContext(ctx context.Context, err *GitHubGraphQLError
107121 return nil , fmt .Errorf ("context does not contain GitHubCtxErrors" )
108122}
109123
124+ func addRawAPIErrorToContext (ctx context.Context , err * GitHubRawAPIError ) (context.Context , error ) {
125+ if val , ok := ctx .Value (GitHubErrorKey {}).(* GitHubCtxErrors ); ok {
126+ val .raw = append (val .raw , err )
127+ return ctx , nil
128+ }
129+
130+ return nil , fmt .Errorf ("context does not contain GitHubCtxErrors" )
131+ }
132+
110133// NewGitHubAPIErrorResponse returns an mcp.NewToolResultError and retains the error in the context for access via middleware
111134func NewGitHubAPIErrorResponse (ctx context.Context , message string , resp * github.Response , err error ) * mcp.CallToolResult {
112135 apiErr := newGitHubAPIError (message , resp , err )
@@ -124,3 +147,11 @@ func NewGitHubGraphQLErrorResponse(ctx context.Context, message string, err erro
124147 }
125148 return utils .NewToolResultErrorFromErr (message , err )
126149}
150+
151+ func NewGitHubRawAPIErrorResponse (ctx context.Context , message string , resp * http.Response , err error ) * mcp.CallToolResult {
152+ rawAPIErr := newGitHubRawAPIError (message , err )
153+ if ctx != nil {
154+ _ , _ = addRawAPIErrorToContext (ctx , rawAPIErr ) // Explicitly ignore error for graceful handling
155+ }
156+ return utils .NewToolResultErrorFromErr (message , err )
157+ }
0 commit comments