GetFileContents creates a tool to get the contents of a file or directory from a GitHub repository.
(t translations.TranslationHelperFunc)
| 695 | |
| 696 | // GetFileContents creates a tool to get the contents of a file or directory from a GitHub repository. |
| 697 | func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool { |
| 698 | return NewTool( |
| 699 | ToolsetMetadataRepos, |
| 700 | mcp.Tool{ |
| 701 | Name: "get_file_contents", |
| 702 | Description: t("TOOL_GET_FILE_CONTENTS_DESCRIPTION", "Get the contents of a file or directory from a GitHub repository"), |
| 703 | Annotations: &mcp.ToolAnnotations{ |
| 704 | Title: t("TOOL_GET_FILE_CONTENTS_USER_TITLE", "Get file or directory contents"), |
| 705 | ReadOnlyHint: true, |
| 706 | }, |
| 707 | InputSchema: &jsonschema.Schema{ |
| 708 | Type: "object", |
| 709 | Properties: map[string]*jsonschema.Schema{ |
| 710 | "owner": { |
| 711 | Type: "string", |
| 712 | Description: "Repository owner (username or organization)", |
| 713 | }, |
| 714 | "repo": { |
| 715 | Type: "string", |
| 716 | Description: "Repository name", |
| 717 | }, |
| 718 | "path": { |
| 719 | Type: "string", |
| 720 | Description: "Path to file/directory", |
| 721 | Default: json.RawMessage(`"/"`), |
| 722 | }, |
| 723 | "ref": { |
| 724 | Type: "string", |
| 725 | Description: "Accepts optional git refs such as `refs/tags/{tag}`, `refs/heads/{branch}` or `refs/pull/{pr_number}/head`", |
| 726 | }, |
| 727 | "sha": { |
| 728 | Type: "string", |
| 729 | Description: "Accepts optional commit SHA. If specified, it will be used instead of ref", |
| 730 | }, |
| 731 | }, |
| 732 | Required: []string{"owner", "repo"}, |
| 733 | }, |
| 734 | }, |
| 735 | []scopes.Scope{scopes.Repo}, |
| 736 | func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { |
| 737 | owner, err := RequiredParam[string](args, "owner") |
| 738 | if err != nil { |
| 739 | return utils.NewToolResultError(err.Error()), nil, nil |
| 740 | } |
| 741 | repo, err := RequiredParam[string](args, "repo") |
| 742 | if err != nil { |
| 743 | return utils.NewToolResultError(err.Error()), nil, nil |
| 744 | } |
| 745 | |
| 746 | path, err := OptionalParam[string](args, "path") |
| 747 | if err != nil { |
| 748 | return utils.NewToolResultError(err.Error()), nil, nil |
| 749 | } |
| 750 | path = strings.TrimPrefix(path, "/") |
| 751 | |
| 752 | ref, err := OptionalParam[string](args, "ref") |
| 753 | if err != nil { |
| 754 | return utils.NewToolResultError(err.Error()), nil, nil |