Skip to content

Commit ce8b67c

Browse files
author
Marek Safarik
committed
Reactivate agent tool
Signed-off-by: Marek Safarik <[email protected]>
1 parent ab10946 commit ce8b67c

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

backend/client.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"bytes"
45
"crypto/tls"
56
"crypto/x509"
7+
"encoding/json"
68
"fmt"
79
"log"
810
"net/http"
@@ -86,8 +88,34 @@ func (kc *KeylimeClient) Get(endpoint string) (*http.Response, error) {
8688

8789
func (kc *KeylimeClient) Post(endpoint string, body interface{}) (*http.Response, error) {
8890
url := fmt.Sprintf("%s/%s/%s", kc.baseURL, kc.apiVersion, strings.TrimPrefix(endpoint, "/"))
89-
// TODO: implement body marshaling
90-
return kc.httpClient.Post(url, "application/json", nil)
91+
var buf bytes.Buffer
92+
if body != nil {
93+
if err := json.NewEncoder(&buf).Encode(body); err != nil {
94+
return nil, fmt.Errorf("failed to marshal body: %w", err)
95+
}
96+
}
97+
req, err := http.NewRequest("POST", url, &buf)
98+
if err != nil {
99+
return nil, err
100+
}
101+
req.Header.Set("Content-Type", "application/json")
102+
return kc.httpClient.Do(req)
103+
}
104+
105+
func (kc *KeylimeClient) Put(endpoint string, body interface{}) (*http.Response, error) {
106+
url := fmt.Sprintf("%s/%s/%s", kc.baseURL, kc.apiVersion, strings.TrimPrefix(endpoint, "/"))
107+
var buf bytes.Buffer
108+
if body != nil {
109+
if err := json.NewEncoder(&buf).Encode(body); err != nil {
110+
return nil, fmt.Errorf("failed to marshal body: %w", err)
111+
}
112+
}
113+
req, err := http.NewRequest("PUT", url, &buf)
114+
if err != nil {
115+
return nil, err
116+
}
117+
req.Header.Set("Content-Type", "application/json")
118+
return kc.httpClient.Do(req)
91119
}
92120

93121
func (kc *KeylimeClient) Delete(endpoint string) (*http.Response, error) {

backend/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func main() {
4949
mcp.AddTool(server, &mcp.Tool{Name: "Get_all_agents", Description: "Retrieves a list of all registered agent UUIDs"}, getAllAgents)
5050
mcp.AddTool(server, &mcp.Tool{Name: "Get_agent_status", Description: "Retrieves the current status information for a specific agent identified by its UUID"}, getAgentStatus)
5151
mcp.AddTool(server, &mcp.Tool{Name: "Get_failed_agents", Description: "Retrieves all agents currently in a failed operational state with their detailed status information including attestation history and failure reasons"}, getFailedAgents)
52+
mcp.AddTool(server, &mcp.Tool{Name: "Reactivate_agent", Description: "Reactivates a failed agent identified by its UUID"}, reactivate_agent)
5253
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
5354
log.Fatal(err)
5455
}

backend/tools.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
58

69
"github.com/modelcontextprotocol/go-sdk/mcp"
710
)
@@ -56,3 +59,26 @@ func getFailedAgents(ctx context.Context, req *mcp.CallToolRequest, input getFai
5659

5760
return nil, failedAgents, nil
5861
}
62+
63+
func reactivate_agent(ctx context.Context, req *mcp.CallToolRequest, input reactivateAgentInput) (
64+
*mcp.CallToolResult,
65+
reactivateAgentOutput,
66+
error,
67+
) {
68+
endpoint := fmt.Sprintf("agents/%s/reactivate", input.AgentUUID)
69+
resp, err := keylimeVerifierClient.Put(endpoint, nil)
70+
if err != nil {
71+
log.Printf("Error reactivating agent: %v", err)
72+
return nil, reactivateAgentOutput{}, err
73+
}
74+
defer resp.Body.Close()
75+
76+
var response reactivateAgentOutput
77+
err = json.NewDecoder(resp.Body).Decode(&response)
78+
if err != nil {
79+
log.Printf("Error decoding response: %v", err)
80+
return nil, reactivateAgentOutput{}, err
81+
}
82+
83+
return nil, response, nil
84+
}

backend/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,13 @@ type getAgentStatusOutput struct {
134134
HasMeasuredBoot bool `json:"has_measured_boot"`
135135
HasRuntimePolicy bool `json:"has_runtime_policy"`
136136
}
137+
138+
type reactivateAgentInput struct {
139+
AgentUUID string `json:"agent_uuid"`
140+
}
141+
142+
type reactivateAgentOutput struct {
143+
Code int `json:"code"`
144+
Status string `json:"status"`
145+
Results struct{} `json:"results"`
146+
}

0 commit comments

Comments
 (0)