-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathserver.go
More file actions
230 lines (190 loc) · 8.32 KB
/
Copy pathserver.go
File metadata and controls
230 lines (190 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package github
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strings"
"time"
gherrors "github.com/github/github-mcp-server/pkg/errors"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/octicons"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
type MCPServerConfig struct {
// Version of the server
Version string
// GitHub Host to target for API requests (e.g. github.com or github.enterprise.com)
Host string
// GitHub Token to authenticate with the GitHub API
Token string
// EnabledToolsets is a list of toolsets to enable
// See: https://github.com/github/github-mcp-server?tab=readme-ov-file#tool-configuration
EnabledToolsets []string
// EnabledTools is a list of specific tools to enable (additive to toolsets)
// When specified, these tools are registered in addition to any specified toolset tools
EnabledTools []string
// EnabledFeatures is a list of feature flags that are enabled
// Tool feature rules evaluate entries in this list.
EnabledFeatures []string
// ReadOnly indicates if we should only offer read-only tools
ReadOnly bool
// ReadOnlyToolsets restricts write tools in the listed toolsets.
ReadOnlyToolsets []string
// Translator provides translated text for the server tooling
Translator translations.TranslationHelperFunc
// Content window size
ContentWindowSize int
// LockdownMode indicates if we should enable lockdown mode
LockdownMode bool
// InsidersMode expands to the curated set of feature flags enabled for insiders.
InsidersMode bool
// Logger is used for logging within the server
Logger *slog.Logger
// RepoAccessTTL overrides the default TTL for repository access cache entries.
RepoAccessTTL *time.Duration
// ExcludeTools is a list of tool names that should be disabled regardless of
// other configuration. These tools will be excluded even if their toolset is enabled
// or they are explicitly listed in EnabledTools.
ExcludeTools []string
// TokenScopes contains the OAuth scopes available to the token.
// When non-nil, tools requiring scopes not in this list will be hidden.
// This is used for PAT scope filtering where we can't issue scope challenges.
TokenScopes []string
// TokenProvider, when non-nil, supplies the GitHub token for each API
// request instead of the static Token.
TokenProvider func() string
// ToolHandlerMiddleware wraps every registered tool handler. Unlike MCP
// receiving middleware, these wrappers execute inside Server.callTool, so
// SDK result finalization still runs on results they return.
ToolHandlerMiddleware []inventory.ToolHandlerMiddleware
// Additional server options to apply
ServerOptions []MCPServerOption
}
type MCPServerOption func(*mcp.ServerOptions)
func NewMCPServer(ctx context.Context, cfg *MCPServerConfig, deps ToolDependencies, inv *inventory.Inventory, middleware ...mcp.Middleware) (*mcp.Server, error) {
// Create the MCP server
serverOpts := &mcp.ServerOptions{
Instructions: inv.Instructions(),
Logger: cfg.Logger,
CompletionHandler: CompletionsHandler(deps.GetClient),
// Advertise tools, prompts, and resources without list-changed
// notifications. The server has a static set of tools/prompts/resources
// and never mutates them at runtime, so it never emits list_changed
// notifications. Left unset, the SDK would infer listChanged:true from
// the presence of items and advertise a capability we don't support -
// which the 2026-07-28 spec (subscriptions/listen) makes stricter still.
// Explicitly declaring these keeps the advertised capabilities honest.
Capabilities: &mcp.ServerCapabilities{
Tools: &mcp.ToolCapabilities{},
Prompts: &mcp.PromptCapabilities{},
Resources: &mcp.ResourceCapabilities{},
},
}
// Apply any additional server options
for _, o := range cfg.ServerOptions {
o(serverOpts)
}
ghServer := NewServer(cfg.Version, cfg.Translator("SERVER_NAME", "github-mcp-server"), cfg.Translator("SERVER_TITLE", "GitHub MCP Server"), serverOpts)
// Add middlewares. Order matters - for example, the error context middleware should be applied last so that it runs FIRST (closest to the handler) to ensure all errors are captured,
// and any middleware that needs to read or modify the context should be before it.
ghServer.AddReceivingMiddleware(middleware...)
ghServer.AddReceivingMiddleware(injectFeatureStateMiddleware(inv))
ghServer.AddReceivingMiddleware(InjectDepsMiddleware(deps))
ghServer.AddReceivingMiddleware(addGitHubAPIErrorToContext)
if unrecognized := inv.UnrecognizedToolsets(); len(unrecognized) > 0 {
cfg.Logger.Warn("Warning: unrecognized toolsets ignored", "toolsets", strings.Join(unrecognized, ", "))
}
// Register GitHub tools/resources/prompts from the inventory.
inv.RegisterAll(ctx, ghServer, deps, cfg.ToolHandlerMiddleware...)
// Register MCP App UI resources whenever the embedded UI assets are
// available. The resources are static HTML and are only referenced by
// tools when the remote_mcp_ui_apps feature flag is enabled for the
// request (the inventory strips the _meta.ui block otherwise via
// stripMCPAppsMetadata), so registering them unconditionally is safe.
// Registering here — rather than in the stdio bootstrap — ensures the
// remote/HTTP server also serves them, fixing the "-32002 Resource not
// found" error clients hit after the tool returns a ui:// URI.
if UIAssetsAvailable() {
RegisterUIResources(ghServer, cfg.ReadOnly)
}
return ghServer, nil
}
func injectFeatureStateMiddleware(inv *inventory.Inventory) mcp.Middleware {
return func(next mcp.MethodHandler) mcp.MethodHandler {
return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
return next(inv.WithFeatureState(ctx), method, req)
}
}
}
// ResolvedEnabledToolsets determines which toolsets should be enabled based on config.
// Returns nil for "use defaults", empty slice for "none", or explicit list.
func ResolvedEnabledToolsets(enabledToolsets []string, enabledTools []string) []string {
if enabledToolsets != nil {
return enabledToolsets
}
if len(enabledTools) > 0 {
// When specific tools are requested but no toolsets, don't use default toolsets
// This matches the original behavior: --tools=X alone registers only X
return []string{}
}
// nil means "use defaults" in WithToolsets
return nil
}
func addGitHubAPIErrorToContext(next mcp.MethodHandler) mcp.MethodHandler {
return func(ctx context.Context, method string, req mcp.Request) (result mcp.Result, err error) {
// Ensure the context is cleared of any previous errors
// as context isn't propagated through middleware
ctx = gherrors.ContextWithGitHubErrors(ctx)
return next(ctx, method, req)
}
}
// NewServer creates a new GitHub MCP server with the given version, server
// name, display title, and options. If name or title are empty the defaults
// "github-mcp-server" and "GitHub MCP Server" are used.
func NewServer(version, name, title string, opts *mcp.ServerOptions) *mcp.Server {
if opts == nil {
opts = &mcp.ServerOptions{}
}
if name == "" {
name = "github-mcp-server"
}
if title == "" {
title = "GitHub MCP Server"
}
// Create a new MCP server
s := mcp.NewServer(&mcp.Implementation{
Name: name,
Title: title,
Version: version,
Icons: octicons.Icons("mark-github"),
}, opts)
return s
}
func CompletionsHandler(getClient GetClientFn) func(ctx context.Context, req *mcp.CompleteRequest) (*mcp.CompleteResult, error) {
return func(ctx context.Context, req *mcp.CompleteRequest) (*mcp.CompleteResult, error) {
if req == nil || req.Params == nil || req.Params.Ref == nil {
return nil, fmt.Errorf("missing required parameter: ref")
}
switch req.Params.Ref.Type {
case "ref/resource":
if strings.HasPrefix(req.Params.Ref.URI, "repo://") {
return RepositoryResourceCompletionHandler(getClient)(ctx, req)
}
return nil, fmt.Errorf("unsupported resource URI: %s", req.Params.Ref.URI)
case "ref/prompt":
return nil, nil
default:
return nil, fmt.Errorf("unsupported ref type: %s", req.Params.Ref.Type)
}
}
}
func MarshalledTextResult(v any) *mcp.CallToolResult {
data, err := json.Marshal(v)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal text result to json", err)
}
return utils.NewToolResultText(string(data))
}