|
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | 6 | "math" |
| 7 | + "net/http" |
7 | 8 | "path" |
8 | 9 | "strings" |
9 | 10 | "time" |
@@ -206,6 +207,23 @@ var ( |
206 | 207 | ), |
207 | 208 | ) |
208 | 209 |
|
| 210 | + // Tools for Prometheus Management API. |
| 211 | + prometheusHealthyTool = mcp.NewTool("healthy", |
| 212 | + mcp.WithDescription("Management API endpoint that can be used to check Prometheus health."), |
| 213 | + ) |
| 214 | + |
| 215 | + prometheusReadyTool = mcp.NewTool("ready", |
| 216 | + mcp.WithDescription("Management API endpoint that can be used to check Prometheus is ready to serve traffic (i.e. respond to queries.)"), |
| 217 | + ) |
| 218 | + |
| 219 | + prometheusReloadTool = mcp.NewTool("reload", |
| 220 | + mcp.WithDescription("Management API endpoint that can be used to trigger a reload of the Prometheus configuration and rule files."), |
| 221 | + ) |
| 222 | + |
| 223 | + prometheusQuitTool = mcp.NewTool("quit", |
| 224 | + mcp.WithDescription("Management API endpoint that can be used to trigger a graceful shutdown of Prometheus."), |
| 225 | + ) |
| 226 | + |
209 | 227 | // Tools for Prometheus documentation. |
210 | 228 | prometheusDocsListTool = mcp.NewTool("docs_list", |
211 | 229 | mcp.WithDescription("List of Official Prometheus Documentation Files."), |
@@ -700,3 +718,43 @@ func prometheusDocsSearchToolHandler(ctx context.Context, request mcp.CallToolRe |
700 | 718 |
|
701 | 719 | return toolRes, nil |
702 | 720 | } |
| 721 | + |
| 722 | +func doPrometheusManagementApiCall(ctx context.Context, method, path string) (*mcp.CallToolResult, error) { |
| 723 | + client, err := getApiClientFromContext(ctx) |
| 724 | + if err != nil { |
| 725 | + return mcp.NewToolResultError("error getting prometheus api client from context: " + err.Error()), nil |
| 726 | + } |
| 727 | + ctx, cancel := context.WithTimeout(ctx, apiTimeout) |
| 728 | + defer cancel() |
| 729 | + |
| 730 | + data, err := doHttpRequest(ctx, method, client.roundtripper, client.url, path, false) |
| 731 | + if err != nil { |
| 732 | + return mcp.NewToolResultError(fmt.Sprintf("error making Prometheus Management API call to %s: %s", path, err.Error())), nil |
| 733 | + } |
| 734 | + |
| 735 | + return mcp.NewToolResultText(strings.Trim(data, "\\n\"")), nil |
| 736 | +} |
| 737 | + |
| 738 | +const ( |
| 739 | + mgmtApiEndpointPrefix = "/-/" |
| 740 | + mgmtApiHealthyEndpoint = mgmtApiEndpointPrefix + "healthy" |
| 741 | + mgmtApiReadyEndpoint = mgmtApiEndpointPrefix + "ready" |
| 742 | + mgmtApiReloadEndpoint = mgmtApiEndpointPrefix + "reload" |
| 743 | + mgmtApiQuitEndpoint = mgmtApiEndpointPrefix + "quit" |
| 744 | +) |
| 745 | + |
| 746 | +func prometheusHealthyToolHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 747 | + return doPrometheusManagementApiCall(ctx, http.MethodGet, mgmtApiHealthyEndpoint) |
| 748 | +} |
| 749 | + |
| 750 | +func prometheusReadyToolHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 751 | + return doPrometheusManagementApiCall(ctx, http.MethodGet, mgmtApiReadyEndpoint) |
| 752 | +} |
| 753 | + |
| 754 | +func prometheusReloadToolHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 755 | + return doPrometheusManagementApiCall(ctx, http.MethodPost, mgmtApiReloadEndpoint) |
| 756 | +} |
| 757 | + |
| 758 | +func prometheusQuitToolHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 759 | + return doPrometheusManagementApiCall(ctx, http.MethodPost, mgmtApiQuitEndpoint) |
| 760 | +} |
0 commit comments