GetGist creates a tool to get the content of a gist
(t translations.TranslationHelperFunc)
| 109 | |
| 110 | // GetGist creates a tool to get the content of a gist |
| 111 | func GetGist(t translations.TranslationHelperFunc) inventory.ServerTool { |
| 112 | return NewTool( |
| 113 | ToolsetMetadataGists, |
| 114 | mcp.Tool{ |
| 115 | Name: "get_gist", |
| 116 | Description: t("TOOL_GET_GIST_DESCRIPTION", "Get gist content of a particular gist, by gist ID"), |
| 117 | Annotations: &mcp.ToolAnnotations{ |
| 118 | Title: t("TOOL_GET_GIST", "Get Gist Content"), |
| 119 | ReadOnlyHint: true, |
| 120 | }, |
| 121 | InputSchema: &jsonschema.Schema{ |
| 122 | Type: "object", |
| 123 | Properties: map[string]*jsonschema.Schema{ |
| 124 | "gist_id": { |
| 125 | Type: "string", |
| 126 | Description: "The ID of the gist", |
| 127 | }, |
| 128 | }, |
| 129 | Required: []string{"gist_id"}, |
| 130 | }, |
| 131 | }, |
| 132 | nil, |
| 133 | func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { |
| 134 | gistID, err := RequiredParam[string](args, "gist_id") |
| 135 | if err != nil { |
| 136 | return utils.NewToolResultError(err.Error()), nil, nil |
| 137 | } |
| 138 | |
| 139 | client, err := deps.GetClient(ctx) |
| 140 | if err != nil { |
| 141 | return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil |
| 142 | } |
| 143 | |
| 144 | gist, resp, err := client.Gists.Get(ctx, gistID) |
| 145 | if err != nil { |
| 146 | return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to get gist", resp, err), nil, nil |
| 147 | } |
| 148 | defer func() { _ = resp.Body.Close() }() |
| 149 | |
| 150 | if resp.StatusCode != http.StatusOK { |
| 151 | body, err := io.ReadAll(resp.Body) |
| 152 | if err != nil { |
| 153 | return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil |
| 154 | } |
| 155 | return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to get gist", resp, body), nil, nil |
| 156 | } |
| 157 | |
| 158 | r, err := json.Marshal(gist) |
| 159 | if err != nil { |
| 160 | return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil |
| 161 | } |
| 162 | |
| 163 | result := utils.NewToolResultText(string(r)) |
| 164 | result = attachStaticIFCLabel(ctx, deps, result, ifc.LabelGist()) |
| 165 | return result, nil, nil |
| 166 | }, |
| 167 | ) |
| 168 | } |