Skip to content

Commit fb301c6

Browse files
committed
add param passing for context window size
1 parent 0434b7e commit fb301c6

File tree

6 files changed

+37
-36
lines changed

6 files changed

+37
-36
lines changed

cmd/github-mcp-server/generate_docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func generateReadmeDocs(readmePath string) error {
6464
t, _ := translations.TranslationHelper()
6565

6666
// Create toolset group with mock clients
67-
tsg := github.DefaultToolsetGroup(false, mockGetClient, mockGetGQLClient, mockGetRawClient, t)
67+
tsg := github.DefaultToolsetGroup(false, mockGetClient, mockGetGQLClient, mockGetRawClient, t, 5000)
6868

6969
// Generate toolsets documentation
7070
toolsetsDoc := generateToolsetsDoc(tsg)
@@ -302,7 +302,7 @@ func generateRemoteToolsetsDoc() string {
302302
t, _ := translations.TranslationHelper()
303303

304304
// Create toolset group with mock clients
305-
tsg := github.DefaultToolsetGroup(false, mockGetClient, mockGetGQLClient, mockGetRawClient, t)
305+
tsg := github.DefaultToolsetGroup(false, mockGetClient, mockGetGQLClient, mockGetRawClient, t, 5000)
306306

307307
// Generate table header
308308
buf.WriteString("| Name | Description | API URL | 1-Click Install (VS Code) | Read-only Link | 1-Click Read-only Install (VS Code) |\n")

cmd/github-mcp-server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var (
5555
ExportTranslations: viper.GetBool("export-translations"),
5656
EnableCommandLogging: viper.GetBool("enable-command-logging"),
5757
LogFilePath: viper.GetString("log-file"),
58+
ContextWindowSize: viper.GetInt("content_window_size"),
5859
}
5960
return ghmcp.RunStdioServer(stdioServerConfig)
6061
},

internal/ghmcp/server.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type MCPServerConfig struct {
4747

4848
// Translator provides translated text for the server tooling
4949
Translator translations.TranslationHelperFunc
50+
51+
// Context window size
52+
ContextWindowSize int
5053
}
5154

5255
const stdioServerLogPrefix = "stdioserver"
@@ -132,7 +135,7 @@ func NewMCPServer(cfg MCPServerConfig) (*server.MCPServer, error) {
132135
}
133136

134137
// Create default toolsets
135-
tsg := github.DefaultToolsetGroup(cfg.ReadOnly, getClient, getGQLClient, getRawClient, cfg.Translator)
138+
tsg := github.DefaultToolsetGroup(cfg.ReadOnly, getClient, getGQLClient, getRawClient, cfg.Translator, cfg.ContextWindowSize)
136139
err = tsg.EnableToolsets(enabledToolsets)
137140

138141
if err != nil {
@@ -180,6 +183,9 @@ type StdioServerConfig struct {
180183

181184
// Path to the log file if not stderr
182185
LogFilePath string
186+
187+
// Context window size
188+
ContextWindowSize int
183189
}
184190

185191
// RunStdioServer is not concurrent safe.
@@ -191,13 +197,14 @@ func RunStdioServer(cfg StdioServerConfig) error {
191197
t, dumpTranslations := translations.TranslationHelper()
192198

193199
ghServer, err := NewMCPServer(MCPServerConfig{
194-
Version: cfg.Version,
195-
Host: cfg.Host,
196-
Token: cfg.Token,
197-
EnabledToolsets: cfg.EnabledToolsets,
198-
DynamicToolsets: cfg.DynamicToolsets,
199-
ReadOnly: cfg.ReadOnly,
200-
Translator: t,
200+
Version: cfg.Version,
201+
Host: cfg.Host,
202+
Token: cfg.Token,
203+
EnabledToolsets: cfg.EnabledToolsets,
204+
DynamicToolsets: cfg.DynamicToolsets,
205+
ReadOnly: cfg.ReadOnly,
206+
Translator: t,
207+
ContextWindowSize: cfg.ContextWindowSize,
201208
})
202209
if err != nil {
203210
return fmt.Errorf("failed to create MCP server: %w", err)

pkg/github/actions.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"strconv"
99
"strings"
1010

11-
"github.com/spf13/viper"
12-
1311
buffer "github.com/github/github-mcp-server/pkg/buffer"
1412
ghErrors "github.com/github/github-mcp-server/pkg/errors"
1513
"github.com/github/github-mcp-server/pkg/profiler"
@@ -533,7 +531,7 @@ func ListWorkflowJobs(getClient GetClientFn, t translations.TranslationHelperFun
533531
}
534532

535533
// GetJobLogs creates a tool to download logs for a specific workflow job or efficiently get all failed job logs for a workflow run
536-
func GetJobLogs(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
534+
func GetJobLogs(getClient GetClientFn, t translations.TranslationHelperFunc, contextWindowSize int) (tool mcp.Tool, handler server.ToolHandlerFunc) {
537535
return mcp.NewTool("get_job_logs",
538536
mcp.WithDescription(t("TOOL_GET_JOB_LOGS_DESCRIPTION", "Download logs for a specific workflow job or efficiently get all failed job logs for a workflow run")),
539537
mcp.WithToolAnnotation(mcp.ToolAnnotation{
@@ -616,18 +614,18 @@ func GetJobLogs(getClient GetClientFn, t translations.TranslationHelperFunc) (to
616614

617615
if failedOnly && runID > 0 {
618616
// Handle failed-only mode: get logs for all failed jobs in the workflow run
619-
return handleFailedJobLogs(ctx, client, owner, repo, int64(runID), returnContent, tailLines)
617+
return handleFailedJobLogs(ctx, client, owner, repo, int64(runID), returnContent, tailLines, contextWindowSize)
620618
} else if jobID > 0 {
621619
// Handle single job mode
622-
return handleSingleJobLogs(ctx, client, owner, repo, int64(jobID), returnContent, tailLines)
620+
return handleSingleJobLogs(ctx, client, owner, repo, int64(jobID), returnContent, tailLines, contextWindowSize)
623621
}
624622

625623
return mcp.NewToolResultError("Either job_id must be provided for single job logs, or run_id with failed_only=true for failed job logs"), nil
626624
}
627625
}
628626

629627
// handleFailedJobLogs gets logs for all failed jobs in a workflow run
630-
func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo string, runID int64, returnContent bool, tailLines int) (*mcp.CallToolResult, error) {
628+
func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo string, runID int64, returnContent bool, tailLines int, contextWindowSize int) (*mcp.CallToolResult, error) {
631629
// First, get all jobs for the workflow run
632630
jobs, resp, err := client.Actions.ListWorkflowJobs(ctx, owner, repo, runID, &github.ListWorkflowJobsOptions{
633631
Filter: "latest",
@@ -659,7 +657,7 @@ func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo
659657
// Collect logs for all failed jobs
660658
var logResults []map[string]any
661659
for _, job := range failedJobs {
662-
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, job.GetID(), job.GetName(), returnContent, tailLines)
660+
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, job.GetID(), job.GetName(), returnContent, tailLines, contextWindowSize)
663661
if err != nil {
664662
// Continue with other jobs even if one fails
665663
jobResult = map[string]any{
@@ -692,8 +690,8 @@ func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo
692690
}
693691

694692
// handleSingleJobLogs gets logs for a single job
695-
func handleSingleJobLogs(ctx context.Context, client *github.Client, owner, repo string, jobID int64, returnContent bool, tailLines int) (*mcp.CallToolResult, error) {
696-
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, jobID, "", returnContent, tailLines)
693+
func handleSingleJobLogs(ctx context.Context, client *github.Client, owner, repo string, jobID int64, returnContent bool, tailLines int, contextWindowSize int) (*mcp.CallToolResult, error) {
694+
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, jobID, "", returnContent, tailLines, contextWindowSize)
697695
if err != nil {
698696
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to get job logs", resp, err), nil
699697
}
@@ -707,7 +705,7 @@ func handleSingleJobLogs(ctx context.Context, client *github.Client, owner, repo
707705
}
708706

709707
// getJobLogData retrieves log data for a single job, either as URL or content
710-
func getJobLogData(ctx context.Context, client *github.Client, owner, repo string, jobID int64, jobName string, returnContent bool, tailLines int) (map[string]any, *github.Response, error) {
708+
func getJobLogData(ctx context.Context, client *github.Client, owner, repo string, jobID int64, jobName string, returnContent bool, tailLines int, contextWindowSize int) (map[string]any, *github.Response, error) {
711709
// Get the download URL for the job logs
712710
url, resp, err := client.Actions.GetWorkflowJobLogs(ctx, owner, repo, jobID, 1)
713711
if err != nil {
@@ -724,7 +722,7 @@ func getJobLogData(ctx context.Context, client *github.Client, owner, repo strin
724722

725723
if returnContent {
726724
// Download and return the actual log content
727-
content, originalLength, httpResp, err := downloadLogContent(ctx, url.String(), tailLines) //nolint:bodyclose // Response body is closed in downloadLogContent, but we need to return httpResp
725+
content, originalLength, httpResp, err := downloadLogContent(ctx, url.String(), tailLines, contextWindowSize) //nolint:bodyclose // Response body is closed in downloadLogContent, but we need to return httpResp
728726
if err != nil {
729727
// To keep the return value consistent wrap the response as a GitHub Response
730728
ghRes := &github.Response{
@@ -745,15 +743,10 @@ func getJobLogData(ctx context.Context, client *github.Client, owner, repo strin
745743
return result, resp, nil
746744
}
747745

748-
func downloadLogContent(ctx context.Context, logURL string, tailLines int) (string, int, *http.Response, error) {
746+
func downloadLogContent(ctx context.Context, logURL string, tailLines int, maxLines int) (string, int, *http.Response, error) {
749747
prof := profiler.New(nil, profiler.IsProfilingEnabled())
750748
finish := prof.Start(ctx, "log_buffer_processing")
751749

752-
maxJobLogLines := viper.GetInt("content_window_size")
753-
if maxJobLogLines <= 0 || maxJobLogLines > 10000 {
754-
maxJobLogLines = 10000
755-
}
756-
757750
httpResp, err := http.Get(logURL) //nolint:gosec
758751
if err != nil {
759752
return "", 0, httpResp, fmt.Errorf("failed to download logs: %w", err)
@@ -769,8 +762,8 @@ func downloadLogContent(ctx context.Context, logURL string, tailLines int) (stri
769762
}
770763

771764
bufferSize := tailLines
772-
if bufferSize > maxJobLogLines {
773-
bufferSize = maxJobLogLines
765+
if bufferSize > maxLines {
766+
bufferSize = maxLines
774767
}
775768

776769
processedInput, totalLines, httpResp, err := buffer.ProcessResponseAsRingBufferToEnd(httpResp, bufferSize)

pkg/github/actions_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ func Test_GetWorkflowRunUsage(t *testing.T) {
814814
func Test_GetJobLogs(t *testing.T) {
815815
// Verify tool definition once
816816
mockClient := github.NewClient(nil)
817-
tool, _ := GetJobLogs(stubGetClientFn(mockClient), translations.NullTranslationHelper)
817+
tool, _ := GetJobLogs(stubGetClientFn(mockClient), translations.NullTranslationHelper, 5000)
818818

819819
assert.Equal(t, "get_job_logs", tool.Name)
820820
assert.NotEmpty(t, tool.Description)
@@ -1043,7 +1043,7 @@ func Test_GetJobLogs(t *testing.T) {
10431043
t.Run(tc.name, func(t *testing.T) {
10441044
// Setup client with mock
10451045
client := github.NewClient(tc.mockedClient)
1046-
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper)
1046+
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper, 5000)
10471047

10481048
// Create call request
10491049
request := createMCPRequest(tc.requestArgs)
@@ -1102,7 +1102,7 @@ func Test_GetJobLogs_WithContentReturn(t *testing.T) {
11021102
)
11031103

11041104
client := github.NewClient(mockedClient)
1105-
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper)
1105+
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper, 5000)
11061106

11071107
request := createMCPRequest(map[string]any{
11081108
"owner": "owner",
@@ -1149,7 +1149,7 @@ func Test_GetJobLogs_WithContentReturnAndTailLines(t *testing.T) {
11491149
)
11501150

11511151
client := github.NewClient(mockedClient)
1152-
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper)
1152+
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper, 5000)
11531153

11541154
request := createMCPRequest(map[string]any{
11551155
"owner": "owner",
@@ -1196,7 +1196,7 @@ func Test_GetJobLogs_WithContentReturnAndLargeTailLines(t *testing.T) {
11961196
)
11971197

11981198
client := github.NewClient(mockedClient)
1199-
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper)
1199+
_, handler := GetJobLogs(stubGetClientFn(client), translations.NullTranslationHelper, 5000)
12001200

12011201
request := createMCPRequest(map[string]any{
12021202
"owner": "owner",

pkg/github/tools.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type GetGQLClientFn func(context.Context) (*githubv4.Client, error)
1616

1717
var DefaultTools = []string{"all"}
1818

19-
func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetGQLClientFn, getRawClient raw.GetRawClientFn, t translations.TranslationHelperFunc) *toolsets.ToolsetGroup {
19+
func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetGQLClientFn, getRawClient raw.GetRawClientFn, t translations.TranslationHelperFunc, contextWindowSize int) *toolsets.ToolsetGroup {
2020
tsg := toolsets.NewToolsetGroup(readOnly)
2121

2222
// Define all available features with their default state (disabled)
@@ -146,7 +146,7 @@ func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetG
146146
toolsets.NewServerTool(GetWorkflowRun(getClient, t)),
147147
toolsets.NewServerTool(GetWorkflowRunLogs(getClient, t)),
148148
toolsets.NewServerTool(ListWorkflowJobs(getClient, t)),
149-
toolsets.NewServerTool(GetJobLogs(getClient, t)),
149+
toolsets.NewServerTool(GetJobLogs(getClient, t, contextWindowSize)),
150150
toolsets.NewServerTool(ListWorkflowRunArtifacts(getClient, t)),
151151
toolsets.NewServerTool(DownloadWorkflowRunArtifact(getClient, t)),
152152
toolsets.NewServerTool(GetWorkflowRunUsage(getClient, t)),

0 commit comments

Comments
 (0)