|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | +"errors" |
| 5 | +"fmt" |
| 6 | + |
| 7 | +"github.com/google/go-github/v69/github" |
| 8 | +"github.com/mark3labs/mcp-go/mcp" |
| 9 | +"github.com/mark3labs/mcp-go/server" |
| 10 | +) |
| 11 | + |
| 12 | +// NewServer creates a new GitHub MCP server with the specified GH client and logger. |
| 13 | + |
| 14 | +funcNewServer(versionstring,opts...server.ServerOption)*server.MCPServer { |
| 15 | +// Add default options |
| 16 | +defaultOpts:= []server.ServerOption{ |
| 17 | +server.WithToolCapabilities(true), |
| 18 | +server.WithResourceCapabilities(true,true), |
| 19 | +server.WithLogging(), |
| 20 | +} |
| 21 | +opts=append(defaultOpts,opts...) |
| 22 | + |
| 23 | +// Create a new MCP server |
| 24 | +s:=server.NewMCPServer( |
| 25 | +"github-mcp-server", |
| 26 | +version, |
| 27 | +opts..., |
| 28 | +) |
| 29 | +returns |
| 30 | +} |
| 31 | + |
| 32 | +// OptionalParamOK is a helper function that can be used to fetch a requested parameter from the request. |
| 33 | +// It returns the value, a boolean indicating if the parameter was present, and an error if the type is wrong. |
| 34 | +funcOptionalParamOK[Tany](r mcp.CallToolRequest,pstring) (valueT,okbool,errerror) { |
| 35 | +// Check if the parameter is present in the request |
| 36 | +val,exists:=r.Params.Arguments[p] |
| 37 | +if!exists { |
| 38 | +// Not present, return zero value, false, no error |
| 39 | +return |
| 40 | +} |
| 41 | + |
| 42 | +// Check if the parameter is of the expected type |
| 43 | +value,ok=val.(T) |
| 44 | +if!ok { |
| 45 | +// Present but wrong type |
| 46 | +err=fmt.Errorf("parameter %s is not of type %T, is %T",p,value,val) |
| 47 | +ok=true// Set ok to true because the parameter *was* present, even if wrong type |
| 48 | +return |
| 49 | +} |
| 50 | + |
| 51 | +// Present and correct type |
| 52 | +ok=true |
| 53 | +return |
| 54 | +} |
| 55 | + |
| 56 | +// isAcceptedError checks if the error is an accepted error. |
| 57 | +funcisAcceptedError(errerror)bool { |
| 58 | +varacceptedError*github.AcceptedError |
| 59 | +returnerrors.As(err,&acceptedError) |
| 60 | +} |
| 61 | + |
| 62 | +// requiredParam is a helper function that can be used to fetch a requested parameter from the request. |
| 63 | +// It does the following checks: |
| 64 | +// 1. Checks if the parameter is present in the request. |
| 65 | +// 2. Checks if the parameter is of the expected type. |
| 66 | +// 3. Checks if the parameter is not empty, i.e: non-zero value |
| 67 | +funcrequiredParam[Tcomparable](r mcp.CallToolRequest,pstring) (T,error) { |
| 68 | +varzeroT |
| 69 | + |
| 70 | +// Check if the parameter is present in the request |
| 71 | +if_,ok:=r.Params.Arguments[p];!ok { |
| 72 | +returnzero,fmt.Errorf("missing required parameter: %s",p) |
| 73 | +} |
| 74 | + |
| 75 | +// Check if the parameter is of the expected type |
| 76 | +if_,ok:=r.Params.Arguments[p].(T);!ok { |
| 77 | +returnzero,fmt.Errorf("parameter %s is not of type %T",p,zero) |
| 78 | +} |
| 79 | + |
| 80 | +ifr.Params.Arguments[p].(T)==zero { |
| 81 | +returnzero,fmt.Errorf("missing required parameter: %s",p) |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +returnr.Params.Arguments[p].(T),nil |
| 86 | +} |
| 87 | + |
| 88 | +// RequiredInt is a helper function that can be used to fetch a requested parameter from the request. |
| 89 | +// It does the following checks: |
| 90 | +// 1. Checks if the parameter is present in the request. |
| 91 | +// 2. Checks if the parameter is of the expected type. |
| 92 | +// 3. Checks if the parameter is not empty, i.e: non-zero value |
| 93 | +funcRequiredInt(r mcp.CallToolRequest,pstring) (int,error) { |
| 94 | +v,err:=requiredParam[float64](r,p) |
| 95 | +iferr!=nil { |
| 96 | +return0,err |
| 97 | +} |
| 98 | +returnint(v),nil |
| 99 | +} |
| 100 | + |
| 101 | +// OptionalParam is a helper function that can be used to fetch a requested parameter from the request. |
| 102 | +// It does the following checks: |
| 103 | +// 1. Checks if the parameter is present in the request, if not, it returns its zero-value |
| 104 | +// 2. If it is present, it checks if the parameter is of the expected type and returns it |
| 105 | +funcOptionalParam[Tany](r mcp.CallToolRequest,pstring) (T,error) { |
| 106 | +varzeroT |
| 107 | + |
| 108 | +// Check if the parameter is present in the request |
| 109 | +if_,ok:=r.Params.Arguments[p];!ok { |
| 110 | +returnzero,nil |
| 111 | +} |
| 112 | + |
| 113 | +// Check if the parameter is of the expected type |
| 114 | +if_,ok:=r.Params.Arguments[p].(T);!ok { |
| 115 | +returnzero,fmt.Errorf("parameter %s is not of type %T, is %T",p,zero,r.Params.Arguments[p]) |
| 116 | +} |
| 117 | + |
| 118 | +returnr.Params.Arguments[p].(T),nil |
| 119 | +} |
| 120 | + |
| 121 | +// OptionalIntParam is a helper function that can be used to fetch a requested parameter from the request. |
| 122 | +// It does the following checks: |
| 123 | +// 1. Checks if the parameter is present in the request, if not, it returns its zero-value |
| 124 | +// 2. If it is present, it checks if the parameter is of the expected type and returns it |
| 125 | +funcOptionalIntParam(r mcp.CallToolRequest,pstring) (int,error) { |
| 126 | +v,err:=OptionalParam[float64](r,p) |
| 127 | +iferr!=nil { |
| 128 | +return0,err |
| 129 | +} |
| 130 | +returnint(v),nil |
| 131 | +} |
| 132 | + |
| 133 | +// OptionalIntParamWithDefault is a helper function that can be used to fetch a requested parameter from the request |
| 134 | +// similar to optionalIntParam, but it also takes a default value. |
| 135 | +funcOptionalIntParamWithDefault(r mcp.CallToolRequest,pstring,dint) (int,error) { |
| 136 | +v,err:=OptionalIntParam(r,p) |
| 137 | +iferr!=nil { |
| 138 | +return0,err |
| 139 | +} |
| 140 | +ifv==0 { |
| 141 | +returnd,nil |
| 142 | +} |
| 143 | +returnv,nil |
| 144 | +} |
| 145 | + |
| 146 | +// OptionalBoolParamWithDefault is a helper function that can be used to fetch a requested parameter from the request |
| 147 | +// similar to optionalParam, but it also takes a default value. |
| 148 | +funcOptionalBoolParamWithDefault(r mcp.CallToolRequest,pstring,dbool) (bool,error) { |
| 149 | +v,err:=OptionalParam[bool](r,p) |
| 150 | +iferr!=nil { |
| 151 | +returnfalse,err |
| 152 | +} |
| 153 | +if!v { |
| 154 | +returnd,nil |
| 155 | +} |
| 156 | +returnv,nil |
| 157 | +} |
| 158 | + |
| 159 | +// OptionalStringParam is a helper function that can be used to fetch a requested parameter from the request. |
| 160 | +// It does the following checks: |
| 161 | +// 1. Checks if the parameter is present in the request, if not, it returns its zero-value |
| 162 | +// 2. If it is present, it checks if the parameter is of the expected type and returns it |
| 163 | +funcOptionalStringParam(r mcp.CallToolRequest,pstring) (string,error) { |
| 164 | +v,err:=OptionalParam[string](r,p) |
| 165 | +iferr!=nil { |
| 166 | +return"",err |
| 167 | +} |
| 168 | +ifv=="" { |
| 169 | +return"",nil |
| 170 | +} |
| 171 | +returnv,nil |
| 172 | +} |
| 173 | + |
| 174 | +// OptionalStringParamWithDefault is a helper function that can be used to fetch a requested parameter from the request |
| 175 | +// similar to optionalParam, but it also takes a default value. |
| 176 | +funcOptionalStringParamWithDefault(r mcp.CallToolRequest,pstring,dstring) (string,error) { |
| 177 | +v,err:=OptionalParam[string](r,p) |
| 178 | +iferr!=nil { |
| 179 | +return"",err |
| 180 | +} |
| 181 | +ifv=="" { |
| 182 | +returnd,nil |
| 183 | +} |
| 184 | +returnv,nil |
| 185 | +} |
| 186 | + |
| 187 | +// OptionalStringArrayParam is a helper function that can be used to fetch a requested parameter from the request. |
| 188 | +// It does the following checks: |
| 189 | +// 1. Checks if the parameter is present in the request, if not, it returns its zero-value |
| 190 | +// 2. If it is present, iterates the elements and checks each is a string |
| 191 | +funcOptionalStringArrayParam(r mcp.CallToolRequest,pstring) ([]string,error) { |
| 192 | +// Check if the parameter is present in the request |
| 193 | +if_,ok:=r.Params.Arguments[p];!ok { |
| 194 | +return []string{},nil |
| 195 | +} |
| 196 | + |
| 197 | +switchv:=r.Params.Arguments[p].(type) { |
| 198 | +casenil: |
| 199 | +return []string{},nil |
| 200 | +case []string: |
| 201 | +returnv,nil |
| 202 | +case []any: |
| 203 | +strSlice:=make([]string,len(v)) |
| 204 | +fori,v:=rangev { |
| 205 | +s,ok:=v.(string) |
| 206 | +if!ok { |
| 207 | +return []string{},fmt.Errorf("parameter %s is not of type string, is %T",p,v) |
| 208 | +} |
| 209 | +strSlice[i]=s |
| 210 | +} |
| 211 | +returnstrSlice,nil |
| 212 | +default: |
| 213 | +return []string{},fmt.Errorf("parameter %s could not be coerced to []string, is %T",p,r.Params.Arguments[p]) |
| 214 | +} |
| 215 | +} |
| 216 | + |
| 217 | +// WithPagination returns a ToolOption that adds "page" and "perPage" parameters to the tool. |
| 218 | +// The "page" parameter is optional, min 1. The "perPage" parameter is optional, min 1, max 100. |
| 219 | +funcWithPagination() mcp.ToolOption { |
| 220 | +returnfunc(tool*mcp.Tool) { |
| 221 | +mcp.WithNumber("page", |
| 222 | +mcp.Description("Page number for pagination (min 1)"), |
| 223 | +mcp.Min(1), |
| 224 | +)(tool) |
| 225 | + |
| 226 | +mcp.WithNumber("perPage", |
| 227 | +mcp.Description("Results per page for pagination (min 1, max 100)"), |
| 228 | +mcp.Min(1), |
| 229 | +mcp.Max(100), |
| 230 | +)(tool) |
| 231 | +} |
| 232 | +} |
| 233 | + |
| 234 | +typePaginationParamsstruct { |
| 235 | +pageint |
| 236 | +perPageint |
| 237 | +} |
| 238 | + |
| 239 | +// OptionalPaginationParams returns the "page" and "perPage" parameters from the request, |
| 240 | +// or their default values if not present, "page" default is 1, "perPage" default is 30. |
| 241 | +// In future, we may want to make the default values configurable, or even have this |
| 242 | +// function returned from `withPagination`, where the defaults are provided alongside |
| 243 | +// the min/max values. |
| 244 | +funcOptionalPaginationParams(r mcp.CallToolRequest) (PaginationParams,error) { |
| 245 | +page,err:=OptionalIntParamWithDefault(r,"page",1) |
| 246 | +iferr!=nil { |
| 247 | +returnPaginationParams{},err |
| 248 | +} |
| 249 | +perPage,err:=OptionalIntParamWithDefault(r,"perPage",30) |
| 250 | +iferr!=nil { |
| 251 | +returnPaginationParams{},err |
| 252 | +} |
| 253 | +returnPaginationParams{ |
| 254 | +page:page, |
| 255 | +perPage:perPage, |
| 256 | +},nil |
| 257 | +} |