-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathmcp_renderer_github.go
More file actions
423 lines (367 loc) · 16.8 KB
/
mcp_renderer_github.go
File metadata and controls
423 lines (367 loc) · 16.8 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package workflow
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/github/gh-aw/pkg/constants"
)
// RenderGitHubMCP generates the GitHub MCP server configuration
// Supports both local (Docker) and remote (hosted) modes
func (r *MCPConfigRendererUnified) RenderGitHubMCP(yaml *strings.Builder, githubTool any, workflowData *WorkflowData) {
githubType := getGitHubType(githubTool)
readOnly := getGitHubReadOnly(githubTool)
// Get explicit lockdown value (only used when lockdown is explicitly configured)
lockdown := getGitHubLockdown(githubTool)
// Guard policies from step: automatically applied for public repositories when no explicit
// guard policy is configured and no GitHub App token is in use.
// The determine-automatic-lockdown step outputs min_integrity and repos for public repos.
explicitGuardPolicies := getGitHubGuardPolicies(githubTool)
// Inject integrity reaction fields into the allow-only policy when the feature flag is
// enabled and the MCPG version supports it.
if len(explicitGuardPolicies) > 0 {
if toolConfig, ok := githubTool.(map[string]any); ok {
if allowOnly, ok := explicitGuardPolicies["allow-only"].(map[string]any); ok {
var gatewayConfig *MCPGatewayRuntimeConfig
if workflowData != nil && workflowData.SandboxConfig != nil {
gatewayConfig = workflowData.SandboxConfig.MCP
}
injectIntegrityReactionFields(allowOnly, toolConfig, workflowData, gatewayConfig)
}
}
}
shouldUseStepOutputForGuardPolicy := len(explicitGuardPolicies) == 0 && !hasGitHubApp(githubTool)
toolsets := getGitHubToolsets(githubTool)
mcpRendererLog.Printf("Rendering GitHub MCP: type=%s, read_only=%t, lockdown=%t (explicit=%t), guard_from_step=%t, toolsets=%v, format=%s",
githubType, readOnly, lockdown, hasGitHubLockdownExplicitlySet(githubTool), shouldUseStepOutputForGuardPolicy, toolsets, r.options.Format)
if r.options.Format == "toml" {
r.renderGitHubTOML(yaml, githubTool, workflowData)
return
}
yaml.WriteString(" \"github\": {\n")
// Check if remote mode is enabled (type: remote)
if githubType == "remote" {
// Determine authorization value based on engine requirements
// Copilot uses MCP passthrough syntax: "Bearer \${GITHUB_PERSONAL_ACCESS_TOKEN}"
// Other engines use shell variable: "Bearer $GITHUB_MCP_SERVER_TOKEN"
authValue := "Bearer $GITHUB_MCP_SERVER_TOKEN"
if r.options.IncludeCopilotFields {
authValue = "Bearer \\${GITHUB_PERSONAL_ACCESS_TOKEN}"
}
RenderGitHubMCPRemoteConfig(yaml, GitHubMCPRemoteOptions{
ReadOnly: readOnly,
Lockdown: lockdown,
LockdownFromStep: false,
GuardPoliciesFromStep: shouldUseStepOutputForGuardPolicy,
Toolsets: toolsets,
AuthorizationValue: authValue,
IncludeToolsField: r.options.IncludeCopilotFields,
AllowedTools: getGitHubAllowedTools(githubTool),
IncludeEnvSection: r.options.IncludeCopilotFields,
GuardPolicies: explicitGuardPolicies,
})
} else {
// Local mode - use Docker-based GitHub MCP server (default)
githubDockerImageVersion := getGitHubDockerImageVersion(githubTool)
customArgs := getGitHubCustomArgs(githubTool)
RenderGitHubMCPDockerConfig(yaml, GitHubMCPDockerOptions{
ReadOnly: readOnly,
Lockdown: lockdown,
LockdownFromStep: false,
GuardPoliciesFromStep: shouldUseStepOutputForGuardPolicy,
Toolsets: toolsets,
DockerImageVersion: githubDockerImageVersion,
CustomArgs: customArgs,
IncludeTypeField: r.options.IncludeCopilotFields,
AllowedTools: getGitHubAllowedTools(githubTool),
EffectiveToken: "", // Token passed via env
GuardPolicies: explicitGuardPolicies,
})
}
if r.options.IsLast {
yaml.WriteString(" }\n")
} else {
yaml.WriteString(" },\n")
}
}
// renderGitHubTOML generates GitHub MCP configuration in TOML format (for Codex engine)
func (r *MCPConfigRendererUnified) renderGitHubTOML(yaml *strings.Builder, githubTool any, workflowData *WorkflowData) {
githubType := getGitHubType(githubTool)
readOnly := getGitHubReadOnly(githubTool)
lockdown := getGitHubLockdown(githubTool)
toolsets := getGitHubToolsets(githubTool)
mcpRendererLog.Printf("Rendering GitHub MCP TOML: type=%s, read_only=%t, lockdown=%t, toolsets=%s", githubType, readOnly, lockdown, toolsets)
yaml.WriteString(" \n")
yaml.WriteString(" [mcp_servers.github]\n")
// Add user_agent field defaulting to workflow identifier
userAgent := "github-agentic-workflow"
if workflowData != nil {
// Check if user_agent is configured in engine config first
if workflowData.EngineConfig != nil && workflowData.EngineConfig.UserAgent != "" {
userAgent = workflowData.EngineConfig.UserAgent
} else if workflowData.Name != "" {
// Fall back to sanitizing workflow name to identifier
userAgent = SanitizeIdentifier(workflowData.Name)
}
}
yaml.WriteString(" user_agent = \"" + userAgent + "\"\n")
// Use tools.startup-timeout if specified, otherwise default to DefaultMCPStartupTimeout
// For GitHub Actions expressions, fall back to default (TOML format doesn't support expressions)
startupTimeout := int(constants.DefaultMCPStartupTimeout / time.Second)
if workflowData != nil && workflowData.ToolsStartupTimeout != "" {
if n := templatableIntValue(&workflowData.ToolsStartupTimeout); n > 0 {
startupTimeout = n
}
}
fmt.Fprintf(yaml, " startup_timeout_sec = %d\n", startupTimeout)
// Use tools.timeout if specified, otherwise default to DefaultToolTimeout
// For GitHub Actions expressions, fall back to default (TOML format doesn't support expressions)
toolTimeout := int(constants.DefaultToolTimeout / time.Second)
if workflowData != nil && workflowData.ToolsTimeout != "" {
if n := templatableIntValue(&workflowData.ToolsTimeout); n > 0 {
toolTimeout = n
}
}
fmt.Fprintf(yaml, " tool_timeout_sec = %d\n", toolTimeout)
// Check if remote mode is enabled
if githubType == "remote" {
// Remote mode - use hosted GitHub MCP server with streamable HTTP
// Use readonly endpoint if read-only mode is enabled
if readOnly {
yaml.WriteString(" url = \"https://api.githubcopilot.com/mcp-readonly/\"\n")
} else {
yaml.WriteString(" url = \"https://api.githubcopilot.com/mcp/\"\n")
}
// Use bearer_token_env_var for authentication
yaml.WriteString(" bearer_token_env_var = \"GH_AW_GITHUB_TOKEN\"\n")
} else {
// Local mode - use Docker-based GitHub MCP server with MCP Gateway spec format
githubDockerImageVersion := getGitHubDockerImageVersion(githubTool)
customArgs := getGitHubCustomArgs(githubTool)
// MCP Gateway spec fields for containerized stdio servers
yaml.WriteString(" container = \"ghcr.io/github/github-mcp-server:" + githubDockerImageVersion + "\"\n")
// Append custom args if present (these are Docker runtime args, go before container image)
if len(customArgs) > 0 {
yaml.WriteString(" args = [\n")
for _, arg := range customArgs {
yaml.WriteString(" " + strconv.Quote(arg) + ",\n")
}
yaml.WriteString(" ]\n")
}
// Build environment variables
envVars := make(map[string]string)
envVars["GITHUB_PERSONAL_ACCESS_TOKEN"] = "$GH_AW_GITHUB_TOKEN"
// GitHub host for enterprise deployments (format: https://hostname, e.g. https://myorg.ghe.com).
// GITHUB_SERVER_URL is set by GitHub Actions as a full URL (https://hostname, no trailing slash),
// which matches the format expected by github-mcp-server for GITHUB_HOST.
envVars["GITHUB_HOST"] = "$GITHUB_SERVER_URL"
if readOnly {
envVars["GITHUB_READ_ONLY"] = "1"
}
if lockdown {
envVars["GITHUB_LOCKDOWN_MODE"] = "1"
}
envVars["GITHUB_TOOLSETS"] = toolsets
// Write environment variables in sorted order for deterministic output
envKeys := sortedMapKeys(envVars)
yaml.WriteString(" env = { ")
for i, key := range envKeys {
if i > 0 {
yaml.WriteString(", ")
}
fmt.Fprintf(yaml, "\"%s\" = \"%s\"", key, envVars[key])
}
yaml.WriteString(" }\n")
// Use env_vars array to reference environment variables
yaml.WriteString(" env_vars = [")
for i, key := range envKeys {
if i > 0 {
yaml.WriteString(", ")
}
fmt.Fprintf(yaml, "\"%s\"", key)
}
yaml.WriteString("]\n")
}
}
// RenderGitHubMCPDockerConfig renders the GitHub MCP server configuration for Docker (local mode).
// Per MCP Gateway Specification v1.0.0 section 3.2.1, stdio-based MCP servers MUST be containerized.
// Uses MCP Gateway spec format: container, entrypointArgs, and env fields.
//
// Parameters:
// - yaml: The string builder for YAML output
// - options: GitHub MCP Docker rendering options
func RenderGitHubMCPDockerConfig(yaml *strings.Builder, options GitHubMCPDockerOptions) {
mcpRendererLog.Printf("Rendering GitHub MCP Docker config: image=%s, read_only=%t, lockdown=%t", options.DockerImageVersion, options.ReadOnly, options.Lockdown)
// Add type field if needed (Copilot requires this, Claude doesn't)
// Per MCP Gateway Specification v1.0.0 section 4.1.2, use "stdio" for containerized servers
if options.IncludeTypeField {
yaml.WriteString(" \"type\": \"stdio\",\n")
}
// MCP Gateway spec fields for containerized stdio servers
yaml.WriteString(" \"container\": \"ghcr.io/github/github-mcp-server:" + options.DockerImageVersion + "\",\n")
// Append custom args if present (these are Docker runtime args, go before container image)
if len(options.CustomArgs) > 0 {
yaml.WriteString(" \"args\": [\n")
for _, arg := range options.CustomArgs {
quotedArg, _ := json.Marshal(arg)
yaml.WriteString(" " + string(quotedArg) + ",\n")
}
yaml.WriteString(" ],\n")
}
// Note: tools field is NOT included here - the converter script adds it back
// for Copilot (see convert_gateway_config_copilot.sh). This keeps the gateway
// config compatible with the schema which doesn't have the tools field.
// Add env section for GitHub MCP server environment variables
yaml.WriteString(" \"env\": {\n")
// Build environment variables map
envVars := make(map[string]string)
// GitHub token (always required)
if options.IncludeTypeField {
// Copilot engine: use escaped variable for Copilot CLI to interpolate
envVars["GITHUB_PERSONAL_ACCESS_TOKEN"] = "\\${GITHUB_MCP_SERVER_TOKEN}"
// GitHub host for enterprise deployments (format: https://hostname, e.g. https://myorg.ghe.com).
// GITHUB_SERVER_URL is set by GitHub Actions as a full URL (https://hostname, no trailing slash),
// which matches the format expected by github-mcp-server for GITHUB_HOST.
// Copilot CLI interpolation syntax used here.
envVars["GITHUB_HOST"] = "\\${GITHUB_SERVER_URL}"
} else {
// Non-Copilot engines (Claude/Custom): use plain shell variable
envVars["GITHUB_PERSONAL_ACCESS_TOKEN"] = "$GITHUB_MCP_SERVER_TOKEN"
// GitHub host for enterprise deployments (format: https://hostname, e.g. https://myorg.ghe.com).
// GITHUB_SERVER_URL is set by GitHub Actions as a full URL (https://hostname, no trailing slash),
// which matches the format expected by github-mcp-server for GITHUB_HOST.
envVars["GITHUB_HOST"] = "$GITHUB_SERVER_URL"
}
// Read-only mode
if options.ReadOnly {
envVars["GITHUB_READ_ONLY"] = "1"
}
// GitHub lockdown mode (only when explicitly configured)
if options.Lockdown {
// Use explicit lockdown value from configuration
envVars["GITHUB_LOCKDOWN_MODE"] = "1"
}
// Toolsets (always configured, defaults to "default")
envVars["GITHUB_TOOLSETS"] = options.Toolsets
// Write environment variables in sorted order for deterministic output
envKeys := sortedMapKeys(envVars)
for i, key := range envKeys {
isLast := i == len(envKeys)-1
comma := ""
if !isLast {
comma = ","
}
fmt.Fprintf(yaml, " \"%s\": \"%s\"%s\n", key, envVars[key], comma)
}
// Close env section, with trailing comma if guard-policies follows
hasGuardPolicies := len(options.GuardPolicies) > 0 || options.GuardPoliciesFromStep
if hasGuardPolicies {
yaml.WriteString(" },\n")
if options.GuardPoliciesFromStep {
// Render guard-policies with env var refs resolved at runtime from step outputs
// GITHUB_MCP_GUARD_MIN_INTEGRITY and GITHUB_MCP_GUARD_REPOS are set in Start MCP
// Gateway step from the determine-automatic-lockdown step outputs. They are
// non-empty only for public repositories.
renderGuardPoliciesJSON(yaml, map[string]any{
"allow-only": map[string]any{
"min-integrity": "$GITHUB_MCP_GUARD_MIN_INTEGRITY",
"repos": "$GITHUB_MCP_GUARD_REPOS",
},
}, " ")
} else {
renderGuardPoliciesJSON(yaml, options.GuardPolicies, " ")
}
} else {
yaml.WriteString(" }\n")
}
}
// RenderGitHubMCPRemoteConfig renders the GitHub MCP server configuration for remote (hosted) mode.
// This shared function extracts the duplicate pattern from Claude and Copilot engines.
//
// Parameters:
// - yaml: The string builder for YAML output
// - options: GitHub MCP remote rendering options
func RenderGitHubMCPRemoteConfig(yaml *strings.Builder, options GitHubMCPRemoteOptions) {
mcpRendererLog.Printf("Rendering GitHub MCP remote config: read_only=%t, lockdown=%t, toolsets=%s", options.ReadOnly, options.Lockdown, options.Toolsets)
// Remote mode - use hosted GitHub MCP server
yaml.WriteString(" \"type\": \"http\",\n")
yaml.WriteString(" \"url\": \"https://api.githubcopilot.com/mcp/\",\n")
yaml.WriteString(" \"headers\": {\n")
// Collect headers in a map
headers := make(map[string]string)
headers["Authorization"] = options.AuthorizationValue
// Add X-MCP-Readonly header if read-only mode is enabled
if options.ReadOnly {
headers["X-MCP-Readonly"] = "true"
}
// Add X-MCP-Lockdown header only when explicitly configured
if options.Lockdown {
// Use explicit lockdown value from configuration
headers["X-MCP-Lockdown"] = "true"
}
// Add X-MCP-Toolsets header if toolsets are configured
if options.Toolsets != "" {
headers["X-MCP-Toolsets"] = options.Toolsets
}
// Write headers using helper
writeHeadersToYAML(yaml, headers, " ")
// Determine if guard-policies section follows (explicit or from step)
hasGuardPolicies := len(options.GuardPolicies) > 0 || options.GuardPoliciesFromStep
// Close headers section
if options.IncludeToolsField || options.IncludeEnvSection || hasGuardPolicies {
yaml.WriteString(" },\n")
} else {
yaml.WriteString(" }\n")
}
// Add tools field if requested (Copilot needs it, Claude doesn't)
// Note: This is added here when IncludeToolsField is true, but in some cases
// the converter script also adds it back (see convert_gateway_config_copilot.sh).
if options.IncludeToolsField && len(options.AllowedTools) > 0 {
yaml.WriteString(" \"tools\": [\n")
for i, tool := range options.AllowedTools {
yaml.WriteString(" \"")
yaml.WriteString(tool)
yaml.WriteString("\"")
if i < len(options.AllowedTools)-1 {
yaml.WriteString(",")
}
yaml.WriteString("\n")
}
if options.IncludeEnvSection || hasGuardPolicies {
yaml.WriteString(" ],\n")
} else {
yaml.WriteString(" ]\n")
}
}
// Add env section if needed (Copilot uses this, Claude doesn't)
if options.IncludeEnvSection {
yaml.WriteString(" \"env\": {\n")
yaml.WriteString(" \"GITHUB_PERSONAL_ACCESS_TOKEN\": \"\\${GITHUB_MCP_SERVER_TOKEN}\",\n")
// GitHub host for enterprise deployments (format: https://hostname, e.g. https://myorg.ghe.com).
// GITHUB_SERVER_URL is set by GitHub Actions as a full URL (https://hostname, no trailing slash),
// which matches the format expected by github-mcp-server for GITHUB_HOST.
yaml.WriteString(" \"GITHUB_HOST\": \"\\${GITHUB_SERVER_URL}\"\n")
// Close env section, with trailing comma if guard-policies follows
if hasGuardPolicies {
yaml.WriteString(" },\n")
} else {
yaml.WriteString(" }\n")
}
}
// Add guard-policies if configured or from step
if options.GuardPoliciesFromStep {
// Render guard-policies with env var refs resolved at runtime from step outputs
// GITHUB_MCP_GUARD_MIN_INTEGRITY and GITHUB_MCP_GUARD_REPOS are set in Start MCP
// Gateway step from the determine-automatic-lockdown step outputs. They are
// non-empty only for public repositories.
renderGuardPoliciesJSON(yaml, map[string]any{
"allow-only": map[string]any{
"min-integrity": "$GITHUB_MCP_GUARD_MIN_INTEGRITY",
"repos": "$GITHUB_MCP_GUARD_REPOS",
},
}, " ")
} else if len(options.GuardPolicies) > 0 {
renderGuardPoliciesJSON(yaml, options.GuardPolicies, " ")
}
}