|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + apiv1alpha1 "github.com/kagent-dev/kagent/go/api/gen/kagent/api/v1alpha1" |
| 10 | + "github.com/kagent-dev/kagent/go/core/v2/checkpoint" |
| 11 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + createCheckpointToolName = "create_agent_instance_checkpoint" |
| 16 | + listCheckpointsToolName = "list_agent_instance_checkpoints" |
| 17 | + forkAgentInstanceToolName = "fork_agent_instance" |
| 18 | +) |
| 19 | + |
| 20 | +type CreateCheckpointInput struct { |
| 21 | + Namespace string `json:"namespace" jsonschema:"Kubernetes namespace containing the AgentInstance"` |
| 22 | + AgentInstanceID string `json:"agent_instance_id" jsonschema:"AgentInstance UUID"` |
| 23 | + RequestID string `json:"request_id,omitempty" jsonschema:"Optional stable request ID for idempotency"` |
| 24 | +} |
| 25 | + |
| 26 | +type CheckpointSummary struct { |
| 27 | + ID string `json:"id"` |
| 28 | + Namespace string `json:"namespace"` |
| 29 | + AgentInstanceID string `json:"agent_instance_id"` |
| 30 | + HeadTaskID string `json:"head_task_id,omitempty"` |
| 31 | + HistorySequence uint64 `json:"history_sequence"` |
| 32 | + State string `json:"state"` |
| 33 | + CreatedAt string `json:"created_at,omitempty"` |
| 34 | + Failure *FailureSummary `json:"failure,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +type FailureSummary struct { |
| 38 | + Reason string `json:"reason"` |
| 39 | + Message string `json:"message"` |
| 40 | +} |
| 41 | + |
| 42 | +type CreateCheckpointOutput struct { |
| 43 | + Checkpoint CheckpointSummary `json:"checkpoint"` |
| 44 | +} |
| 45 | + |
| 46 | +type ListCheckpointsInput struct { |
| 47 | + Namespace string `json:"namespace" jsonschema:"Kubernetes namespace containing the AgentInstance"` |
| 48 | + AgentInstanceID string `json:"agent_instance_id" jsonschema:"AgentInstance UUID"` |
| 49 | + PageSize int `json:"page_size,omitempty" jsonschema:"Maximum number of checkpoints to return"` |
| 50 | + PageToken string `json:"page_token,omitempty" jsonschema:"Token returned by a previous call"` |
| 51 | +} |
| 52 | + |
| 53 | +type ListCheckpointsOutput struct { |
| 54 | + Checkpoints []CheckpointSummary `json:"checkpoints"` |
| 55 | + NextPageToken string `json:"next_page_token,omitempty"` |
| 56 | +} |
| 57 | + |
| 58 | +type ForkAgentInstanceInput struct { |
| 59 | + Namespace string `json:"namespace" jsonschema:"Kubernetes namespace containing the checkpoint"` |
| 60 | + CheckpointID string `json:"checkpoint_id" jsonschema:"Checkpoint UUID"` |
| 61 | + RequestID string `json:"request_id,omitempty" jsonschema:"Optional stable request ID for idempotency"` |
| 62 | +} |
| 63 | + |
| 64 | +type ForkAgentInstanceOutput struct { |
| 65 | + AgentInstance AgentInstanceSummary `json:"agent_instance"` |
| 66 | +} |
| 67 | + |
| 68 | +func (h *Handler) registerCheckpointTools(server *mcp.Server) { |
| 69 | + mcp.AddTool(server, &mcp.Tool{Name: createCheckpointToolName, Description: "Create a checkpoint at an AgentInstance turn boundary"}, h.createCheckpoint) |
| 70 | + mcp.AddTool(server, &mcp.Tool{Name: listCheckpointsToolName, Description: "List checkpoints for an AgentInstance"}, h.listCheckpoints) |
| 71 | + mcp.AddTool(server, &mcp.Tool{Name: forkAgentInstanceToolName, Description: "Create an AgentInstance from a checkpoint"}, h.forkAgentInstance) |
| 72 | +} |
| 73 | + |
| 74 | +func (h *Handler) createCheckpoint(ctx context.Context, _ *mcp.CallToolRequest, input CreateCheckpointInput) (*mcp.CallToolResult, CreateCheckpointOutput, error) { |
| 75 | + created, err := h.checkpoints.Create(ctx, input.Namespace, input.AgentInstanceID, stableRequestID(input.RequestID)) |
| 76 | + if err != nil { |
| 77 | + return toolError(err), CreateCheckpointOutput{}, nil |
| 78 | + } |
| 79 | + output := CreateCheckpointOutput{Checkpoint: checkpointSummary(created)} |
| 80 | + return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Created checkpoint %s", created.GetId())}}}, output, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (h *Handler) listCheckpoints(ctx context.Context, _ *mcp.CallToolRequest, input ListCheckpointsInput) (*mcp.CallToolResult, ListCheckpointsOutput, error) { |
| 84 | + listed, err := h.checkpoints.List(ctx, checkpoint.ListRequest{ |
| 85 | + Namespace: input.Namespace, InstanceID: input.AgentInstanceID, |
| 86 | + PageSize: input.PageSize, PageToken: input.PageToken, |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + return toolError(err), ListCheckpointsOutput{}, nil |
| 90 | + } |
| 91 | + output := ListCheckpointsOutput{Checkpoints: make([]CheckpointSummary, len(listed.Checkpoints)), NextPageToken: listed.NextPageToken} |
| 92 | + for i, item := range listed.Checkpoints { |
| 93 | + output.Checkpoints[i] = checkpointSummary(item) |
| 94 | + } |
| 95 | + return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Found %d checkpoints", len(output.Checkpoints))}}}, output, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (h *Handler) forkAgentInstance(ctx context.Context, _ *mcp.CallToolRequest, input ForkAgentInstanceInput) (*mcp.CallToolResult, ForkAgentInstanceOutput, error) { |
| 99 | + instance, err := h.checkpoints.Fork(ctx, input.Namespace, input.CheckpointID, stableRequestID(input.RequestID)) |
| 100 | + if err != nil { |
| 101 | + return toolError(err), ForkAgentInstanceOutput{}, nil |
| 102 | + } |
| 103 | + output := ForkAgentInstanceOutput{AgentInstance: agentInstanceSummary(instance)} |
| 104 | + return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Created AgentInstance %s", instance.GetId())}}}, output, nil |
| 105 | +} |
| 106 | + |
| 107 | +func stableRequestID(id string) string { |
| 108 | + if id != "" { |
| 109 | + return id |
| 110 | + } |
| 111 | + return uuid.NewString() |
| 112 | +} |
| 113 | + |
| 114 | +func checkpointSummary(value *apiv1alpha1.Checkpoint) CheckpointSummary { |
| 115 | + result := CheckpointSummary{ |
| 116 | + ID: value.GetId(), Namespace: value.GetNamespace(), AgentInstanceID: value.GetAgentInstanceId(), |
| 117 | + HeadTaskID: value.GetHeadTaskId(), HistorySequence: value.GetHistorySequence(), State: value.GetState().String(), |
| 118 | + } |
| 119 | + if value.GetCreatedAt() != nil { |
| 120 | + result.CreatedAt = value.GetCreatedAt().AsTime().Format(time.RFC3339Nano) |
| 121 | + } |
| 122 | + if value.GetFailure() != nil { |
| 123 | + result.Failure = &FailureSummary{Reason: value.GetFailure().GetReason(), Message: value.GetFailure().GetMessage()} |
| 124 | + } |
| 125 | + return result |
| 126 | +} |
| 127 | + |
| 128 | +func agentInstanceSummary(instance *apiv1alpha1.AgentInstance) AgentInstanceSummary { |
| 129 | + return AgentInstanceSummary{ |
| 130 | + Namespace: instance.GetNamespace(), ID: instance.GetId(), |
| 131 | + AgentTemplate: instance.GetAgentTemplate().GetName(), Harness: instance.GetHarness().GetName(), |
| 132 | + State: instance.GetState().String(), |
| 133 | + } |
| 134 | +} |
0 commit comments