diff --git a/internal/ghmcp/server.go b/internal/ghmcp/server.go index 39a6726ce..8c34fd482 100644 --- a/internal/ghmcp/server.go +++ b/internal/ghmcp/server.go @@ -57,8 +57,6 @@ type MCPServerConfig struct { Logger *slog.Logger } -const stdioServerLogPrefix = "stdioserver" - func NewMCPServer(cfg MCPServerConfig) (*mcp.Server, error) { apiHost, err := parseAPIHost(cfg.Host) if err != nil { diff --git a/pkg/github/__toolsnaps__/get_code_scanning_alert.snap b/pkg/github/__toolsnaps__/get_code_scanning_alert.snap index eedc20b46..9e46b960a 100644 --- a/pkg/github/__toolsnaps__/get_code_scanning_alert.snap +++ b/pkg/github/__toolsnaps__/get_code_scanning_alert.snap @@ -1,30 +1,30 @@ { "annotations": { - "title": "Get code scanning alert", - "readOnlyHint": true + "readOnlyHint": true, + "title": "Get code scanning alert" }, "description": "Get details of a specific code scanning alert in a GitHub repository.", "inputSchema": { + "type": "object", + "required": [ + "owner", + "repo", + "alertNumber" + ], "properties": { "alertNumber": { - "description": "The number of the alert.", - "type": "number" + "type": "number", + "description": "The number of the alert." }, "owner": { - "description": "The owner of the repository.", - "type": "string" + "type": "string", + "description": "The owner of the repository." }, "repo": { - "description": "The name of the repository.", - "type": "string" + "type": "string", + "description": "The name of the repository." } - }, - "required": [ - "owner", - "repo", - "alertNumber" - ], - "type": "object" + } }, "name": "get_code_scanning_alert" } \ No newline at end of file diff --git a/pkg/github/__toolsnaps__/list_code_scanning_alerts.snap b/pkg/github/__toolsnaps__/list_code_scanning_alerts.snap index 470f0d01f..6f2a4e342 100644 --- a/pkg/github/__toolsnaps__/list_code_scanning_alerts.snap +++ b/pkg/github/__toolsnaps__/list_code_scanning_alerts.snap @@ -1,24 +1,30 @@ { "annotations": { - "title": "List code scanning alerts", - "readOnlyHint": true + "readOnlyHint": true, + "title": "List code scanning alerts" }, "description": "List code scanning alerts in a GitHub repository.", "inputSchema": { + "type": "object", + "required": [ + "owner", + "repo" + ], "properties": { "owner": { - "description": "The owner of the repository.", - "type": "string" + "type": "string", + "description": "The owner of the repository." }, "ref": { - "description": "The Git reference for the results you want to list.", - "type": "string" + "type": "string", + "description": "The Git reference for the results you want to list." }, "repo": { - "description": "The name of the repository.", - "type": "string" + "type": "string", + "description": "The name of the repository." }, "severity": { + "type": "string", "description": "Filter code scanning alerts by severity", "enum": [ "critical", @@ -28,30 +34,24 @@ "warning", "note", "error" - ], - "type": "string" + ] }, "state": { - "default": "open", + "type": "string", "description": "Filter code scanning alerts by state. Defaults to open", + "default": "open", "enum": [ "open", "closed", "dismissed", "fixed" - ], - "type": "string" + ] }, "tool_name": { - "description": "The name of the tool used for code scanning.", - "type": "string" + "type": "string", + "description": "The name of the tool used for code scanning." } - }, - "required": [ - "owner", - "repo" - ], - "type": "object" + } }, "name": "list_code_scanning_alerts" } \ No newline at end of file diff --git a/pkg/github/code_scanning.go b/pkg/github/code_scanning.go index a087bf0bf..0f8e2780b 100644 --- a/pkg/github/code_scanning.go +++ b/pkg/github/code_scanning.go @@ -1,5 +1,3 @@ -//go:build ignore - package github import ( @@ -11,48 +9,56 @@ import ( ghErrors "github.com/github/github-mcp-server/pkg/errors" "github.com/github/github-mcp-server/pkg/translations" + "github.com/github/github-mcp-server/pkg/utils" "github.com/google/go-github/v79/github" - "github.com/mark3labs/mcp-go/mcp" - "github.com/mark3labs/mcp-go/server" + "github.com/google/jsonschema-go/jsonschema" + "github.com/modelcontextprotocol/go-sdk/mcp" ) -func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { - return mcp.NewTool("get_code_scanning_alert", - mcp.WithDescription(t("TOOL_GET_CODE_SCANNING_ALERT_DESCRIPTION", "Get details of a specific code scanning alert in a GitHub repository.")), - mcp.WithToolAnnotation(mcp.ToolAnnotation{ +func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) { + return mcp.Tool{ + Name: "get_code_scanning_alert", + Description: t("TOOL_GET_CODE_SCANNING_ALERT_DESCRIPTION", "Get details of a specific code scanning alert in a GitHub repository."), + Annotations: &mcp.ToolAnnotations{ Title: t("TOOL_GET_CODE_SCANNING_ALERT_USER_TITLE", "Get code scanning alert"), - ReadOnlyHint: ToBoolPtr(true), - }), - mcp.WithString("owner", - mcp.Required(), - mcp.Description("The owner of the repository."), - ), - mcp.WithString("repo", - mcp.Required(), - mcp.Description("The name of the repository."), - ), - mcp.WithNumber("alertNumber", - mcp.Required(), - mcp.Description("The number of the alert."), - ), - ), - func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { - owner, err := RequiredParam[string](request, "owner") + ReadOnlyHint: true, + }, + InputSchema: &jsonschema.Schema{ + Type: "object", + Properties: map[string]*jsonschema.Schema{ + "owner": { + Type: "string", + Description: "The owner of the repository.", + }, + "repo": { + Type: "string", + Description: "The name of the repository.", + }, + "alertNumber": { + Type: "number", + Description: "The number of the alert.", + }, + }, + Required: []string{"owner", "repo", "alertNumber"}, + }, + }, + func(ctx context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { + owner, err := RequiredParam[string](args, "owner") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } - repo, err := RequiredParam[string](request, "repo") + repo, err := RequiredParam[string](args, "repo") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } - alertNumber, err := RequiredInt(request, "alertNumber") + alertNumber, err := RequiredInt(args, "alertNumber") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } client, err := getClient(ctx) if err != nil { - return nil, fmt.Errorf("failed to get GitHub client: %w", err) + return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil } alert, resp, err := client.CodeScanning.GetAlert(ctx, owner, repo, int64(alertNumber)) @@ -61,87 +67,98 @@ func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelpe "failed to get alert", resp, err, - ), nil + ), nil, nil } defer func() { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { body, err := io.ReadAll(resp.Body) if err != nil { - return nil, fmt.Errorf("failed to read response body: %w", err) + return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil } - return mcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil + return utils.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil, nil } r, err := json.Marshal(alert) if err != nil { - return nil, fmt.Errorf("failed to marshal alert: %w", err) + return utils.NewToolResultErrorFromErr("failed to marshal alert", err), nil, nil } - return mcp.NewToolResultText(string(r)), nil + return utils.NewToolResultText(string(r)), nil, nil } } -func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { - return mcp.NewTool("list_code_scanning_alerts", - mcp.WithDescription(t("TOOL_LIST_CODE_SCANNING_ALERTS_DESCRIPTION", "List code scanning alerts in a GitHub repository.")), - mcp.WithToolAnnotation(mcp.ToolAnnotation{ +func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) { + return mcp.Tool{ + Name: "list_code_scanning_alerts", + Description: t("TOOL_LIST_CODE_SCANNING_ALERTS_DESCRIPTION", "List code scanning alerts in a GitHub repository."), + Annotations: &mcp.ToolAnnotations{ Title: t("TOOL_LIST_CODE_SCANNING_ALERTS_USER_TITLE", "List code scanning alerts"), - ReadOnlyHint: ToBoolPtr(true), - }), - mcp.WithString("owner", - mcp.Required(), - mcp.Description("The owner of the repository."), - ), - mcp.WithString("repo", - mcp.Required(), - mcp.Description("The name of the repository."), - ), - mcp.WithString("state", - mcp.Description("Filter code scanning alerts by state. Defaults to open"), - mcp.DefaultString("open"), - mcp.Enum("open", "closed", "dismissed", "fixed"), - ), - mcp.WithString("ref", - mcp.Description("The Git reference for the results you want to list."), - ), - mcp.WithString("severity", - mcp.Description("Filter code scanning alerts by severity"), - mcp.Enum("critical", "high", "medium", "low", "warning", "note", "error"), - ), - mcp.WithString("tool_name", - mcp.Description("The name of the tool used for code scanning."), - ), - ), - func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { - owner, err := RequiredParam[string](request, "owner") + ReadOnlyHint: true, + }, + InputSchema: &jsonschema.Schema{ + Type: "object", + Properties: map[string]*jsonschema.Schema{ + "owner": { + Type: "string", + Description: "The owner of the repository.", + }, + "repo": { + Type: "string", + Description: "The name of the repository.", + }, + "state": { + Type: "string", + Description: "Filter code scanning alerts by state. Defaults to open", + Enum: []any{"open", "closed", "dismissed", "fixed"}, + Default: json.RawMessage(`"open"`), + }, + "ref": { + Type: "string", + Description: "The Git reference for the results you want to list.", + }, + "severity": { + Type: "string", + Description: "Filter code scanning alerts by severity", + Enum: []any{"critical", "high", "medium", "low", "warning", "note", "error"}, + }, + "tool_name": { + Type: "string", + Description: "The name of the tool used for code scanning.", + }, + }, + Required: []string{"owner", "repo"}, + }, + }, + func(ctx context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { + owner, err := RequiredParam[string](args, "owner") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } - repo, err := RequiredParam[string](request, "repo") + repo, err := RequiredParam[string](args, "repo") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } - ref, err := OptionalParam[string](request, "ref") + ref, err := OptionalParam[string](args, "ref") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } - state, err := OptionalParam[string](request, "state") + state, err := OptionalParam[string](args, "state") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } - severity, err := OptionalParam[string](request, "severity") + severity, err := OptionalParam[string](args, "severity") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } - toolName, err := OptionalParam[string](request, "tool_name") + toolName, err := OptionalParam[string](args, "tool_name") if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return utils.NewToolResultError(err.Error()), nil, nil } client, err := getClient(ctx) if err != nil { - return nil, fmt.Errorf("failed to get GitHub client: %w", err) + return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil } alerts, resp, err := client.CodeScanning.ListAlertsForRepo(ctx, owner, repo, &github.AlertListOptions{Ref: ref, State: state, Severity: severity, ToolName: toolName}) if err != nil { @@ -149,23 +166,23 @@ func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHel "failed to list alerts", resp, err, - ), nil + ), nil, nil } defer func() { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { body, err := io.ReadAll(resp.Body) if err != nil { - return nil, fmt.Errorf("failed to read response body: %w", err) + return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil } - return mcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil + return utils.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil, nil } r, err := json.Marshal(alerts) if err != nil { - return nil, fmt.Errorf("failed to marshal alerts: %w", err) + return utils.NewToolResultErrorFromErr("failed to marshal alerts", err), nil, nil } - return mcp.NewToolResultText(string(r)), nil + return utils.NewToolResultText(string(r)), nil, nil } } diff --git a/pkg/github/code_scanning_test.go b/pkg/github/code_scanning_test.go index dc2d66446..13e89fc30 100644 --- a/pkg/github/code_scanning_test.go +++ b/pkg/github/code_scanning_test.go @@ -1,5 +1,3 @@ -//go:build ignore - package github import ( @@ -11,6 +9,7 @@ import ( "github.com/github/github-mcp-server/internal/toolsnaps" "github.com/github/github-mcp-server/pkg/translations" "github.com/google/go-github/v79/github" + "github.com/google/jsonschema-go/jsonschema" "github.com/migueleliasweb/go-github-mock/src/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -24,10 +23,14 @@ func Test_GetCodeScanningAlert(t *testing.T) { assert.Equal(t, "get_code_scanning_alert", tool.Name) assert.NotEmpty(t, tool.Description) - assert.Contains(t, tool.InputSchema.Properties, "owner") - assert.Contains(t, tool.InputSchema.Properties, "repo") - assert.Contains(t, tool.InputSchema.Properties, "alertNumber") - assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "alertNumber"}) + + // InputSchema is of type any, need to cast to *jsonschema.Schema + schema, ok := tool.InputSchema.(*jsonschema.Schema) + require.True(t, ok, "InputSchema should be *jsonschema.Schema") + assert.Contains(t, schema.Properties, "owner") + assert.Contains(t, schema.Properties, "repo") + assert.Contains(t, schema.Properties, "alertNumber") + assert.ElementsMatch(t, schema.Required, []string{"owner", "repo", "alertNumber"}) // Setup mock alert for success case mockAlert := &github.Alert{ @@ -91,8 +94,8 @@ func Test_GetCodeScanningAlert(t *testing.T) { // Create call request request := createMCPRequest(tc.requestArgs) - // Call handler - result, err := handler(context.Background(), request) + // Call handler with new signature + result, _, err := handler(context.Background(), &request, tc.requestArgs) // Verify results if tc.expectError { @@ -130,13 +133,17 @@ func Test_ListCodeScanningAlerts(t *testing.T) { assert.Equal(t, "list_code_scanning_alerts", tool.Name) assert.NotEmpty(t, tool.Description) - assert.Contains(t, tool.InputSchema.Properties, "owner") - assert.Contains(t, tool.InputSchema.Properties, "repo") - assert.Contains(t, tool.InputSchema.Properties, "ref") - assert.Contains(t, tool.InputSchema.Properties, "state") - assert.Contains(t, tool.InputSchema.Properties, "severity") - assert.Contains(t, tool.InputSchema.Properties, "tool_name") - assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // InputSchema is of type any, need to cast to *jsonschema.Schema + schema, ok := tool.InputSchema.(*jsonschema.Schema) + require.True(t, ok, "InputSchema should be *jsonschema.Schema") + assert.Contains(t, schema.Properties, "owner") + assert.Contains(t, schema.Properties, "repo") + assert.Contains(t, schema.Properties, "ref") + assert.Contains(t, schema.Properties, "state") + assert.Contains(t, schema.Properties, "severity") + assert.Contains(t, schema.Properties, "tool_name") + assert.ElementsMatch(t, schema.Required, []string{"owner", "repo"}) // Setup mock alerts for success case mockAlerts := []*github.Alert{ @@ -217,8 +224,8 @@ func Test_ListCodeScanningAlerts(t *testing.T) { // Create call request request := createMCPRequest(tc.requestArgs) - // Call handler - result, err := handler(context.Background(), request) + // Call handler with new signature + result, _, err := handler(context.Background(), &request, tc.requestArgs) // Verify results if tc.expectError { diff --git a/pkg/github/tools.go b/pkg/github/tools.go index c024a31e9..22477838a 100644 --- a/pkg/github/tools.go +++ b/pkg/github/tools.go @@ -159,6 +159,7 @@ func GetDefaultToolsetIDs() []string { } } +//nolint:unused func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetGQLClientFn, getRawClient raw.GetRawClientFn, t translations.TranslationHelperFunc, contentWindowSize int, flags FeatureFlags) *toolsets.ToolsetGroup { tsg := toolsets.NewToolsetGroup(readOnly) @@ -239,11 +240,11 @@ func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetG // toolsets.NewServerTool(PullRequestReviewWrite(getGQLClient, t)), // toolsets.NewServerTool(AddCommentToPendingReview(getGQLClient, t)), // ) - // codeSecurity := toolsets.NewToolset(ToolsetMetadataCodeSecurity.ID, ToolsetMetadataCodeSecurity.Description). - // AddReadTools( - // toolsets.NewServerTool(GetCodeScanningAlert(getClient, t)), - // toolsets.NewServerTool(ListCodeScanningAlerts(getClient, t)), - // ) + codeSecurity := toolsets.NewToolset(ToolsetMetadataCodeSecurity.ID, ToolsetMetadataCodeSecurity.Description). + AddReadTools( + toolsets.NewServerTool(GetCodeScanningAlert(getClient, t)), + toolsets.NewServerTool(ListCodeScanningAlerts(getClient, t)), + ) // secretProtection := toolsets.NewToolset(ToolsetMetadataSecretProtection.ID, ToolsetMetadataSecretProtection.Description). // AddReadTools( // toolsets.NewServerTool(GetSecretScanningAlert(getClient, t)), @@ -366,7 +367,7 @@ func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetG // tsg.AddToolset(users) // tsg.AddToolset(pullRequests) // tsg.AddToolset(actions) - // tsg.AddToolset(codeSecurity) + tsg.AddToolset(codeSecurity) // tsg.AddToolset(secretProtection) // tsg.AddToolset(dependabot) // tsg.AddToolset(notifications) @@ -382,6 +383,8 @@ func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetG } // InitDynamicToolset creates a dynamic toolset that can be used to enable other toolsets, and so requires the server and toolset group as arguments +// +//nolint:unused func InitDynamicToolset(s *mcp.Server, tsg *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) *toolsets.Toolset { // Create a new dynamic toolset // Need to add the dynamic toolset last so it can be used to enable other toolsets